gitstack

grindlemire/go-tui code browser

6.5 KB Go 346 lines 2026-06-03 ยท d15bb9f raw
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package tui

import (
	"testing"
)

func TestRef_SetAndGet(t *testing.T) {
	type tc struct {
		setup    func() (*Ref, *Element)
		wantNil  bool
		wantSame bool
	}

	tests := map[string]tc{
		"set and get returns same element": {
			setup: func() (*Ref, *Element) {
				r := NewRef()
				el := New()
				r.Set(el)
				return r, el
			},
			wantNil:  false,
			wantSame: true,
		},
		"overwrite returns latest element": {
			setup: func() (*Ref, *Element) {
				r := NewRef()
				r.Set(New()) // first
				el := New()  // second
				r.Set(el)
				return r, el
			},
			wantNil:  false,
			wantSame: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			r, el := tt.setup()
			got := r.El()
			if tt.wantNil && got != nil {
				t.Error("El() should return nil")
			}
			if tt.wantSame && got != el {
				t.Error("El() should return the same element that was Set()")
			}
		})
	}
}

func TestRef_IsSet(t *testing.T) {
	type tc struct {
		setup func() *Ref
		want  bool
	}

	tests := map[string]tc{
		"false before set": {
			setup: func() *Ref {
				return NewRef()
			},
			want: false,
		},
		"true after set": {
			setup: func() *Ref {
				r := NewRef()
				r.Set(New())
				return r
			},
			want: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			r := tt.setup()
			if r.IsSet() != tt.want {
				t.Errorf("IsSet() = %v, want %v", r.IsSet(), tt.want)
			}
		})
	}
}

func TestRef_NilBeforeSet(t *testing.T) {
	r := NewRef()
	if r.El() != nil {
		t.Error("El() should return nil before Set() is called")
	}
}

func TestRefList_AppendAndAll(t *testing.T) {
	type tc struct {
		count int
	}

	tests := map[string]tc{
		"empty list":        {count: 0},
		"single element":    {count: 1},
		"multiple elements": {count: 5},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			r := NewRefList()
			elems := make([]*Element, tt.count)
			for i := 0; i < tt.count; i++ {
				elems[i] = New()
				r.Append(elems[i])
			}

			all := r.All()
			if len(all) != tt.count {
				t.Errorf("All() len = %d, want %d", len(all), tt.count)
			}
			for i, el := range all {
				if el != elems[i] {
					t.Errorf("All()[%d] is not the expected element", i)
				}
			}
		})
	}
}

func TestRefList_At(t *testing.T) {
	type tc struct {
		index   int
		wantNil bool
	}

	r := NewRefList()
	el0 := New()
	el1 := New()
	el2 := New()
	r.Append(el0)
	r.Append(el1)
	r.Append(el2)

	tests := map[string]tc{
		"valid index 0":          {index: 0, wantNil: false},
		"valid index 1":          {index: 1, wantNil: false},
		"valid index 2":          {index: 2, wantNil: false},
		"out of bounds high":     {index: 3, wantNil: true},
		"out of bounds negative": {index: -1, wantNil: true},
	}

	elems := []*Element{el0, el1, el2}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := r.At(tt.index)
			if tt.wantNil {
				if got != nil {
					t.Error("At() should return nil for out of bounds index")
				}
			} else {
				if got != elems[tt.index] {
					t.Error("At() should return the correct element")
				}
			}
		})
	}
}

func TestRefList_Len(t *testing.T) {
	type tc struct {
		count int
	}

	tests := map[string]tc{
		"empty":    {count: 0},
		"one":      {count: 1},
		"multiple": {count: 3},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			r := NewRefList()
			for i := 0; i < tt.count; i++ {
				r.Append(New())
			}
			if r.Len() != tt.count {
				t.Errorf("Len() = %d, want %d", r.Len(), tt.count)
			}
		})
	}
}

func TestRefMap_PutAndGet(t *testing.T) {
	type tc struct {
		key     string
		wantNil bool
	}

	r := NewRefMap[string]()
	elA := New()
	elB := New()
	r.Put("a", elA)
	r.Put("b", elB)

	tests := map[string]tc{
		"existing key a": {key: "a", wantNil: false},
		"existing key b": {key: "b", wantNil: false},
		"missing key":    {key: "c", wantNil: true},
	}

	expected := map[string]*Element{"a": elA, "b": elB}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := r.Get(tt.key)
			if tt.wantNil {
				if got != nil {
					t.Error("Get() should return nil for missing key")
				}
			} else {
				if got != expected[tt.key] {
					t.Error("Get() should return the correct element")
				}
			}
		})
	}
}

func TestRefMap_All(t *testing.T) {
	r := NewRefMap[string]()
	elA := New()
	elB := New()
	r.Put("a", elA)
	r.Put("b", elB)

	all := r.All()
	if len(all) != 2 {
		t.Errorf("All() len = %d, want 2", len(all))
	}
	if all["a"] != elA {
		t.Error("All()[\"a\"] should be elA")
	}
	if all["b"] != elB {
		t.Error("All()[\"b\"] should be elB")
	}
}

func TestRefMap_GetMissing(t *testing.T) {
	r := NewRefMap[string]()
	if r.Get("nonexistent") != nil {
		t.Error("Get() should return nil for missing key")
	}
}

// stubComponent is a minimal Component for testing RefComponent.
type stubComponent struct{}

func (s *stubComponent) Render(app *App) *Element { return New() }

// otherComponent is a different type to test type mismatch.
type otherComponent struct{}

func (o *otherComponent) Render(app *App) *Element { return New() }

func TestRefComponent(t *testing.T) {
	type tc struct {
		setup func() *Ref
		want  bool // whether result should be non-nil
	}

	tests := map[string]tc{
		"returns typed component": {
			setup: func() *Ref {
				r := NewRef()
				el := New()
				el.component = &stubComponent{}
				r.Set(el)
				return r
			},
			want: true,
		},
		"nil ref": {
			setup: func() *Ref {
				return nil
			},
			want: false,
		},
		"no element set": {
			setup: func() *Ref {
				return NewRef()
			},
			want: false,
		},
		"element has no component": {
			setup: func() *Ref {
				r := NewRef()
				r.Set(New())
				return r
			},
			want: false,
		},
		"type mismatch": {
			setup: func() *Ref {
				r := NewRef()
				el := New()
				el.component = &otherComponent{}
				r.Set(el)
				return r
			},
			want: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			ref := tt.setup()
			got := RefComponent[*stubComponent](ref)
			if tt.want && got == nil {
				t.Error("RefComponent() should return non-nil component")
			}
			if !tt.want && got != nil {
				t.Error("RefComponent() should return nil")
			}
		})
	}
}

func TestRefMap_Len(t *testing.T) {
	type tc struct {
		count int
	}

	tests := map[string]tc{
		"empty":    {count: 0},
		"one":      {count: 1},
		"multiple": {count: 3},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			r := NewRefMap[int]()
			for i := 0; i < tt.count; i++ {
				r.Put(i, New())
			}
			if r.Len() != tt.count {
				t.Errorf("Len() = %d, want %d", r.Len(), tt.count)
			}
		})
	}
}