-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_test.go
409 lines (352 loc) · 11.5 KB
/
processor_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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"cloud.google.com/go/civil"
"fmt"
"os"
"regexp"
"slices"
"strconv"
"strings"
"testing"
)
const testSiteName = "[TEST SITE]"
const globalIncludeHeaderContent = "[TEST HEADER - GLOBAL LEVEL]"
const themeIncludeHeaderContent = "[TEST HEADER - THEME LEVEL]"
var expectedNonIndexFileContent = /* const */ []string{
"<title>" + testSiteName + " - %s</title>",
}
var trimRegexp = /* const */ regexp.MustCompile(`\n+\s+`)
func TestWithoutCustomHomePage(t *testing.T) {
basicTest(t, false)
}
func TestWithCustomHomePage(t *testing.T) {
basicTest(t, true)
}
func basicTest(t *testing.T, customHomePage bool) {
page1Id := "test-page-1"
page1 := page{
Id: page1Id,
Title: "Test Page 1 Title",
Body: "Test Page 1 Body",
}
page2Id := "test-page-2"
page2 := page{
Id: page2Id,
Title: "Test Page 2 Title",
Body: "Test Page 2 Body",
}
pages := []page{page1, page2}
tag1 := "Tag1"
tag2 := "Tag2"
tag3 := "Tag3"
tags := []string{tag1, tag2, tag3}
post1Id := "post-1"
post1 := post{
Id: post1Id,
Title: "Test Post 1 Title",
Body: "Test Post 1 Body",
Tags: []string{tag1, tag2},
}
post1.Date, _ = civil.ParseDate("2023-08-01")
post1.Time, _ = civil.ParseTime("19:15:00")
post2Id := "post-2"
post2 := post{
Id: post2Id,
Title: "Test Post 2 Title",
Body: "Test Post 2 Body",
Tags: []string{tag1, tag3},
}
post2.Date, _ = civil.ParseDate("2023-09-01")
post2.Time, _ = civil.ParseTime("09:05:00")
posts := []post{post1, post2}
globalIncludes := map[string]string{
"header.html": globalIncludeHeaderContent,
}
themeIncludes := map[string]string{
"header.html": themeIncludeHeaderContent,
}
config := defaultConfig()
config.enableSearch = false
config.siteName = testSiteName
if customHomePage {
config.homePage = page1Id
}
output := processOutput(pages, posts, globalIncludes, themeIncludes, config)
expectedIndexFile := deployDirName + "/" + indexPageFileName
expectedPost1File := deployDirName + "/" + deployPostDirName + "/" + post1.Id + contentFileExtension
expectedPost2File := deployDirName + "/" + deployPostDirName + "/" + post2.Id + contentFileExtension
expectedPage1File := deployDirName + "/" + deployPageDirName + "/" + page1.Id + contentFileExtension
expectedPage2File := deployDirName + "/" + deployPageDirName + "/" + page2.Id + contentFileExtension
expectedFiles := []string{
expectedIndexFile,
expectedPost1File,
expectedPost2File,
expectedPage2File,
}
for _, tag := range tags {
expectedFiles = append(expectedFiles, deployDirName+"/"+deployTagsDirName+"/"+strings.ToLower(tag)+"/"+indexPageFileName)
}
if !customHomePage {
expectedFiles = append(expectedFiles, expectedPage1File)
}
expectedAnyFileContent := []string{
"<header>" + globalIncludeHeaderContent + themeIncludeHeaderContent + "</header>",
}
var expectedIndexFileContent []string
if customHomePage {
expectedIndexFileContent = append(expectedIndexFileContent,
"<title>"+testSiteName+" - "+page1.Title+"</title>")
} else {
expectedIndexFileContent = append(expectedIndexFileContent,
"<title>"+testSiteName+"</title>")
for _, post := range posts {
expectedIndexFileContent = append(expectedIndexFileContent, "<a href=\"/"+deployPostDirName+"/"+post.Id+contentFileExtension+"\"")
for _, epc := range getExpectedPostFileContent(post) {
expectedIndexFileContent = append(expectedIndexFileContent, epc)
}
}
}
for _, ef := range expectedFiles {
outputFileContent, ok := output[ef]
if !ok {
t.Error("Missing expected output file: " + ef)
} else {
for _, ec := range expectedAnyFileContent {
if !strings.Contains(outputFileContent, ec) {
missingExpectedContentError(t, ef, ec)
}
}
if ef == expectedIndexFile {
for _, ec := range expectedIndexFileContent {
if !strings.Contains(outputFileContent, ec) {
missingExpectedContentError(t, ef, ec)
}
}
} else {
for _, page := range pages {
if strings.Contains(ef, page.Id+contentFileExtension) {
verifyExpectedNonIndexOutputFileContentContainsPageContent(ef, outputFileContent, page, t)
}
}
for _, post := range posts {
if strings.Contains(ef, post.Id+contentFileExtension) {
verifyExpectedNonIndexOutputFileContentContainsPostContent(ef, outputFileContent, post, t)
}
}
for _, tag := range tags {
if strings.Contains(ef, strings.ToLower(tag)+contentFileExtension) {
for _, post := range posts {
if slices.Contains(post.Tags, tag) {
verifyExpectedNonIndexOutputFileContentContainsPostContent(ef, outputFileContent, post, t)
}
}
break
}
}
}
}
}
}
func TestPagination1(t *testing.T) {
testPagination(t, 19)
}
func TestPagination2(t *testing.T) {
testPagination(t, 40)
}
func TestPagination3(t *testing.T) {
testPagination(t, 59)
}
func testPagination(t *testing.T, postCnt int) {
var pages []page
var posts []post
globalIncludes := map[string]string{
"header.html": globalIncludeHeaderContent,
}
themeIncludes := map[string]string{
"header.html": themeIncludeHeaderContent,
}
tag1 := "Tag1"
tag2 := "Tag2"
tagCnt := map[string]int{tag1: 0, tag2: 0}
for i := 1; i <= postCnt; i++ {
pn := strconv.Itoa(i)
p := post{
Id: "post-" + pn,
Title: "Test Post " + pn + " Title",
Body: "Test Post " + pn + " Body",
Tags: []string{tag1},
}
tagCnt[tag1] += 1
if i < defaultPageSize {
p.Tags = append(p.Tags, tag2)
tagCnt[tag2] += 1
}
posts = append(posts, p)
}
config := defaultConfig()
config.enableSearch = false
config.siteName = testSiteName
output := processOutput(pages, posts, globalIncludes, themeIncludes, config)
expectedFiles := []string{
deployDirName + "/" + indexPageFileName,
}
unexpectedFiles := []string{
deployDirName + "/" + deployTagsDirName + "/" + strings.ToLower(tag2) + "/2" + contentFileExtension,
}
totalPageCnt := postCnt / defaultPageSize
if postCnt%defaultPageSize > 0 {
totalPageCnt++
}
for i := 2; i <= totalPageCnt; i++ {
expectedFiles = append(expectedFiles, deployDirName+"/"+deployPostsDirName+"/"+strconv.Itoa(i)+contentFileExtension)
}
for tag, cnt := range tagCnt {
expectedFiles = append(expectedFiles, deployDirName+"/"+deployTagsDirName+"/"+strings.ToLower(tag)+"/"+indexPageFileName)
if cnt > defaultPageSize {
tagPageCnt := cnt / defaultPageSize
if cnt%defaultPageSize > 0 {
tagPageCnt++
}
for i := 2; i <= tagPageCnt; i++ {
expectedFiles = append(expectedFiles, deployDirName+"/"+deployTagsDirName+"/"+strings.ToLower(tag)+"/"+strconv.Itoa(i)+contentFileExtension)
}
}
}
for _, ef := range expectedFiles {
_, ok := output[ef]
if !ok {
t.Error("Missing expected output file: " + ef)
}
}
for _, ef := range unexpectedFiles {
_, ok := output[ef]
if ok {
t.Error("Unexpected output file: " + ef)
}
}
for i := 1; i <= totalPageCnt; i++ {
var outputFile string
if i == 1 {
outputFile = deployDirName + "/" + indexPageFileName
} else {
outputFile = deployDirName + "/" + deployPostsDirName + "/" + strconv.Itoa(i) + contentFileExtension
}
outputFileContent := output[outputFile]
for j := (i - 1) * defaultPageSize; j < i*defaultPageSize && j < postCnt; j++ {
verifyExpectedOutputFileContentContainsPostContent(outputFile, outputFileContent, posts[j], t)
}
}
for tag, cnt := range tagCnt {
for i := 1; i <= cnt; i++ {
var outputFile string
if i == 1 {
outputFile = deployDirName + "/" + deployTagsDirName + "/" + strings.ToLower(tag) + "/" + indexPageFileName
} else {
outputFile = deployDirName + "/" + deployTagsDirName + "/" + strings.ToLower(tag) + "/" + strconv.Itoa(i) + contentFileExtension
}
outputFileContent := output[outputFile]
for j := (i - 1) * defaultPageSize; j < i*defaultPageSize && j < cnt; j++ {
verifyExpectedOutputFileContentContainsPostContent(outputFile, outputFileContent, posts[j], t)
}
}
}
}
func processOutput(pages []page, posts []post,
globalIncludes map[string]string,
themeIncludes map[string]string,
config appConfig) map[string]string {
output := make(map[string]string)
defaultThemeTemplatesDir := fmt.Sprintf("%s%c%s%c%s", "themes", os.PathSeparator, defaultThemeName, os.PathSeparator, "templates")
process(pages, posts,
resourceLoader{
config: config,
loadTemplate: func(templateFileName string) ([]byte, error) {
templateFilePath := fmt.Sprintf("%s%c%s", defaultThemeTemplatesDir, os.PathSeparator, templateFileName)
return os.ReadFile(templateFilePath)
},
loadInclude: func(includeFileName string, level templateIncludeLevel) ([]byte, error) {
var includeContent string
var ok bool
switch level {
case Global:
includeContent, ok = globalIncludes[includeFileName]
case Theme:
includeContent, ok = themeIncludes[includeFileName]
}
if ok {
return []byte(includeContent), nil
} else {
return nil, nil
}
},
},
func(outputFilePath string, data []byte) bool {
output[outputFilePath] = strings.ReplaceAll(string(trimRegexp.ReplaceAll(data, []byte(""))), "\n", "")
return false
})
return output
}
func verifyExpectedNonIndexOutputFileContentContainsPageContent(outputFile string, outputFileContent string, p page, t *testing.T) {
for _, ec := range expectedNonIndexFileContent {
etc := fmt.Sprintf(ec, p.Title)
if !strings.Contains(outputFileContent, etc) {
missingExpectedContentError(t, outputFile, etc)
}
for _, epc := range getExpectedPageFileContent(p) {
if !strings.Contains(outputFileContent, epc) {
missingExpectedContentError(t, outputFile, epc)
}
}
}
}
func verifyExpectedNonIndexOutputFileContentContainsPostContent(outputFile string, outputFileContent string, p post, t *testing.T) {
for _, ec := range expectedNonIndexFileContent {
etc := fmt.Sprintf(ec, p.Title)
if !strings.Contains(outputFileContent, etc) {
missingExpectedContentError(t, outputFile, etc)
}
for _, epc := range getExpectedPostFileContent(p) {
if !strings.Contains(outputFileContent, epc) {
missingExpectedContentError(t, outputFile, epc)
}
}
}
}
func verifyExpectedOutputFileContentContainsPostContent(outputFile string, outputFileContent string, p post, t *testing.T) {
for _, epc := range getExpectedPostFileContent(p) {
if !strings.Contains(outputFileContent, epc) {
missingExpectedContentError(t, outputFile, epc)
}
}
}
func getExpectedPageFileContent(p page) []string {
expectedContent := []string{
fmt.Sprintf("<header class=\"title\"><span class=\"title\">%s</span></header>", p.Title),
fmt.Sprintf("<section class=\"content\">%s</section>", p.Body),
}
return expectedContent
}
func getExpectedPostFileContent(p post) []string {
expectedContent := []string{
fmt.Sprintf("<span class=\"title\">%s</span>", p.Title),
fmt.Sprintf("<section class=\"content\">%s</section>", p.Body),
}
if !p.Date.IsZero() {
expectedContent = append(expectedContent, fmt.Sprintf("<span class=\"date\">%s</span>", p.FmtDate()))
}
if !p.Time.IsZero() {
expectedContent = append(expectedContent, fmt.Sprintf("<span class=\"time\">%s</span>", p.FmtTime()))
}
if len(p.Tags) > 0 {
expectedTagsContent := "<span class=\"tags\">"
for _, tag := range p.Tags {
expectedTagsContent += fmt.Sprintf("<a class=\"tag\" href=\"/tags/%s/\">%s</a>", strings.ToLower(tag), tag)
}
expectedTagsContent += "</span>"
expectedContent = append(expectedContent, expectedTagsContent)
}
return expectedContent
}
func missingExpectedContentError(t *testing.T, fileName string, expectedContent string) {
t.Errorf("Processed file - %s - is missing expected content: %s", fileName, expectedContent)
}