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
|
package tui
import (
"testing"
)
func TestApp_SetRootAndRoot(t *testing.T) {
type tc struct {
createRoot bool
}
tests := map[string]tc{
"with root element": {
createRoot: true,
},
"without root element": {
createRoot: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
app := &App{
focus: newFocusManager(),
buffer: NewBuffer(80, 24),
}
if tt.createRoot {
root := New()
app.SetRoot(root)
if app.Root() != root {
t.Error("Root() should return the element passed to SetRoot()")
}
} else {
if app.Root() != nil {
t.Error("Root() should return nil when no root set")
}
}
})
}
}
func TestApp_FocusedNoElements(t *testing.T) {
app := &App{
focus: newFocusManager(),
}
if app.Focused() != nil {
t.Error("Focused() should return nil when no elements are registered")
}
}
func TestApp_DispatchResizeEvent(t *testing.T) {
type tc struct {
initialWidth int
initialHeight int
resizeWidth int
resizeHeight int
hasRoot bool
}
tests := map[string]tc{
"resize with root": {
initialWidth: 80,
initialHeight: 24,
resizeWidth: 100,
resizeHeight: 30,
hasRoot: true,
},
"resize without root": {
initialWidth: 80,
initialHeight: 24,
resizeWidth: 100,
resizeHeight: 30,
hasRoot: false,
},
"shrink terminal": {
initialWidth: 100,
initialHeight: 50,
resizeWidth: 60,
resizeHeight: 20,
hasRoot: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
buffer := NewBuffer(tt.initialWidth, tt.initialHeight)
app := &App{
focus: newFocusManager(),
buffer: buffer,
}
var root *Element
if tt.hasRoot {
root = New()
app.SetRoot(root)
root.SetDirty(false)
}
event := ResizeEvent{Width: tt.resizeWidth, Height: tt.resizeHeight}
handled := app.Dispatch(event)
if !handled {
t.Error("Dispatch(ResizeEvent) should return true")
}
bufW, bufH := app.buffer.Size()
if bufW != tt.resizeWidth || bufH != tt.resizeHeight {
t.Errorf("Buffer size = (%d, %d), want (%d, %d)", bufW, bufH, tt.resizeWidth, tt.resizeHeight)
}
if tt.hasRoot && !root.IsDirty() {
t.Error("Root should be dirty after resize")
}
})
}
}
func TestApp_DispatchKeyEvent(t *testing.T) {
type tc struct {
hasFocused bool
handled bool
expectReturn bool
}
tests := map[string]tc{
"event handled by focused element": {
hasFocused: true,
handled: true,
expectReturn: true,
},
"event not handled by focused element": {
hasFocused: true,
handled: false,
expectReturn: false,
},
"no focused element": {
hasFocused: false,
handled: false,
expectReturn: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
focus := newFocusManager()
if tt.hasFocused {
mock := newMockFocusable("a", true)
mock.handled = tt.handled
focus.Register(mock)
focus.SetFocus(mock)
}
app := &App{
focus: focus,
buffer: NewBuffer(80, 24),
}
event := KeyEvent{Key: KeyEnter}
result := app.Dispatch(event)
if result != tt.expectReturn {
t.Errorf("Dispatch(KeyEvent) = %v, want %v", result, tt.expectReturn)
}
})
}
}
func TestApp_RenderWithRoot(t *testing.T) {
buffer := NewBuffer(80, 24)
app := &App{
buffer: buffer,
focus: newFocusManager(),
}
root := New(WithText("hello"))
app.SetRoot(root)
root.Render(buffer, 80, 24)
if root.IsDirty() {
t.Error("Root should not be dirty after Render()")
}
}
|