-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer_test.go
288 lines (276 loc) · 8.51 KB
/
printer_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
package treetop
import (
"regexp"
"strings"
"testing"
)
func Test_previewString(t *testing.T) {
type args struct {
str string
before int
after int
}
tests := []struct {
name string
args args
want string
}{
{
name: "basic",
args: args{
str: "some/path/on/fs.html",
before: 10,
after: 10,
},
want: `some/path/on/fs.html`,
},
{
name: "realistic html",
args: args{
str: fromExampleDotCom,
before: 10,
after: 10,
},
want: `<!doctypeh……dy></html>`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := previewString(tt.args.str, tt.args.before, tt.args.after); got != tt.want {
t.Errorf("previewString() = %v, want %v", got, tt.want)
}
})
}
}
func TestSprintViewTreeDebug(t *testing.T) {
tests := []struct {
name string
v *View
want string
}{
{
name: "nil case",
v: nil,
want: `- nil`,
},
{
name: "single view usage",
v: NewView("test.html", Constant("test!")),
want: `- View("test.html", github.com/rur/treetop.TestSprintViewTreeDebug.func2)`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expecting := normalizeTreePrint(tt.want)
got := normalizeTreePrint(SprintViewTree(tt.v))
if got != expecting {
t.Errorf("SprintViewTree() =\n%s\nwant\n%s", got, expecting)
}
})
}
}
func TestSprintViewTree(t *testing.T) {
tests := []struct {
name string
v *View
want string
}{
{
name: "nil case",
v: nil,
want: `- nil`,
},
{
name: "single view usage",
v: NewView("test.html", Constant("test!")),
want: `- View("test.html", github.com/rur/treetop.Constant.func1)`,
},
{
name: "view with sub views",
v: func() *View {
v := NewView("base.html", Constant("base!"))
v.NewDefaultSubView("A", "A.html", Constant("A!"))
v.NewDefaultSubView("B", "B.html", Constant("B!"))
return v
}(),
want: `
- View("base.html", github.com/rur/treetop.Constant.func1)
|- A: SubView("A", "A.html", github.com/rur/treetop.Constant.func1)
'- B: SubView("B", "B.html", github.com/rur/treetop.Constant.func1)
`,
},
{
name: "view with sub sub views",
v: func() *View {
v := NewView("base.html", Constant("base!"))
a := v.NewDefaultSubView("A", "A.html", Constant("A!"))
a.NewDefaultSubView("A1", "A1.html", Constant("A1!"))
a.NewDefaultSubView("A2", "A2.html", Constant("A2!"))
b := v.NewDefaultSubView("B", "B.html", Constant("B!"))
b.NewDefaultSubView("B1", "B1.html", Constant("B1!"))
b.NewDefaultSubView("B2", "B2.html", Constant("B2!"))
return v
}(),
want: `
- View("base.html", github.com/rur/treetop.Constant.func1)
|- A: SubView("A", "A.html", github.com/rur/treetop.Constant.func1)
| |- A1: SubView("A1", "A1.html", github.com/rur/treetop.Constant.func1)
| '- A2: SubView("A2", "A2.html", github.com/rur/treetop.Constant.func1)
|
'- B: SubView("B", "B.html", github.com/rur/treetop.Constant.func1)
|- B1: SubView("B1", "B1.html", github.com/rur/treetop.Constant.func1)
'- B2: SubView("B2", "B2.html", github.com/rur/treetop.Constant.func1)
`,
},
{
name: "view with sub sub sub views",
v: func() *View {
v := NewView("base.html", Constant("base!"))
a := v.NewDefaultSubView("A", "A.html", Constant("A!"))
a1 := a.NewDefaultSubView("A1", "A1.html", Constant("A1!"))
a2 := a.NewDefaultSubView("A2", "A2.html", Constant("A2!"))
a1.NewDefaultSubView("A11", "A11.html", Constant("A11!"))
a2.NewDefaultSubView("A21", "A21.html", Constant("A21!"))
b := v.NewDefaultSubView("B", "B.html", Constant("B!"))
b1 := b.NewDefaultSubView("B1", "B1.html", Constant("B1!"))
b1.NewDefaultSubView("B11", "B11.html", Constant("B11!"))
b1.NewDefaultSubView("B12", "B12.html", Constant("B12!"))
b2 := b.NewDefaultSubView("B2", "B2.html", Constant("B2!"))
b2.NewDefaultSubView("B21", "B21.html", Constant("B21!"))
b2.NewDefaultSubView("B22", "B22.html", Constant("B22!"))
return v
}(),
want: `
- View("base.html", github.com/rur/treetop.Constant.func1)
|- A: SubView("A", "A.html", github.com/rur/treetop.Constant.func1)
| |- A1: SubView("A1", "A1.html", github.com/rur/treetop.Constant.func1)
| | '- A11: SubView("A11", "A11.html", github.com/rur/treetop.Constant.func1)
| |
| '- A2: SubView("A2", "A2.html", github.com/rur/treetop.Constant.func1)
| '- A21: SubView("A21", "A21.html", github.com/rur/treetop.Constant.func1)
|
'- B: SubView("B", "B.html", github.com/rur/treetop.Constant.func1)
|- B1: SubView("B1", "B1.html", github.com/rur/treetop.Constant.func1)
| |- B11: SubView("B11", "B11.html", github.com/rur/treetop.Constant.func1)
| '- B12: SubView("B12", "B12.html", github.com/rur/treetop.Constant.func1)
|
'- B2: SubView("B2", "B2.html", github.com/rur/treetop.Constant.func1)
|- B21: SubView("B21", "B21.html", github.com/rur/treetop.Constant.func1)
'- B22: SubView("B22", "B22.html", github.com/rur/treetop.Constant.func1)
`,
},
{
name: "view with nil sub views",
v: func() *View {
v := NewView("base.html", Constant("base!"))
a := v.NewDefaultSubView("A", "A.html", Constant("A!"))
a.NewSubView("A1", "a1.html", Noop)
a.NewDefaultSubView("A2", "A2.html", Constant("A2!"))
b := v.NewDefaultSubView("B", "B.html", Constant("B!"))
b.NewSubView("B1", "b1.html", Noop)
return v
}(),
want: `
- View("base.html", github.com/rur/treetop.Constant.func1)
|- A: SubView("A", "A.html", github.com/rur/treetop.Constant.func1)
| |- A1: nil
| '- A2: SubView("A2", "A2.html", github.com/rur/treetop.Constant.func1)
|
'- B: SubView("B", "B.html", github.com/rur/treetop.Constant.func1)
'- B1: nil
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expecting := normalizeTreePrint(tt.want)
got := normalizeTreePrint(SprintViewTree(tt.v))
if got != expecting {
t.Errorf("SprintViewTree() =\n%s\nwant\n%s", got, expecting)
}
})
}
}
// -----------
// helpers
// -----------
var funNamePattern = regexp.MustCompile(`github\.com/rur/treet[\w\d.…/]*\.func\d+`)
// normalizeTreePrint will trim whitespace from test assertions
// and account for indentation in multiline raw strings.
func normalizeTreePrint(s string) string {
lines := strings.Split(s, "\n")
out := make([]string, 0, len(lines))
var trimLeader int
FirstLineScan:
for i, line := range lines {
for j := range line {
if line[j] != ' ' && line[j] != '\t' {
// this is the first line
// leading whitespace on first line should be
// removed from all subsequent lines
trimLeader = j
lines = lines[i:]
break FirstLineScan
}
}
}
for _, line := range lines {
if len(line) <= trimLeader {
continue
}
modified := line[trimLeader:]
modified = funNamePattern.ReplaceAllString(modified, "<func-path-here>")
out = append(out, modified)
}
return strings.Join(out, "\n")
}
// fromExampleDotCom is used for testing template abbreviation
var fromExampleDotCom = `
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`