@@ -9,6 +9,30 @@ import (
9
9
"text/template"
10
10
)
11
11
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
+
12
36
func TestContains (t * testing.T ) {
13
37
env := map [string ]string {
14
38
"PORT" : "1234" ,
@@ -24,24 +48,14 @@ func TestContains(t *testing.T) {
24
48
}
25
49
26
50
func TestKeys (t * testing.T ) {
27
- expected := "VIRTUAL_HOST"
28
51
env := map [string ]string {
29
- expected : "demo.local" ,
52
+ "VIRTUAL_HOST" : "demo.local" ,
30
53
}
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` },
39
56
}
40
57
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" )
45
59
}
46
60
47
61
func TestKeysEmpty (t * testing.T ) {
@@ -216,11 +230,7 @@ func TestWhere(t *testing.T) {
216
230
},
217
231
}
218
232
219
- tests := []struct {
220
- tmpl string
221
- context interface {}
222
- expected string
223
- }{
233
+ tests := templateTestList {
224
234
{`{{where . "Env.VIRTUAL_HOST" "demo1.localhost" | len}}` , containers , `1` },
225
235
{`{{where . "Env.VIRTUAL_HOST" "demo2.localhost" | len}}` , containers , `2` },
226
236
{`{{where . "Env.VIRTUAL_HOST" "demo3.localhost" | len}}` , containers , `1` },
@@ -240,21 +250,7 @@ func TestWhere(t *testing.T) {
240
250
},
241
251
}
242
252
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" )
258
254
}
259
255
260
256
func TestWhereExist (t * testing.T ) {
@@ -287,21 +283,14 @@ func TestWhereExist(t *testing.T) {
287
283
},
288
284
}
289
285
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` },
296
291
}
297
292
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" )
305
294
}
306
295
307
296
func TestWhereNotExist (t * testing.T ) {
@@ -334,21 +323,14 @@ func TestWhereNotExist(t *testing.T) {
334
323
},
335
324
}
336
325
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` },
347
331
}
348
332
349
- if len (whereNotExist (containers , "Env.VIRTUAL_PROTO" )) != 3 {
350
- t .Fatalf ("Env.VIRTUAL_PROTO expected 3 matches" )
351
- }
333
+ tests .run (t , "whereNotExist" )
352
334
}
353
335
354
336
func TestWhereSomeMatch (t * testing.T ) {
@@ -379,21 +361,14 @@ func TestWhereSomeMatch(t *testing.T) {
379
361
},
380
362
}
381
363
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` },
392
369
}
393
370
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" )
397
372
}
398
373
399
374
func TestWhereRequires (t * testing.T ) {
@@ -424,21 +399,14 @@ func TestWhereRequires(t *testing.T) {
424
399
},
425
400
}
426
401
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` },
429
407
}
430
408
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" )
442
410
}
443
411
444
412
func TestHasPrefix (t * testing.T ) {
@@ -558,11 +526,7 @@ func TestJson(t *testing.T) {
558
526
}
559
527
560
528
func TestParseJson (t * testing.T ) {
561
- tests := []struct {
562
- tmpl string
563
- context interface {}
564
- expected string
565
- }{
529
+ tests := templateTestList {
566
530
{`{{parseJson .}}` , `null` , `<no value>` },
567
531
{`{{parseJson .}}` , `true` , `true` },
568
532
{`{{parseJson .}}` , `1` , `1` },
@@ -571,50 +535,18 @@ func TestParseJson(t *testing.T) {
571
535
{`{{index (parseJson . | first) "enabled"}}` , `[{"enabled":true}]` , `true` },
572
536
}
573
537
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" )
589
539
}
590
540
591
541
func TestQueryEscape (t * testing.T ) {
592
- tests := []struct {
593
- tmpl string
594
- context interface {}
595
- expected string
596
- }{
542
+ tests := templateTestList {
597
543
{`{{queryEscape .}}` , `example.com` , `example.com` },
598
544
{`{{queryEscape .}}` , `.example.com` , `.example.com` },
599
545
{`{{queryEscape .}}` , `*.example.com` , `%2A.example.com` },
600
546
{`{{queryEscape .}}` , `~^example\.com(\..*\.xip\.io)?$` , `~%5Eexample%5C.com%28%5C..%2A%5C.xip%5C.io%29%3F%24` },
601
547
}
602
548
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" )
618
550
}
619
551
620
552
func TestArrayClosestExact (t * testing.T ) {
0 commit comments