-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagr_test.go
246 lines (220 loc) · 6.13 KB
/
pagr_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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
/* shared *_test.go functions, since pagr.go doesn't have anything that requires testing */
var templates = map[string]string{ // [ext]template
"tmpl": "{{.Contents}}",
"hmpl": "{{.Contents}}",
"mst": "{{Contents}}",
}
func createTestTemplates(dir string) (err error) {
writef := func(path, data string) {
if err == nil {
err = ioutil.WriteFile(path, []byte(data), 0644)
}
}
for ext, data := range templates {
writef(filepath.Join(dir, fmt.Sprintf("root.%s", ext)), data)
writef(filepath.Join(dir, fmt.Sprintf("root.ignore.%s", ext)), data)
writef(filepath.Join(dir, fmt.Sprintf("root.%s.ignore", ext)), data)
writef(filepath.Join(dir, fmt.Sprintf("partial.%s", ext)), data)
writef(filepath.Join(dir, fmt.Sprintf("partial.ignore.%s", ext)), data)
writef(filepath.Join(dir, fmt.Sprintf("partial.%s.ignore", ext)), data)
if err != nil {
break
}
}
return err
}
// file contents used in "contents" files
const contentsTxt = `p1
p2
pre1
pre2
p3
p4
`
const contentsHtml = `<p>p1<br>
p2</p>
<pre>pre1
pre2
</pre>
<p>p3</p>
<p>p4</p>`
const contentsMd = `p1
p2
pre1
pre2
p3
`
var contents = map[string]string{
"": contentsTxt,
".txt": contentsTxt,
".html": contentsHtml,
".md": contentsMd,
}
var asset = []byte{ // 5x5 black png image - unecessary but I think this is cool
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00,
0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
0x00, 0x05, 0x01, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xa1, 0xb4, 0xa6,
0x00, 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xa5, 0x67, 0xb9, 0xcf, 0x00, 0x00, 0x00, 0x02,
0x74, 0x52, 0x4e, 0x53, 0xff, 0x00, 0xe5, 0xb7, 0x30, 0x4a, 0x00,
0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc4,
0x00, 0x00, 0x0e, 0xc4, 0x01, 0x95, 0x2b, 0x0e, 0x1b, 0x00, 0x00,
0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x08, 0x99, 0x63, 0x60, 0x66,
0x60, 0x66, 0x60, 0x00, 0x62, 0x76, 0x00, 0x00, 0x4a, 0x00, 0x11,
0x3a, 0x34, 0x8c, 0xad, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
0x44, 0xae, 0x42, 0x60, 0x82,
}
func createTestContents(dir string) (err error) {
writef := func(path, data string) {
if err == nil {
err = ioutil.WriteFile(path, []byte(data), 0644)
}
}
for l, lang := range contentExts {
if l == 0 {
writef(filepath.Join(dir, "defaults.json"), "{ \"default\": \"data\" }")
} else if l == 1 {
dir = filepath.Join(dir, lang[1:])
} else if l >= 2 {
dir = filepath.Join(filepath.Dir(dir), lang[1:])
}
if l >= 1 {
if err := os.MkdirAll(dir, 0775); err != nil {
fmt.Errorf("failed to create temporary test dir '%s': %s", dir, err)
}
}
writef(filepath.Join(dir, "meta.toml"), "page = \"data\"")
writef(filepath.Join(dir, fmt.Sprintf("body%d%s", l, lang)), contents[lang])
writef(filepath.Join(dir, "image.png"), string(asset))
writef(filepath.Join(dir, "video.mkv"), "foo")
writef(filepath.Join(dir, "audio.mp3"), "foo")
writef(filepath.Join(dir, "misc.zip"), "foo")
if err != nil {
break
}
}
return
}
func validateTestPagesNav(t *testing.T, pages []Page) {
for _, p := range pages {
var allUnique []string
for _, navp := range p.Nav.All {
for _, a := range allUnique {
if a == navp.Path {
t.Errorf("'%s' has .Nav.All items with duplicate .Path values (%s)",
p.Path, a)
}
}
allUnique = append(allUnique, navp.Path)
}
if len(p.Nav.All) != len(pages) {
t.Errorf("'%s' has %d in .Nav.All (should be %d)",
p.Path, len(p.Nav.All), len(pages))
}
foundAll := 0
for _, navp := range p.Nav.All {
for _, pp := range pages {
if navp.Path == pp.Path {
foundAll++
break
}
}
}
if foundAll != len(p.Nav.All) {
t.Errorf("found %d/%d pages in .Nav.All for '%s'",
foundAll, len(p.Nav.All), p.Path)
}
foundRoot := false
foundParent := false
for _, pp := range pages {
if !foundRoot {
if p.Nav.Root == nil {
foundRoot = true
} else if p.Nav.Root.Path == pp.Path {
foundRoot = true
}
}
if !foundParent {
if p.Nav.Parent == nil {
foundParent = true
} else if p.Nav.Parent.Path == pp.Path {
foundParent = true
}
}
if foundRoot && foundParent {
break
}
}
if !foundRoot {
t.Errorf("could not find .Root '%s' for '%s'",
p.Nav.Root.Path, p.Path)
}
if !foundParent {
t.Errorf("could not find .Parent '%s' for '%s'",
p.Nav.Parent.Path, p.Path)
}
// TODO test .Nav.Children, figure out how many should exist
// TODO test .Nav.Crumbs, figure out how many should exist
}
}
func validateTestPages(t *testing.T, pages []Page, e error) {
if len(pages) != len(contents) {
t.Fatalf("invalid number of pages returned (%d should be %d)",
len(pages), len(contents))
}
var last, pt time.Time
for i, p := range pages {
if len(p.Slug) == 0 && p.Slug != filepath.Base(p.Path) {
t.Errorf("bad Slug for page: '%s' (%s) - should be '%s'",
p.Slug, p.Path, filepath.Base(p.Path))
}
if len(p.Path) == 0 {
t.Error("empty Path for page:", p)
}
validateTestPagesNav(t, pages)
if _, ok := p.Meta["page"]; !ok || len(p.Meta) == 0 {
t.Errorf("missing page Meta key for page: '%s'", p.Path)
}
if _, ok := p.Meta["default"]; !ok || len(p.Meta) == 0 {
t.Error("empty 'default' Meta key for page:", p.Path)
}
if len(p.Contents) == 0 {
t.Error("empty Contents for page:", p.Path)
}
if len(p.Assets.All) != 4 {
t.Error("invalid number of Assets.All for page:", p.Path)
}
if len(p.Assets.Image) != 1 {
t.Error("invalid number of Assets.Image for page:", p.Path)
}
if len(p.Assets.Video) != 1 {
t.Error("invalid number of Assets.Video for page:", p.Path)
}
if len(p.Assets.Audio) != 1 {
t.Error("invalid number of Assets.Audio for page:", p.Path)
}
if len(p.Assets.Misc) != 1 {
t.Error("invalid number of Assets.Misc for page:", p.Path)
}
if pt, e = time.Parse(timefmt, p.Updated); e != nil {
t.Fatal(e)
}
if i == 0 {
last = pt
} else if pt.Before(last) {
for _, pp := range pages {
t.Logf("%s - %s", pp.Path, pp.Updated)
}
t.Error("Contents Pages returned in wrong order")
}
}
}