-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlayout_table_test.go
More file actions
276 lines (227 loc) · 8.17 KB
/
layout_table_test.go
File metadata and controls
276 lines (227 loc) · 8.17 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
package ltml
import "testing"
func testTableContainer(width, height float64, cols int) *StdContainer {
page := &StdPage{pageStyle: &PageStyle{width: 400, height: 400}}
c := &StdContainer{}
_ = c.SetContainer(page)
c.SetWidth(width)
if height > 0 {
c.SetHeight(height)
}
c.cols = cols
c.order = TableOrderRows
c.layout = defaultLayouts["table"].Clone()
return c
}
func addTableTestWidget(t *testing.T, c *StdContainer, preferredWidth, preferredHeight float64) *positionedTestWidget {
t.Helper()
w := &positionedTestWidget{preferredWidth: preferredWidth, preferredHeight: preferredHeight}
if err := w.SetContainer(c); err != nil {
t.Fatal(err)
}
c.AddChild(w)
return w
}
func TestLayoutTable_AutoWidthUsesSurplusWithoutChangingOmittedColumns(t *testing.T) {
c := testTableContainer(300, 80, 4)
fixed := addTableTestWidget(t, c, 40, 20)
fixed.SetWidth(60)
omitted := addTableTestWidget(t, c, 35, 20)
autoA := addTableTestWidget(t, c, 40, 20)
autoA.SetWidthAuto()
autoB := addTableTestWidget(t, c, 30, 20)
autoB.SetWidthAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := fixed.Width(); got != 60 {
t.Fatalf("fixed.Width() = %v, want 60", got)
}
if got := omitted.Width(); got != 35 {
t.Fatalf("omitted.Width() = %v, want preferred width 35", got)
}
if got := autoA.Width(); got != 102.5 {
t.Fatalf("autoA.Width() = %v, want 102.5", got)
}
if got := autoB.Width(); got != 102.5 {
t.Fatalf("autoB.Width() = %v, want 102.5", got)
}
}
func TestLayoutTable_AutoWidthPreservesPercentColumns(t *testing.T) {
c := testTableContainer(400, 100, 4)
percent := addTableTestWidget(t, c, 30, 20)
percent.SetWidthPct(25)
fixed := addTableTestWidget(t, c, 30, 20)
fixed.SetWidth(50)
omitted := addTableTestWidget(t, c, 40, 20)
auto := addTableTestWidget(t, c, 50, 20)
auto.SetWidthAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := percent.Width(); got != 100 {
t.Fatalf("percent.Width() = %v, want 100", got)
}
if got := fixed.Width(); got != 50 {
t.Fatalf("fixed.Width() = %v, want 50", got)
}
if got := omitted.Width(); got != 40 {
t.Fatalf("omitted.Width() = %v, want preferred width 40", got)
}
if got := auto.Width(); got != 210 {
t.Fatalf("auto.Width() = %v, want remaining width 210", got)
}
}
func TestLayoutTable_LaterSingleColumnAutoCellMarksColumnAuto(t *testing.T) {
c := testTableContainer(220, 100, 2)
omittedA := addTableTestWidget(t, c, 40, 20)
omittedB := addTableTestWidget(t, c, 30, 20)
laterAutoA := addTableTestWidget(t, c, 45, 20)
laterAutoA.SetWidthAuto()
laterAutoB := addTableTestWidget(t, c, 35, 20)
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := omittedB.Width(); got != 35 {
t.Fatalf("omittedB.Width() = %v, want preferred width 35", got)
}
if got := omittedA.Width(); got != 185 {
t.Fatalf("omittedA.Width() = %v, want auto column surplus width 185", got)
}
if got := laterAutoA.Width(); got != 185 {
t.Fatalf("laterAutoA.Width() = %v, want auto column surplus width 185", got)
}
if got := laterAutoB.Width(); got != 35 {
t.Fatalf("laterAutoB.Width() = %v, want omitted column preferred width 35", got)
}
}
func TestLayoutTable_OmittedWidthsKeepLegacyEqualShareWithoutAutoColumns(t *testing.T) {
c := testTableContainer(300, 80, 3)
a := addTableTestWidget(t, c, 20, 20)
b := addTableTestWidget(t, c, 60, 20)
d := addTableTestWidget(t, c, 100, 20)
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
for i, w := range []*positionedTestWidget{a, b, d} {
if got := w.Width(); got != 100 {
t.Fatalf("widget %d Width() = %v, want legacy equal share 100", i, got)
}
}
}
func TestLayoutTable_AutoWidthPreservesOmittedPreferredWidthWhenAutosCanShrink(t *testing.T) {
c := testTableContainer(120, 80, 3)
omitted := addTableTestWidget(t, c, 100, 20)
autoA := addTableTestWidget(t, c, 80, 20)
autoA.SetWidthAuto()
autoB := addTableTestWidget(t, c, 80, 20)
autoB.SetWidthAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := omitted.Width(); got != 100 {
t.Fatalf("omitted.Width() = %v, want preferred width 100", got)
}
if got := autoA.Width(); got != 10 {
t.Fatalf("autoA.Width() = %v, want remaining auto share 10", got)
}
if got := autoB.Width(); got != 10 {
t.Fatalf("autoB.Width() = %v, want remaining auto share 10", got)
}
}
func TestLayoutTable_AutoWidthFallsBackToEqualShareWhenOmittedPreferredCannotFit(t *testing.T) {
c := testTableContainer(90, 80, 3)
omitted := addTableTestWidget(t, c, 100, 20)
autoA := addTableTestWidget(t, c, 80, 20)
autoA.SetWidthAuto()
autoB := addTableTestWidget(t, c, 80, 20)
autoB.SetWidthAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
for i, w := range []*positionedTestWidget{omitted, autoA, autoB} {
if got := w.Width(); got != 30 {
t.Fatalf("widget %d Width() = %v, want impossible-case equal share 30", i, got)
}
}
}
func TestLayoutTable_ColspanDoesNotCreateAutoColumnConstraint(t *testing.T) {
c := testTableContainer(240, 100, 2)
spanning := addTableTestWidget(t, c, 200, 20)
spanning.SetWidthAuto()
spanning.SetAttrs(map[string]string{"colspan": "2"})
a := addTableTestWidget(t, c, 30, 20)
b := addTableTestWidget(t, c, 50, 20)
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := a.Width(); got != 120 {
t.Fatalf("a.Width() = %v, want legacy equal share 120", got)
}
if got := b.Width(); got != 120 {
t.Fatalf("b.Width() = %v, want legacy equal share 120", got)
}
if got := spanning.Width(); got != 240 {
t.Fatalf("spanning.Width() = %v, want full two-column span 240", got)
}
}
func TestLayoutTable_AutoHeightRowsShareSurplus(t *testing.T) {
c := testTableContainer(200, 150, 2)
fixedA := addTableTestWidget(t, c, 30, 20)
fixedA.SetHeight(20)
fixedB := addTableTestWidget(t, c, 30, 20)
fixedB.SetHeight(20)
omittedA := addTableTestWidget(t, c, 30, 30)
omittedB := addTableTestWidget(t, c, 30, 30)
autoA := addTableTestWidget(t, c, 30, 10)
autoA.SetHeightAuto()
autoB := addTableTestWidget(t, c, 30, 10)
autoB.SetHeightAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := omittedA.Height(); got != 30 {
t.Fatalf("omittedA.Height() = %v, want preferred height 30", got)
}
if got := omittedB.Height(); got != 30 {
t.Fatalf("omittedB.Height() = %v, want preferred height 30", got)
}
if got := autoA.Height(); got != 100 {
t.Fatalf("autoA.Height() = %v, want 100", got)
}
if got := autoB.Height(); got != 100 {
t.Fatalf("autoB.Height() = %v, want 100", got)
}
}
func TestLayoutTable_AutoHeightRowsPreservePercentRows(t *testing.T) {
c := testTableContainer(200, 200, 2)
percentA := addTableTestWidget(t, c, 30, 20)
percentA.SetHeightPct(25)
percentB := addTableTestWidget(t, c, 30, 20)
percentB.SetHeightPct(25)
omittedA := addTableTestWidget(t, c, 30, 30)
omittedB := addTableTestWidget(t, c, 30, 30)
autoA := addTableTestWidget(t, c, 30, 10)
autoA.SetHeightAuto()
autoB := addTableTestWidget(t, c, 30, 10)
autoB.SetHeightAuto()
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := percentA.Height(); got != 50 {
t.Fatalf("percentA.Height() = %v, want 50", got)
}
if got := percentB.Height(); got != 50 {
t.Fatalf("percentB.Height() = %v, want 50", got)
}
if got := omittedA.Height(); got != 30 {
t.Fatalf("omittedA.Height() = %v, want preferred height 30", got)
}
if got := omittedB.Height(); got != 30 {
t.Fatalf("omittedB.Height() = %v, want preferred height 30", got)
}
if got := autoA.Height(); got != 120 {
t.Fatalf("autoA.Height() = %v, want remaining auto height 120", got)
}
if got := autoB.Height(); got != 120 {
t.Fatalf("autoB.Height() = %v, want remaining auto height 120", got)
}
}
func TestLayoutTable_NaturalHeightLeavesAutoRowsAtPreferredHeight(t *testing.T) {
c := testTableContainer(200, 0, 1)
auto := addTableTestWidget(t, c, 30, 25)
auto.SetHeightAuto()
omitted := addTableTestWidget(t, c, 30, 35)
LayoutTable(c, c.LayoutStyle(), &labelTestWriter{t: t})
if got := auto.Height(); got != 25 {
t.Fatalf("auto.Height() = %v, want preferred height 25", got)
}
if got := omitted.Height(); got != 35 {
t.Fatalf("omitted.Height() = %v, want preferred height 35", got)
}
if got := c.Height(); got != 60 {
t.Fatalf("container.Height() = %v, want natural height 60", got)
}
}