-
Notifications
You must be signed in to change notification settings - Fork 4
/
sitegen_test.go
302 lines (281 loc) · 7.05 KB
/
sitegen_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"encoding/json"
"fmt"
"path/filepath"
"testing"
)
func testSiteGen() *SiteGen {
return newSiteGen("./site", "templates", "data", "src", "./public", "/", nil, true, true)
}
func TestGetSources(t *testing.T) {
sg := testSiteGen()
var tests = []struct {
pattern string
key string
want int
}{
{"/img/promo.svg", "Path", 1},
{"/news/*", "Path", 5},
{"**about.html", "Filename", 1},
{"index.html", "Filename", 1},
{"Homepage", "Meta.title", 0},
{"About Us", "Meta.title", 1},
}
for _, tt := range tests {
testname := fmt.Sprintf("%s,%s", tt.pattern, tt.key)
t.Run(testname, func(t *testing.T) {
ans := len(sg.getSources(tt.key, tt.pattern))
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
func TestOffset(t *testing.T) {
sources := testSiteGen().sourceList()
total := len(sources)
var tests = []struct {
offset int
want int
}{
{1, total - 1},
{5, total - 5},
{3, total - 3},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d", tt.offset)
t.Run(testname, func(t *testing.T) {
scs, ok := offset(tt.offset, sources).([]*Source)
if !ok {
t.Errorf("expected []*Source type got %v", scs)
}
ans := len(scs)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
func TestLimit(t *testing.T) {
sources := testSiteGen().sourceList()
total := len(sources)
var tests = []struct {
limit int
want int
}{
{1, 1},
{2, 2},
{total + 5, total},
{total, total},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d", tt.limit)
t.Run(testname, func(t *testing.T) {
scs, ok := limit(tt.limit, sources).([]*Source)
if !ok {
t.Errorf("expected []*Source type got %v", scs)
}
ans := len(scs)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
func TestMapToList(t *testing.T) {
sg := testSiteGen()
data := sg.data("site.json")
val, ok := data.(map[string]interface{})
if !ok {
t.Errorf("expected site.json map[string]interface got %T", data)
}
list := mapToList(val)
for _, kv := range list {
if val[kv.Key] != kv.Value {
t.Errorf("expects %s got %s", val[kv.Key], kv.Value)
}
}
}
func TestSort(t *testing.T) {
sg := testSiteGen()
sources := sg.getSources("Path", "/news/*")
var tests = []struct {
by string
order string
want []string
}{
{"Meta.date", "desc", []string{"2020-01-05", "2020-01-04", "2020-01-03", "2020-01-02", "2020-01-01"}},
{"Meta.date", "asc", []string{"2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05"}},
}
for _, tt := range tests {
testname := fmt.Sprintf("sort sources %s,%s", tt.by, tt.order)
t.Run(testname, func(t *testing.T) {
ans := sortBy(tt.by, tt.order, sources)
for i, w := range tt.want {
a, ok := ans[i].(*Source)
if !ok {
t.Errorf("%s expects *Source got %T", tt.order, ans[i])
} else if a.Meta["date"] != w {
t.Errorf("%s got %v, want %v", tt.order, a.Meta["date"], w)
}
}
})
}
var testJson = []struct {
by string
order string
want []string
}{
{"name", "desc", []string{"Posts", "News", "Home", "Contact", "About"}},
{"name", "asc", []string{"About", "Contact", "Home", "News", "Posts"}},
}
data := sg.data("links.json")
for _, tt := range testJson {
testname := fmt.Sprintf("sort links.json %s,%s", tt.by, tt.order)
t.Run(testname, func(t *testing.T) {
ans := sortBy(tt.by, tt.order, data)
for i, w := range tt.want {
a, ok := ans[i].(map[string]interface{})
if !ok {
t.Errorf("%s expects *Source got %T", tt.order, ans[i])
} else if a[tt.by] != w {
t.Errorf("%s got %v, want %v", tt.order, a[tt.by], w)
}
}
})
}
var testSortedMap = []struct {
by string
order string
want []string
}{
{"Key", "desc", []string{"url", "title", "description"}},
{"Key", "asc", []string{"description", "title", "url"}},
}
val := sg.data("site.json").(map[string]interface{})
list := mapToList(val)
for _, tt := range testSortedMap {
testname := fmt.Sprintf("sort map %s,%s", tt.by, tt.order)
t.Run(testname, func(t *testing.T) {
ans := sortBy(tt.by, tt.order, list)
for i, w := range tt.want {
a, ok := ans[i].(kv)
if !ok {
t.Errorf("%s expects kv got %T", tt.order, ans[i])
} else if a.Key != w {
t.Errorf("%s got %v, want %v", tt.order, a.Key, w)
}
}
})
}
}
func TestFilter(t *testing.T) {
var d interface{}
data := []byte(`[
{"Page":"Abc"},
{"Page":"A2c"},
{"Page":"def"}
]`)
if err := json.Unmarshal(data, &d); err != nil {
t.Fatalf("failed unmarshal %v", err)
}
var tests = []struct {
by string
pattern string
want []string
}{
{"Page", `^[A-Za-z]+$`, []string{"Abc", "def"}},
{"Page", `\d+`, []string{"A2c"}},
}
for _, tt := range tests {
testname := fmt.Sprintf("filter %s,%s", tt.by, tt.pattern)
t.Run(testname, func(t *testing.T) {
ans := filterBy(tt.by, tt.pattern, d)
for i, w := range tt.want {
a, ok := ans[i].(map[string]interface{})
if !ok {
t.Errorf("%s expects kv got %T", tt.pattern, ans[i])
} else if a[tt.by] != w {
t.Errorf("%s got %v, want %v", tt.pattern, a[tt.by], w)
}
}
})
}
}
func TestLocalToPath(t *testing.T) {
sg := testSiteGen()
var tests = []struct {
source *Source
want string
}{
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "index.html")], "/"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "css", "styles.css")], "/css/styles.css"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "img", "promo.svg")], "/img/promo.svg"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "404.html")], "/404.html"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "news.html")], "/news"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "contact.html")], "/contact"},
{sg.sources[filepath.Join(sg.sitePath, sg.sourceDir, "news", "2020-01-01.html")], "/news/2020-01-01"},
}
for _, tt := range tests {
testname := tt.source.Local
t.Run(testname, func(t *testing.T) {
ans := sg.localToPath(tt.source)
if ans != tt.want {
t.Errorf("got %s, want %s", ans, tt.want)
}
})
}
}
func TestParseContent(t *testing.T) {
var tests = []struct {
input string
sep string
want1 string
want2 string
}{
{"ABC---DEF---GHI", "---", "DEF", "ABCGHI"},
{`ABC
---
DEF: 123
---
GHI`, "---", "\nDEF: 123\n", "ABC\n\nGHI"},
{`/*
---
serve: npm run build
build: npm run prod
---
*/`, "---", `
serve: npm run build
build: npm run prod
`, "/*\n\n*/"},
}
for _, tt := range tests {
testname := tt.input
t.Run(testname, func(t *testing.T) {
ans1, ans2 := parseContent([]byte(tt.input), tt.sep)
if string(ans1) != tt.want1 {
t.Errorf("got %s, want1 %s", string(ans1), tt.want1)
} else if string(ans2) != tt.want2 {
t.Errorf("got %s, want2 %s", string(ans2), tt.want2)
}
})
}
}
func TestData(t *testing.T) {
sg := testSiteGen()
var tests = []struct {
data string
want int
}{
{"links.json", 4},
{"site.json", 2},
}
for _, tt := range tests {
testname := tt.data
t.Run(testname, func(t *testing.T) {
sg.data(tt.data)
// todo
})
}
}