Skip to content

Commit 8fa153d

Browse files
committed
nvim/plugin: add CommandHandler testcases
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent 547c511 commit 8fa153d

File tree

1 file changed

+80
-35
lines changed

1 file changed

+80
-35
lines changed

nvim/plugin/plugin_test.go

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,104 @@ import (
1111
)
1212

1313
func TestRegister(t *testing.T) {
14+
t.Parallel()
15+
1416
p := plugin.New(nvimtest.NewChildProcess(t))
1517

16-
// simple handler
17-
p.Handle("hello", func(s string) (string, error) {
18-
return "Hello, " + s, nil
19-
})
18+
// SimpleHandler
19+
p.Handle("hello",
20+
func(s string) (string, error) {
21+
return "Hello, " + s, nil
22+
},
23+
)
2024

21-
// function handler
22-
p.HandleFunction(&plugin.FunctionOptions{Name: "Hello"},
25+
// FunctionHandler
26+
p.HandleFunction(
27+
&plugin.FunctionOptions{Name: "Hello"},
2328
func(args []string) (string, error) {
2429
return "Hello, " + strings.Join(args, " "), nil
25-
})
30+
},
31+
)
2632

27-
// function handler with eval
33+
// FunctionEvalHandler
2834
type testEval struct {
2935
BaseDir string `eval:"fnamemodify(getcwd(), ':t')"`
3036
}
31-
p.HandleFunction(&plugin.FunctionOptions{Name: "TestEval", Eval: "*"},
37+
p.HandleFunction(
38+
&plugin.FunctionOptions{Name: "TestEval", Eval: "*"},
3239
func(_ []string, eval *testEval) (string, error) {
3340
return eval.BaseDir, nil
34-
})
41+
},
42+
)
43+
44+
p.HandleCommand(
45+
&plugin.CommandOptions{Name: "Hello", NArgs: "*"},
46+
func(n *nvim.Nvim, args []string) error {
47+
chunks := []nvim.TextChunk{
48+
{
49+
Text: `Hello`,
50+
},
51+
}
52+
for _, arg := range args {
53+
chunks = append(chunks, nvim.TextChunk{Text: arg})
54+
}
55+
56+
return n.Echo(chunks, true, make(map[string]interface{}))
57+
},
58+
)
3559

3660
if err := p.RegisterForTests(); err != nil {
3761
t.Fatalf("register for test: %v", err)
3862
}
3963

40-
result, err := p.Nvim.Exec(`:echo Hello('John', 'Doe')`, true)
41-
if err != nil {
42-
t.Fatalf("exec 'echo Hello' function: %v", err)
43-
}
44-
expected := `Hello, John Doe`
45-
if result != expected {
46-
t.Fatalf("Hello returned %q, want %q", result, expected)
47-
}
64+
t.Run("SimpleHandler", func(t *testing.T) {
65+
result, err := p.Nvim.Exec(`:echo Hello('John', 'Doe')`, true)
66+
if err != nil {
67+
t.Fatalf("exec 'echo Hello' function: %v", err)
68+
}
4869

49-
cid := p.Nvim.ChannelID()
50-
var result2 string
51-
if err := p.Nvim.Call("rpcrequest", &result2, cid, "hello", "world"); err != nil {
52-
t.Fatalf("call rpcrequest(%v, %v, %v, %v): %v", &result2, cid, "hello", "world", err)
53-
}
54-
expected2 := `Hello, world`
55-
if result2 != expected2 {
56-
t.Fatalf("hello returned %q, want %q", result2, expected2)
57-
}
70+
expected := `Hello, John Doe`
71+
if result != expected {
72+
t.Fatalf("Hello returned %q, want %q", result, expected)
73+
}
74+
})
5875

59-
var result3 string
60-
if err := p.Nvim.Eval(`TestEval()`, &result3); err != nil {
61-
t.Fatalf("eval 'TestEval()' function: %v", err)
62-
}
63-
expected3 := `plugin`
64-
if result3 != expected3 {
65-
t.Fatalf("EvalTest returned %q, want %q", result3, expected3)
66-
}
76+
t.Run("FunctionHandler", func(t *testing.T) {
77+
cid := p.Nvim.ChannelID()
78+
var result string
79+
if err := p.Nvim.Call(`rpcrequest`, &result, cid, `hello`, `world`); err != nil {
80+
t.Fatalf("call rpcrequest(%v, %v, %v, %v): %v", &result, cid, "hello", "world", err)
81+
}
82+
83+
expected2 := `Hello, world`
84+
if result != expected2 {
85+
t.Fatalf("hello returned %q, want %q", result, expected2)
86+
}
87+
})
88+
89+
t.Run("FunctionEvalHandler", func(t *testing.T) {
90+
var result string
91+
if err := p.Nvim.Eval(`TestEval()`, &result); err != nil {
92+
t.Fatalf("eval 'TestEval()' function: %v", err)
93+
}
94+
95+
expected3 := `plugin`
96+
if result != expected3 {
97+
t.Fatalf("EvalTest returned %q, want %q", result, expected3)
98+
}
99+
})
100+
101+
t.Run("CommandHandler", func(t *testing.T) {
102+
result, err := p.Nvim.Exec(`Hello World`, true)
103+
if err != nil {
104+
t.Fatalf("exec 'Hello' command: %v", err)
105+
}
106+
107+
expected := `HelloWorld`
108+
if result != expected {
109+
t.Fatalf("Hello returned %q, want %q", result, expected)
110+
}
111+
})
67112
}
68113

69114
func TestSubscribe(t *testing.T) {

0 commit comments

Comments
 (0)