-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout_test.go
More file actions
303 lines (274 loc) · 8.51 KB
/
Copy pathlayout_test.go
File metadata and controls
303 lines (274 loc) · 8.51 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
// SPDX-FileCopyrightText: Copyright 2026 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package termtable
import (
"errors"
"testing"
)
// layoutOverhead returns the fixed (non-content) cost in columns for
// an nCols table with DefaultPadding.
func layoutOverheadCols(nCols int) int {
return (nCols + 1) + nCols*2
}
func TestLayoutEqualSplitBaseline(t *testing.T) {
tbl := NewTable(WithTargetWidth(37))
r := tbl.AddRow()
r.AddCell(WithContent("a"))
r.AddCell(WithContent("b"))
r.AddCell(WithContent("c"))
m := Measure(tbl)
l := Layout(tbl, m)
if l.err != nil {
t.Fatalf("unexpected err: %v", l.err)
}
// target=37, overhead=(3+1) + 3*2 = 10 → available=27, 3 cols → 9 each.
want := []int{9, 9, 9}
for i := range want {
if l.colAssigned[i] != want[i] {
t.Errorf("colAssigned[%d] = %d, want %d", i, l.colAssigned[i], want[i])
}
}
}
func TestLayoutExtraRemainderDistributedLeftFirst(t *testing.T) {
// Force a remainder: available not divisible by 3.
tbl := NewTable(WithTargetWidth(39))
r := tbl.AddRow()
r.AddCell(WithContent("a"))
r.AddCell(WithContent("b"))
r.AddCell(WithContent("c"))
l := Layout(tbl, Measure(tbl))
// overhead=10, avail=29, base=9 remainder 2 → [10,10,9]
want := []int{10, 10, 9}
for i := range want {
if l.colAssigned[i] != want[i] {
t.Errorf("colAssigned[%d] = %d, want %d", i, l.colAssigned[i], want[i])
}
}
}
func TestLayoutMinFloorTriggered(t *testing.T) {
tbl := NewTable(WithTargetWidth(30))
r := tbl.AddRow()
r.AddCell(WithContent("x"))
r.AddCell(WithContent("looooongword")) // min = 12
r.AddCell(WithContent("y"))
l := Layout(tbl, Measure(tbl))
if l.err != nil {
t.Fatalf("unexpected err: %v", l.err)
}
// Column 1 must be at least 12.
if l.colAssigned[1] < 12 {
t.Errorf("col 1 = %d, want >= 12", l.colAssigned[1])
}
// Total content sum should equal available budget.
target := 30
overhead := layoutOverheadCols(3)
want := target - overhead
var got int
for _, v := range l.colAssigned {
got += v
}
if got != want {
t.Errorf("sum of assigned = %d, want %d", got, want)
}
}
// TestLayoutTooNarrowNoRoomPerColumn verifies that
// ErrTargetTooNarrow fires only when the budget cannot give every
// column at least one glyph of content space. Overhead for 4 cols is
// (4+1) + 4*2 = 13, so target=15 leaves 2 content cols for 4 columns
// — genuinely pathological.
func TestLayoutTooNarrowNoRoomPerColumn(t *testing.T) {
tbl := NewTable(WithTargetWidth(15))
r := tbl.AddRow()
r.AddCell(WithContent("a"))
r.AddCell(WithContent("b"))
r.AddCell(WithContent("c"))
r.AddCell(WithContent("d"))
l := Layout(tbl, Measure(tbl))
if !errors.Is(l.err, ErrTargetTooNarrow) {
t.Fatalf("expected ErrTargetTooNarrow, got %v", l.err)
}
}
// TestLayoutShrinksBelowMinimumSilently verifies that when content
// minimums exceed the target but every column can still be given at
// least one glyph, the layout silently shrinks — no error. Content
// clipping happens inside cells via the normal wrap path.
func TestLayoutShrinksBelowMinimumSilently(t *testing.T) {
tbl := NewTable(WithTargetWidth(12))
r := tbl.AddRow()
r.AddCell(WithContent("longwordone"))
r.AddCell(WithContent("longwordtwo"))
l := Layout(tbl, Measure(tbl))
if l.err != nil {
t.Fatalf("expected no error for silent shrink, got %v", l.err)
}
}
// TestLayoutTooNarrowHardFitsToTarget verifies that when content
// minimums exceed the target, the layout still fits within the target
// by shrinking each column below its natural minimum (cells will clip
// their content with an ellipsis). The rendered width must equal the
// target — borders never spill outside it. No error is raised since
// every column still gets at least one glyph.
func TestLayoutTooNarrowHardFitsToTarget(t *testing.T) {
tbl := NewTable(WithTargetWidth(20))
r := tbl.AddRow()
r.AddCell(WithContent("longwordonelongwordone"))
r.AddCell(WithContent("longwordtwolongwordtwo"))
l := Layout(tbl, Measure(tbl))
if l.err != nil {
t.Fatalf("expected no error (every column fits one glyph): %v", l.err)
}
// overhead for 2 cols: (2+1) + 2*2 = 7 → available = 13
available := 20 - layoutOverheadCols(2)
total := 0
for _, v := range l.colAssigned {
total += v
}
if total != available {
t.Errorf("colAssigned sum = %d, want %d (hard-fit to available)", total, available)
}
for i, v := range l.colAssigned {
if v < 1 {
t.Errorf("colAssigned[%d] = %d, want >= 1 (every column keeps a glyph)", i, v)
}
}
// End-to-end: every rendered line is exactly the target width.
out := tbl.String()
for i, ln := range splitLines(out) {
if w := DisplayWidth(ln); w != 20 {
t.Errorf("line %d width = %d, want 20: %q", i, w, ln)
}
}
}
func splitLines(s string) []string {
var out []string
curr := ""
for _, ch := range s {
if ch == '\n' {
out = append(out, curr)
curr = ""
continue
}
curr += string(ch)
}
if curr != "" {
out = append(out, curr)
}
return out
}
func TestLayoutMultiSpanConstraintBorrows(t *testing.T) {
tbl := NewTable(WithTargetWidth(40))
// Row 0: a single cell spanning 2 columns with a long minimum.
r0 := tbl.AddRow()
r0.AddCell(WithContent("averylongbannerword"), WithColSpan(2))
// Row 1: two normal cells to populate the columns.
r1 := tbl.AddRow()
r1.AddCell(WithContent("x"))
r1.AddCell(WithContent("y"))
// Force a third column so there's outside-span slack to borrow.
r1.AddCell(WithContent("z")) // col 2
// Row 0 only has the banner, so column 2 gets populated by row 1.
// Actually the banner is in row 0 and the banner cell occupies cols
// 0..1; then row 1 has three cells 0..2. NumColumns = 3.
if tbl.NumColumns() != 3 {
t.Fatalf("expected 3 columns, got %d", tbl.NumColumns())
}
m := Measure(tbl)
l := Layout(tbl, m)
if l.err != nil {
t.Fatalf("unexpected err: %v", l.err)
}
// Span minimum ~ 19. seamWidth=3 for a single seam. So columns 0+1
// must sum to at least 19-3 = 16.
span := l.colAssigned[0] + l.colAssigned[1]
if span < 16 {
t.Errorf("span sum = %d, want >= 16", span)
}
}
func TestLayoutDesiredUpgrade(t *testing.T) {
// Short content: base widths exceed desired easily; leftover
// budget should NOT inflate columns past their desired.
tbl := NewTable(WithTargetWidth(60))
r := tbl.AddRow()
r.AddCell(WithContent("a"))
r.AddCell(WithContent("b"))
l := Layout(tbl, Measure(tbl))
if l.err != nil {
t.Fatalf("unexpected err: %v", l.err)
}
// When desired equals min (single-char content), assigned still
// goes up from equal-split. That's fine; just ensure sum ≤ budget.
overhead := layoutOverheadCols(2)
budget := 60 - overhead
var got int
for _, v := range l.colAssigned {
got += v
}
if got > budget {
t.Errorf("sum of assigned = %d, exceeds budget %d", got, budget)
}
}
func TestLayoutRowHeightsSingleLineContent(t *testing.T) {
tbl := NewTable(WithTargetWidth(50))
r := tbl.AddRow()
r.AddCell(WithContent("one"))
r.AddCell(WithContent("two"))
l := Layout(tbl, Measure(tbl))
if len(l.rowHeights) != 1 {
t.Fatalf("rowHeights len = %d, want 1", len(l.rowHeights))
}
if l.rowHeights[0] != 1 {
t.Errorf("row height = %d, want 1", l.rowHeights[0])
}
}
func TestLayoutRowHeightsWrapsToMultiLine(t *testing.T) {
// Narrow column forces wrapping.
tbl := NewTable(WithTargetWidth(20))
r := tbl.AddRow()
r.AddCell(WithContent("a very long sentence that must wrap several times"))
l := Layout(tbl, Measure(tbl))
if len(l.rowHeights) != 1 {
t.Fatalf("rowHeights len = %d", len(l.rowHeights))
}
if l.rowHeights[0] < 2 {
t.Errorf("row height = %d, want >= 2", l.rowHeights[0])
}
}
func TestLayoutRowSpanBumpsTailRow(t *testing.T) {
tbl := NewTable(WithTargetWidth(25))
r0 := tbl.AddRow()
r0.AddCell(
WithContent("first\nsecond\nthird\nfourth"),
WithRowSpan(3),
)
r1 := tbl.AddRow()
r1.AddCell(WithContent("x"))
r2 := tbl.AddRow()
r2.AddCell(WithContent("y"))
l := Layout(tbl, Measure(tbl))
if len(l.rowHeights) != 3 {
t.Fatalf("rowHeights len = %d", len(l.rowHeights))
}
// Each base row has height 1 (from x, y in rows 1, 2, nothing from
// row 0 since the rowspan is not counted until the tail bump).
// Total must be >= 4 (content lines). Tail row bumped.
var sum int
for _, hh := range l.rowHeights {
sum += hh
}
if sum < 4 {
t.Errorf("sum of heights = %d, want >= 4", sum)
}
}
func TestLayoutEmptyTable(t *testing.T) {
tbl := NewTable()
l := Layout(tbl, Measure(tbl))
if l.err != nil {
t.Errorf("unexpected err on empty table: %v", l.err)
}
if len(l.colAssigned) != 0 {
t.Errorf("colAssigned = %v, want empty", l.colAssigned)
}
if len(l.rowHeights) != 0 {
t.Errorf("rowHeights = %v, want empty", l.rowHeights)
}
}