Debian Codegraph @

core/templatecache_test.go

raw
1 package core
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/miniscruff/changie/then"
10 )
11
12 func TestCachesTemplateForReuse(t *testing.T) {
13 cache := NewTemplateCache()
14
15 first, err := cache.Load("### {{.Kind}}")
16 then.Nil(t, err)
17
18 second, err := cache.Load("### {{.Kind}}")
19 then.Nil(t, err)
20
21 // use == to compare pointers
22 then.True(t, first == second)
23 }
24
25 func TestCacheExecuteTemplate(t *testing.T) {
26 cache := NewTemplateCache()
27 builder := &strings.Builder{}
28 data := map[string]string{
29 "A": "apple",
30 }
31
32 err := cache.Execute("A: {{.A}}", builder, data)
33 then.Nil(t, err)
34 then.Equals(t, "A: apple", builder.String())
35 }
36
37 func TestCanUseSprigFuncs(t *testing.T) {
38 cache := NewTemplateCache()
39 builder := &strings.Builder{}
40 expected := "HELLO!HELLO!HELLO!HELLO!HELLO!"
41
42 err := cache.Execute("{{ \"hello!\" | upper | repeat 5 }}", builder, nil)
43 then.Nil(t, err)
44 then.Equals(t, expected, builder.String())
45 }
46
47 func TestCanCountStrings(t *testing.T) {
48 cache := NewTemplateCache()
49 builder := &strings.Builder{}
50 data := BatchData{
51 Changes: []Change{
52 {Kind: "added", Body: "A"},
53 {Kind: "added", Body: "B"},
54 {Kind: "removed", Body: "C"},
55 },
56 }
57
58 err := cache.Execute("{{ kinds .Changes | count \"added\" }} kinds", builder, data)
59 then.Nil(t, err)
60 then.Equals(t, "2 kinds", builder.String())
61 }
62
63 func TestCanGetKinds(t *testing.T) {
64 cache := NewTemplateCache()
65 builder := &strings.Builder{}
66 data := BatchData{
67 Changes: []Change{
68 {Kind: "added", Body: "A"},
69 {Kind: "added", Body: "B"},
70 {Kind: "removed", Body: "C"},
71 },
72 }
73
74 err := cache.Execute("{{ kinds .Changes }} kinds", builder, data)
75 then.Nil(t, err)
76 then.Equals(t, "[added added removed] kinds", builder.String())
77 }
78
79 func TestCanGetComponents(t *testing.T) {
80 cache := NewTemplateCache()
81 builder := &strings.Builder{}
82 data := BatchData{
83 Changes: []Change{
84 {Component: "ui", Body: "A"},
85 {Component: "ui", Body: "B"},
86 {Component: "backend", Body: "C"},
87 },
88 }
89
90 err := cache.Execute("{{ components .Changes }} components", builder, data)
91 then.Nil(t, err)
92 then.Equals(t, "[ui ui backend] components", builder.String())
93 }
94
95 func TestCanGetBodies(t *testing.T) {
96 cache := NewTemplateCache()
97 builder := &strings.Builder{}
98 data := BatchData{
99 Changes: []Change{
100 {Component: "ui", Body: "A"},
101 {Component: "ui", Body: "B"},
102 {Component: "backend", Body: "C"},
103 },
104 }
105
106 err := cache.Execute("{{ bodies .Changes }} bodies", builder, data)
107 then.Nil(t, err)
108 then.Equals(t, "[A B C] bodies", builder.String())
109 }
110
111 func TestCanGetTimes(t *testing.T) {
112 cache := NewTemplateCache()
113 builder := &strings.Builder{}
114 expected := fmt.Sprintf(
115 "[%v %v %v] times",
116 "2022-01-01 09:30:00 +0000 UTC",
117 "2022-02-01 10:30:00 +0000 UTC",
118 "2022-03-01 11:30:00 +0000 UTC",
119 )
120 data := BatchData{
121 Changes: []Change{
122 {Time: time.Date(2022, time.January, 1, 9, 30, 0, 0, time.UTC)},
123 {Time: time.Date(2022, time.February, 1, 10, 30, 0, 0, time.UTC)},
124 {Time: time.Date(2022, time.March, 1, 11, 30, 0, 0, time.UTC)},
125 },
126 }
127
128 err := cache.Execute("{{ times .Changes }} times", builder, data)
129 then.Nil(t, err)
130 then.Equals(t, expected, builder.String())
131 }
132
133 func TestCanGetCustoms(t *testing.T) {
134 cache := NewTemplateCache()
135 builder := &strings.Builder{}
136 data := BatchData{
137 Changes: []Change{
138 {Custom: map[string]string{"Author": "miniscruff"}},
139 {Custom: map[string]string{"Author": "rocktavious"}},
140 {Custom: map[string]string{"Author": "emmyoop"}},
141 },
142 }
143
144 err := cache.Execute("{{ customs .Changes \"Author\" }} authors", builder, data)
145 then.Nil(t, err)
146 then.Equals(t, "[miniscruff rocktavious emmyoop] authors", builder.String())
147 }
148
149 func TestCanGetCustomsMissingKey(t *testing.T) {
150 cache := NewTemplateCache()
151 builder := &strings.Builder{}
152 data := BatchData{
153 Changes: []Change{
154 {Custom: map[string]string{"Author": "miniscruff"}},
155 {Custom: map[string]string{"NotAuthor": "rocktavious"}},
156 {Custom: map[string]string{"Author": "emmyoop"}},
157 },
158 }
159
160 err := cache.Execute("{{ customs .Changes \"Author\" | compact }} authors", builder, data)
161 then.Nil(t, err)
162 then.Equals(t, "[miniscruff emmyoop] authors", builder.String())
163 }
164
165 func TestErrorBadTemplate(t *testing.T) {
166 cache := NewTemplateCache()
167 _, err := cache.Load("{{...___..__}}")
168 then.NotNil(t, err)
169 }
170
171 func TestErrorUsingSameTemplateTwice(t *testing.T) {
172 cache := NewTemplateCache()
173 _, err := cache.Load("{{...___..__}}")
174 then.NotNil(t, err)
175
176 _, err = cache.Load("{{...___..__}}")
177 then.NotNil(t, err)
178 }
179
180 func TestErrorBadExecute(t *testing.T) {
181 cache := NewTemplateCache()
182 builder := &strings.Builder{}
183
184 err := cache.Execute("bad template {{...__.}}", builder, struct{}{})
185 then.NotNil(t, err)
186 then.Equals(t, builder.Len(), 0)
187 }