github.com/woremacx/kocha@v0.7.1-0.20150731103243-a5889322afc9/template_test.go (about) 1 package kocha_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "html/template" 7 "io/ioutil" 8 "net/http" 9 "net/http/httptest" 10 "path/filepath" 11 "reflect" 12 "strings" 13 "testing" 14 15 "github.com/woremacx/kocha" 16 ) 17 18 func TestTemplate_FuncMap_in_withInvalidType(t *testing.T) { 19 app := kocha.NewTestApp() 20 funcMap := template.FuncMap(app.Template.FuncMap) 21 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{in 1 1}}`)) 22 var buf bytes.Buffer 23 if err := tmpl.Execute(&buf, nil); err == nil { 24 t.Errorf("Expect errors, but no errors") 25 } 26 } 27 28 func TestTemplate_FuncMap_in(t *testing.T) { 29 app := kocha.NewTestApp() 30 funcMap := template.FuncMap(app.Template.FuncMap) 31 var buf bytes.Buffer 32 for _, v := range []struct { 33 Arr interface{} 34 Sep interface{} 35 expect string 36 err error 37 }{ 38 {[]string{"b", "a", "c"}, "a", "true", nil}, 39 {[]string{"ab", "b", "c"}, "a", "false", nil}, 40 {nil, "a", "", fmt.Errorf("valid types are slice, array and string, got `invalid'")}, 41 } { 42 buf.Reset() 43 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{in .Arr .Sep}}`)) 44 err := tmpl.Execute(&buf, v) 45 if !strings.HasSuffix(fmt.Sprint(err), fmt.Sprint(v.err)) { 46 t.Errorf(`{{in %#v %#v}}; error has "%v"; want "%v"`, v.Arr, v.Sep, err, v.err) 47 } 48 actual := buf.String() 49 expect := v.expect 50 if !reflect.DeepEqual(actual, expect) { 51 t.Errorf(`{{in %#v %#v}} => %#v; want %#v`, v.Arr, v.Sep, actual, expect) 52 } 53 } 54 } 55 56 func TestTemplate_FuncMap_url(t *testing.T) { 57 app := kocha.NewTestApp() 58 funcMap := template.FuncMap(app.Template.FuncMap) 59 60 func() { 61 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "root"}}`)) 62 var buf bytes.Buffer 63 if err := tmpl.Execute(&buf, nil); err != nil { 64 panic(err) 65 } 66 actual := buf.String() 67 expected := "/" 68 if !reflect.DeepEqual(actual, expected) { 69 t.Errorf("Expect %q, but %q", expected, actual) 70 } 71 }() 72 73 func() { 74 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "user" 713}}`)) 75 var buf bytes.Buffer 76 if err := tmpl.Execute(&buf, nil); err != nil { 77 panic(err) 78 } 79 actual := buf.String() 80 expected := "/user/713" 81 if !reflect.DeepEqual(actual, expected) { 82 t.Errorf("Expect %v, but %v", expected, actual) 83 } 84 }() 85 } 86 87 func TestTemplate_FuncMap_nl2br(t *testing.T) { 88 app := kocha.NewTestApp() 89 funcMap := template.FuncMap(app.Template.FuncMap) 90 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{nl2br "a\nb\nc\n"}}`)) 91 var buf bytes.Buffer 92 if err := tmpl.Execute(&buf, nil); err != nil { 93 panic(err) 94 } 95 actual := buf.String() 96 expected := "a<br>b<br>c<br>" 97 if !reflect.DeepEqual(actual, expected) { 98 t.Errorf("Expect %q, but %q", expected, actual) 99 } 100 } 101 102 func TestTemplate_FuncMap_raw(t *testing.T) { 103 app := kocha.NewTestApp() 104 funcMap := template.FuncMap(app.Template.FuncMap) 105 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{raw "\n<br>"}}`)) 106 var buf bytes.Buffer 107 if err := tmpl.Execute(&buf, nil); err != nil { 108 panic(err) 109 } 110 actual := buf.String() 111 expected := "\n<br>" 112 if !reflect.DeepEqual(actual, expected) { 113 t.Errorf("Expect %q, but %q", expected, actual) 114 } 115 } 116 117 func TestTemplate_FuncMap_invokeTemplate(t *testing.T) { 118 // test that if ActiveIf returns true. 119 func() { 120 app := kocha.NewTestApp() 121 funcMap := template.FuncMap(app.Template.FuncMap) 122 c := &kocha.Context{ 123 Data: map[interface{}]interface{}{ 124 "unit": &testUnit{"test1", true, 0}, 125 "ctx": "testctx1", 126 }, 127 } 128 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`)) 129 var buf bytes.Buffer 130 if err := tmpl.Execute(&buf, c); err != nil { 131 t.Error(err) 132 } 133 actual := buf.String() 134 expected := "test_tmpl1: testctx1\n" 135 if !reflect.DeepEqual(actual, expected) { 136 t.Errorf("Expect %q, but %q", expected, actual) 137 } 138 }() 139 140 // test that if ActiveIf returns false. 141 func() { 142 app := kocha.NewTestApp() 143 funcMap := template.FuncMap(app.Template.FuncMap) 144 c := &kocha.Context{ 145 Data: map[interface{}]interface{}{ 146 "unit": &testUnit{"test2", false, 0}, 147 "ctx": "testctx2", 148 }, 149 } 150 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`)) 151 var buf bytes.Buffer 152 if err := tmpl.Execute(&buf, c); err != nil { 153 t.Error(err) 154 } 155 actual := buf.String() 156 expected := "def_tmpl: testctx2\n" 157 if !reflect.DeepEqual(actual, expected) { 158 t.Errorf("Expect %q, but %q", expected, actual) 159 } 160 }() 161 162 // test that unknown template. 163 func() { 164 app := kocha.NewTestApp() 165 funcMap := template.FuncMap(app.Template.FuncMap) 166 c := &kocha.Context{ 167 Data: map[interface{}]interface{}{ 168 "unit": &testUnit{"test3", true, 0}, 169 "ctx": "testctx3", 170 }, 171 } 172 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "unknown_tmpl" "def_tmpl" $}}`)) 173 var buf bytes.Buffer 174 if err := tmpl.Execute(&buf, c); err != nil { 175 t.Error(err) 176 } 177 actual := buf.String() 178 expected := "def_tmpl: testctx3\n" 179 if !reflect.DeepEqual(actual, expected) { 180 t.Errorf("Expect %q, but %q", expected, actual) 181 } 182 }() 183 184 // test that unknown templates. 185 func() { 186 app := kocha.NewTestApp() 187 funcMap := template.FuncMap(app.Template.FuncMap) 188 c := &kocha.Context{ 189 Data: map[interface{}]interface{}{ 190 "unit": &testUnit{"test4", true, 0}, 191 "ctx": "testctx4", 192 }, 193 } 194 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "unknown_tmpl" "unknown_def_tmpl" $}}`)) 195 var buf bytes.Buffer 196 if err := tmpl.Execute(&buf, c); !strings.HasSuffix(err.Error(), "template not found: appname:unknown_def_tmpl.html") { 197 t.Error(err) 198 } 199 }() 200 201 // test that unknown default template. 202 func() { 203 app := kocha.NewTestApp() 204 funcMap := template.FuncMap(app.Template.FuncMap) 205 c := &kocha.Context{ 206 Data: map[interface{}]interface{}{ 207 "unit": &testUnit{"test5", true, 0}, 208 "ctx": "testctx5", 209 }, 210 } 211 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "unknown" $}}`)) 212 var buf bytes.Buffer 213 if err := tmpl.Execute(&buf, c); err != nil { 214 t.Error(err) 215 } 216 }() 217 218 // test that single context. 219 func() { 220 app := kocha.NewTestApp() 221 funcMap := template.FuncMap(app.Template.FuncMap) 222 c := &kocha.Context{ 223 Data: map[interface{}]interface{}{ 224 "unit": &testUnit{"test6", true, 0}, 225 "ctx": "testctx6", 226 }, 227 } 228 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`)) 229 var buf bytes.Buffer 230 if err := tmpl.Execute(&buf, c); err != nil { 231 t.Error(err) 232 } 233 actual := buf.String() 234 expected := "test_tmpl1: testctx6\n" 235 if !reflect.DeepEqual(actual, expected) { 236 t.Errorf("Expect %q, but %q", expected, actual) 237 } 238 }() 239 240 // test that too many contexts. 241 func() { 242 app := kocha.NewTestApp() 243 funcMap := template.FuncMap(app.Template.FuncMap) 244 c := &kocha.Context{ 245 Data: map[interface{}]interface{}{ 246 "unit": &testUnit{"test7", true, 0}, 247 "ctx": "testctx7", 248 }, 249 } 250 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $ $}}`)) 251 var buf bytes.Buffer 252 if err := tmpl.Execute(&buf, c); !strings.HasSuffix(err.Error(), "number of context must be 0 or 1") { 253 t.Error(err) 254 } 255 }() 256 } 257 258 func TestTemplateFuncMap_flash(t *testing.T) { 259 c := newTestContext("testctrlr", "") 260 funcMap := template.FuncMap(c.App.Template.FuncMap) 261 for _, v := range []struct { 262 key string 263 expect string 264 }{ 265 {"", ""}, 266 {"success", "test succeeded"}, 267 {"success", "test successful"}, 268 {"error", "test failed"}, 269 {"error", "test failure"}, 270 } { 271 c.Flash = kocha.Flash{} 272 c.Flash.Set(v.key, v.expect) 273 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(fmt.Sprintf(`{{flash . "unknown"}}{{flash . "%s"}}`, v.key))) 274 var buf bytes.Buffer 275 if err := tmpl.Execute(&buf, c); err != nil { 276 t.Error(err) 277 continue 278 } 279 actual := buf.String() 280 expect := v.expect 281 if !reflect.DeepEqual(actual, expect) { 282 t.Errorf(`{{flash . %#v}} => %#v; want %#v`, v.key, actual, expect) 283 } 284 } 285 } 286 287 func TestTemplateFuncMap_join(t *testing.T) { 288 app := kocha.NewTestApp() 289 funcMap := template.FuncMap(app.Template.FuncMap) 290 tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{join .Arr .Sep}}`)) 291 var buf bytes.Buffer 292 for _, v := range []struct { 293 Arr interface{} 294 Sep string 295 expect string 296 }{ 297 {[]int{1, 2, 3}, "&", "1&2&3"}, 298 {[2]uint{12, 34}, " and ", "12 and 34"}, 299 {[]string{"alice", "bob", "carol"}, ", ", "alice, bob, carol"}, 300 {[]string(nil), "|", ""}, 301 {[]bool{}, " or ", ""}, 302 {[]interface{}{"1", 2, "three", uint32(4)}, "-", "1-2-three-4"}, 303 {[]string{"あ", "い", "う", "え", "お"}, "_", "あ_い_う_え_お"}, 304 {[]string{"a", "b", "c"}, "∧", "a∧b∧c"}, 305 } { 306 buf.Reset() 307 if err := tmpl.Execute(&buf, v); err != nil { 308 t.Error(err) 309 continue 310 } 311 actual := buf.String() 312 expect := v.expect 313 if !reflect.DeepEqual(actual, expect) { 314 t.Errorf(`{{join %#v %#v}} => %#v; want %#v`, v.Arr, v.Sep, actual, expect) 315 } 316 } 317 } 318 319 func TestTemplate_Get(t *testing.T) { 320 app := kocha.NewTestApp() 321 func() { 322 for _, v := range []struct { 323 appName string 324 layout string 325 ctrlrName string 326 format string 327 }{ 328 {"appname", "application", "testctrlr", "html"}, 329 {"appname", "", "testctrlr", "js"}, 330 {"appname", "another_layout", "testctrlr", "html"}, 331 } { 332 tmpl, err := app.Template.Get(v.appName, v.layout, v.ctrlrName, v.format) 333 var actual interface{} = err 334 var expect interface{} = nil 335 if !reflect.DeepEqual(actual, expect) { 336 t.Fatalf(`Template.Get(%#v, %#v, %#v, %#v) => %T, %#v, want *template.Template, %#v`, v.appName, v.layout, v.ctrlrName, v.format, tmpl, actual, expect) 337 } 338 } 339 }() 340 341 func() { 342 for _, v := range []struct { 343 appName string 344 layout string 345 ctrlrName string 346 format string 347 expectErr error 348 }{ 349 {"unknownAppName", "app", "test_tmpl1", "html", fmt.Errorf("kocha: template not found: unknownAppName:%s", filepath.Join("layout", "app.html"))}, 350 {"testAppName", "app", "unknown_tmpl1", "html", fmt.Errorf("kocha: template not found: testAppName:%s", filepath.Join("layout", "app.html"))}, 351 {"testAppName", "app", "test_tmpl1", "xml", fmt.Errorf("kocha: template not found: testAppName:%s", filepath.Join("layout", "app.xml"))}, 352 {"testAppName", "", "test_tmpl1", "xml", fmt.Errorf("kocha: template not found: testAppName:test_tmpl1.xml")}, 353 } { 354 tmpl, err := app.Template.Get(v.appName, v.layout, v.ctrlrName, v.format) 355 actual := tmpl 356 expect := (*template.Template)(nil) 357 actualErr := err 358 expectErr := v.expectErr 359 if !reflect.DeepEqual(actual, expect) || !reflect.DeepEqual(actualErr, expectErr) { 360 t.Errorf(`Template.Get(%#v, %#v, %#v, %#v) => %#v, %#v, ; want %#v, %#v`, v.appName, v.layout, v.ctrlrName, v.format, actual, actualErr, expect, expectErr) 361 } 362 } 363 }() 364 } 365 366 func TestTemplateDelims(t *testing.T) { 367 app, err := kocha.New(&kocha.Config{ 368 AppPath: "testdata", 369 AppName: "appname", 370 DefaultLayout: "", 371 Template: &kocha.Template{ 372 PathInfo: kocha.TemplatePathInfo{ 373 Name: "appname", 374 Paths: []string{ 375 filepath.Join("testdata", "app", "view"), 376 }, 377 }, 378 LeftDelim: "{%", 379 RightDelim: "%}", 380 }, 381 RouteTable: []*kocha.Route{ 382 { 383 Name: "another_delims", 384 Path: "/", 385 Controller: &kocha.FixtureAnotherDelimsTestCtrl{ 386 Ctx: "test_other_delims_ctx", 387 }, 388 }, 389 }, 390 Middlewares: []kocha.Middleware{ 391 &kocha.DispatchMiddleware{}, 392 }, 393 Logger: &kocha.LoggerConfig{ 394 Writer: ioutil.Discard, 395 }, 396 }) 397 if err != nil { 398 t.Fatal(err) 399 } 400 req, err := http.NewRequest("GET", "/", nil) 401 if err != nil { 402 t.Fatal(err) 403 } 404 w := httptest.NewRecorder() 405 app.ServeHTTP(w, req) 406 var actual interface{} = w.Code 407 var expect interface{} = 200 408 if !reflect.DeepEqual(actual, expect) { 409 t.Errorf(`GET / status => %#v; want %#v`, actual, expect) 410 } 411 actual = w.Body.String() 412 expect = "This is other delims: test_other_delims_ctx\n" 413 if !reflect.DeepEqual(actual, expect) { 414 t.Errorf(`GET / => %#v; want %#v`, actual, expect) 415 } 416 }