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