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