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