@@ -11,59 +11,104 @@ import (
11
11
)
12
12
13
13
func TestRegister (t * testing.T ) {
14
+ t .Parallel ()
15
+
14
16
p := plugin .New (nvimtest .NewChildProcess (t ))
15
17
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
+ )
20
24
21
- // function handler
22
- p .HandleFunction (& plugin.FunctionOptions {Name : "Hello" },
25
+ // FunctionHandler
26
+ p .HandleFunction (
27
+ & plugin.FunctionOptions {Name : "Hello" },
23
28
func (args []string ) (string , error ) {
24
29
return "Hello, " + strings .Join (args , " " ), nil
25
- })
30
+ },
31
+ )
26
32
27
- // function handler with eval
33
+ // FunctionEvalHandler
28
34
type testEval struct {
29
35
BaseDir string `eval:"fnamemodify(getcwd(), ':t')"`
30
36
}
31
- p .HandleFunction (& plugin.FunctionOptions {Name : "TestEval" , Eval : "*" },
37
+ p .HandleFunction (
38
+ & plugin.FunctionOptions {Name : "TestEval" , Eval : "*" },
32
39
func (_ []string , eval * testEval ) (string , error ) {
33
40
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
+ )
35
59
36
60
if err := p .RegisterForTests (); err != nil {
37
61
t .Fatalf ("register for test: %v" , err )
38
62
}
39
63
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
+ }
48
69
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
+ })
58
75
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
+ })
67
112
}
68
113
69
114
func TestSubscribe (t * testing.T ) {
0 commit comments