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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
package tui
import "testing"
type mockComponent struct {
renderCount int
}
func (m *mockComponent) Render(app *App) *Element {
m.renderCount++
return New()
}
type mockInitComponent struct {
renderCount int
initCalled bool
cleanupCalls int
}
func (m *mockInitComponent) Render(app *App) *Element {
m.renderCount++
return New()
}
func (m *mockInitComponent) Init() func() {
m.initCalled = true
return func() {
m.cleanupCalls++
}
}
type mockInitNoCleanup struct {
initCalled bool
}
func (m *mockInitNoCleanup) Render(app *App) *Element {
return New()
}
func (m *mockInitNoCleanup) Init() func() {
m.initCalled = true
return nil
}
type mockParent struct {
id int
}
func (m *mockParent) Render(app *App) *Element {
return New()
}
func setupTestMountState() func() {
prevMounts := testApp.mounts
testApp.mounts = newMountState()
return func() {
testApp.mounts = prevMounts
}
}
func TestMount_FirstCallCreatesInstance(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
factoryCalls := 0
var created *mockComponent
el := testApp.Mount(parent, 0, func() Component {
factoryCalls++
created = &mockComponent{}
return created
})
if factoryCalls != 1 {
t.Errorf("factory called %d times, want 1", factoryCalls)
}
if created == nil {
t.Fatal("factory did not create component")
}
if created.renderCount != 1 {
t.Errorf("Render called %d times, want 1", created.renderCount)
}
if el == nil {
t.Fatal("Mount returned nil element")
}
if el.component != created {
t.Error("element.component not set to created instance")
}
}
func TestMount_SubsequentCallReturnsCached(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
factoryCalls := 0
instance := &mockComponent{}
testApp.Mount(parent, 0, func() Component {
factoryCalls++
return instance
})
el := testApp.Mount(parent, 0, func() Component {
factoryCalls++
return &mockComponent{}
})
if factoryCalls != 1 {
t.Errorf("factory called %d times, want 1 (should use cache)", factoryCalls)
}
if instance.renderCount != 2 {
t.Errorf("Render called %d times, want 2 (once per Mount call)", instance.renderCount)
}
if el.component != instance {
t.Error("element.component should be the cached instance")
}
}
func TestMount_InitCalledOnFirstMount(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitComponent{}
testApp.Mount(parent, 0, func() Component {
return instance
})
if !instance.initCalled {
t.Error("Init() should be called on first mount")
}
}
func TestMount_InitNotCalledOnSubsequentMount(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitComponent{}
testApp.Mount(parent, 0, func() Component {
return instance
})
instance.initCalled = false
testApp.Mount(parent, 0, func() Component {
return instance
})
if instance.initCalled {
t.Error("Init() should not be called on subsequent mount")
}
}
func TestMount_NilCleanupHandled(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitNoCleanup{}
testApp.Mount(parent, 0, func() Component {
return instance
})
if !instance.initCalled {
t.Error("Init() should be called")
}
ms := testApp.mounts
ms.activeKeys = make(map[mountKey]bool)
ms.sweep()
}
func TestMount_DifferentKeysIndependent(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instanceA := &mockComponent{}
instanceB := &mockComponent{}
testApp.Mount(parent, 0, func() Component { return instanceA })
testApp.Mount(parent, 1, func() Component { return instanceB })
if instanceA.renderCount != 1 {
t.Errorf("instanceA.renderCount = %d, want 1", instanceA.renderCount)
}
if instanceB.renderCount != 1 {
t.Errorf("instanceB.renderCount = %d, want 1", instanceB.renderCount)
}
}
func TestMount_DifferentParentsIndependent(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parentA := &mockParent{id: 1}
parentB := &mockParent{id: 2}
factoryCalls := 0
testApp.Mount(parentA, 0, func() Component {
factoryCalls++
return &mockComponent{}
})
testApp.Mount(parentB, 0, func() Component {
factoryCalls++
return &mockComponent{}
})
if factoryCalls != 2 {
t.Errorf("factory called %d times, want 2 (different parents)", factoryCalls)
}
}
func TestMountState_SweepCleansInactive(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitComponent{}
testApp.Mount(parent, 0, func() Component { return instance })
ms := testApp.mounts
ms.activeKeys = make(map[mountKey]bool)
ms.sweep()
if instance.cleanupCalls != 1 {
t.Errorf("cleanup called %d times, want 1", instance.cleanupCalls)
}
if len(ms.cache) != 0 {
t.Errorf("cache has %d entries, want 0 after sweep", len(ms.cache))
}
}
func TestMountState_SweepKeepsActive(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitComponent{}
testApp.Mount(parent, 0, func() Component { return instance })
ms := testApp.mounts
ms.sweep()
if instance.cleanupCalls != 0 {
t.Errorf("cleanup called %d times, want 0 (component still active)", instance.cleanupCalls)
}
if len(ms.cache) != 1 {
t.Errorf("cache has %d entries, want 1 after sweep", len(ms.cache))
}
}
func TestMountState_SweepResetsActiveKeys(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
testApp.Mount(parent, 0, func() Component { return &mockComponent{} })
ms := testApp.mounts
if len(ms.activeKeys) != 1 {
t.Fatalf("activeKeys has %d entries before sweep, want 1", len(ms.activeKeys))
}
ms.sweep()
if len(ms.activeKeys) != 0 {
t.Errorf("activeKeys has %d entries after sweep, want 0 (reset for next render)", len(ms.activeKeys))
}
}
func TestMountState_SweepMultipleComponents(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
active := &mockInitComponent{}
inactive := &mockInitComponent{}
testApp.Mount(parent, 0, func() Component { return active })
testApp.Mount(parent, 1, func() Component { return inactive })
ms := testApp.mounts
ms.activeKeys = make(map[mountKey]bool)
key0 := mountKey{parent: parent, index: 0}
ms.activeKeys[key0] = true
ms.sweep()
if active.cleanupCalls != 0 {
t.Errorf("active.cleanupCalls = %d, want 0", active.cleanupCalls)
}
if inactive.cleanupCalls != 1 {
t.Errorf("inactive.cleanupCalls = %d, want 1", inactive.cleanupCalls)
}
if len(ms.cache) != 1 {
t.Errorf("cache has %d entries, want 1", len(ms.cache))
}
}
func TestWalkComponents_FindsComponents(t *testing.T) {
comp1 := &mockComponent{}
comp2 := &mockComponent{}
child2 := New()
child2.component = comp2
child1 := New()
child1.component = comp1
child1.AddChild(child2)
root := New()
root.AddChild(child1)
var found []Component
walkComponents(nil, root, func(c Component) {
found = append(found, c)
})
if len(found) != 2 {
t.Fatalf("walkComponents found %d components, want 2", len(found))
}
if found[0] != comp1 {
t.Error("first component should be comp1 (BFS order)")
}
if found[1] != comp2 {
t.Error("second component should be comp2 (BFS order)")
}
}
func TestWalkComponents_SkipsNonComponentElements(t *testing.T) {
comp := &mockComponent{}
child := New()
child.component = comp
root := New()
root.AddChild(New(), child, New())
var found []Component
walkComponents(nil, root, func(c Component) {
found = append(found, c)
})
if len(found) != 1 {
t.Errorf("walkComponents found %d components, want 1", len(found))
}
}
func TestWalkComponents_NilRoot(t *testing.T) {
var found []Component
walkComponents(nil, nil, func(c Component) {
found = append(found, c)
})
if len(found) != 0 {
t.Errorf("walkComponents on nil root found %d components, want 0", len(found))
}
}
func TestWalkComponents_RootCompVisitedFirst(t *testing.T) {
rootComp := &mockComponent{}
treeComp := &mockComponent{}
child := New()
child.component = treeComp
root := New()
root.AddChild(child)
var found []Component
walkComponents(rootComp, root, func(c Component) {
found = append(found, c)
})
if len(found) != 2 {
t.Fatalf("walkComponents found %d components, want 2", len(found))
}
if found[0] != rootComp {
t.Error("first component should be rootComp")
}
if found[1] != treeComp {
t.Error("second component should be treeComp")
}
}
func TestWalkComponents_NilRootCompSkipped(t *testing.T) {
treeComp := &mockComponent{}
child := New()
child.component = treeComp
root := New()
root.AddChild(child)
var found []Component
walkComponents(nil, root, func(c Component) {
found = append(found, c)
})
if len(found) != 1 {
t.Fatalf("walkComponents found %d components, want 1", len(found))
}
if found[0] != treeComp {
t.Error("only component should be treeComp")
}
}
func TestMountPersistent_SurvivesSweep(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockInitComponent{}
testApp.MountPersistent(parent, 0, func() Component { return instance })
ms := testApp.mounts
ms.activeKeys = make(map[mountKey]bool)
ms.sweep()
if instance.cleanupCalls != 0 {
t.Errorf("cleanup called %d times, want 0 (persistent survives sweep)", instance.cleanupCalls)
}
if len(ms.cache) != 1 {
t.Errorf("cache has %d entries, want 1 (persistent survives)", len(ms.cache))
}
}
func TestMountPersistent_StillCachesInstance(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
factoryCalls := 0
instance := &mockComponent{}
testApp.MountPersistent(parent, 0, func() Component {
factoryCalls++
return instance
})
testApp.MountPersistent(parent, 0, func() Component {
factoryCalls++
return &mockComponent{}
})
if factoryCalls != 1 {
t.Errorf("factory called %d times, want 1 (should use cache)", factoryCalls)
}
if instance.renderCount != 2 {
t.Errorf("Render called %d times, want 2 (once per MountPersistent call)", instance.renderCount)
}
}
func TestMountPersistent_RegularMountStillSwept(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
persistent := &mockInitComponent{}
regular := &mockInitComponent{}
testApp.MountPersistent(parent, 0, func() Component { return persistent })
testApp.Mount(parent, 1, func() Component { return regular })
ms := testApp.mounts
ms.activeKeys = make(map[mountKey]bool)
ms.sweep()
if persistent.cleanupCalls != 0 {
t.Errorf("persistent.cleanupCalls = %d, want 0", persistent.cleanupCalls)
}
if regular.cleanupCalls != 1 {
t.Errorf("regular.cleanupCalls = %d, want 1", regular.cleanupCalls)
}
if len(ms.cache) != 1 {
t.Errorf("cache has %d entries, want 1 (only persistent remains)", len(ms.cache))
}
}
func TestMount_ElementComponentGetter(t *testing.T) {
cleanup := setupTestMountState()
defer cleanup()
parent := &mockParent{}
instance := &mockComponent{}
el := testApp.Mount(parent, 0, func() Component { return instance })
got := el.Component()
if got != instance {
t.Errorf("Component() = %v, want %v", got, instance)
}
}
func TestNewMountState(t *testing.T) {
ms := newMountState()
if ms.cache == nil {
t.Error("cache should be initialized")
}
if ms.cleanups == nil {
t.Error("cleanups should be initialized")
}
if ms.activeKeys == nil {
t.Error("activeKeys should be initialized")
}
if len(ms.cache) != 0 {
t.Errorf("cache should be empty, has %d entries", len(ms.cache))
}
}
|