-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathengine_test.go
159 lines (134 loc) · 3.29 KB
/
engine_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package engine
import (
"strings"
"testing"
)
func TestNoDirs(t *testing.T) {
if _, err := New("/no/such/path"); err == nil {
t.Error("Expected failure when given bogus path")
}
// Canary
if _, err := New("testdata"); err != nil {
t.Errorf("Expected success, got '%s'", err)
}
if _, err := New("testdata", "/no/such/path"); err == nil {
t.Error("Expected failure when given bogus path")
}
}
func TestLegalName(t *testing.T) {
data := map[string]bool{
"..": false,
".": true,
"foo/..": false,
"foo/.": true,
"foo": true,
"/foo/": true,
"/foo": true,
"/../": false,
"../../../": false,
"./.././../": false,
"foo/bar baz/": true,
}
for d, expect := range data {
if legalName(d) != expect {
t.Errorf("Expected %s to be %v", d, expect)
}
}
}
func TestClean(t *testing.T) {
data := map[string]string{
"": ".",
"..": "..",
".": ".",
"foo/..": ".",
"foo/.": "foo",
"foo": "foo",
"/foo/": "/foo",
"/foo": "/foo",
"/../": "/",
"../../../": "../../..",
"./.././../": "../..",
"foo/bar baz/": "foo/bar baz",
"foo/bar/..": "foo",
"./testdata": "testdata",
}
for d, expect := range data {
if o := clean(d); o != expect {
t.Errorf("Expected %s to be %s, got %s", d, expect, o)
}
}
}
func TestNew(t *testing.T) {
e, err := New("./testdata")
if err != nil {
t.Errorf("Failed parse of testdata: %s", err)
}
dirs := e.Dirs()
if len(dirs) != 1 {
t.Errorf("Expected only one template directory.")
}
if dirs[0] != "testdata" {
t.Errorf("Expected 'testdata', got '%s'", dirs[0])
}
}
func TestRender(t *testing.T) {
e, err := New("testdata")
if err != nil {
t.Errorf("Failed parse of testdata: %s", err)
}
paths := e.Paths()
if len(paths) != 1 {
t.Errorf("Expected 1 template. Found %d", len(paths))
for _, p := range paths {
println("Template: ", p)
}
}
out, err := e.Render("simple.tpl", "test")
if err != nil {
t.Errorf("Failed render: %s", err)
}
out = strings.TrimSpace(out)
if out != "test" {
t.Errorf("Expected 'test', got '%s'", out)
}
}
func TestCascadingRender(t *testing.T) {
e, err := New("testdata/override", "testdata/base", "testdata")
if err != nil {
t.Errorf("Failed parse of testdata: %s", err)
}
out, err := e.Render("simple.tpl", "test")
if err != nil {
t.Errorf("Failed render: %s", err)
}
out = strings.TrimSpace(out)
if out != "OVERRIDE:test" {
t.Errorf("Expected 'OVERRIDE:test', got '%s'", out)
}
out, err = e.Render("onlybase.tpl", "test")
if err != nil {
t.Errorf("Could not render onlybase: %s", err)
}
out = strings.TrimSpace(out)
if out != "onlybase:test" {
t.Errorf("Expected 'onlybase:test', got '%s'", out)
}
}
func TestAsset(t *testing.T) {
e, err := New("testdata/override", "testdata/base")
if err != nil {
t.Errorf("Failed parse of testdata: %s", err)
}
a, err := e.Asset("asset.dat")
if err != nil {
t.Errorf("Could not find asset.dat: %s", err)
}
expect := "testdata/base/asset.dat"
if a != expect {
t.Errorf("Expected %s, got %s", expect, a)
}
// Make sure we can't render a non-template.
if _, err := e.Render("asset.dat", 42); err == nil {
t.Errorf("Should not be able to render asset.dat")
}
}