-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstd_container_test.go
More file actions
333 lines (294 loc) · 9.67 KB
/
std_container_test.go
File metadata and controls
333 lines (294 loc) · 9.67 KB
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
// Copyright 2017 Brent Rowland.
// Use of this source code is governed the Apache License, Version 2.0, as described in the LICENSE file.
package ltml
import "testing"
func TestStdContainer_Path(t *testing.T) {
i1 := map[string]string{"tag": "foo", "id": "bar", "class": "boom baz"}
e1 := "foo#bar.baz.boom"
var c1 StdContainer
c1.SetIentifiers(i1)
if c1.Path() != e1 {
t.Errorf("Expected <%s>, got <%s>", e1, c1.Path())
}
i2 := map[string]string{"tag": "abc", "id": "def", "class": "jkl ghi"}
e2 := "foo#bar.baz.boom/abc#def.ghi.jkl"
var c2 StdContainer
c2.SetIentifiers(i2)
c2.container = &c1
if c2.Path() != e2 {
t.Errorf("Expected <%s>, got <%s>", e2, c2.Path())
}
}
func TestStdContainer_SetAttrs_ClonesLayoutForLayoutPrefixOverrides(t *testing.T) {
scope := &Scope{}
scope.SetParentScope(&defaultScope)
container := &StdContainer{}
container.SetScope(scope)
container.SetAttrs(map[string]string{
"layout": "vbox",
"layout.vpadding": "9pt",
})
if container.LayoutStyle() == defaultLayouts["vbox"] {
t.Fatal("layout style reused shared vbox layout, want clone")
}
if got := container.LayoutStyle().VPadding(); got != 9 {
t.Fatalf("layout vpadding = %v, want 9", got)
}
if got := defaultLayouts["vbox"].VPadding(); got != 0 {
t.Fatalf("shared vbox vpadding = %v, want 0", got)
}
}
func TestStdContainer_SetAttrs_ParagraphStyleOverridesWithoutParent(t *testing.T) {
container := &StdContainer{}
container.SetAttrs(map[string]string{
"paragraph-style.text-align": "center",
})
if container.paragraphStyle == nil {
t.Fatal("paragraphStyle is nil, want cloned default style")
}
if got := container.ParagraphStyle().ResolvedTextAlign(container); got != HAlignCenter {
t.Fatalf("paragraph text-align = %s, want center", got)
}
}
func TestStdParagraph_SetAttrs_StyleOverridesWithoutParent(t *testing.T) {
paragraph := &StdParagraph{}
paragraph.SetAttrs(map[string]string{
"style.text-align": "right",
})
if paragraph.paragraphStyle == nil {
t.Fatal("paragraphStyle is nil, want cloned default style")
}
if got := paragraph.ParagraphStyle().ResolvedTextAlign(paragraph); got != HAlignRight {
t.Fatalf("paragraph text-align = %s, want right", got)
}
}
func TestStdContainer_PrepareListBullets_ULAssignsDirectChildParagraphsOnly(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<bullet id="custom" text="*" width="12pt" />
<page>
<ul>
<p>First</p>
<label>Not a list item</label>
<p bullet="custom">Second</p>
<p>Third</p>
</ul>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
second := childParagraph(t, list, 2)
third := childParagraph(t, list, 3)
if first.Bullet() == nil || first.Bullet().Shape() != "circle" {
t.Fatalf("first bullet = %#v, want default unordered circle bullet", first.Bullet())
}
if first.Bullet().AlignY() != "middle" {
t.Fatalf("first bullet align-y = %q, want middle", first.Bullet().AlignY())
}
if second.Bullet() == nil || second.Bullet().Text() != "*" {
t.Fatalf("second bullet = %#v, want preserved explicit bullet", second.Bullet())
}
if third.Bullet() == nil || third.Bullet().Shape() != "circle" {
t.Fatalf("third bullet = %#v, want default unordered circle bullet", third.Bullet())
}
if third.Bullet().AlignY() != "middle" {
t.Fatalf("third bullet align-y = %q, want middle", third.Bullet().AlignY())
}
}
func TestStdContainer_PrepareListBullets_OLNumbersParagraphsAndAutoSizesMarkers(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<page>
<ol>
<p>One</p>
<p>Two</p>
<p>Three</p>
<p>Four</p>
<p>Five</p>
<p>Six</p>
<p>Seven</p>
<p>Eight</p>
<p>Nine</p>
<p>Ten</p>
<p>Eleven</p>
<p>Twelve</p>
</ol>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
tenth := childParagraph(t, list, 9)
twelfth := childParagraph(t, list, 11)
if first.Bullet() == nil || first.Bullet().Text() != "1." {
t.Fatalf("first bullet = %#v, want 1.", first.Bullet())
}
if tenth.Bullet() == nil || tenth.Bullet().Text() != "10." {
t.Fatalf("tenth bullet = %#v, want 10.", tenth.Bullet())
}
if twelfth.Bullet() == nil || twelfth.Bullet().Text() != "12." {
t.Fatalf("twelfth bullet = %#v, want 12.", twelfth.Bullet())
}
if first.Bullet().Width() != twelfth.Bullet().Width() || tenth.Bullet().Width() != twelfth.Bullet().Width() {
t.Fatalf("ordered bullet widths = %v/%v/%v, want equal auto-sized widths", first.Bullet().Width(), tenth.Bullet().Width(), twelfth.Bullet().Width())
}
if first.Bullet().Width() <= first.bulletTextWidth(w, first.Bullet()) {
t.Fatalf("ordered marker slot width = %v, want greater than rendered marker width %v", first.Bullet().Width(), first.bulletTextWidth(w, first.Bullet()))
}
}
func TestStdContainer_PrepareListBullets_OLPreservesExplicitBulletAndCountsItsOrdinal(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<bullet id="custom" text="*" width="12pt" />
<page>
<ol>
<p>First</p>
<p bullet="custom">Second</p>
<p>Third</p>
</ol>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
second := childParagraph(t, list, 1)
third := childParagraph(t, list, 2)
if first.Bullet() == nil || first.Bullet().Text() != "1." {
t.Fatalf("first bullet = %#v, want 1.", first.Bullet())
}
if second.Bullet() == nil || second.Bullet().Text() != "*" {
t.Fatalf("second bullet = %#v, want preserved explicit bullet", second.Bullet())
}
if third.Bullet() == nil || third.Bullet().Text() != "3." {
t.Fatalf("third bullet = %#v, want 3.", third.Bullet())
}
}
func TestStdContainer_PrepareListBullets_AssignsMultipleListBulletTemplates(t *testing.T) {
doc, err := Parse([]byte(`
<ltml>
<bullet id="logo" src="logo.svg" width="18pt" height="12pt" />
<page>
<ol bullets="logo ordered">
<p>One</p>
<p>Two</p>
</ol>
</page>
</ltml>`), WithAssetFS(testingMapFS("logo.svg", "<svg/>")))
if err != nil {
t.Fatal(err)
}
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
second := childParagraph(t, list, 1)
if got := first.Bullets(); len(got) != 2 || got[0].Source() != "logo.svg" || got[1].Text() != "1." {
t.Fatalf("first bullets = %#v, want logo and 1.", got)
}
if got := second.Bullets(); len(got) != 2 || got[0].Source() != "logo.svg" || got[1].Text() != "2." {
t.Fatalf("second bullets = %#v, want logo and 2.", got)
}
}
func TestStdContainer_PrepareListBullets_OLKeepsFormattedTemplateWidth(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<bullet id="num" format="%d." width="40pt" />
<page>
<ol bullets="num">
<p>One</p>
<p>Two</p>
<p>Three</p>
<p>Four</p>
<p>Five</p>
<p>Six</p>
<p>Seven</p>
<p>Eight</p>
<p>Nine</p>
<p>Ten</p>
<p>Eleven</p>
<p>Twelve</p>
</ol>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
twelfth := childParagraph(t, list, 11)
if first.Bullet() == nil || first.Bullet().Width() != 40 || twelfth.Bullet() == nil || twelfth.Bullet().Width() != 40 {
t.Fatalf("ordered template widths = %#v / %#v, want 40pt for both", first.Bullet(), twelfth.Bullet())
}
if first.Bullet().Text() != "1." || twelfth.Bullet().Text() != "12." {
t.Fatalf("ordered template texts = %q / %q, want 1. / 12.", first.Bullet().Text(), twelfth.Bullet().Text())
}
}
func TestStdContainer_PrepareListBullets_FormattedTemplateAutoWidthIgnoresDefaultBulletWidth(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<bullet id="num" format="%d." />
<page>
<ol bullets="num">
<p>One</p>
<p>Two</p>
</ol>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
list := firstContainer(t, doc)
first := childParagraph(t, list, 0)
if first.Bullet() == nil {
t.Fatal("first bullet is nil")
}
renderedWidth := first.bulletTextWidth(w, first.Bullet())
if first.Bullet().Width() >= 36 {
t.Fatalf("auto marker width = %v, want less than default 36pt slot", first.Bullet().Width())
}
if first.Bullet().Width() <= renderedWidth {
t.Fatalf("auto marker width = %v, want greater than rendered marker width %v", first.Bullet().Width(), renderedWidth)
}
}
func TestStdContainer_PrepareListBullets_DivCanUseBulletsAttribute(t *testing.T) {
doc := parseDoc(t, `
<ltml>
<page>
<div layout="vbox" bullets="ordered">
<p>One</p>
</div>
<div layout="vbox" bullets="unordered">
<p>One</p>
</div>
</page>
</ltml>`)
w := &labelTestWriter{t: t, fonts: defaultTestFonts(t)}
if err := doc.Print(w); err != nil {
t.Fatal(err)
}
page := firstPage(t, doc)
ordered := page.children[0].(*StdContainer)
unordered := page.children[1].(*StdContainer)
orderedParagraph := childParagraph(t, ordered, 0)
if orderedParagraph.Bullet() == nil || orderedParagraph.Bullet().Text() != "1." {
t.Fatalf("bullets=ordered bullet = %#v, want 1.", orderedParagraph.Bullet())
}
unorderedParagraph := childParagraph(t, unordered, 0)
if unorderedParagraph.Bullet() == nil || unorderedParagraph.Bullet().Shape() != "circle" {
t.Fatalf("bullets=unordered bullet = %#v, want circle", unorderedParagraph.Bullet())
}
}