-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan_test.go
More file actions
187 lines (173 loc) · 5.77 KB
/
Copy pathspan_test.go
File metadata and controls
187 lines (173 loc) · 5.77 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
// SPDX-FileCopyrightText: Copyright 2026 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package termtable
import (
"errors"
"testing"
)
// TestColSpanClaimsMultipleColumns verifies that a cell with colSpan=3
// causes the table to grow to 3 columns and occupies positions 0..2 in
// its row.
func TestColSpanClaimsMultipleColumns(t *testing.T) {
tbl := NewTable()
r := tbl.AddRow()
c := r.AddCell(WithContent("wide"), WithColSpan(3))
if tbl.NumColumns() != 3 {
t.Errorf("NumColumns = %d, want 3", tbl.NumColumns())
}
for col := range 3 {
if got := tbl.bodyOcc.at(0, col); got != c {
t.Errorf("occ[0][%d] = %v, want %v", col, got, c)
}
}
}
// TestAddCellAdvancesPastReservedRowspan verifies that when row 0 has a
// rowspan=2 cell in column 0, the next row's first AddCell lands at
// column 1 (column 0 is reserved by the rowspan).
func TestAddCellAdvancesPastReservedRowspan(t *testing.T) {
tbl := NewTable()
r0 := tbl.AddRow()
r0.AddCell(WithContent("tall"), WithRowSpan(2))
side := r0.AddCell(WithContent("side"))
if side.GridCol() != 1 {
t.Errorf("side grid col = %d, want 1", side.GridCol())
}
r1 := tbl.AddRow()
first := r1.AddCell(WithContent("below"))
if first.GridCol() != 1 {
t.Errorf("below grid col = %d, want 1 (col 0 reserved)", first.GridCol())
}
}
// TestSpanConflictErrors verifies that in strict mode
// (WithSpanOverwrite(false)), a rowspan reaching into a row that
// already has content returns ErrSpanConflict via the WithError variant.
func TestSpanConflictErrors(t *testing.T) {
tbl := NewTable(WithSpanOverwrite(false))
r0 := tbl.AddRow()
r1 := tbl.AddRow()
r1.AddCell(WithContent("below"), WithCellID("below"))
// r0 tries to place a rowspan=2 cell at col 0 — but r1[0] is taken.
_, err := r0.AddCellWithError(WithContent("reach"), WithRowSpan(2))
if !errors.Is(err, ErrSpanConflict) {
t.Fatalf("expected ErrSpanConflict, got %v", err)
}
if err.Error() == "" {
t.Error("error message empty")
}
}
// TestSpanConflictPanicsInStrictMode verifies the non-error AddCell API
// panics on span conflict when WithSpanOverwrite(false).
func TestSpanConflictPanicsInStrictMode(t *testing.T) {
tbl := NewTable(WithSpanOverwrite(false))
r0 := tbl.AddRow()
r1 := tbl.AddRow()
r1.AddCell(WithContent("below"))
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic on span conflict in strict mode")
}
}()
r0.AddCell(WithContent("reach"), WithRowSpan(2))
}
// TestSpanConflictAutoAdvanceWithinRow confirms that within a single
// row, new cells auto-advance past occupied slots rather than erroring.
func TestSpanConflictAutoAdvanceWithinRow(t *testing.T) {
tbl := NewTable()
r0 := tbl.AddRow()
r0.AddCell(WithContent("a"), WithColSpan(3))
c := r0.AddCell(WithContent("b"))
if c.GridCol() != 3 {
t.Errorf("advanced col = %d, want 3", c.GridCol())
}
}
// TestSpanOverwriteDropsAnchorCovered verifies that with
// WithSpanOverwrite(true), a new cell whose rectangle fully covers an
// existing cell's anchor removes the victim entirely.
func TestSpanOverwriteDropsAnchorCovered(t *testing.T) {
tbl := NewTable(WithSpanOverwrite(true))
r0 := tbl.AddRow()
r1 := tbl.AddRow()
r1.AddCell(WithCellID("victim"), WithContent("v"))
// r0 cell with rowspan=2 anchored at col 0 — covers (r1, c0),
// which is the victim's anchor.
r0.AddCell(WithContent("over"), WithRowSpan(2))
if got := len(r1.Cells()); got != 0 {
t.Errorf("victim remained in row, cells=%d", got)
}
if tbl.GetElementByID("victim") != nil {
t.Error("victim id should be unregistered")
}
var sawDrop bool
for _, w := range tbl.Warnings() {
ev, ok := w.(OverwriteEvent)
if ok && ev.DroppedID == "victim" {
sawDrop = true
}
}
if !sawDrop {
t.Errorf("expected OverwriteEvent{DroppedID: victim}, got %v", tbl.Warnings())
}
}
// TestSpanOverwriteTruncatesPartial verifies that with overwrite on, a
// new cell whose rectangle overlaps an existing cell's span without
// covering the victim's anchor reduces (truncates) the victim's span
// rather than dropping it.
//
// Scenario:
//
// r0: [A] . .
// r1: [B] . .
// r2: [V V] . (vic: (2, 0..1), rowSpan=2 — reserves (2..3, 0..1))
//
// Then we attach D to r0 with colSpan=2, rowSpan=3. Auto-advance pushes
// D to col 1 (col 0 is taken by A). D's rectangle becomes (0..2, 1..2).
// That intersects vic at (2, 1). Vic's anchor (2, 0) lies outside D's
// rectangle, so truncation applies: vic's colSpan drops from 2 to 1.
func TestSpanOverwriteTruncatesPartial(t *testing.T) {
tbl := NewTable(WithSpanOverwrite(true))
r0 := tbl.AddRow()
r0.AddCell(WithContent("A"))
r1 := tbl.AddRow()
r1.AddCell(WithContent("B"))
r2 := tbl.AddRow()
vic := r2.AddCell(
WithCellID("vic"), WithContent("V"),
WithColSpan(2), WithRowSpan(2),
)
r0.AddCell(WithContent("D"), WithColSpan(2), WithRowSpan(3))
if vic.ColSpan() != 1 {
t.Errorf("vic colSpan = %d, want 1", vic.ColSpan())
}
if vic.RowSpan() != 2 {
t.Errorf("vic rowSpan = %d, want 2", vic.RowSpan())
}
if tbl.bodyOcc.at(2, 0) != vic {
t.Error("vic should still be at (2,0)")
}
if tbl.bodyOcc.at(2, 1) == vic {
t.Error("vic should no longer occupy (2,1)")
}
var sawTrunc bool
for _, w := range tbl.Warnings() {
ev, ok := w.(OverwriteEvent)
if ok && ev.TruncatedID == "vic" {
sawTrunc = true
}
}
if !sawTrunc {
t.Errorf("expected OverwriteEvent{TruncatedID: vic}, got %v", tbl.Warnings())
}
}
// TestRowSpanReservesAcrossRows verifies the occupancy grid grows to
// cover a rowspan even when subsequent rows haven't been added yet.
func TestRowSpanReservesAcrossRows(t *testing.T) {
tbl := NewTable()
r0 := tbl.AddRow()
c := r0.AddCell(WithContent("tall"), WithRowSpan(3))
if tbl.bodyOcc.at(2, 0) != c {
t.Error("rowspan should reserve row 2 col 0")
}
if tbl.bodyOcc.numRows() < 3 {
t.Errorf("occ numRows = %d, want >= 3", tbl.bodyOcc.numRows())
}
}