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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
package tui
import (
"sync"
"sync/atomic"
"testing"
)
func TestState_Set_CallsBindings(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var called bool
var receivedValue int
s.Bind(func(v int) {
called = true
receivedValue = v
})
s.Set(42)
if !called {
t.Error("binding was not called")
}
if receivedValue != 42 {
t.Errorf("binding received %d, want 42", receivedValue)
}
}
func TestState_Update(t *testing.T) {
s := NewState(10)
s.BindApp(testApp)
s.Update(func(v int) int { return v + 5 })
if got := s.Get(); got != 15 {
t.Errorf("after Update(+5), Get() = %d, want 15", got)
}
}
func TestState_Update_CallsBindings(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var receivedValue int
s.Bind(func(v int) {
receivedValue = v
})
s.Update(func(v int) int { return v + 100 })
if receivedValue != 100 {
t.Errorf("binding received %d, want 100", receivedValue)
}
}
func TestState_Bind(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var callCount int
s.Bind(func(v int) {
callCount++
})
if callCount != 0 {
t.Errorf("binding called %d times on registration, want 0", callCount)
}
s.Set(1)
if callCount != 1 {
t.Errorf("binding called %d times after Set, want 1", callCount)
}
s.Set(2)
if callCount != 2 {
t.Errorf("binding called %d times after second Set, want 2", callCount)
}
}
func TestState_Bind_ReceivesNewValue(t *testing.T) {
s := NewState("initial")
s.BindApp(testApp)
var values []string
s.Bind(func(v string) {
values = append(values, v)
})
s.Set("first")
s.Set("second")
s.Set("third")
expected := []string{"first", "second", "third"}
if len(values) != len(expected) {
t.Errorf("binding called %d times, want %d", len(values), len(expected))
return
}
for i, v := range values {
if v != expected[i] {
t.Errorf("values[%d] = %q, want %q", i, v, expected[i])
}
}
}
func TestState_Unbind(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var callCount int
unbind := s.Bind(func(v int) {
callCount++
})
s.Set(1)
if callCount != 1 {
t.Errorf("before unbind: callCount = %d, want 1", callCount)
}
unbind()
s.Set(2)
if callCount != 1 {
t.Errorf("after unbind: callCount = %d, want 1 (should not increase)", callCount)
}
s.Set(3)
s.Set(4)
if callCount != 1 {
t.Errorf("after multiple sets post-unbind: callCount = %d, want 1", callCount)
}
}
func TestState_MultipleBindings(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var callOrder []int
s.Bind(func(v int) { callOrder = append(callOrder, 1) })
s.Bind(func(v int) { callOrder = append(callOrder, 2) })
s.Bind(func(v int) { callOrder = append(callOrder, 3) })
s.Set(42)
if len(callOrder) != 3 {
t.Errorf("got %d calls, want 3", len(callOrder))
}
for i, v := range callOrder {
if v != i+1 {
t.Errorf("callOrder[%d] = %d, want %d", i, v, i+1)
}
}
}
func TestState_UnbindSpecificBinding(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var calls []int
s.Bind(func(v int) { calls = append(calls, 1) })
unbind2 := s.Bind(func(v int) { calls = append(calls, 2) })
s.Bind(func(v int) { calls = append(calls, 3) })
s.Set(1)
if len(calls) != 3 {
t.Errorf("before unbind: got %d calls, want 3", len(calls))
}
calls = nil
unbind2()
s.Set(2)
if len(calls) != 2 {
t.Errorf("after unbind: got %d calls, want 2", len(calls))
}
if calls[0] != 1 || calls[1] != 3 {
t.Errorf("after unbind: calls = %v, want [1 3]", calls)
}
}
func TestState_ConcurrentGet(t *testing.T) {
s := NewState(42)
var wg sync.WaitGroup
const numGoroutines = 100
results := make([]int, numGoroutines)
for i := range numGoroutines {
wg.Add(1)
go func(idx int) {
defer wg.Done()
results[idx] = s.Get()
}(i)
}
wg.Wait()
for i, v := range results {
if v != 42 {
t.Errorf("results[%d] = %d, want 42", i, v)
}
}
}
func TestState_ConcurrentGetDuringSet(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var wg sync.WaitGroup
const numReaders = 50
const numWrites = 100
var invalidCount atomic.Int64
for range numReaders {
wg.Go(func() {
for range numWrites {
v := s.Get()
if v < 0 || v > numWrites {
invalidCount.Add(1)
}
}
})
}
wg.Go(func() {
for i := 1; i <= numWrites; i++ {
s.Set(i)
}
})
wg.Wait()
if count := invalidCount.Load(); count > 0 {
t.Errorf("Get() returned %d invalid values (expected 0)", count)
}
if got := s.Get(); got != numWrites {
t.Errorf("final Get() = %d, want %d", got, numWrites)
}
}
func TestState_BindingCanCallGet(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var gotValue int
s.Bind(func(v int) {
gotValue = s.Get()
})
s.Set(42)
if gotValue != 42 {
t.Errorf("gotValue = %d, want 42", gotValue)
}
}
func TestState_SetWithZeroBindings(t *testing.T) {
testApp.resetDirty()
s := NewState(0)
s.BindApp(testApp)
s.Set(42)
if got := s.Get(); got != 42 {
t.Errorf("Get() = %d, want 42", got)
}
if !testApp.checkAndClearDirty() {
t.Error("should be dirty after Set() even with no bindings")
}
}
func TestState_UnbindIdempotent(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
var callCount int
unbind := s.Bind(func(v int) {
callCount++
})
unbind()
unbind()
unbind()
s.Set(1)
if callCount != 0 {
t.Errorf("callCount = %d, want 0 after unbind", callCount)
}
}
func TestState_InactiveBindingsCleanup(t *testing.T) {
s := NewState(0)
s.BindApp(testApp)
unbind1 := s.Bind(func(v int) {})
unbind2 := s.Bind(func(v int) {})
unbind3 := s.Bind(func(v int) {})
unbind1()
unbind3()
s.mu.RLock()
beforeSet := len(s.bindings)
s.mu.RUnlock()
if beforeSet != 3 {
t.Errorf("before Set: bindings length = %d, want 3", beforeSet)
}
s.Set(42)
s.mu.RLock()
afterSet := len(s.bindings)
s.mu.RUnlock()
if afterSet != 1 {
t.Errorf("after Set: bindings length = %d, want 1 (only active binding)", afterSet)
}
unbind2()
s.Set(43)
s.mu.RLock()
afterAllUnbind := len(s.bindings)
s.mu.RUnlock()
if afterAllUnbind != 0 {
t.Errorf("after all unbind: bindings length = %d, want 0", afterAllUnbind)
}
}
|