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
|
package tui
import (
"testing"
)
func TestElement_Watchers(t *testing.T) {
type tc struct {
setup func(e *Element)
want int
}
tests := map[string]tc{
"no watchers by default": {
setup: func(e *Element) {},
want: 0,
},
"add one watcher": {
setup: func(e *Element) {
ch := make(chan string)
e.AddWatcher(Watch(ch, func(s string) {}))
},
want: 1,
},
"add multiple watchers": {
setup: func(e *Element) {
ch1 := make(chan string)
ch2 := make(chan int)
e.AddWatcher(Watch(ch1, func(s string) {}))
e.AddWatcher(Watch(ch2, func(i int) {}))
},
want: 2,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
e := New()
tt.setup(e)
if got := len(e.Watchers()); got != tt.want {
t.Errorf("len(Watchers()) = %d, want %d", got, tt.want)
}
})
}
}
func TestElement_WalkWatchers(t *testing.T) {
parent := New()
child := New()
parent.AddChild(child)
ch1 := make(chan string)
ch2 := make(chan int)
parent.AddWatcher(Watch(ch1, func(s string) {}))
child.AddWatcher(Watch(ch2, func(i int) {}))
var count int
parent.WalkWatchers(func(w Watcher) {
count++
})
if count != 2 {
t.Errorf("WalkWatchers visited %d watchers, want 2", count)
}
}
func TestElement_SetOnUpdate(t *testing.T) {
e := New()
called := false
e.SetOnUpdate(func() {
called = true
})
buf := NewBuffer(10, 10)
e.Render(buf, 10, 10)
if !called {
t.Error("SetOnUpdate callback should be called during Render")
}
}
|