@@ -30,10 +30,21 @@ func createTemp(dir, contents string) (*os.File, error) {
30
30
31
31
func TestManagerOpenEmpty (t * testing.T ) {
32
32
wm := NewManager ()
33
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
33
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
34
34
wm .Init (eventCh , redrawCh )
35
35
go func () {
36
- <- redrawCh
36
+ defer func () {
37
+ close (eventCh )
38
+ close (redrawCh )
39
+ close (waitCh )
40
+ }()
41
+ ev := <- eventCh
42
+ if ev .Type != event .Error {
43
+ t .Errorf ("event type should be %d but got: %d" , event .Error , ev .Type )
44
+ }
45
+ if expected := "no file name" ; ev .Error .Error () != expected {
46
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
47
+ }
37
48
}()
38
49
wm .SetSize (110 , 20 )
39
50
if err := wm .Open ("" ); err != nil {
@@ -62,21 +73,14 @@ func TestManagerOpenEmpty(t *testing.T) {
62
73
if err != nil {
63
74
t .Errorf ("err should be nil but got: %v" , err )
64
75
}
65
- go wm .Emit (event.Event {Type : event .Write })
66
- ev := <- eventCh
67
- if ev .Type != event .Error {
68
- t .Errorf ("event type should be %d but got: %d" , event .Error , ev .Type )
69
- }
70
- if expected := "no file name" ; ev .Error .Error () != expected {
71
- t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
72
- }
76
+ wm .Emit (event.Event {Type : event .Write })
77
+ <- waitCh
73
78
wm .Close ()
74
79
}
75
80
76
81
func TestManagerOpenStates (t * testing.T ) {
77
82
wm := NewManager ()
78
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
79
- wm .Init (eventCh , redrawCh )
83
+ wm .Init (nil , nil )
80
84
wm .SetSize (110 , 20 )
81
85
str := "Hello, world! こんにちは、世界!"
82
86
f , err := createTemp (t .TempDir (), str )
@@ -114,14 +118,19 @@ func TestManagerOpenStates(t *testing.T) {
114
118
115
119
func TestManagerOpenNonExistsWrite (t * testing.T ) {
116
120
wm := NewManager ()
117
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
121
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
118
122
wm .Init (eventCh , redrawCh )
119
123
go func () {
120
- for {
121
- select {
122
- case <- eventCh :
123
- case <- redrawCh :
124
- }
124
+ defer func () {
125
+ close (eventCh )
126
+ close (redrawCh )
127
+ close (waitCh )
128
+ }()
129
+ for range 16 {
130
+ <- redrawCh
131
+ }
132
+ if ev := <- eventCh ; ev .Type != event .QuitAll {
133
+ t .Errorf ("event type should be %d but got: %d" , event .QuitAll , ev .Type )
125
134
}
126
135
}()
127
136
wm .SetSize (110 , 20 )
@@ -168,13 +177,13 @@ func TestManagerOpenNonExistsWrite(t *testing.T) {
168
177
if string (bs ) != str {
169
178
t .Errorf ("file contents should be %q but got %q" , str , string (bs ))
170
179
}
180
+ <- waitCh
171
181
wm .Close ()
172
182
}
173
183
174
184
func TestManagerOpenExpandBacktick (t * testing.T ) {
175
185
wm := NewManager ()
176
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
177
- wm .Init (eventCh , redrawCh )
186
+ wm .Init (nil , nil )
178
187
wm .SetSize (110 , 20 )
179
188
cmd , name := "`which ls`" , "ls"
180
189
if runtime .GOOS == "windows" {
@@ -208,8 +217,7 @@ func TestEditorOpenExpandHomedir(t *testing.T) {
208
217
t .Skip ("skip on Windows" )
209
218
}
210
219
wm := NewManager ()
211
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
212
- wm .Init (eventCh , redrawCh )
220
+ wm .Init (nil , nil )
213
221
wm .SetSize (110 , 20 )
214
222
str := "Hello, world!"
215
223
f , err := createTemp (t .TempDir (), str )
@@ -246,21 +254,48 @@ func TestManagerOpenChdirWrite(t *testing.T) {
246
254
t .Skip ("skip on Windows" )
247
255
}
248
256
wm := NewManager ()
249
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
257
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
250
258
wm .Init (eventCh , redrawCh )
251
- go func () {
252
- for {
253
- select {
254
- case <- eventCh :
255
- case <- redrawCh :
256
- }
257
- }
258
- }()
259
- wm .SetSize (110 , 20 )
260
259
f , err := createTemp (t .TempDir (), "Hello" )
261
260
if err != nil {
262
261
t .Fatalf ("err should be nil but got: %v" , err )
263
262
}
263
+ go func () {
264
+ defer func () {
265
+ close (eventCh )
266
+ close (redrawCh )
267
+ close (waitCh )
268
+ }()
269
+ ev := <- eventCh
270
+ if ev .Type != event .Info {
271
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
272
+ }
273
+ dir , err := filepath .EvalSymlinks (filepath .Dir (f .Name ()))
274
+ if err != nil {
275
+ t .Errorf ("err should be nil but got: %v" , err )
276
+ }
277
+ if expected := dir ; ev .Error .Error () != expected {
278
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
279
+ }
280
+ ev = <- eventCh
281
+ if ev .Type != event .Info {
282
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
283
+ }
284
+ if expected := filepath .Dir (dir ); ev .Error .Error () != expected {
285
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
286
+ }
287
+ for range 11 {
288
+ <- redrawCh
289
+ }
290
+ ev = <- eventCh
291
+ if ev .Type != event .Info {
292
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
293
+ }
294
+ if expected := "13 (0xd) bytes written" ; ! strings .HasSuffix (ev .Error .Error (), expected ) {
295
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
296
+ }
297
+ }()
298
+ wm .SetSize (110 , 20 )
264
299
wm .Emit (event.Event {Type : event .Chdir , Arg : filepath .Dir (f .Name ())})
265
300
if err := wm .Open (filepath .Base (f .Name ())); err != nil {
266
301
t .Fatalf ("err should be nil but got: %v" , err )
@@ -284,21 +319,13 @@ func TestManagerOpenChdirWrite(t *testing.T) {
284
319
if expected := "Hello, world!" ; string (bs ) != expected {
285
320
t .Errorf ("file contents should be %q but got %q" , expected , string (bs ))
286
321
}
322
+ <- waitCh
287
323
wm .Close ()
288
324
}
289
325
290
326
func TestManagerOpenDirectory (t * testing.T ) {
291
327
wm := NewManager ()
292
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
293
- wm .Init (eventCh , redrawCh )
294
- go func () {
295
- for {
296
- select {
297
- case <- eventCh :
298
- case <- redrawCh :
299
- }
300
- }
301
- }()
328
+ wm .Init (nil , nil )
302
329
wm .SetSize (110 , 20 )
303
330
dir := t .TempDir ()
304
331
if err := wm .Open (dir ); err != nil {
@@ -313,8 +340,7 @@ func TestManagerOpenDirectory(t *testing.T) {
313
340
314
341
func TestManagerRead (t * testing.T ) {
315
342
wm := NewManager ()
316
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
317
- wm .Init (eventCh , redrawCh )
343
+ wm .Init (nil , nil )
318
344
wm .SetSize (110 , 20 )
319
345
r := strings .NewReader ("Hello, world!" )
320
346
if err := wm .Read (r ); err != nil {
@@ -345,14 +371,16 @@ func TestManagerRead(t *testing.T) {
345
371
346
372
func TestManagerOnly (t * testing.T ) {
347
373
wm := NewManager ()
348
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
374
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
349
375
wm .Init (eventCh , redrawCh )
350
376
go func () {
351
- for {
352
- select {
353
- case <- eventCh :
354
- case <- redrawCh :
355
- }
377
+ defer func () {
378
+ close (eventCh )
379
+ close (redrawCh )
380
+ close (waitCh )
381
+ }()
382
+ for range 4 {
383
+ <- eventCh
356
384
}
357
385
}()
358
386
wm .SetSize (110 , 20 )
@@ -379,19 +407,22 @@ func TestManagerOnly(t *testing.T) {
379
407
t .Errorf ("layout should be %#v but got %#v" , expected , got )
380
408
}
381
409
410
+ <- waitCh
382
411
wm .Close ()
383
412
}
384
413
385
414
func TestManagerAlternative (t * testing.T ) {
386
415
wm := NewManager ()
387
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
416
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
388
417
wm .Init (eventCh , redrawCh )
389
418
go func () {
390
- for {
391
- select {
392
- case <- eventCh :
393
- case <- redrawCh :
394
- }
419
+ defer func () {
420
+ close (eventCh )
421
+ close (redrawCh )
422
+ close (waitCh )
423
+ }()
424
+ for range 9 {
425
+ <- eventCh
395
426
}
396
427
}()
397
428
wm .SetSize (110 , 20 )
@@ -475,19 +506,22 @@ func TestManagerAlternative(t *testing.T) {
475
506
t .Errorf ("windowIndex should be %d but got %d" , expected , windowIndex )
476
507
}
477
508
509
+ <- waitCh
478
510
wm .Close ()
479
511
}
480
512
481
513
func TestManagerWincmd (t * testing.T ) {
482
514
wm := NewManager ()
483
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
515
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
484
516
wm .Init (eventCh , redrawCh )
485
517
go func () {
486
- for {
487
- select {
488
- case <- eventCh :
489
- case <- redrawCh :
490
- }
518
+ defer func () {
519
+ close (eventCh )
520
+ close (redrawCh )
521
+ close (waitCh )
522
+ }()
523
+ for range 17 {
524
+ <- eventCh
491
525
}
492
526
}()
493
527
wm .SetSize (110 , 20 )
@@ -538,6 +572,7 @@ func TestManagerWincmd(t *testing.T) {
538
572
t .Errorf ("layout should be %#v but got %#v" , expected , got )
539
573
}
540
574
575
+ <- waitCh
541
576
wm .Close ()
542
577
}
543
578
@@ -556,6 +591,11 @@ func TestManagerCopyCutPaste(t *testing.T) {
556
591
}
557
592
_ , _ , _ , _ = wm .State ()
558
593
go func () {
594
+ defer func () {
595
+ close (eventCh )
596
+ close (redrawCh )
597
+ close (waitCh )
598
+ }()
559
599
<- redrawCh
560
600
<- redrawCh
561
601
<- redrawCh
@@ -620,7 +660,6 @@ func TestManagerCopyCutPaste(t *testing.T) {
620
660
if expected := "Hefoobarfoobarfoobarlrld!" ; ! strings .HasPrefix (string (ws .Bytes ), expected ) {
621
661
t .Errorf ("Bytes should start with %q but got %q" , expected , string (ws .Bytes ))
622
662
}
623
- close (waitCh )
624
663
}()
625
664
wm .Emit (event.Event {Type : event .CursorNext , Mode : mode .Normal , Count : 3 })
626
665
wm .Emit (event.Event {Type : event .StartVisual })
0 commit comments