Skip to content

Commit 35e18ab

Browse files
committed
nvim: add skipVersion test util for API version compatibility
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent c8dbb8f commit 35e18ab

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

nvim/nvim_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"runtime"
1313
"sort"
14+
"strconv"
1415
"strings"
1516
"sync/atomic"
1617
"testing"
@@ -79,12 +80,71 @@ func newChildProcess(tb testing.TB) (v *Nvim, cleanup func()) {
7980
return v, cleanup
8081
}
8182

83+
type version struct {
84+
Major int
85+
Minor int
86+
Patch int
87+
}
88+
89+
var nvimVersion version
90+
91+
func parseVersion(tb testing.TB, version string) (major, minor, patch int) {
92+
tb.Helper()
93+
94+
version = strings.TrimPrefix(version, "v")
95+
vpair := strings.Split(version, ".")
96+
if len(vpair) != 3 {
97+
tb.Fatal("could not parse neovim version")
98+
}
99+
100+
var err error
101+
major, err = strconv.Atoi(vpair[0])
102+
if err != nil {
103+
tb.Fatal(err)
104+
}
105+
minor, err = strconv.Atoi(vpair[1])
106+
if err != nil {
107+
tb.Fatal(err)
108+
}
109+
patch, err = strconv.Atoi(vpair[2])
110+
if err != nil {
111+
tb.Fatal(err)
112+
}
113+
114+
return major, minor, patch
115+
}
116+
117+
func skipVersion(tb testing.TB, version string) {
118+
major, minor, patch := parseVersion(tb, version)
119+
120+
const skipFmt = "skip test: current neovim version v%d.%d.%d but expected version %s"
121+
if nvimVersion.Major < major || nvimVersion.Minor < minor || nvimVersion.Patch < patch {
122+
tb.Skipf(skipFmt, nvimVersion.Major, nvimVersion.Minor, nvimVersion.Patch, version)
123+
}
124+
}
125+
82126
func TestAPI(t *testing.T) {
83127
t.Parallel()
84128

85129
v, cleanup := newChildProcess(t)
86130
defer cleanup()
87131

132+
apiInfo, err := v.APIInfo()
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
if len(apiInfo) != 2 {
137+
t.Fatalf("unknown APIInfo: %#v", apiInfo)
138+
}
139+
info, ok := apiInfo[1].(map[string]interface{})
140+
if !ok {
141+
t.Fatalf("apiInfo[1] is not map[string]interface{} type: %T", apiInfo[1])
142+
}
143+
infoV := info["version"].(map[string]interface{})
144+
nvimVersion.Major = int(infoV["major"].(int64))
145+
nvimVersion.Minor = int(infoV["minor"].(int64))
146+
nvimVersion.Patch = int(infoV["patch"].(int64))
147+
88148
t.Run("BufAttach", testBufAttach(v))
89149
t.Run("SimpleHandler", testSimpleHandler(v))
90150
t.Run("Buffer", testBuffer(v))

0 commit comments

Comments
 (0)