Skip to content

Commit 9b8bd51

Browse files
committed
nvim: refactoring testcases and use t.Parallel
1 parent f40e3d9 commit 9b8bd51

File tree

1 file changed

+123
-58
lines changed

1 file changed

+123
-58
lines changed

nvim/nvim_test.go

Lines changed: 123 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,47 @@ func newChildProcess(t *testing.T) (*Nvim, func()) {
3535
}
3636
}
3737

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-
4638
func TestAPI(t *testing.T) {
39+
t.Parallel()
4740

4841
v, cleanup := newChildProcess(t)
4942
defer cleanup()
50-
cid := v.ChannelID()
51-
if cid <= 0 {
52-
t.Fatal("could not get channel id")
53-
}
5443

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+
5679
if err := v.RegisterHandler("hello", helloHandler); err != nil {
5780
t.Fatal(err)
5881
}
@@ -74,9 +97,11 @@ func TestAPI(t *testing.T) {
7497
if expected := "\nError invoking 'error' on channel 1:\nouch"; result != expected {
7598
t.Errorf("got error %q, want %q", result, expected)
7699
}
77-
})
100+
}
101+
}
78102

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) {
80105
bufs, err := v.Buffers()
81106
if err != nil {
82107
t.Fatal(err)
@@ -123,9 +148,11 @@ func TestAPI(t *testing.T) {
123148
if err == nil {
124149
t.Errorf("expected key not found error")
125150
}
126-
})
151+
}
152+
}
127153

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) {
129156
wins, err := v.Windows()
130157
if err != nil {
131158
t.Fatal(err)
@@ -147,9 +174,11 @@ func TestAPI(t *testing.T) {
147174
if err != nil {
148175
t.Fatal(err)
149176
}
150-
})
177+
}
178+
}
151179

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) {
153182
pages, err := v.Tabpages()
154183
if err != nil {
155184
t.Fatal(err)
@@ -171,9 +200,11 @@ func TestAPI(t *testing.T) {
171200
if err != nil {
172201
t.Fatal(err)
173202
}
174-
})
203+
}
204+
}
175205

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) {
177208
buf, err := v.CurrentBuffer()
178209
if err != nil {
179210
t.Fatal(err)
@@ -189,9 +220,11 @@ func TestAPI(t *testing.T) {
189220
if !reflect.DeepEqual(lines2, lines) {
190221
t.Fatalf("lines = %+v, want %+v", lines2, lines)
191222
}
192-
})
223+
}
224+
}
193225

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) {
195228
if err := v.SetVar("gvar", "gval"); err != nil {
196229
t.Fatal(err)
197230
}
@@ -212,9 +245,11 @@ func TestAPI(t *testing.T) {
212245
if value != "" {
213246
t.Errorf("got %v, want %q", value, "")
214247
}
215-
})
248+
}
249+
}
216250

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) {
218253
var expected, actual struct {
219254
Str string
220255
Num int
@@ -230,19 +265,23 @@ func TestAPI(t *testing.T) {
230265
if !reflect.DeepEqual(&actual, &expected) {
231266
t.Errorf("got %+v, want %+v", &actual, &expected)
232267
}
233-
})
268+
}
269+
}
234270

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) {
236273
var a, b string
237274
if err := v.Eval(`["hello", "world"]`, []*string{&a, &b}); err != nil {
238275
t.Error(err)
239276
}
240277
if a != "hello" || b != "world" {
241278
t.Errorf("a=%q b=%q, want a=hello b=world", a, b)
242279
}
243-
})
280+
}
281+
}
244282

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) {
246285
b := v.NewBatch()
247286
results := make([]int, 128)
248287

@@ -323,27 +362,33 @@ func TestAPI(t *testing.T) {
323362
if err != nil {
324363
t.Errorf("GetCurrentBuffer returns err %s: %#v", err, err)
325364
}
326-
})
365+
}
366+
}
327367

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) {
329370
var wd string
330371
err := v.Call("getcwd", &wd)
331372
if err != nil {
332373
t.Fatal(err)
333374
}
334-
})
375+
}
376+
}
335377

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) {
337380
m, err := v.Mode()
338381
if err != nil {
339382
t.Fatal(err)
340383
}
341384
if m.Mode != "n" {
342385
t.Errorf("Mode() returned %s, want n", m.Mode)
343386
}
344-
})
387+
}
388+
}
345389

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) {
347392
var n int
348393
err := v.ExecLua("local a, b = ... return a + b", &n, 1, 2)
349394
if err != nil {
@@ -352,9 +397,11 @@ func TestAPI(t *testing.T) {
352397
if n != 3 {
353398
t.Errorf("Mode() returned %v, want 3", n)
354399
}
355-
})
400+
}
401+
}
356402

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) {
358405
cm, err := v.ColorMap()
359406
if err != nil {
360407
t.Fatal(err)
@@ -382,9 +429,11 @@ func TestAPI(t *testing.T) {
382429
if !reflect.DeepEqual(hl, gui) {
383430
t.Errorf("HLByID(id, true)\n got %+v,\nwant %+v", hl, gui)
384431
}
385-
})
432+
}
433+
}
386434

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) {
388437
clearBuffer(t, v, 0) // clear curret buffer text
389438

390439
type ChangedtickEvent struct {
@@ -483,9 +532,11 @@ func TestAPI(t *testing.T) {
483532
t.Fatal(err)
484533
}
485534
}
486-
})
535+
}
536+
}
487537

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) {
489540
clearBuffer(t, v, Buffer(0)) // clear curret buffer text
490541

491542
nsID, err := v.CreateNamespace("test_virtual_text")
@@ -524,9 +575,11 @@ func TestAPI(t *testing.T) {
524575
if err := v.ClearBufferNamespace(Buffer(0), nsID, 0, -1); err != nil {
525576
t.Fatal(err)
526577
}
527-
})
578+
}
579+
}
528580

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) {
530583
clearBuffer(t, v, 0) // clear curret buffer text
531584
curwin, err := v.CurrentWindow()
532585
if err != nil {
@@ -595,9 +648,11 @@ func TestAPI(t *testing.T) {
595648
if numberOpt || relativenumberOpt || cursorlineOpt || cursorcolumnOpt || spellOpt || listOpt || signcolumnOpt != "auto" || colorcolumnOpt != "" {
596649
t.Fatal("expected minimal style")
597650
}
598-
})
651+
}
652+
}
599653

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) {
601656
ctxt, err := v.Context(make(map[string][]string))
602657
if err != nil {
603658
t.Fatal(err)
@@ -610,9 +665,11 @@ func TestAPI(t *testing.T) {
610665
if result != nil {
611666
t.Fatal("expected result to nil")
612667
}
613-
})
668+
}
669+
}
614670

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) {
616673
clearBuffer(t, v, 0) // clear curret buffer text
617674

618675
lines := [][]byte{[]byte("hello"), []byte("world")}
@@ -668,9 +725,11 @@ func TestAPI(t *testing.T) {
668725
if err := v.ClearBufferNamespace(Buffer(0), nsID, 0, -1); err != nil {
669726
t.Fatal(err)
670727
}
671-
})
728+
}
729+
}
672730

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) {
674733
files, err := v.RuntimeFiles("doc/*_diff.txt", true)
675734
if err != nil {
676735
t.Fatal(err)
@@ -685,14 +744,25 @@ func TestAPI(t *testing.T) {
685744
t.Fatal(err)
686745
}
687746

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)
689750
if got := strings.Join(files, ","); !strings.EqualFold(got, want) {
690751
t.Fatalf("got %s but want %s", got, want)
691752
}
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+
}
693761
}
694762

695763
func TestDial(t *testing.T) {
764+
t.Parallel()
765+
696766
v1, cleanup := newChildProcess(t)
697767
defer cleanup()
698768

@@ -726,6 +796,8 @@ func TestDial(t *testing.T) {
726796
}
727797

728798
func TestEmbedded(t *testing.T) {
799+
t.Parallel()
800+
729801
v, err := NewEmbedded(&EmbedOptions{
730802
Args: []string{"-u", "NONE", "-n"},
731803
Env: []string{},
@@ -763,10 +835,3 @@ func TestEmbedded(t *testing.T) {
763835
t.Fatal("timeout waiting for serve to exit")
764836
}
765837
}
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

Comments
 (0)