@@ -9,6 +9,30 @@ import (
99 "text/template"
1010)
1111
12+ type templateTestList []struct {
13+ tmpl string
14+ context interface {}
15+ expected string
16+ }
17+
18+ func (tests templateTestList ) run (t * testing.T , prefix string ) {
19+ for n , test := range tests {
20+ tmplName := fmt .Sprintf ("%s-test-%d" , prefix , n )
21+ tmpl := template .Must (newTemplate (tmplName ).Parse (test .tmpl ))
22+
23+ var b bytes.Buffer
24+ err := tmpl .ExecuteTemplate (& b , tmplName , test .context )
25+ if err != nil {
26+ t .Fatalf ("Error executing template: %v (test %s)" , err , tmplName )
27+ }
28+
29+ got := b .String ()
30+ if test .expected != got {
31+ t .Fatalf ("Incorrect output found; expected %s, got %s (test %s)" , test .expected , got , tmplName )
32+ }
33+ }
34+ }
35+
1236func TestContains (t * testing.T ) {
1337 env := map [string ]string {
1438 "PORT" : "1234" ,
@@ -24,24 +48,14 @@ func TestContains(t *testing.T) {
2448}
2549
2650func TestKeys (t * testing.T ) {
27- expected := "VIRTUAL_HOST"
2851 env := map [string ]string {
29- expected : "demo.local" ,
52+ "VIRTUAL_HOST" : "demo.local" ,
3053 }
31-
32- const text = "{{range (keys $)}}{{.}}{{end}}"
33- tmpl := template .Must (newTemplate ("keys-test" ).Parse (text ))
34-
35- var b bytes.Buffer
36- err := tmpl .ExecuteTemplate (& b , "keys-test" , env )
37- if err != nil {
38- t .Fatalf ("Error executing template: %v" , err )
54+ tests := templateTestList {
55+ {`{{range (keys $)}}{{.}}{{end}}` , env , `VIRTUAL_HOST` },
3956 }
4057
41- got := b .String ()
42- if expected != got {
43- t .Fatalf ("Incorrect key found; expected %s, got %s" , expected , got )
44- }
58+ tests .run (t , "keys" )
4559}
4660
4761func TestKeysEmpty (t * testing.T ) {
@@ -216,11 +230,7 @@ func TestWhere(t *testing.T) {
216230 },
217231 }
218232
219- tests := []struct {
220- tmpl string
221- context interface {}
222- expected string
223- }{
233+ tests := templateTestList {
224234 {`{{where . "Env.VIRTUAL_HOST" "demo1.localhost" | len}}` , containers , `1` },
225235 {`{{where . "Env.VIRTUAL_HOST" "demo2.localhost" | len}}` , containers , `2` },
226236 {`{{where . "Env.VIRTUAL_HOST" "demo3.localhost" | len}}` , containers , `1` },
@@ -240,21 +250,7 @@ func TestWhere(t *testing.T) {
240250 },
241251 }
242252
243- for n , test := range tests {
244- tmplName := fmt .Sprintf ("where-test-%d" , n )
245- tmpl := template .Must (newTemplate (tmplName ).Parse (test .tmpl ))
246-
247- var b bytes.Buffer
248- err := tmpl .ExecuteTemplate (& b , tmplName , test .context )
249- if err != nil {
250- t .Fatalf ("Error executing template: %v" , err )
251- }
252-
253- got := b .String ()
254- if test .expected != got {
255- t .Fatalf ("Incorrect output found; expected %s, got %s" , test .expected , got )
256- }
257- }
253+ tests .run (t , "where" )
258254}
259255
260256func TestWhereExist (t * testing.T ) {
@@ -287,21 +283,14 @@ func TestWhereExist(t *testing.T) {
287283 },
288284 }
289285
290- if len (whereExist (containers , "Env.VIRTUAL_HOST" )) != 3 {
291- t .Fatalf ("Env.VIRTUAL_HOST expected 3 matches" )
292- }
293-
294- if len (whereExist (containers , "Env.VIRTUAL_PATH" )) != 2 {
295- t .Fatalf ("Env.VIRTUAL_PATH expected 2 matches" )
286+ tests := templateTestList {
287+ {`{{whereExist . "Env.VIRTUAL_HOST" | len}}` , containers , `3` },
288+ {`{{whereExist . "Env.VIRTUAL_PATH" | len}}` , containers , `2` },
289+ {`{{whereExist . "Env.NOT_A_KEY" | len}}` , containers , `0` },
290+ {`{{whereExist . "Env.VIRTUAL_PROTO" | len}}` , containers , `1` },
296291 }
297292
298- if len (whereExist (containers , "Env.NOT_A_KEY" )) != 0 {
299- t .Fatalf ("Env.NOT_A_KEY expected 0 matches" )
300- }
301-
302- if len (whereExist (containers , "Env.VIRTUAL_PROTO" )) != 1 {
303- t .Fatalf ("Env.VIRTUAL_PROTO expected 1 matche" )
304- }
293+ tests .run (t , "whereExist" )
305294}
306295
307296func TestWhereNotExist (t * testing.T ) {
@@ -334,21 +323,14 @@ func TestWhereNotExist(t *testing.T) {
334323 },
335324 }
336325
337- if len (whereNotExist (containers , "Env.VIRTUAL_HOST" )) != 1 {
338- t .Fatalf ("Env.VIRTUAL_HOST expected 1 match" )
339- }
340-
341- if len (whereNotExist (containers , "Env.VIRTUAL_PATH" )) != 2 {
342- t .Fatalf ("Env.VIRTUAL_PATH expected 2 matches" )
343- }
344-
345- if len (whereNotExist (containers , "Env.NOT_A_KEY" )) != 4 {
346- t .Fatalf ("Env.NOT_A_KEY expected 4 matches" )
326+ tests := templateTestList {
327+ {`{{whereNotExist . "Env.VIRTUAL_HOST" | len}}` , containers , `1` },
328+ {`{{whereNotExist . "Env.VIRTUAL_PATH" | len}}` , containers , `2` },
329+ {`{{whereNotExist . "Env.NOT_A_KEY" | len}}` , containers , `4` },
330+ {`{{whereNotExist . "Env.VIRTUAL_PROTO" | len}}` , containers , `3` },
347331 }
348332
349- if len (whereNotExist (containers , "Env.VIRTUAL_PROTO" )) != 3 {
350- t .Fatalf ("Env.VIRTUAL_PROTO expected 3 matches" )
351- }
333+ tests .run (t , "whereNotExist" )
352334}
353335
354336func TestWhereSomeMatch (t * testing.T ) {
@@ -379,21 +361,14 @@ func TestWhereSomeMatch(t *testing.T) {
379361 },
380362 }
381363
382- if len (whereAny (containers , "Env.VIRTUAL_HOST" , "," , []string {"demo1.localhost" })) != 1 {
383- t .Fatalf ("demo1.localhost expected 1 match" )
384- }
385-
386- if len (whereAny (containers , "Env.VIRTUAL_HOST" , "," , []string {"demo2.localhost" , "lala" })) != 2 {
387- t .Fatalf ("demo2.localhost expected 2 matches" )
388- }
389-
390- if len (whereAny (containers , "Env.VIRTUAL_HOST" , "," , []string {"something" , "demo3.localhost" })) != 1 {
391- t .Fatalf ("demo3.localhost expected 1 match" )
364+ tests := templateTestList {
365+ {`{{whereAny . "Env.VIRTUAL_HOST" "," (split "demo1.localhost" ",") | len}}` , containers , `1` },
366+ {`{{whereAny . "Env.VIRTUAL_HOST" "," (split "demo2.localhost,lala" ",") | len}}` , containers , `2` },
367+ {`{{whereAny . "Env.VIRTUAL_HOST" "," (split "something,demo3.localhost" ",") | len}}` , containers , `1` },
368+ {`{{whereAny . "Env.NOEXIST" "," (split "demo3.localhost" ",") | len}}` , containers , `0` },
392369 }
393370
394- if len (whereAny (containers , "Env.NOEXIST" , "," , []string {"demo3.localhost" })) != 0 {
395- t .Fatalf ("NOEXIST demo3.localhost expected 0 match" )
396- }
371+ tests .run (t , "whereAny" )
397372}
398373
399374func TestWhereRequires (t * testing.T ) {
@@ -424,21 +399,14 @@ func TestWhereRequires(t *testing.T) {
424399 },
425400 }
426401
427- if len (whereAll (containers , "Env.VIRTUAL_HOST" , "," , []string {"demo1.localhost" })) != 1 {
428- t .Fatalf ("demo1.localhost expected 1 match" )
402+ tests := templateTestList {
403+ {`{{whereAll . "Env.VIRTUAL_HOST" "," (split "demo1.localhost" ",") | len}}` , containers , `1` },
404+ {`{{whereAll . "Env.VIRTUAL_HOST" "," (split "demo2.localhost,lala" ",") | len}}` , containers , `0` },
405+ {`{{whereAll . "Env.VIRTUAL_HOST" "," (split "demo3.localhost" ",") | len}}` , containers , `1` },
406+ {`{{whereAll . "Env.NOEXIST" "," (split "demo3.localhost" ",") | len}}` , containers , `0` },
429407 }
430408
431- if len (whereAll (containers , "Env.VIRTUAL_HOST" , "," , []string {"demo2.localhost" , "lala" })) != 0 {
432- t .Fatalf ("demo2.localhost,lala expected 0 matches" )
433- }
434-
435- if len (whereAll (containers , "Env.VIRTUAL_HOST" , "," , []string {"demo3.localhost" })) != 1 {
436- t .Fatalf ("demo3.localhost expected 1 match" )
437- }
438-
439- if len (whereAll (containers , "Env.NOEXIST" , "," , []string {"demo3.localhost" })) != 0 {
440- t .Fatalf ("NOEXIST demo3.localhost expected 0 match" )
441- }
409+ tests .run (t , "whereAll" )
442410}
443411
444412func TestHasPrefix (t * testing.T ) {
@@ -558,11 +526,7 @@ func TestJson(t *testing.T) {
558526}
559527
560528func TestParseJson (t * testing.T ) {
561- tests := []struct {
562- tmpl string
563- context interface {}
564- expected string
565- }{
529+ tests := templateTestList {
566530 {`{{parseJson .}}` , `null` , `<no value>` },
567531 {`{{parseJson .}}` , `true` , `true` },
568532 {`{{parseJson .}}` , `1` , `1` },
@@ -571,50 +535,18 @@ func TestParseJson(t *testing.T) {
571535 {`{{index (parseJson . | first) "enabled"}}` , `[{"enabled":true}]` , `true` },
572536 }
573537
574- for n , test := range tests {
575- tmplName := fmt .Sprintf ("parseJson-test-%d" , n )
576- tmpl := template .Must (newTemplate (tmplName ).Parse (test .tmpl ))
577-
578- var b bytes.Buffer
579- err := tmpl .ExecuteTemplate (& b , tmplName , test .context )
580- if err != nil {
581- t .Fatalf ("Error executing template: %v" , err )
582- }
583-
584- got := b .String ()
585- if test .expected != got {
586- t .Fatalf ("Incorrect output found; expected %s, got %s" , test .expected , got )
587- }
588- }
538+ tests .run (t , "parseJson" )
589539}
590540
591541func TestQueryEscape (t * testing.T ) {
592- tests := []struct {
593- tmpl string
594- context interface {}
595- expected string
596- }{
542+ tests := templateTestList {
597543 {`{{queryEscape .}}` , `example.com` , `example.com` },
598544 {`{{queryEscape .}}` , `.example.com` , `.example.com` },
599545 {`{{queryEscape .}}` , `*.example.com` , `%2A.example.com` },
600546 {`{{queryEscape .}}` , `~^example\.com(\..*\.xip\.io)?$` , `~%5Eexample%5C.com%28%5C..%2A%5C.xip%5C.io%29%3F%24` },
601547 }
602548
603- for n , test := range tests {
604- tmplName := fmt .Sprintf ("queryEscape-test-%d" , n )
605- tmpl := template .Must (newTemplate (tmplName ).Parse (test .tmpl ))
606-
607- var b bytes.Buffer
608- err := tmpl .ExecuteTemplate (& b , tmplName , test .context )
609- if err != nil {
610- t .Fatalf ("Error executing template: %v" , err )
611- }
612-
613- got := b .String ()
614- if test .expected != got {
615- t .Fatalf ("Incorrect output found; expected %s, got %s" , test .expected , got )
616- }
617- }
549+ tests .run (t , "queryEscape" )
618550}
619551
620552func TestArrayClosestExact (t * testing.T ) {
0 commit comments