File: tabstop_test.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (296 lines) | stat: -rw-r--r-- 6,453 bytes parent folder | download
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
package cellbuf

import (
	"testing"
)

func TestTabStops(t *testing.T) {
	tests := []struct {
		name     string
		width    int
		interval int
		checks   []struct {
			col      int
			expected bool
		}
	}{
		{
			name:     "default interval of 8",
			width:    24,
			interval: DefaultTabInterval,
			checks: []struct {
				col      int
				expected bool
			}{
				{0, true},   // First tab stop
				{7, false},  // Not a tab stop
				{8, true},   // Second tab stop
				{15, false}, // Not a tab stop
				{16, true},  // Third tab stop
				{23, false}, // Not a tab stop
			},
		},
		{
			name:     "custom interval of 4",
			width:    16,
			interval: 4,
			checks: []struct {
				col      int
				expected bool
			}{
				{0, true},   // First tab stop
				{3, false},  // Not a tab stop
				{4, true},   // Second tab stop
				{7, false},  // Not a tab stop
				{8, true},   // Third tab stop
				{12, true},  // Fourth tab stop
				{15, false}, // Not a tab stop
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ts := NewTabStops(tt.width, tt.interval)

			// Test initial tab stops
			for _, check := range tt.checks {
				if got := ts.IsStop(check.col); got != check.expected {
					t.Errorf("IsStop(%d) = %v, want %v", check.col, got, check.expected)
				}
			}

			// Test setting a custom tab stop
			customCol := tt.interval + 1
			ts.Set(customCol)
			if !ts.IsStop(customCol) {
				t.Errorf("After Set(%d), IsStop(%d) = false, want true", customCol, customCol)
			}

			// Test resetting a tab stop
			regularStop := tt.interval
			ts.Reset(regularStop)
			if ts.IsStop(regularStop) {
				t.Errorf("After Reset(%d), IsStop(%d) = true, want false", regularStop, regularStop)
			}
		})
	}
}

func TestTabStopsNavigation(t *testing.T) {
	ts := NewTabStops(24, DefaultTabInterval)

	tests := []struct {
		name     string
		col      int
		wantNext int
		wantPrev int
	}{
		{
			name:     "from column 0",
			col:      0,
			wantNext: 8,
			wantPrev: 0,
		},
		{
			name:     "from column 4",
			col:      4,
			wantNext: 8,
			wantPrev: 0,
		},
		{
			name:     "from column 8",
			col:      8,
			wantNext: 16,
			wantPrev: 0,
		},
		{
			name:     "from column 20",
			col:      20,
			wantNext: 23,
			wantPrev: 16,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := ts.Next(tt.col); got != tt.wantNext {
				t.Errorf("Next(%d) = %v, want %v", tt.col, got, tt.wantNext)
			}
			if got := ts.Prev(tt.col); got != tt.wantPrev {
				t.Errorf("Prev(%d) = %v, want %v", tt.col, got, tt.wantPrev)
			}
		})
	}
}

func TestTabStopsClear(t *testing.T) {
	ts := NewTabStops(24, DefaultTabInterval)

	// Verify initial state
	if !ts.IsStop(0) || !ts.IsStop(8) || !ts.IsStop(16) {
		t.Error("Initial tab stops not set correctly")
	}

	// Clear all tab stops
	ts.Clear()

	// Verify all stops are cleared
	for i := range 24 {
		if ts.IsStop(i) {
			t.Errorf("Tab stop at column %d still set after Clear()", i)
		}
	}
}

func TestTabStopsResize(t *testing.T) {
	tests := []struct {
		name        string
		initialSize int
		newSize     int
		checks      []struct {
			col      int
			expected bool
		}
	}{
		{
			name:        "grow buffer",
			initialSize: 16,
			newSize:     24,
			checks: []struct {
				col      int
				expected bool
			}{
				{0, true},   // Original tab stop
				{8, true},   // Original tab stop
				{16, true},  // New tab stop
				{23, false}, // Not a tab stop
			},
		},
		{
			name:        "same size - no change",
			initialSize: 16,
			newSize:     16,
			checks: []struct {
				col      int
				expected bool
			}{
				{0, true},   // Original tab stop
				{8, true},   // Original tab stop
				{15, false}, // Not a tab stop
			},
		},
		{
			name:        "resize with custom interval",
			initialSize: 8,
			newSize:     16,
			checks: []struct {
				col      int
				expected bool
			}{
				{0, true},   // First tab stop
				{4, true},   // Second tab stop
				{8, true},   // Third tab stop
				{12, true},  // Fourth tab stop
				{15, false}, // Not a tab stop
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var ts *TabStops
			if tt.name == "resize with custom interval" {
				ts = NewTabStops(tt.initialSize, 4) // Custom interval of 4
			} else {
				ts = DefaultTabStops(tt.initialSize)
			}

			// Verify initial state
			if ts.width != tt.initialSize {
				t.Errorf("Initial width = %d, want %d", ts.width, tt.initialSize)
			}

			// Perform resize
			ts.Resize(tt.newSize)

			// Verify new size
			if ts.width != tt.newSize {
				t.Errorf("After resize, width = %d, want %d", ts.width, tt.newSize)
			}

			// Check tab stops after resize
			for _, check := range tt.checks {
				if got := ts.IsStop(check.col); got != check.expected {
					t.Errorf("After resize, IsStop(%d) = %v, want %v",
						check.col, got, check.expected)
				}
			}

			// Verify stops slice has correct length
			expectedStopsLen := (tt.newSize + (ts.interval - 1)) / ts.interval
			if len(ts.stops) != expectedStopsLen {
				t.Errorf("stops slice length = %d, want %d",
					len(ts.stops), expectedStopsLen)
			}
		})
	}
}

func TestTabStopsResizeEdgeCases(t *testing.T) {
	t.Run("resize to zero", func(t *testing.T) {
		ts := DefaultTabStops(8)
		ts.Resize(0)

		if ts.width != 0 {
			t.Errorf("width = %d, want 0", ts.width)
		}

		// Verify no tab stops are accessible
		if ts.IsStop(0) {
			t.Error("IsStop(0) should return false for zero width")
		}
	})

	t.Run("resize to very large width", func(t *testing.T) {
		ts := DefaultTabStops(8)
		largeWidth := 1000
		ts.Resize(largeWidth)

		// Check some tab stops at higher positions
		checks := []struct {
			col      int
			expected bool
		}{
			{992, true},  // Multiple of 8
			{999, false}, // Not a tab stop
		}

		for _, check := range checks {
			if got := ts.IsStop(check.col); got != check.expected {
				t.Errorf("IsStop(%d) = %v, want %v",
					check.col, got, check.expected)
			}
		}
	})

	t.Run("multiple resizes", func(t *testing.T) {
		ts := DefaultTabStops(8)

		// Perform multiple resizes
		sizes := []int{16, 8, 24, 4}
		for _, size := range sizes {
			ts.Resize(size)

			// Verify basic properties after each resize
			if ts.width != size {
				t.Errorf("width = %d, want %d", ts.width, size)
			}

			// Check first tab stop is always set
			if !ts.IsStop(0) {
				t.Errorf("After resize to %d, IsStop(0) = false, want true", size)
			}
		}
	})
}