| 1 |
package cellbuf |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestTabStops(t *testing.T) { |
| 8 |
tests := []struct { |
| 9 |
name string |
| 10 |
width int |
| 11 |
interval int |
| 12 |
checks []struct { |
| 13 |
col int |
| 14 |
expected bool |
| 15 |
} |
| 16 |
}{ |
| 17 |
{ |
| 18 |
name: "default interval of 8", |
| 19 |
width: 24, |
| 20 |
interval: DefaultTabInterval, |
| 21 |
checks: []struct { |
| 22 |
col int |
| 23 |
expected bool |
| 24 |
}{ |
| 25 |
{0, true}, // First tab stop |
| 26 |
{7, false}, // Not a tab stop |
| 27 |
{8, true}, // Second tab stop |
| 28 |
{15, false}, // Not a tab stop |
| 29 |
{16, true}, // Third tab stop |
| 30 |
{23, false}, // Not a tab stop |
| 31 |
}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "custom interval of 4", |
| 35 |
width: 16, |
| 36 |
interval: 4, |
| 37 |
checks: []struct { |
| 38 |
col int |
| 39 |
expected bool |
| 40 |
}{ |
| 41 |
{0, true}, // First tab stop |
| 42 |
{3, false}, // Not a tab stop |
| 43 |
{4, true}, // Second tab stop |
| 44 |
{7, false}, // Not a tab stop |
| 45 |
{8, true}, // Third tab stop |
| 46 |
{12, true}, // Fourth tab stop |
| 47 |
{15, false}, // Not a tab stop |
| 48 |
}, |
| 49 |
}, |
| 50 |
} |
| 51 |
|
| 52 |
for _, tt := range tests { |
| 53 |
t.Run(tt.name, func(t *testing.T) { |
| 54 |
ts := NewTabStops(tt.width, tt.interval) |
| 55 |
|
| 56 |
// Test initial tab stops |
| 57 |
for _, check := range tt.checks { |
| 58 |
if got := ts.IsStop(check.col); got != check.expected { |
| 59 |
t.Errorf("IsStop(%d) = %v, want %v", check.col, got, check.expected) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Test setting a custom tab stop |
| 64 |
customCol := tt.interval + 1 |
| 65 |
ts.Set(customCol) |
| 66 |
if !ts.IsStop(customCol) { |
| 67 |
t.Errorf("After Set(%d), IsStop(%d) = false, want true", customCol, customCol) |
| 68 |
} |
| 69 |
|
| 70 |
// Test resetting a tab stop |
| 71 |
regularStop := tt.interval |
| 72 |
ts.Reset(regularStop) |
| 73 |
if ts.IsStop(regularStop) { |
| 74 |
t.Errorf("After Reset(%d), IsStop(%d) = true, want false", regularStop, regularStop) |
| 75 |
} |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestTabStopsNavigation(t *testing.T) { |
| 81 |
ts := NewTabStops(24, DefaultTabInterval) |
| 82 |
|
| 83 |
tests := []struct { |
| 84 |
name string |
| 85 |
col int |
| 86 |
wantNext int |
| 87 |
wantPrev int |
| 88 |
}{ |
| 89 |
{ |
| 90 |
name: "from column 0", |
| 91 |
col: 0, |
| 92 |
wantNext: 8, |
| 93 |
wantPrev: 0, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "from column 4", |
| 97 |
col: 4, |
| 98 |
wantNext: 8, |
| 99 |
wantPrev: 0, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "from column 8", |
| 103 |
col: 8, |
| 104 |
wantNext: 16, |
| 105 |
wantPrev: 0, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "from column 20", |
| 109 |
col: 20, |
| 110 |
wantNext: 23, |
| 111 |
wantPrev: 16, |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
for _, tt := range tests { |
| 116 |
t.Run(tt.name, func(t *testing.T) { |
| 117 |
if got := ts.Next(tt.col); got != tt.wantNext { |
| 118 |
t.Errorf("Next(%d) = %v, want %v", tt.col, got, tt.wantNext) |
| 119 |
} |
| 120 |
if got := ts.Prev(tt.col); got != tt.wantPrev { |
| 121 |
t.Errorf("Prev(%d) = %v, want %v", tt.col, got, tt.wantPrev) |
| 122 |
} |
| 123 |
}) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestTabStopsClear(t *testing.T) { |
| 128 |
ts := NewTabStops(24, DefaultTabInterval) |
| 129 |
|
| 130 |
// Verify initial state |
| 131 |
if !ts.IsStop(0) || !ts.IsStop(8) || !ts.IsStop(16) { |
| 132 |
t.Error("Initial tab stops not set correctly") |
| 133 |
} |
| 134 |
|
| 135 |
// Clear all tab stops |
| 136 |
ts.Clear() |
| 137 |
|
| 138 |
// Verify all stops are cleared |
| 139 |
for i := range 24 { |
| 140 |
if ts.IsStop(i) { |
| 141 |
t.Errorf("Tab stop at column %d still set after Clear()", i) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestTabStopsResize(t *testing.T) { |
| 147 |
tests := []struct { |
| 148 |
name string |
| 149 |
initialSize int |
| 150 |
newSize int |
| 151 |
checks []struct { |
| 152 |
col int |
| 153 |
expected bool |
| 154 |
} |
| 155 |
}{ |
| 156 |
{ |
| 157 |
name: "grow buffer", |
| 158 |
initialSize: 16, |
| 159 |
newSize: 24, |
| 160 |
checks: []struct { |
| 161 |
col int |
| 162 |
expected bool |
| 163 |
}{ |
| 164 |
{0, true}, // Original tab stop |
| 165 |
{8, true}, // Original tab stop |
| 166 |
{16, true}, // New tab stop |
| 167 |
{23, false}, // Not a tab stop |
| 168 |
}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "same size - no change", |
| 172 |
initialSize: 16, |
| 173 |
newSize: 16, |
| 174 |
checks: []struct { |
| 175 |
col int |
| 176 |
expected bool |
| 177 |
}{ |
| 178 |
{0, true}, // Original tab stop |
| 179 |
{8, true}, // Original tab stop |
| 180 |
{15, false}, // Not a tab stop |
| 181 |
}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "resize with custom interval", |
| 185 |
initialSize: 8, |
| 186 |
newSize: 16, |
| 187 |
checks: []struct { |
| 188 |
col int |
| 189 |
expected bool |
| 190 |
}{ |
| 191 |
{0, true}, // First tab stop |
| 192 |
{4, true}, // Second tab stop |
| 193 |
{8, true}, // Third tab stop |
| 194 |
{12, true}, // Fourth tab stop |
| 195 |
{15, false}, // Not a tab stop |
| 196 |
}, |
| 197 |
}, |
| 198 |
} |
| 199 |
|
| 200 |
for _, tt := range tests { |
| 201 |
t.Run(tt.name, func(t *testing.T) { |
| 202 |
var ts *TabStops |
| 203 |
if tt.name == "resize with custom interval" { |
| 204 |
ts = NewTabStops(tt.initialSize, 4) // Custom interval of 4 |
| 205 |
} else { |
| 206 |
ts = DefaultTabStops(tt.initialSize) |
| 207 |
} |
| 208 |
|
| 209 |
// Verify initial state |
| 210 |
if ts.width != tt.initialSize { |
| 211 |
t.Errorf("Initial width = %d, want %d", ts.width, tt.initialSize) |
| 212 |
} |
| 213 |
|
| 214 |
// Perform resize |
| 215 |
ts.Resize(tt.newSize) |
| 216 |
|
| 217 |
// Verify new size |
| 218 |
if ts.width != tt.newSize { |
| 219 |
t.Errorf("After resize, width = %d, want %d", ts.width, tt.newSize) |
| 220 |
} |
| 221 |
|
| 222 |
// Check tab stops after resize |
| 223 |
for _, check := range tt.checks { |
| 224 |
if got := ts.IsStop(check.col); got != check.expected { |
| 225 |
t.Errorf("After resize, IsStop(%d) = %v, want %v", |
| 226 |
check.col, got, check.expected) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// Verify stops slice has correct length |
| 231 |
expectedStopsLen := (tt.newSize + (ts.interval - 1)) / ts.interval |
| 232 |
if len(ts.stops) != expectedStopsLen { |
| 233 |
t.Errorf("stops slice length = %d, want %d", |
| 234 |
len(ts.stops), expectedStopsLen) |
| 235 |
} |
| 236 |
}) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestTabStopsResizeEdgeCases(t *testing.T) { |
| 241 |
t.Run("resize to zero", func(t *testing.T) { |
| 242 |
ts := DefaultTabStops(8) |
| 243 |
ts.Resize(0) |
| 244 |
|
| 245 |
if ts.width != 0 { |
| 246 |
t.Errorf("width = %d, want 0", ts.width) |
| 247 |
} |
| 248 |
|
| 249 |
// Verify no tab stops are accessible |
| 250 |
if ts.IsStop(0) { |
| 251 |
t.Error("IsStop(0) should return false for zero width") |
| 252 |
} |
| 253 |
}) |
| 254 |
|
| 255 |
t.Run("resize to very large width", func(t *testing.T) { |
| 256 |
ts := DefaultTabStops(8) |
| 257 |
largeWidth := 1000 |
| 258 |
ts.Resize(largeWidth) |
| 259 |
|
| 260 |
// Check some tab stops at higher positions |
| 261 |
checks := []struct { |
| 262 |
col int |
| 263 |
expected bool |
| 264 |
}{ |
| 265 |
{992, true}, // Multiple of 8 |
| 266 |
{999, false}, // Not a tab stop |
| 267 |
} |
| 268 |
|
| 269 |
for _, check := range checks { |
| 270 |
if got := ts.IsStop(check.col); got != check.expected { |
| 271 |
t.Errorf("IsStop(%d) = %v, want %v", |
| 272 |
check.col, got, check.expected) |
| 273 |
} |
| 274 |
} |
| 275 |
}) |
| 276 |
|
| 277 |
t.Run("multiple resizes", func(t *testing.T) { |
| 278 |
ts := DefaultTabStops(8) |
| 279 |
|
| 280 |
// Perform multiple resizes |
| 281 |
sizes := []int{16, 8, 24, 4} |
| 282 |
for _, size := range sizes { |
| 283 |
ts.Resize(size) |
| 284 |
|
| 285 |
// Verify basic properties after each resize |
| 286 |
if ts.width != size { |
| 287 |
t.Errorf("width = %d, want %d", ts.width, size) |
| 288 |
} |
| 289 |
|
| 290 |
// Check first tab stop is always set |
| 291 |
if !ts.IsStop(0) { |
| 292 |
t.Errorf("After resize to %d, IsStop(0) = false, want true", size) |
| 293 |
} |
| 294 |
} |
| 295 |
}) |
| 296 |
} |