-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloader_test.go
More file actions
177 lines (149 loc) · 4.36 KB
/
loader_test.go
File metadata and controls
177 lines (149 loc) · 4.36 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package template
import (
"errors"
"testing"
)
// Phase A: Loader contract — name validation + happy path
func TestMemoryLoader_ValidPath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
wantErr error
}{
{"relative escape", "../a.html", ErrInvalidTemplateName},
{"deep relative escape", "foo/../../bar", ErrInvalidTemplateName},
{"absolute", "/etc/passwd", ErrInvalidTemplateName},
{"backslash", "a\\b", ErrInvalidTemplateName},
{"nul byte", "a\x00b", ErrInvalidTemplateName},
{"trailing slash", "a/", ErrInvalidTemplateName},
{"empty", "", ErrInvalidTemplateName},
}
loader := NewMemoryLoader(map[string]string{"a.html": "hi"})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, _, err := loader.Open(tt.input)
if !errors.Is(err, tt.wantErr) {
t.Errorf("Open(%q) err = %v, want %v", tt.input, err, tt.wantErr)
}
})
}
}
func TestMemoryLoader_HappyPath(t *testing.T) {
t.Parallel()
loader := NewMemoryLoader(map[string]string{
"a.html": "hello {{ name }}",
})
src, resolved, err := loader.Open("a.html")
if err != nil {
t.Fatalf("Open() err = %v", err)
}
if src != "hello {{ name }}" {
t.Errorf("src = %q", src)
}
if resolved != "a.html" {
t.Errorf("resolved = %q, want a.html", resolved)
}
}
func TestMemoryLoader_NotFound(t *testing.T) {
t.Parallel()
loader := NewMemoryLoader(map[string]string{})
_, _, err := loader.Open("missing.html")
if !errors.Is(err, ErrTemplateNotFound) {
t.Errorf("err = %v, want ErrTemplateNotFound", err)
}
}
// Phase M cycle 1: ChainLoader returns the first loader that has the file.
func TestChainLoader_FirstHitWins(t *testing.T) {
t.Parallel()
user := NewMemoryLoader(map[string]string{"x.txt": "USER"})
theme := NewMemoryLoader(map[string]string{"x.txt": "THEME"})
chain := NewChainLoader(user, theme)
src, _, err := chain.Open("x.txt")
if err != nil {
t.Fatalf("err = %v", err)
}
if src != "USER" {
t.Errorf("src = %q, want USER", src)
}
}
// Phase M cycle 2: ChainLoader falls through to the next loader on miss.
func TestChainLoader_FallthroughOnMiss(t *testing.T) {
t.Parallel()
user := NewMemoryLoader(map[string]string{})
theme := NewMemoryLoader(map[string]string{"x.txt": "THEME"})
chain := NewChainLoader(user, theme)
src, _, err := chain.Open("x.txt")
if err != nil {
t.Fatalf("err = %v", err)
}
if src != "THEME" {
t.Errorf("src = %q, want THEME", src)
}
}
// Phase M cycle 3: all misses return ErrTemplateNotFound.
func TestChainLoader_AllMiss_NotFound(t *testing.T) {
t.Parallel()
chain := NewChainLoader(
NewMemoryLoader(map[string]string{}),
NewMemoryLoader(map[string]string{}),
)
_, _, err := chain.Open("x.txt")
if !errors.Is(err, ErrTemplateNotFound) {
t.Errorf("err = %v", err)
}
}
// Phase M cycle 4: resolved name carries layer info so same-name files
// in different layers don't collide in cache.
func TestChainLoader_ResolvedNamePrefixDistinguishesLayers(t *testing.T) {
t.Parallel()
user := NewMemoryLoader(map[string]string{"x.txt": "USER"})
theme := NewMemoryLoader(map[string]string{"x.txt": "THEME"})
// User loader wins when present.
chain1 := NewChainLoader(user, theme)
_, r1, _ := chain1.Open("x.txt")
// Theme loader wins when user is empty.
chain2 := NewChainLoader(
NewMemoryLoader(map[string]string{}),
theme,
)
_, r2, _ := chain2.Open("x.txt")
// The two resolved names should differ so their cache entries do
// not collide.
if r1 == r2 {
t.Errorf("resolved names collided: %q == %q", r1, r2)
}
}
// Phase M cycle 5: empty chain returns ErrTemplateNotFound.
func TestChainLoader_EmptyChain_NotFound(t *testing.T) {
t.Parallel()
chain := NewChainLoader()
_, _, err := chain.Open("x.txt")
if !errors.Is(err, ErrTemplateNotFound) {
t.Errorf("err = %v", err)
}
}
// Phase M cycle 6: Engine using ChainLoader caches keyed on resolved name
// so the same user/theme layers do not clobber each other.
func TestChainLoader_EngineCacheKeyedOnResolvedName(t *testing.T) {
t.Parallel()
user := NewMemoryLoader(map[string]string{
"a.txt": "USER",
})
theme := NewMemoryLoader(map[string]string{
"a.txt": "THEME",
})
engine := New(
WithLoader(NewChainLoader(user, theme)),
WithFormat(FormatText),
WithLayout(),
)
got, err := engine.Render("a.txt", nil)
if err != nil {
t.Fatalf("err = %v", err)
}
if got != "USER" {
t.Errorf("got %q, want USER", got)
}
}