@@ -30,10 +30,19 @@ 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 close (eventCh )
37
+ defer close (redrawCh )
38
+ defer close (waitCh )
39
+ ev := <- eventCh
40
+ if ev .Type != event .Error {
41
+ t .Errorf ("event type should be %d but got: %d" , event .Error , ev .Type )
42
+ }
43
+ if expected := "no file name" ; ev .Error .Error () != expected {
44
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
45
+ }
37
46
}()
38
47
wm .SetSize (110 , 20 )
39
48
if err := wm .Open ("" ); err != nil {
@@ -62,21 +71,14 @@ func TestManagerOpenEmpty(t *testing.T) {
62
71
if err != nil {
63
72
t .Errorf ("err should be nil but got: %v" , err )
64
73
}
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
- }
74
+ wm .Emit (event.Event {Type : event .Write })
75
+ <- waitCh
73
76
wm .Close ()
74
77
}
75
78
76
79
func TestManagerOpenStates (t * testing.T ) {
77
80
wm := NewManager ()
78
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
79
- wm .Init (eventCh , redrawCh )
81
+ wm .Init (nil , nil )
80
82
wm .SetSize (110 , 20 )
81
83
str := "Hello, world! こんにちは、世界!"
82
84
f , err := createTemp (t .TempDir (), str )
@@ -114,14 +116,17 @@ func TestManagerOpenStates(t *testing.T) {
114
116
115
117
func TestManagerOpenNonExistsWrite (t * testing.T ) {
116
118
wm := NewManager ()
117
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
119
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
118
120
wm .Init (eventCh , redrawCh )
119
121
go func () {
120
- for {
121
- select {
122
- case <- eventCh :
123
- case <- redrawCh :
124
- }
122
+ defer close (eventCh )
123
+ defer close (redrawCh )
124
+ defer close (waitCh )
125
+ for range 16 {
126
+ <- redrawCh
127
+ }
128
+ if ev := <- eventCh ; ev .Type != event .QuitAll {
129
+ t .Errorf ("event type should be %d but got: %d" , event .QuitAll , ev .Type )
125
130
}
126
131
}()
127
132
wm .SetSize (110 , 20 )
@@ -168,13 +173,13 @@ func TestManagerOpenNonExistsWrite(t *testing.T) {
168
173
if string (bs ) != str {
169
174
t .Errorf ("file contents should be %q but got %q" , str , string (bs ))
170
175
}
176
+ <- waitCh
171
177
wm .Close ()
172
178
}
173
179
174
180
func TestManagerOpenExpandBacktick (t * testing.T ) {
175
181
wm := NewManager ()
176
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
177
- wm .Init (eventCh , redrawCh )
182
+ wm .Init (nil , nil )
178
183
wm .SetSize (110 , 20 )
179
184
cmd , name := "`which ls`" , "ls"
180
185
if runtime .GOOS == "windows" {
@@ -208,8 +213,7 @@ func TestEditorOpenExpandHomedir(t *testing.T) {
208
213
t .Skip ("skip on Windows" )
209
214
}
210
215
wm := NewManager ()
211
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
212
- wm .Init (eventCh , redrawCh )
216
+ wm .Init (nil , nil )
213
217
wm .SetSize (110 , 20 )
214
218
str := "Hello, world!"
215
219
f , err := createTemp (t .TempDir (), str )
@@ -246,21 +250,46 @@ func TestManagerOpenChdirWrite(t *testing.T) {
246
250
t .Skip ("skip on Windows" )
247
251
}
248
252
wm := NewManager ()
249
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
253
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
250
254
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
255
f , err := createTemp (t .TempDir (), "Hello" )
261
256
if err != nil {
262
257
t .Fatalf ("err should be nil but got: %v" , err )
263
258
}
259
+ go func () {
260
+ defer close (eventCh )
261
+ defer close (redrawCh )
262
+ defer close (waitCh )
263
+ ev := <- eventCh
264
+ if ev .Type != event .Info {
265
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
266
+ }
267
+ dir , err := filepath .EvalSymlinks (filepath .Dir (f .Name ()))
268
+ if err != nil {
269
+ t .Errorf ("err should be nil but got: %v" , err )
270
+ }
271
+ if expected := dir ; ev .Error .Error () != expected {
272
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
273
+ }
274
+ ev = <- eventCh
275
+ if ev .Type != event .Info {
276
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
277
+ }
278
+ if expected := filepath .Dir (dir ); ev .Error .Error () != expected {
279
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
280
+ }
281
+ for range 11 {
282
+ <- redrawCh
283
+ }
284
+ ev = <- eventCh
285
+ if ev .Type != event .Info {
286
+ t .Errorf ("event type should be %d but got: %d" , event .Info , ev .Type )
287
+ }
288
+ if expected := "13 (0xd) bytes written" ; ! strings .HasSuffix (ev .Error .Error (), expected ) {
289
+ t .Errorf ("err should be %q but got: %v" , expected , ev .Error )
290
+ }
291
+ }()
292
+ wm .SetSize (110 , 20 )
264
293
wm .Emit (event.Event {Type : event .Chdir , Arg : filepath .Dir (f .Name ())})
265
294
if err := wm .Open (filepath .Base (f .Name ())); err != nil {
266
295
t .Fatalf ("err should be nil but got: %v" , err )
@@ -284,21 +313,13 @@ func TestManagerOpenChdirWrite(t *testing.T) {
284
313
if expected := "Hello, world!" ; string (bs ) != expected {
285
314
t .Errorf ("file contents should be %q but got %q" , expected , string (bs ))
286
315
}
316
+ <- waitCh
287
317
wm .Close ()
288
318
}
289
319
290
320
func TestManagerOpenDirectory (t * testing.T ) {
291
321
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
- }()
322
+ wm .Init (nil , nil )
302
323
wm .SetSize (110 , 20 )
303
324
dir := t .TempDir ()
304
325
if err := wm .Open (dir ); err != nil {
@@ -313,8 +334,7 @@ func TestManagerOpenDirectory(t *testing.T) {
313
334
314
335
func TestManagerRead (t * testing.T ) {
315
336
wm := NewManager ()
316
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
317
- wm .Init (eventCh , redrawCh )
337
+ wm .Init (nil , nil )
318
338
wm .SetSize (110 , 20 )
319
339
r := strings .NewReader ("Hello, world!" )
320
340
if err := wm .Read (r ); err != nil {
@@ -345,14 +365,14 @@ func TestManagerRead(t *testing.T) {
345
365
346
366
func TestManagerOnly (t * testing.T ) {
347
367
wm := NewManager ()
348
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
368
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
349
369
wm .Init (eventCh , redrawCh )
350
370
go func () {
351
- for {
352
- select {
353
- case <- eventCh :
354
- case <- redrawCh :
355
- }
371
+ defer close ( eventCh )
372
+ defer close ( redrawCh )
373
+ defer close ( waitCh )
374
+ for range 4 {
375
+ <- eventCh
356
376
}
357
377
}()
358
378
wm .SetSize (110 , 20 )
@@ -379,19 +399,20 @@ func TestManagerOnly(t *testing.T) {
379
399
t .Errorf ("layout should be %#v but got %#v" , expected , got )
380
400
}
381
401
402
+ <- waitCh
382
403
wm .Close ()
383
404
}
384
405
385
406
func TestManagerAlternative (t * testing.T ) {
386
407
wm := NewManager ()
387
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
408
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
388
409
wm .Init (eventCh , redrawCh )
389
410
go func () {
390
- for {
391
- select {
392
- case <- eventCh :
393
- case <- redrawCh :
394
- }
411
+ defer close ( eventCh )
412
+ defer close ( redrawCh )
413
+ defer close ( waitCh )
414
+ for range 9 {
415
+ <- eventCh
395
416
}
396
417
}()
397
418
wm .SetSize (110 , 20 )
@@ -475,19 +496,20 @@ func TestManagerAlternative(t *testing.T) {
475
496
t .Errorf ("windowIndex should be %d but got %d" , expected , windowIndex )
476
497
}
477
498
499
+ <- waitCh
478
500
wm .Close ()
479
501
}
480
502
481
503
func TestManagerWincmd (t * testing.T ) {
482
504
wm := NewManager ()
483
- eventCh , redrawCh := make (chan event.Event ), make (chan struct {})
505
+ eventCh , redrawCh , waitCh := make (chan event.Event ), make ( chan struct {} ), make (chan struct {})
484
506
wm .Init (eventCh , redrawCh )
485
507
go func () {
486
- for {
487
- select {
488
- case <- eventCh :
489
- case <- redrawCh :
490
- }
508
+ defer close ( eventCh )
509
+ defer close ( redrawCh )
510
+ defer close ( waitCh )
511
+ for range 17 {
512
+ <- eventCh
491
513
}
492
514
}()
493
515
wm .SetSize (110 , 20 )
@@ -538,6 +560,7 @@ func TestManagerWincmd(t *testing.T) {
538
560
t .Errorf ("layout should be %#v but got %#v" , expected , got )
539
561
}
540
562
563
+ <- waitCh
541
564
wm .Close ()
542
565
}
543
566
@@ -556,6 +579,9 @@ func TestManagerCopyCutPaste(t *testing.T) {
556
579
}
557
580
_ , _ , _ , _ = wm .State ()
558
581
go func () {
582
+ defer close (eventCh )
583
+ defer close (redrawCh )
584
+ defer close (waitCh )
559
585
<- redrawCh
560
586
<- redrawCh
561
587
<- redrawCh
@@ -620,7 +646,6 @@ func TestManagerCopyCutPaste(t *testing.T) {
620
646
if expected := "Hefoobarfoobarfoobarlrld!" ; ! strings .HasPrefix (string (ws .Bytes ), expected ) {
621
647
t .Errorf ("Bytes should start with %q but got %q" , expected , string (ws .Bytes ))
622
648
}
623
- close (waitCh )
624
649
}()
625
650
wm .Emit (event.Event {Type : event .CursorNext , Mode : mode .Normal , Count : 3 })
626
651
wm .Emit (event.Event {Type : event .StartVisual })
0 commit comments