-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_test.go
390 lines (369 loc) · 11.9 KB
/
views_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
package treetop
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestNewView(t *testing.T) {
base := NewView("base.html", Constant("ok!"))
err := assertViewDetails(base, "base.html", "ok!")
if err != nil {
t.Error(err)
}
}
func TestSubView(t *testing.T) {
base := NewView("base.html", Constant("base!"))
sub := base.NewSubView("sub-block", "sub.html", Constant("sub!"))
var err error
err = assertViewDetails(sub, "sub.html", "sub!")
if err != nil {
t.Error(err)
}
err = assertViewDetails(sub.Parent, "base.html", "base!")
if err != nil {
t.Error(err)
}
}
func TestDefaultSubView(t *testing.T) {
base := NewView("base.html", Constant("base!"))
base.NewDefaultSubView("sub-block", "sub.html", Constant("sub!"))
sub, ok := base.SubViews["sub-block"]
if !ok {
t.Error("Failed to register block with base")
}
err := assertViewDetails(sub, "sub.html", "sub!")
if err != nil {
t.Error(err)
}
}
func TestHasSubview(t *testing.T) {
base := NewView("base.html", Constant("base!"))
base.HasSubView("sub-block")
sub, ok := base.SubViews["sub-block"]
if !ok {
t.Error("Failed to register block with base")
}
if sub != nil {
t.Errorf("HasSubView unexpected subview: %v", sub)
}
}
func TestHasSubviewNoEffectOnDefault(t *testing.T) {
base := NewView("base.html", Constant("base!"))
base.NewDefaultSubView("sub-block", "sub.html", Constant("sub!"))
base.HasSubView("sub-block")
sub, ok := base.SubViews["sub-block"]
if !ok {
t.Error("Failed to register block with base")
}
err := assertViewDetails(sub, "sub.html", "sub!")
if err != nil {
t.Error(err)
}
}
func TestCopyView(t *testing.T) {
// create a base view then copy it and make changes
base := NewView("base.html", Constant("base!"))
base.NewDefaultSubView("sub-block", "sub.html", Constant("sub!"))
copy := base.Copy()
copy.Template = "copy.html"
copy.HandlerFunc = Constant("copy!")
copy.NewDefaultSubView("sub-block", "subCopy.html", Constant("subCopy!"))
// assert that changes to the copy did not affect the original view
var err error
err = assertViewDetails(base, "base.html", "base!")
if err != nil {
t.Error(err)
}
basesub, ok := base.SubViews["sub-block"]
if !ok {
t.Error("Failed to register block with base")
}
err = assertViewDetails(basesub, "sub.html", "sub!")
if err != nil {
t.Error(err)
}
err = assertViewDetails(copy, "copy.html", "copy!")
if err != nil {
t.Error(err)
}
copysub, ok := copy.SubViews["sub-block"]
if !ok {
t.Error("Failed to register block with copy")
}
err = assertViewDetails(copysub, "subCopy.html", "subCopy!")
if err != nil {
t.Error(err)
}
}
func TestCopySubView(t *testing.T) {
base := NewView("base.html", Constant("github.com/rur/treetop.Constant.func1"))
sub := base.NewSubView("test", "sub.html", Constant("sub!"))
copy := sub.Copy()
if copy.Defines != "test" {
t.Errorf("Expecting copy to have defines of 'test', got %s", copy.Defines)
}
err := assertViewDetails(copy.Parent, "base.html", "github.com/rur/treetop.Constant.func1")
if err != nil {
t.Error(err)
}
}
func TestCompileViews(t *testing.T) {
type TestCase struct {
name string
view *View
includes []*View
expectPage string
expectView string
expectIncludes []string
}
cases := []TestCase{
{
name: "nil case",
view: nil,
expectPage: "- nil",
expectView: "- nil",
},
{
name: "basic",
view: NewView("base.html", Constant("github.com/rur/treetop.Constant.func1")),
expectPage: `- View("base.html", github.com/rur/treetop.Constant.func1)`,
expectView: `- View("base.html", github.com/rur/treetop.Constant.func1)`,
expectIncludes: []string{},
}, {
name: "with includes",
view: NewView("base.html", Constant("github.com/rur/treetop.Constant.func1")),
includes: []*View{
NewView("other.html", Constant("other!")),
},
expectPage: `- View("base.html", github.com/rur/treetop.Constant.func1)`,
expectView: `- View("base.html", github.com/rur/treetop.Constant.func1)`,
expectIncludes: []string{
`- View("other.html", github.com/rur/treetop.Constant.func1)`,
},
}, {
name: "with parent view",
view: func() *View {
base := NewView("base.html", Noop)
return base.NewSubView("test", "test.html", Constant("test!"))
}(),
expectPage: `
- View("base.html", github.com/rur/treetop.Noop)
'- test: SubView("test", "test.html", github.com/rur/treetop.Constant.func1)
`,
expectView: `- SubView("test", "test.html", github.com/rur/treetop.Constant.func1)`,
}, {
name: "with parent and overrideing includes",
view: func() *View {
base := NewView("base.html", Noop)
base.NewSubView("other", "never_used.html", Noop)
return base.NewSubView("test", "test.html", Constant("test!"))
}(),
expectPage: `
- View("base.html", github.com/rur/treetop.Noop)
|- other: SubView("other", "other.html", github.com/rur/treetop.Constant.func1)
'- test: SubView("test", "test.html", github.com/rur/treetop.Constant.func1)
`,
expectView: `- SubView("test", "test.html", github.com/rur/treetop.Constant.func1)`,
expectIncludes: []string{
`- SubView("other", "other.html", github.com/rur/treetop.Constant.func1)`,
},
includes: []*View{
NewSubView("other", "other.html", Constant("other!")),
},
}, {
name: "include that overrides block in view",
view: func() *View {
base := NewView("base.html", Noop)
base.NewSubView("other", "never_used.html", Noop)
t := base.NewSubView("test", "test.html", Constant("test!"))
t.NewSubView("subother", "never_used.html", Noop)
return t
}(),
expectPage: `
- View("base.html", github.com/rur/treetop.Noop)
|- other: SubView("other", "other.html", github.com/rur/treetop.Constant.func1)
'- test: SubView("test", "test.html", github.com/rur/treetop.Constant.func1)
'- subother: SubView("subother", "subother.html", github.com/rur/treetop.Constant.func1)
`,
expectView: `
- SubView("test", "test.html", github.com/rur/treetop.Constant.func1)
'- subother: SubView("subother", "subother.html", github.com/rur/treetop.Constant.func1)
`,
expectIncludes: []string{
`- SubView("other", "other.html", github.com/rur/treetop.Constant.func1)`,
},
includes: []*View{
NewSubView("other", "other.html", Constant("other!")),
NewSubView("subother", "subother.html", Constant("subother!")),
},
}, {
name: "page include a chain of children",
view: func() *View {
base := NewView("base.html", Noop)
c := base.NewSubView("content", "content.html", Noop)
s := c.NewSubView("sub", "sub.html", Noop)
return s
}(),
expectPage: `
- View("base.html", github.com/rur/treetop.Noop)
'- content: SubView("content", "content.html", github.com/rur/treetop.Noop)
'- sub: SubView("sub", "sub.html", github.com/rur/treetop.Noop)
`,
expectView: `- SubView("sub", "sub.html", github.com/rur/treetop.Noop)`,
},
}
for _, tCase := range cases {
t.Run(tCase.name, func(t *testing.T) {
page, view, incl := CompileViews(tCase.view, tCase.includes...)
pageStr := normalizeTreePrint(SprintViewTree(page))
expectPage := normalizeTreePrint(tCase.expectPage)
if expectPage != pageStr {
t.Errorf("Expecting Page =\n%s\nwant\n%s", pageStr, expectPage)
return
}
viewStr := normalizeTreePrint(SprintViewTree(view))
expectView := normalizeTreePrint(tCase.expectView)
if expectView != viewStr {
t.Errorf("Expecting View =\n%s\nwant\n%s", viewStr, expectView)
return
}
inclS := make([]string, len(incl))
for i, inc := range incl {
inclS[i] = normalizeTreePrint(SprintViewTree(inc))
}
expectIncl := make([]string, len(tCase.expectIncludes))
for i, inc := range tCase.expectIncludes {
expectIncl[i] = normalizeTreePrint(inc)
}
if !reflect.DeepEqual(expectIncl, inclS) {
t.Errorf("Expecting Include =\n%v\nwant\n%v", inclS, expectIncl)
return
}
})
}
}
// ---------
// Helpers
// ---------
// assertViewDetails is used for asserting that a view matches an expected template and
// data handler return value. This is for tests because it is expecting that the view handler
// will return string and not require use a the request object to do so.
func assertViewDetails(v *View, t string, data string) error {
if v.Template != t {
return fmt.Errorf("expecting template %s got %s", t, v.Template)
}
switch got := v.HandlerFunc(&ResponseWrapper{}, nil).(type) {
case string:
if got != data {
return fmt.Errorf("expecting %s got %s", data, got)
}
return nil
default:
return fmt.Errorf("unexpected return value from base handler %v", got)
}
}
func Test_insertView(t *testing.T) {
tests := []struct {
name string
view *View
child *View
want string
wantFound bool
}{
{
name: "basic",
view: NewView("test.html", Noop),
child: nil,
want: `- View("test.html", github.com/rur/treetop.Noop)`,
wantFound: false,
},
{
name: "found",
view: func() *View {
v := NewView("base.html", Noop)
v.NewDefaultSubView("test", "gone.html", Noop)
return v
}(),
child: NewSubView("test", "inserted.html", Noop),
want: `
- View("base.html", github.com/rur/treetop.Noop)
'- test: SubView("test", "inserted.html", github.com/rur/treetop.Noop)
`,
wantFound: true,
},
{
name: "found, without interfering with other views",
view: func() *View {
v := NewView("base.html", Noop)
v.NewDefaultSubView("test", "gone.html", Noop)
v.NewDefaultSubView("test_other", "unaffected.html", Noop)
return v
}(),
child: func() *View {
b := NewView("different_base.html", Noop)
incl := b.NewDefaultSubView("test", "included.html", Noop)
incl.NewDefaultSubView("test_sub", "inclSub.html", Noop)
return incl
}(),
want: `
- View("base.html", github.com/rur/treetop.Noop)
|- test: SubView("test", "included.html", github.com/rur/treetop.Noop)
| '- test_sub: SubView("test_sub", "inclSub.html", github.com/rur/treetop.Noop)
|
'- test_other: SubView("test_other", "unaffected.html", github.com/rur/treetop.Noop)
`,
wantFound: true,
},
{
name: "keep nil",
view: func() *View {
v := NewView("base.html", Noop)
v.NewDefaultSubView("test", "gone.html", Noop)
v.NewSubView("another", "gone.html", Noop)
return v
}(),
child: NewSubView("test", "inserted.html", Noop),
want: `
- View("base.html", github.com/rur/treetop.Noop)
|- another: nil
'- test: SubView("test", "inserted.html", github.com/rur/treetop.Noop)
`,
wantFound: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
view, found := insertView(tt.view, tt.child)
if found != tt.wantFound {
t.Errorf("insetView() got found %v, want %v", found, tt.wantFound)
}
expect := normalizeTreePrint(tt.want)
got := SprintViewTree(view)
if expect != got {
t.Errorf("insertView() got \n%s\nexpecting\n%s", got, expect)
}
})
}
}
func TestViewBaseCopied(t *testing.T) {
// test that views altered by compiling have been made immutable
base := NewView("base.html", Noop)
firstChild := base.NewSubView("test", "firstChild.html", Noop)
p, _, _ := CompileViews(firstChild)
base.NewDefaultSubView("test2", "secondChild.html", Noop)
if strings.Contains(SprintViewTree(p), "test2") {
t.Errorf("Changing base cause compiled view to change: got\n%s", SprintViewTree(p))
}
}
func TestViewUnchangedUntilAfterCompile(t *testing.T) {
// have a child of the base which is mutated AFTER compiling the views
base := NewView("base.html", Noop)
test2 := base.NewDefaultSubView("test2", "secondChild.html", Noop)
firstChild := base.NewSubView("test", "firstChild.html", Noop)
p, _, _ := CompileViews(firstChild)
test2.NewDefaultSubView("test2_sub", "shouldNotBeInFirstChild.html", Noop)
if strings.Contains(SprintViewTree(p), "test2_sub") {
t.Errorf("Changing sibling view cause compiled view page to change: got\n%s", SprintViewTree(p))
}
}