forked from jroimartin/gocui
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtesting_test.go
277 lines (240 loc) · 6.85 KB
/
testing_test.go
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
// Copyright 2021 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
)
func TestTestingScreenReturnsCorrectContent(t *testing.T) {
// Track what happened in the view, we'll assert on these
didCallCTRLC := false
expectedViewContent := "Hello world!"
viewName := "testView1"
// Create a view specifying the "OutputSimulator" mode
g, err := NewGui(OutputSimulator, true)
if err != nil {
t.Error(err)
}
g.SetManagerFunc(func(g *Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView(viewName, maxX/2-7, maxY/2, maxX/2+7, maxY/2+2, 0); err != nil {
if !errors.Is(err, ErrUnknownView) {
return err
}
if _, err := g.SetCurrentView(viewName); err != nil {
return err
}
// Have the view draw "Hello world!"
fmt.Fprintln(v, expectedViewContent)
}
return nil
})
// Create a key binding which sets "didCallCTRLC" when triggered
exampleBindingToTest := func(g *Gui, v *View) error {
didCallCTRLC = true
return nil
}
if err := g.SetKeybinding("", KeyCtrlC, ModNone, exampleBindingToTest); err != nil {
t.Error(err)
}
// Create a test screen and start gocui
testingScreen := g.GetTestingScreen()
cleanup := testingScreen.StartGui()
defer cleanup()
// NOTE: This sequence can be replaced with `testingScreen.SendKeySync(KeyCtrlC)` (we keep it for covering the use case)
// Send a key to gocui
testingScreen.SendKey(KeyCtrlC)
// Wait for key to be processed
testingScreen.WaitSync()
// Test that the keybinding fired and set "didCallCTRLC" to true
if !didCallCTRLC {
t.Error("Expect the simulator to invoke the key handler for CTRLC")
}
// check view content
assertView(t, testingScreen, viewName, expectedViewContent)
}
func TestTestingScreenMultipleKeys(t *testing.T) {
// Track what happened in the view, we'll assert on these
didCallCTRLC := false
expectedViewContent := "Hello world!"
expectedViewContent1 := "Hello World!"
expectedViewContent2 := "HELLO WORLD!"
expectedViewContent3 := "Hello lord!!"
viewName := "testView1"
// Create a view specifying the "OutputSimulator" mode
g, err := NewGui(OutputSimulator, true)
if err != nil {
t.Error(err)
}
g.SetManagerFunc(func(g *Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView(viewName, maxX/2-7, maxY/2, maxX/2+7, maxY/2+2, 0); err != nil {
if !errors.Is(err, ErrUnknownView) {
return err
}
if _, err := g.SetCurrentView(viewName); err != nil {
return err
}
// Have the view draw "Hello world!"
fmt.Fprintln(v, expectedViewContent)
}
return nil
})
// Create a key binding which sets "didCallCTRLC" when triggered
exampleBindingToTest := func(g *Gui, v *View) error {
didCallCTRLC = true
return nil
}
if err := g.SetKeybinding("", KeyCtrlC, ModNone, exampleBindingToTest); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF1, ModNone, func(g *Gui, v *View) error {
v.Clear()
fmt.Fprintln(v, expectedViewContent1)
return nil
}); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF2, ModNone, func(g *Gui, v *View) error {
v.Clear()
<-time.After(time.Millisecond * 100)
fmt.Fprintln(v, expectedViewContent2)
return nil
}); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF3, ModNone, func(g *Gui, v *View) error {
v.Clear()
fmt.Fprintln(v, expectedViewContent3)
return nil
}); err != nil {
t.Error(err)
}
// Create a test screen and start gocui
testingScreen := g.GetTestingScreen()
cleanup := testingScreen.StartGui()
defer cleanup()
// check view content
assertView(t, testingScreen, viewName, expectedViewContent)
// Send a key to gocui
testingScreen.SendKeySync(KeyCtrlC)
// Test that the keybinding fired and set "didCallCTRLC" to true
if !didCallCTRLC {
t.Error("Expect the simulator to invoke the key handler for CTRLC")
}
tests := []struct {
key Key
content string
}{
{KeyF1, expectedViewContent1},
{KeyF2, expectedViewContent2},
{KeyF3, expectedViewContent3},
}
for _, key := range tests {
// Send a key to gocui
testingScreen.SendKeySync(key.key)
// check view content
assertView(t, testingScreen, viewName, key.content)
}
}
func TestTestingScreenParallelKeys(t *testing.T) {
// Track what happened in the view, we'll assert on these
didCallCTRLC := false
didCallF1 := false
didCallF2 := false
didCallF3 := false
expectedViewContent := "Hello world!"
viewName := "testView1"
// Create a view specifying the "OutputSimulator" mode
g, err := NewGui(OutputSimulator, true)
if err != nil {
t.Error(err)
}
g.SetManagerFunc(func(g *Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView(viewName, maxX/2-7, maxY/2, maxX/2+7, maxY/2+2, 0); err != nil {
if !errors.Is(err, ErrUnknownView) {
return err
}
if _, err := g.SetCurrentView(viewName); err != nil {
return err
}
// Have the view draw "Hello world!"
fmt.Fprintln(v, expectedViewContent)
}
return nil
})
// Create a key bindings
if err := g.SetKeybinding("", KeyCtrlC, ModNone, func(g *Gui, v *View) error {
didCallCTRLC = true
return nil
}); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF1, ModNone, func(g *Gui, v *View) error {
didCallF1 = true
return nil
}); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF2, ModNone, func(g *Gui, v *View) error {
didCallF2 = true
return nil
}); err != nil {
t.Error(err)
}
if err := g.SetKeybinding("", KeyF3, ModNone, func(g *Gui, v *View) error {
didCallF3 = true
return nil
}); err != nil {
t.Error(err)
}
// Create a test screen and start gocui
testingScreen := g.GetTestingScreen()
cleanup := testingScreen.StartGui()
defer cleanup()
// check view content
assertView(t, testingScreen, viewName, expectedViewContent)
// Send a key to gocui
testingScreen.SendKeySync(KeyCtrlC)
var wg sync.WaitGroup
wg.Add(3)
go func() {
testingScreen.SendKeySync(KeyF1)
wg.Done()
}()
go func() {
testingScreen.SendKeySync(KeyF2)
wg.Done()
}()
go func() {
testingScreen.SendKeySync(KeyF3)
wg.Done()
}()
wg.Wait()
// Test that the keybinding fired
if !didCallCTRLC {
t.Error("Expect the simulator to invoke the key handler for CTRLC")
}
if !didCallF1 || !didCallF2 || !didCallF3 {
t.Error("Expect the simulator to invoke the key handler for F1, F2 and F3")
}
}
// assertView checks if view contains provided content.
func assertView(t *testing.T, ts TestingScreen, viewName, content string) {
t.Helper()
// Get the content from the testing screen
if actualContent, err := ts.GetViewContent(viewName); err != nil {
t.Error(err)
} else {
// Test that it contains the "Hello World!" we thought the view should draw
if strings.TrimSpace(actualContent) != content {
t.Error(fmt.Printf("Expected view content to be: %q got: %q", content, actualContent))
}
}
}