Skip to content

Commit f7394a4

Browse files
committed
nvim: add AddUserCommand and DeleteUserCommand testcases
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent ac2e873 commit f7394a4

File tree

1 file changed

+93
-25
lines changed

1 file changed

+93
-25
lines changed

nvim/api_test.go

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestAPI(t *testing.T) {
107107
t.Run("Window", testWindow(v))
108108
t.Run("Tabpage", testTabpage(v))
109109
t.Run("Lines", testLines(v))
110-
t.Run("Commands", testCommands(v))
110+
t.Run("Command", testCommand(v))
111111
t.Run("Var", testVar(v))
112112
t.Run("Message", testMessage(v))
113113
t.Run("Key", testKey(v))
@@ -2046,34 +2046,102 @@ func testLines(v *Nvim) func(*testing.T) {
20462046
}
20472047
}
20482048

2049-
func testCommands(v *Nvim) func(*testing.T) {
2049+
func testCommand(v *Nvim) func(*testing.T) {
20502050
return func(t *testing.T) {
2051-
t.Run("Nvim", func(t *testing.T) {
2052-
opts := map[string]interface{}{
2053-
"builtin": false,
2054-
}
2055-
cmds, err := v.Commands(opts)
2056-
if err != nil {
2057-
t.Fatal(err)
2058-
}
2059-
if len(cmds) > 0 {
2060-
t.Fatalf("expected 0 length but got %#v", cmds)
2061-
}
2062-
})
2051+
t.Run("Commands", func(t *testing.T) {
2052+
t.Run("Nvim", func(t *testing.T) {
2053+
opts := map[string]interface{}{
2054+
"builtin": false,
2055+
}
2056+
cmds, err := v.Commands(opts)
2057+
if err != nil {
2058+
t.Fatal(err)
2059+
}
2060+
if len(cmds) > 0 {
2061+
t.Fatalf("expected 0 length but got %#v", cmds)
2062+
}
2063+
})
20632064

2064-
t.Run("Batch", func(t *testing.T) {
2065-
b := v.NewBatch()
2065+
t.Run("Batch", func(t *testing.T) {
2066+
b := v.NewBatch()
20662067

2067-
opts := map[string]interface{}{
2068-
"builtin": false,
2069-
}
2070-
var cmds map[string]*Command
2071-
b.Commands(opts, &cmds)
2072-
if err := b.Execute(); err != nil {
2073-
t.Fatal(err)
2068+
opts := map[string]interface{}{
2069+
"builtin": false,
2070+
}
2071+
var cmds map[string]*Command
2072+
b.Commands(opts, &cmds)
2073+
if err := b.Execute(); err != nil {
2074+
t.Fatal(err)
2075+
}
2076+
if len(cmds) > 0 {
2077+
t.Fatalf("expected 0 length but got %#v", cmds)
2078+
}
2079+
})
2080+
})
2081+
2082+
t.Run("UserCommand", func(t *testing.T) {
2083+
tests := map[string]struct {
2084+
name string
2085+
command UserCommand
2086+
opts map[string]interface{}
2087+
want string
2088+
}{
2089+
"SayHello": {
2090+
name: "SayHello",
2091+
command: UserVimCommand(`echo "Hello world!"`),
2092+
opts: map[string]interface{}{
2093+
"force": false,
2094+
},
2095+
want: "Hello world!",
2096+
},
20742097
}
2075-
if len(cmds) > 0 {
2076-
t.Fatalf("expected 0 length but got %#v", cmds)
2098+
for name, tt := range tests {
2099+
t.Run(path.Join(name, "Nvim"), func(t *testing.T) {
2100+
skipVersion(t, "v0.7.0")
2101+
2102+
if err := v.AddUserCommand(tt.name, tt.command, tt.opts); err != nil {
2103+
t.Fatal(err)
2104+
}
2105+
t.Cleanup(func() {
2106+
if err := v.DeleteUserCommand(tt.name); err != nil {
2107+
t.Fatal(err)
2108+
}
2109+
})
2110+
2111+
got, err := v.Exec(tt.name, true)
2112+
if err != nil {
2113+
t.Fatal(err)
2114+
}
2115+
if !strings.EqualFold(tt.want, got) {
2116+
t.Fatalf("expected %s but got %s", tt.want, got)
2117+
}
2118+
})
2119+
2120+
t.Run(path.Join(name, "Batch"), func(t *testing.T) {
2121+
skipVersion(t, "v0.7.0")
2122+
2123+
b := v.NewBatch()
2124+
2125+
b.AddUserCommand(tt.name, tt.command, tt.opts)
2126+
if err := b.Execute(); err != nil {
2127+
t.Fatal(err)
2128+
}
2129+
t.Cleanup(func() {
2130+
b.DeleteUserCommand(tt.name)
2131+
if err := b.Execute(); err != nil {
2132+
t.Fatal(err)
2133+
}
2134+
})
2135+
2136+
var got string
2137+
b.Exec(tt.name, true, &got)
2138+
if err := b.Execute(); err != nil {
2139+
t.Fatal(err)
2140+
}
2141+
if !strings.EqualFold(tt.want, got) {
2142+
t.Fatalf("expected %s but got %s", tt.want, got)
2143+
}
2144+
})
20772145
}
20782146
})
20792147
}

0 commit comments

Comments
 (0)