...
1 package gonja_test
2
3 import (
4 "bytes"
5 "fmt"
6 "io/ioutil"
7 "testing"
8
9 "github.com/noirbizarre/gonja"
10 "github.com/noirbizarre/gonja/config"
11 "github.com/pmezard/go-difflib/difflib"
12
13 tu "github.com/noirbizarre/gonja/testutils"
14 )
15
16 var testCases = []struct {
17 name string
18 trim_blocks bool
19 lstrip_blocks bool
20 keep_trailing_newline bool
21 }{
22 {"default", false, false, false},
23 {"trim_blocks", true, false, false},
24 {"lstrip_blocks", false, true, false},
25 {"keep_trailing_newline", false, false, true},
26 {"all", true, true, true},
27 }
28
29 const source = "testData/whitespaces/source.tpl"
30 const result = "testData/whitespaces/%s.out"
31
32 func TestWhiteSpace(t *testing.T) {
33 for _, tc := range testCases {
34 test := tc
35 t.Run(test.name, func(t *testing.T) {
36 defer func() {
37 if err := recover(); err != nil {
38 t.Error(err)
39 }
40 }()
41 cfg := config.NewConfig()
42 env := gonja.NewEnvironment(cfg, gonja.DefaultLoader)
43 env.TrimBlocks = test.trim_blocks
44 env.LstripBlocks = test.lstrip_blocks
45 env.KeepTrailingNewline = test.keep_trailing_newline
46
47 tpl, err := env.FromFile(source)
48 if err != nil {
49 t.Fatalf("Error on FromFile('%s'): %s", source, err.Error())
50 }
51 output := fmt.Sprintf(result, test.name)
52 expected, rerr := ioutil.ReadFile(output)
53 if rerr != nil {
54 t.Fatalf("Error on ReadFile('%s'): %s", output, rerr.Error())
55 }
56 rendered, err := tpl.ExecuteBytes(tu.Fixtures)
57 if err != nil {
58 t.Fatalf("Error on Execute('%s'): %s", source, err.Error())
59 }
60
61 if bytes.Compare(expected, rendered) != 0 {
62 diff := difflib.UnifiedDiff{
63 A: difflib.SplitLines(string(expected)),
64 B: difflib.SplitLines(string(rendered)),
65 FromFile: "Expected",
66 ToFile: "Rendered",
67 Context: 2,
68 Eol: "\n",
69 }
70 result, _ := difflib.GetUnifiedDiffString(diff)
71 t.Errorf("%s rendered with diff:\n%v", source, result)
72 }
73 })
74 }
75 }
76
View as plain text