1
0

templatefunc_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "html/template"
  17. "net/url"
  18. "reflect"
  19. "testing"
  20. "time"
  21. )
  22. func TestSubstr(t *testing.T) {
  23. s := `012345`
  24. if Substr(s, 0, 2) != "01" {
  25. t.Error("should be equal")
  26. }
  27. if Substr(s, 0, 100) != "012345" {
  28. t.Error("should be equal")
  29. }
  30. if Substr(s, 12, 100) != "012345" {
  31. t.Error("should be equal")
  32. }
  33. }
  34. func TestHtml2str(t *testing.T) {
  35. h := `<HTML><style></style><script>x<x</script></HTML><123> 123\n
  36. \n`
  37. if HTML2str(h) != "123\\n\n\\n" {
  38. t.Error("should be equal")
  39. }
  40. }
  41. func TestDateFormat(t *testing.T) {
  42. ts := "Mon, 01 Jul 2013 13:27:42 CST"
  43. tt, _ := time.Parse(time.RFC1123, ts)
  44. if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 13:27:42" {
  45. t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
  46. }
  47. }
  48. func TestDate(t *testing.T) {
  49. ts := "Mon, 01 Jul 2013 13:27:42 CST"
  50. tt, _ := time.Parse(time.RFC1123, ts)
  51. if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 13:27:42" {
  52. t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
  53. }
  54. if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 01:27:42 PM" {
  55. t.Errorf("13-7-1 01:27:42 PM does not equal %v", ss)
  56. }
  57. if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 1:27:42 pm" {
  58. t.Errorf("Mon, 01 Jul 2013 1:27:42 pm does not equal %v", ss)
  59. }
  60. if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 13:27:42" {
  61. t.Errorf("Monday, 01 July 2013 13:27:42 does not equal %v", ss)
  62. }
  63. }
  64. func TestCompareRelated(t *testing.T) {
  65. if !Compare("abc", "abc") {
  66. t.Error("should be equal")
  67. }
  68. if Compare("abc", "aBc") {
  69. t.Error("should be not equal")
  70. }
  71. if !Compare("1", 1) {
  72. t.Error("should be equal")
  73. }
  74. if CompareNot("abc", "abc") {
  75. t.Error("should be equal")
  76. }
  77. if !CompareNot("abc", "aBc") {
  78. t.Error("should be not equal")
  79. }
  80. if !NotNil("a string") {
  81. t.Error("should not be nil")
  82. }
  83. }
  84. func TestHtmlquote(t *testing.T) {
  85. h := `&lt;&#39;&nbsp;&rdquo;&ldquo;&amp;&quot;&gt;`
  86. s := `<' ”“&">`
  87. if Htmlquote(s) != h {
  88. t.Error("should be equal")
  89. }
  90. }
  91. func TestHtmlunquote(t *testing.T) {
  92. h := `&lt;&#39;&nbsp;&rdquo;&ldquo;&amp;&quot;&gt;`
  93. s := `<' ”“&">`
  94. if Htmlunquote(h) != s {
  95. t.Error("should be equal")
  96. }
  97. }
  98. func TestParseForm(t *testing.T) {
  99. type ExtendInfo struct {
  100. Hobby string `form:"hobby"`
  101. Memo string
  102. }
  103. type OtherInfo struct {
  104. Organization string `form:"organization"`
  105. Title string `form:"title"`
  106. ExtendInfo
  107. }
  108. type user struct {
  109. ID int `form:"-"`
  110. tag string `form:"tag"`
  111. Name interface{} `form:"username"`
  112. Age int `form:"age,text"`
  113. Email string
  114. Intro string `form:",textarea"`
  115. StrBool bool `form:"strbool"`
  116. Date time.Time `form:"date,2006-01-02"`
  117. OtherInfo
  118. }
  119. u := user{}
  120. form := url.Values{
  121. "ID": []string{"1"},
  122. "-": []string{"1"},
  123. "tag": []string{"no"},
  124. "username": []string{"test"},
  125. "age": []string{"40"},
  126. "Email": []string{"test@gmail.com"},
  127. "Intro": []string{"I am an engineer!"},
  128. "strbool": []string{"yes"},
  129. "date": []string{"2014-11-12"},
  130. "organization": []string{"beego"},
  131. "title": []string{"CXO"},
  132. "hobby": []string{"Basketball"},
  133. "memo": []string{"nothing"},
  134. }
  135. if err := ParseForm(form, u); err == nil {
  136. t.Fatal("nothing will be changed")
  137. }
  138. if err := ParseForm(form, &u); err != nil {
  139. t.Fatal(err)
  140. }
  141. if u.ID != 0 {
  142. t.Errorf("ID should equal 0 but got %v", u.ID)
  143. }
  144. if len(u.tag) != 0 {
  145. t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
  146. }
  147. if u.Name.(string) != "test" {
  148. t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
  149. }
  150. if u.Age != 40 {
  151. t.Errorf("Age should equal 40 but got %v", u.Age)
  152. }
  153. if u.Email != "test@gmail.com" {
  154. t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
  155. }
  156. if u.Intro != "I am an engineer!" {
  157. t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
  158. }
  159. if u.StrBool != true {
  160. t.Errorf("strboll should equal `true`, but got `%v`", u.StrBool)
  161. }
  162. y, m, d := u.Date.Date()
  163. if y != 2014 || m.String() != "November" || d != 12 {
  164. t.Errorf("Date should equal `2014-11-12`, but got `%v`", u.Date.String())
  165. }
  166. if u.Organization != "beego" {
  167. t.Errorf("Organization should equal `beego`, but got `%v`", u.Organization)
  168. }
  169. if u.Title != "CXO" {
  170. t.Errorf("Title should equal `CXO`, but got `%v`", u.Title)
  171. }
  172. if u.Hobby != "Basketball" {
  173. t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby)
  174. }
  175. if len(u.Memo) != 0 {
  176. t.Errorf("Memo's length should equal 0 but got %v", len(u.Memo))
  177. }
  178. }
  179. func TestRenderForm(t *testing.T) {
  180. type user struct {
  181. ID int `form:"-"`
  182. tag string `form:"tag"`
  183. Name interface{} `form:"username"`
  184. Age int `form:"age,text,年龄:"`
  185. Sex string
  186. Email []string
  187. Intro string `form:",textarea"`
  188. Ignored string `form:"-"`
  189. }
  190. u := user{Name: "test", Intro: "Some Text"}
  191. output := RenderForm(u)
  192. if output != template.HTML("") {
  193. t.Errorf("output should be empty but got %v", output)
  194. }
  195. output = RenderForm(&u)
  196. result := template.HTML(
  197. `Name: <input name="username" type="text" value="test"></br>` +
  198. `年龄:<input name="age" type="text" value="0"></br>` +
  199. `Sex: <input name="Sex" type="text" value=""></br>` +
  200. `Intro: <textarea name="Intro">Some Text</textarea>`)
  201. if output != result {
  202. t.Errorf("output should equal `%v` but got `%v`", result, output)
  203. }
  204. }
  205. func TestRenderFormField(t *testing.T) {
  206. html := renderFormField("Label: ", "Name", "text", "Value", "", "", false)
  207. if html != `Label: <input name="Name" type="text" value="Value">` {
  208. t.Errorf("Wrong html output for input[type=text]: %v ", html)
  209. }
  210. html = renderFormField("Label: ", "Name", "textarea", "Value", "", "", false)
  211. if html != `Label: <textarea name="Name">Value</textarea>` {
  212. t.Errorf("Wrong html output for textarea: %v ", html)
  213. }
  214. html = renderFormField("Label: ", "Name", "textarea", "Value", "", "", true)
  215. if html != `Label: <textarea name="Name" required>Value</textarea>` {
  216. t.Errorf("Wrong html output for textarea: %v ", html)
  217. }
  218. }
  219. func TestParseFormTag(t *testing.T) {
  220. // create struct to contain field with different types of struct-tag `form`
  221. type user struct {
  222. All int `form:"name,text,年龄:"`
  223. NoName int `form:",hidden,年龄:"`
  224. OnlyLabel int `form:",,年龄:"`
  225. OnlyName int `form:"name" id:"name" class:"form-name"`
  226. Ignored int `form:"-"`
  227. Required int `form:"name" required:"true"`
  228. IgnoreRequired int `form:"name"`
  229. NotRequired int `form:"name" required:"false"`
  230. }
  231. objT := reflect.TypeOf(&user{}).Elem()
  232. label, name, fType, id, class, ignored, required := parseFormTag(objT.Field(0))
  233. if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
  234. t.Errorf("Form Tag with name, label and type was not correctly parsed.")
  235. }
  236. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(1))
  237. if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
  238. t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
  239. }
  240. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(2))
  241. if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
  242. t.Errorf("Form Tag containing only label was not correctly parsed.")
  243. }
  244. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(3))
  245. if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
  246. id == "name" && class == "form-name") {
  247. t.Errorf("Form Tag containing only name was not correctly parsed.")
  248. }
  249. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(4))
  250. if ignored == false {
  251. t.Errorf("Form Tag that should be ignored was not correctly parsed.")
  252. }
  253. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(5))
  254. if !(name == "name" && required == true) {
  255. t.Errorf("Form Tag containing only name and required was not correctly parsed.")
  256. }
  257. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(6))
  258. if !(name == "name" && required == false) {
  259. t.Errorf("Form Tag containing only name and ignore required was not correctly parsed.")
  260. }
  261. label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(7))
  262. if !(name == "name" && required == false) {
  263. t.Errorf("Form Tag containing only name and not required was not correctly parsed.")
  264. }
  265. }
  266. func TestMapGet(t *testing.T) {
  267. // test one level map
  268. m1 := map[string]int64{
  269. "a": 1,
  270. "1": 2,
  271. }
  272. if res, err := MapGet(m1, "a"); err == nil {
  273. if res.(int64) != 1 {
  274. t.Errorf("Should return 1, but return %v", res)
  275. }
  276. } else {
  277. t.Errorf("Error happens %v", err)
  278. }
  279. if res, err := MapGet(m1, "1"); err == nil {
  280. if res.(int64) != 2 {
  281. t.Errorf("Should return 2, but return %v", res)
  282. }
  283. } else {
  284. t.Errorf("Error happens %v", err)
  285. }
  286. if res, err := MapGet(m1, 1); err == nil {
  287. if res.(int64) != 2 {
  288. t.Errorf("Should return 2, but return %v", res)
  289. }
  290. } else {
  291. t.Errorf("Error happens %v", err)
  292. }
  293. // test 2 level map
  294. m2 := map[string]interface{}{
  295. "1": map[string]float64{
  296. "2": 3.5,
  297. },
  298. }
  299. if res, err := MapGet(m2, 1, 2); err == nil {
  300. if res.(float64) != 3.5 {
  301. t.Errorf("Should return 3.5, but return %v", res)
  302. }
  303. } else {
  304. t.Errorf("Error happens %v", err)
  305. }
  306. // test 5 level map
  307. m5 := map[string]interface{}{
  308. "1": map[string]interface{}{
  309. "2": map[string]interface{}{
  310. "3": map[string]interface{}{
  311. "4": map[string]interface{}{
  312. "5": 1.2,
  313. },
  314. },
  315. },
  316. },
  317. }
  318. if res, err := MapGet(m5, 1, 2, 3, 4, 5); err == nil {
  319. if res.(float64) != 1.2 {
  320. t.Errorf("Should return 1.2, but return %v", res)
  321. }
  322. } else {
  323. t.Errorf("Error happens %v", err)
  324. }
  325. // check whether element not exists in map
  326. if res, err := MapGet(m5, 5, 4, 3, 2, 1); err == nil {
  327. if res != nil {
  328. t.Errorf("Should return nil, but return %v", res)
  329. }
  330. } else {
  331. t.Errorf("Error happens %v", err)
  332. }
  333. }