-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout_test.go
327 lines (300 loc) · 7.47 KB
/
layout_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
package bubblelayout_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
bl "github.com/winder/bubblelayout"
)
func TestUnknownComponent(t *testing.T) {
l := bl.New()
msg := l.Resize(10, 10)
_, err := msg.Size(1)
require.Error(t, err)
require.Equal(t, "view not registered", err.Error())
}
func TestOneComponent(t *testing.T) {
l := bl.New()
id1 := l.Cell(bl.Cell{})
msg := l.Resize(10, 10)
size, err := msg.Size(id1)
require.NoError(t, err)
require.Equal(t, bl.Size{Width: 10, Height: 10}, size)
}
func TestHorizontalComponents(t *testing.T) {
l := bl.New()
id1 := l.Cell(bl.Cell{})
id2 := l.Cell(bl.Cell{})
msg := l.Resize(10, 10)
{
size, err := msg.Size(id1)
require.NoError(t, err)
require.Equal(t, bl.Size{Width: 5, Height: 10}, size)
}
{
size, err := msg.Size(id2)
require.NoError(t, err)
require.Equal(t, bl.Size{Width: 5, Height: 10}, size)
}
}
func TestVerticalComponents(t *testing.T) {
l := bl.New()
id1 := l.Cell(bl.Cell{})
l.Wrap()
id2 := l.Cell(bl.Cell{})
msg := l.Resize(10, 10)
{
size, err := msg.Size(id1)
require.NoError(t, err)
require.Equal(t, bl.Size{Width: 10, Height: 5}, size)
}
{
size, err := msg.Size(id2)
require.NoError(t, err)
require.Equal(t, bl.Size{Width: 10, Height: 5}, size)
}
}
func TestComplex(t *testing.T) {
// inputLayout:
// ---------------------------------
// | 1 | 2 (2, 2) | 3 |
// ---------------------------------
// | 4 (1, 2) | 5 |
// -----------------------
// | 6 | 7 (1, 2) |
// -----------------------
// expected
// ---------------------------------
// | 1 | - | 3 |
// --------- - - 2 - - |--------
// | - | - | 5 |
// | - 4 - -------------------------
// | - | 6 | 7 |
// ---------------------------------
l := bl.New()
var ids []bl.ID
ids = append(ids, l.Cell(bl.Cell{}))
ids = append(ids, l.Cell(bl.Cell{SpanWidth: 2, SpanHeight: 2}))
ids = append(ids, l.Cell(bl.Cell{}))
l.Wrap()
ids = append(ids, l.Cell(bl.Cell{SpanHeight: 2}))
ids = append(ids, l.Cell(bl.Cell{}))
l.Wrap()
ids = append(ids, l.Cell(bl.Cell{}))
ids = append(ids, l.Cell(bl.Cell{SpanWidth: 2}))
msg := l.Resize(100, 75)
{
for _, id := range []bl.ID{ids[0], ids[2], ids[4], ids[5]} {
size, err := msg.Size(id)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 25, Height: 25}, size, "1x1 cell should be 25x25")
}
}
{
size, err := msg.Size(ids[1])
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 50}, size, "2x2 cell should be 50x50")
}
{
size, err := msg.Size(ids[3])
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 25, Height: 50}, size, "1x2 cell should be 25x50")
}
{
size, err := msg.Size(ids[6])
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 25}, size, "2x1 cell should be 50x25")
}
}
func TestValidateCache(t *testing.T) {
l := bl.New()
l.Cell(bl.Cell{})
require.NoError(t, l.Validate())
require.NoError(t, l.Validate())
}
func TestResize_Panic(t *testing.T) {
l := bl.New()
l.Cell(bl.Cell{MinHeight: 100})
l.Cell(bl.Cell{MaxHeight: 10})
panicFunc := func() {
l.Resize(100, 100)
}
require.Panicsf(t, panicFunc, "constraint violation")
}
func TestAdd_Panic(t *testing.T) {
l := bl.New()
panicFunc := func() { l.Add("invalid constraint options") }
require.Panics(t, panicFunc)
}
func TestMaybeAdd(t *testing.T) {
l := bl.New()
_, err := l.MaybeAdd("invalid constraint options")
require.ErrorContains(t, err, "invalid constraint")
}
func TestDockAPI(t *testing.T) {
l := bl.New()
id1 := l.Cell(bl.Cell{})
l.Wrap()
id2 := l.Cell(bl.Cell{SpanHeight: 2})
id3 := l.Dock(bl.Dock{Cardinal: bl.EAST})
msg := l.Resize(100, 75)
{
size, err := msg.Size(id1)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 25}, size)
}
{
size, err := msg.Size(id2)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 50}, size)
}
{
size, err := msg.Size(id3)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 75}, size)
}
}
func TestDockAdd(t *testing.T) {
l := bl.New()
id1 := l.Add("wrap")
id2 := l.Add("spanh 2")
id3 := l.Add("dock east")
msg := l.Resize(100, 75)
{
size, err := msg.Size(id1)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 25}, size)
}
{
size, err := msg.Size(id2)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 50}, size)
}
{
size, err := msg.Size(id3)
require.NoError(t, err)
assert.Equal(t, bl.Size{Width: 50, Height: 75}, size)
}
}
func TestResize(t *testing.T) {
const width = 80
const height = 40
testcases := []struct {
name string
wOverride int
hOverride int
in func() bl.BubbleLayout
out map[bl.ID]bl.Size
}{
{
name: "simple",
in: func() bl.BubbleLayout {
l := bl.New()
l.Add("width 10")
l.Add("grow")
return l
},
out: map[bl.ID]bl.Size{
1: {Width: 10, Height: height},
2: {Width: width - 10, Height: height},
},
}, {
name: "dock",
in: func() bl.BubbleLayout {
l := bl.New()
l.Add("")
l.Add("span 2")
l.Add("north 5!")
return l
},
out: map[bl.ID]bl.Size{
1: {Width: 27, Height: 35},
2: {Width: 53, Height: 35},
3: {Width: 80, Height: 5},
},
}, {
name: "empty remainder",
hOverride: 9,
in: func() bl.BubbleLayout {
l := bl.New()
l.Add("width 9!")
l.Add("width 9!")
l.Add("")
return l
},
out: map[bl.ID]bl.Size{
1: {Width: 9, Height: 9},
2: {Width: 9, Height: 9},
3: {Width: 62, Height: 9},
},
}, {
name: "Min and preferred grows to preferred.",
in: func() bl.BubbleLayout {
l := bl.New()
l.Add("width 9:10")
l.Add("width 9:10")
l.Add("width 9:10")
return l
},
out: map[bl.ID]bl.Size{
1: {Width: 10, Height: height},
2: {Width: 10, Height: height},
3: {Width: 10, Height: height},
},
}, {
// This is a regression test for a bug where the layout would
// have an infinite loop. It happens when there is a remainder
// less than the number of bounds that still have unmet preferences.
// It led to a case where "remainder / len(pref)" equals 0, so the
// iterations would never finish out the remainder.
name: "not enough room infinite loop regression test",
in: func() bl.BubbleLayout {
l := bl.New()
sz := width / 2
l.Add(fmt.Sprintf("width %d", (width/4)-1))
l.Add(fmt.Sprintf("width %d", sz))
l.Add(fmt.Sprintf("width %d", sz))
l.Add(fmt.Sprintf("width %d", sz))
return l
},
out: map[bl.ID]bl.Size{
1: {Width: (width / 4) - 1, Height: height},
2: {Width: width / 4, Height: height},
3: {Width: width / 4, Height: height},
4: {Width: width / 4, Height: height},
},
}, {
name: "Remainder doesn't go to fully allocated cells.",
in: func() bl.BubbleLayout {
l := bl.New()
l.Add("width 10!")
l.Add(fmt.Sprintf("grow, width 0:%d:1000", width-11))
return l
},
out: map[bl.ID]bl.Size{
1: {Width: 10, Height: height},
2: {Width: width - 10, Height: height},
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
l := tc.in()
w := width
h := height
if tc.wOverride != 0 {
w = tc.wOverride
}
if tc.hOverride != 0 {
h = tc.hOverride
}
msg := l.Resize(w, h)
for id, size := range tc.out {
actual, err := msg.Size(id)
require.NoError(t, err)
assert.Equal(t, size, actual)
}
})
}
}