-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset_test.go
More file actions
131 lines (117 loc) · 2.96 KB
/
set_test.go
File metadata and controls
131 lines (117 loc) · 2.96 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
package template
import (
"bytes"
"errors"
"strings"
"testing"
)
// Phase B cycle 1: Engine constructs and renders a basic text template.
func TestEngine_RenderBasicInterpolation(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{
"hello.txt": "Hello {{ name }}",
})),
WithFormat(FormatText),
)
var buf bytes.Buffer
if err := engine.RenderTo("hello.txt", &buf, Data{"name": "world"}); err != nil {
t.Fatalf("renderSourceTemplate() err = %v", err)
}
if got := buf.String(); got != "Hello world" {
t.Errorf("renderSourceTemplate() = %q, want %q", got, "Hello world")
}
}
// Phase B cycle 2: Engine.Load propagates loader not-found errors.
func TestEngine_Load_PropagatesNotFound(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{})),
WithFormat(FormatText),
)
_, err := engine.Load("missing.txt")
if !errors.Is(err, ErrTemplateNotFound) {
t.Errorf("err = %v, want ErrTemplateNotFound", err)
}
}
// Phase B cycle 3: Engine.Load propagates parse errors.
func TestEngine_Load_PropagatesParseError(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{
"bad.txt": "{% if %}unterminated",
})),
WithFormat(FormatText),
)
_, err := engine.Load("bad.txt")
if err == nil {
t.Fatal("expected parse error, got nil")
}
var parseErr *ParseError
if !errors.As(err, &parseErr) {
t.Fatalf("err = %T, want *ParseError", err)
}
if !strings.Contains(err.Error(), "bad.txt:") {
t.Fatalf("err = %q, want template name prefix", err.Error())
}
}
// Phase B cycle 4: RenderString convenience.
func TestEngine_Render(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{
"t.txt": "a={{ a }} b={{ b }}",
})),
WithFormat(FormatText),
)
got, err := engine.Render("t.txt", Data{"a": 1, "b": 2})
if err != nil {
t.Fatalf("err = %v", err)
}
if got != "a=1 b=2" {
t.Errorf("got %q", got)
}
}
// Phase B cycle 5: Engine.Load caches compiled templates.
func TestEngine_Load_Caches(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{
"a.txt": "hello",
})),
WithFormat(FormatText),
)
first, err := engine.Load("a.txt")
if err != nil {
t.Fatalf("first Load err = %v", err)
}
second, err := engine.Load("a.txt")
if err != nil {
t.Fatalf("second Load err = %v", err)
}
if first != second {
t.Errorf("cache miss: first=%p second=%p", first, second)
}
}
// Phase B cycle 6: Engine.Reset evicts cache.
func TestEngine_Reset_EvictsCache(t *testing.T) {
t.Parallel()
engine := New(
WithLoader(NewMemoryLoader(map[string]string{
"a.txt": "hello",
})),
WithFormat(FormatText),
)
first, err := engine.Load("a.txt")
if err != nil {
t.Fatalf("first Load err = %v", err)
}
engine.Reset()
second, err := engine.Load("a.txt")
if err != nil {
t.Fatalf("second Load err = %v", err)
}
if first == second {
t.Errorf("Reset() did not evict cache")
}
}