@@ -35,24 +35,47 @@ func newChildProcess(t *testing.T) (*Nvim, func()) {
35
35
}
36
36
}
37
37
38
- func helloHandler (s string ) (string , error ) {
39
- return "Hello, " + s , nil
40
- }
41
-
42
- func errorHandler () error {
43
- return errors .New ("ouch" )
44
- }
45
-
46
38
func TestAPI (t * testing.T ) {
39
+ t .Parallel ()
47
40
48
41
v , cleanup := newChildProcess (t )
49
42
defer cleanup ()
50
- cid := v .ChannelID ()
51
- if cid <= 0 {
52
- t .Fatal ("could not get channel id" )
53
- }
54
43
55
- t .Run ("simpleHandler" , func (t * testing.T ) {
44
+ t .Run ("SimpleHandler" , testSimpleHandler (t , v ))
45
+ t .Run ("Buffer" , testBuffer (t , v ))
46
+ t .Run ("Window" , testWindow (t , v ))
47
+ t .Run ("Tabpage" , testTabpage (t , v ))
48
+ t .Run ("Lines" , testLines (t , v ))
49
+ t .Run ("Var" , testVar (t , v ))
50
+ t .Run ("StructValue" , testStructValue (t , v ))
51
+ t .Run ("Eval" , testEval (t , v ))
52
+ t .Run ("Batch" , testBatch (t , v ))
53
+ t .Run ("CallWithNoArgs" , testCallWithNoArgs (t , v ))
54
+ t .Run ("Mode" , testMode (t , v ))
55
+ t .Run ("ExecLua" , testExecLua (t , v ))
56
+ t .Run ("Highlight" , testHighlight (t , v ))
57
+ t .Run ("BufAttach" , testBufAttach (t , v ))
58
+ t .Run ("VirtualText" , testVirtualText (t , v ))
59
+ t .Run ("FloatingWindow" , testFloatingWindow (t , v ))
60
+ t .Run ("Context" , testContext (t , v ))
61
+ t .Run ("Extmarks" , testExtmarks (t , v ))
62
+ t .Run ("RuntimeFiles" , testRuntimeFiles (t , v ))
63
+ }
64
+
65
+ func testSimpleHandler (t * testing.T , v * Nvim ) func (* testing.T ) {
66
+ return func (t * testing.T ) {
67
+ cid := v .ChannelID ()
68
+ if cid <= 0 {
69
+ t .Fatal ("could not get channel id" )
70
+ }
71
+
72
+ helloHandler := func (s string ) (string , error ) {
73
+ return "Hello, " + s , nil
74
+ }
75
+ errorHandler := func () error {
76
+ return errors .New ("ouch" )
77
+ }
78
+
56
79
if err := v .RegisterHandler ("hello" , helloHandler ); err != nil {
57
80
t .Fatal (err )
58
81
}
@@ -74,9 +97,11 @@ func TestAPI(t *testing.T) {
74
97
if expected := "\n Error invoking 'error' on channel 1:\n ouch" ; result != expected {
75
98
t .Errorf ("got error %q, want %q" , result , expected )
76
99
}
77
- })
100
+ }
101
+ }
78
102
79
- t .Run ("buffer" , func (t * testing.T ) {
103
+ func testBuffer (t * testing.T , v * Nvim ) func (* testing.T ) {
104
+ return func (t * testing.T ) {
80
105
bufs , err := v .Buffers ()
81
106
if err != nil {
82
107
t .Fatal (err )
@@ -123,9 +148,11 @@ func TestAPI(t *testing.T) {
123
148
if err == nil {
124
149
t .Errorf ("expected key not found error" )
125
150
}
126
- })
151
+ }
152
+ }
127
153
128
- t .Run ("window" , func (t * testing.T ) {
154
+ func testWindow (t * testing.T , v * Nvim ) func (* testing.T ) {
155
+ return func (t * testing.T ) {
129
156
wins , err := v .Windows ()
130
157
if err != nil {
131
158
t .Fatal (err )
@@ -147,9 +174,11 @@ func TestAPI(t *testing.T) {
147
174
if err != nil {
148
175
t .Fatal (err )
149
176
}
150
- })
177
+ }
178
+ }
151
179
152
- t .Run ("tabpage" , func (t * testing.T ) {
180
+ func testTabpage (t * testing.T , v * Nvim ) func (* testing.T ) {
181
+ return func (t * testing.T ) {
153
182
pages , err := v .Tabpages ()
154
183
if err != nil {
155
184
t .Fatal (err )
@@ -171,9 +200,11 @@ func TestAPI(t *testing.T) {
171
200
if err != nil {
172
201
t .Fatal (err )
173
202
}
174
- })
203
+ }
204
+ }
175
205
176
- t .Run ("lines" , func (t * testing.T ) {
206
+ func testLines (t * testing.T , v * Nvim ) func (* testing.T ) {
207
+ return func (t * testing.T ) {
177
208
buf , err := v .CurrentBuffer ()
178
209
if err != nil {
179
210
t .Fatal (err )
@@ -189,9 +220,11 @@ func TestAPI(t *testing.T) {
189
220
if ! reflect .DeepEqual (lines2 , lines ) {
190
221
t .Fatalf ("lines = %+v, want %+v" , lines2 , lines )
191
222
}
192
- })
223
+ }
224
+ }
193
225
194
- t .Run ("var" , func (t * testing.T ) {
226
+ func testVar (t * testing.T , v * Nvim ) func (* testing.T ) {
227
+ return func (t * testing.T ) {
195
228
if err := v .SetVar ("gvar" , "gval" ); err != nil {
196
229
t .Fatal (err )
197
230
}
@@ -212,9 +245,11 @@ func TestAPI(t *testing.T) {
212
245
if value != "" {
213
246
t .Errorf ("got %v, want %q" , value , "" )
214
247
}
215
- })
248
+ }
249
+ }
216
250
217
- t .Run ("structValue" , func (t * testing.T ) {
251
+ func testStructValue (t * testing.T , v * Nvim ) func (* testing.T ) {
252
+ return func (t * testing.T ) {
218
253
var expected , actual struct {
219
254
Str string
220
255
Num int
@@ -230,19 +265,23 @@ func TestAPI(t *testing.T) {
230
265
if ! reflect .DeepEqual (& actual , & expected ) {
231
266
t .Errorf ("got %+v, want %+v" , & actual , & expected )
232
267
}
233
- })
268
+ }
269
+ }
234
270
235
- t .Run ("eval" , func (t * testing.T ) {
271
+ func testEval (t * testing.T , v * Nvim ) func (* testing.T ) {
272
+ return func (t * testing.T ) {
236
273
var a , b string
237
274
if err := v .Eval (`["hello", "world"]` , []* string {& a , & b }); err != nil {
238
275
t .Error (err )
239
276
}
240
277
if a != "hello" || b != "world" {
241
278
t .Errorf ("a=%q b=%q, want a=hello b=world" , a , b )
242
279
}
243
- })
280
+ }
281
+ }
244
282
245
- t .Run ("batch" , func (t * testing.T ) {
283
+ func testBatch (t * testing.T , v * Nvim ) func (* testing.T ) {
284
+ return func (t * testing.T ) {
246
285
b := v .NewBatch ()
247
286
results := make ([]int , 128 )
248
287
@@ -323,27 +362,33 @@ func TestAPI(t *testing.T) {
323
362
if err != nil {
324
363
t .Errorf ("GetCurrentBuffer returns err %s: %#v" , err , err )
325
364
}
326
- })
365
+ }
366
+ }
327
367
328
- t .Run ("callWithNoArgs" , func (t * testing.T ) {
368
+ func testCallWithNoArgs (t * testing.T , v * Nvim ) func (* testing.T ) {
369
+ return func (t * testing.T ) {
329
370
var wd string
330
371
err := v .Call ("getcwd" , & wd )
331
372
if err != nil {
332
373
t .Fatal (err )
333
374
}
334
- })
375
+ }
376
+ }
335
377
336
- t .Run ("mode" , func (t * testing.T ) {
378
+ func testMode (t * testing.T , v * Nvim ) func (* testing.T ) {
379
+ return func (t * testing.T ) {
337
380
m , err := v .Mode ()
338
381
if err != nil {
339
382
t .Fatal (err )
340
383
}
341
384
if m .Mode != "n" {
342
385
t .Errorf ("Mode() returned %s, want n" , m .Mode )
343
386
}
344
- })
387
+ }
388
+ }
345
389
346
- t .Run ("execLua" , func (t * testing.T ) {
390
+ func testExecLua (t * testing.T , v * Nvim ) func (* testing.T ) {
391
+ return func (t * testing.T ) {
347
392
var n int
348
393
err := v .ExecLua ("local a, b = ... return a + b" , & n , 1 , 2 )
349
394
if err != nil {
@@ -352,9 +397,11 @@ func TestAPI(t *testing.T) {
352
397
if n != 3 {
353
398
t .Errorf ("Mode() returned %v, want 3" , n )
354
399
}
355
- })
400
+ }
401
+ }
356
402
357
- t .Run ("hl" , func (t * testing.T ) {
403
+ func testHighlight (t * testing.T , v * Nvim ) func (* testing.T ) {
404
+ return func (t * testing.T ) {
358
405
cm , err := v .ColorMap ()
359
406
if err != nil {
360
407
t .Fatal (err )
@@ -382,9 +429,11 @@ func TestAPI(t *testing.T) {
382
429
if ! reflect .DeepEqual (hl , gui ) {
383
430
t .Errorf ("HLByID(id, true)\n got %+v,\n want %+v" , hl , gui )
384
431
}
385
- })
432
+ }
433
+ }
386
434
387
- t .Run ("buf_attach" , func (t * testing.T ) {
435
+ func testBufAttach (t * testing.T , v * Nvim ) func (* testing.T ) {
436
+ return func (t * testing.T ) {
388
437
clearBuffer (t , v , 0 ) // clear curret buffer text
389
438
390
439
type ChangedtickEvent struct {
@@ -483,9 +532,11 @@ func TestAPI(t *testing.T) {
483
532
t .Fatal (err )
484
533
}
485
534
}
486
- })
535
+ }
536
+ }
487
537
488
- t .Run ("virtual_text" , func (t * testing.T ) {
538
+ func testVirtualText (t * testing.T , v * Nvim ) func (* testing.T ) {
539
+ return func (t * testing.T ) {
489
540
clearBuffer (t , v , Buffer (0 )) // clear curret buffer text
490
541
491
542
nsID , err := v .CreateNamespace ("test_virtual_text" )
@@ -524,9 +575,11 @@ func TestAPI(t *testing.T) {
524
575
if err := v .ClearBufferNamespace (Buffer (0 ), nsID , 0 , - 1 ); err != nil {
525
576
t .Fatal (err )
526
577
}
527
- })
578
+ }
579
+ }
528
580
529
- t .Run ("floating_window" , func (t * testing.T ) {
581
+ func testFloatingWindow (t * testing.T , v * Nvim ) func (* testing.T ) {
582
+ return func (t * testing.T ) {
530
583
clearBuffer (t , v , 0 ) // clear curret buffer text
531
584
curwin , err := v .CurrentWindow ()
532
585
if err != nil {
@@ -595,9 +648,11 @@ func TestAPI(t *testing.T) {
595
648
if numberOpt || relativenumberOpt || cursorlineOpt || cursorcolumnOpt || spellOpt || listOpt || signcolumnOpt != "auto" || colorcolumnOpt != "" {
596
649
t .Fatal ("expected minimal style" )
597
650
}
598
- })
651
+ }
652
+ }
599
653
600
- t .Run ("context" , func (t * testing.T ) {
654
+ func testContext (t * testing.T , v * Nvim ) func (* testing.T ) {
655
+ return func (t * testing.T ) {
601
656
ctxt , err := v .Context (make (map [string ][]string ))
602
657
if err != nil {
603
658
t .Fatal (err )
@@ -610,9 +665,11 @@ func TestAPI(t *testing.T) {
610
665
if result != nil {
611
666
t .Fatal ("expected result to nil" )
612
667
}
613
- })
668
+ }
669
+ }
614
670
615
- t .Run ("extmarks" , func (t * testing.T ) {
671
+ func testExtmarks (t * testing.T , v * Nvim ) func (* testing.T ) {
672
+ return func (t * testing.T ) {
616
673
clearBuffer (t , v , 0 ) // clear curret buffer text
617
674
618
675
lines := [][]byte {[]byte ("hello" ), []byte ("world" )}
@@ -668,9 +725,11 @@ func TestAPI(t *testing.T) {
668
725
if err := v .ClearBufferNamespace (Buffer (0 ), nsID , 0 , - 1 ); err != nil {
669
726
t .Fatal (err )
670
727
}
671
- })
728
+ }
729
+ }
672
730
673
- t .Run ("runtime_file" , func (t * testing.T ) {
731
+ func testRuntimeFiles (t * testing.T , v * Nvim ) func (* testing.T ) {
732
+ return func (t * testing.T ) {
674
733
files , err := v .RuntimeFiles ("doc/*_diff.txt" , true )
675
734
if err != nil {
676
735
t .Fatal (err )
@@ -685,14 +744,25 @@ func TestAPI(t *testing.T) {
685
744
t .Fatal (err )
686
745
}
687
746
688
- want := fmt .Sprintf ("%s,%s" , filepath .Join (runtimePath , "doc" , "vi_diff.txt" ), filepath .Join (runtimePath , "doc" , "vim_diff.txt" ))
747
+ viDiff := filepath .Join (runtimePath , "doc" , "vi_diff.txt" )
748
+ vimDiff := filepath .Join (runtimePath , "doc" , "vim_diff.txt" )
749
+ want := fmt .Sprintf ("%s,%s" , viDiff , vimDiff )
689
750
if got := strings .Join (files , "," ); ! strings .EqualFold (got , want ) {
690
751
t .Fatalf ("got %s but want %s" , got , want )
691
752
}
692
- })
753
+ }
754
+ }
755
+
756
+ // clearBuffer clears the buffer text.
757
+ func clearBuffer (t * testing.T , v * Nvim , buffer Buffer ) {
758
+ if err := v .SetBufferLines (buffer , 0 , - 1 , true , bytes .Fields (nil )); err != nil {
759
+ t .Fatal (err )
760
+ }
693
761
}
694
762
695
763
func TestDial (t * testing.T ) {
764
+ t .Parallel ()
765
+
696
766
v1 , cleanup := newChildProcess (t )
697
767
defer cleanup ()
698
768
@@ -726,6 +796,8 @@ func TestDial(t *testing.T) {
726
796
}
727
797
728
798
func TestEmbedded (t * testing.T ) {
799
+ t .Parallel ()
800
+
729
801
v , err := NewEmbedded (& EmbedOptions {
730
802
Args : []string {"-u" , "NONE" , "-n" },
731
803
Env : []string {},
@@ -763,10 +835,3 @@ func TestEmbedded(t *testing.T) {
763
835
t .Fatal ("timeout waiting for serve to exit" )
764
836
}
765
837
}
766
-
767
- // clearBuffer clear the buffer text.
768
- func clearBuffer (t * testing.T , v * Nvim , buffer Buffer ) {
769
- if err := v .SetBufferLines (buffer , 0 , - 1 , true , bytes .Fields (nil )); err != nil {
770
- t .Fatal (err )
771
- }
772
- }
0 commit comments