/
test_config.go
105 lines (91 loc) · 3.04 KB
/
test_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"sort"
"strings"
"github.com/myitcv/govim"
"github.com/myitcv/govim/cmd/govim/config"
)
// This file contains config that would otherwise be in the
// github.com/myitcv/govim/cmd/govim/config, but for the fact that these are
// only definitions we need for the purposes of testing
const (
CommandHello config.Command = "Hello"
)
const (
FunctionHello config.Function = "Hello"
FunctionDumpPopups config.Function = config.InternalFunctionPrefix + "DumpPopups"
FunctionSimpleBatch config.Function = "SimpleBatch"
FunctionCancelBatch config.Function = "CancelBatch"
FunctionBadBatch config.Function = "BadBatch"
FunctionAssertFailedBatch config.Function = "AssertFailedBatch"
)
func (g *govimplugin) InitTestAPI() {
if !exposeTestAPI {
return
}
g.DefineFunction(string(FunctionHello), []string{}, g.vimstate.hello)
g.DefineCommand(string(CommandHello), g.vimstate.helloComm, govim.NArgsZeroOrOne)
g.DefineFunction(string(FunctionDumpPopups), []string{}, g.vimstate.dumpPopups)
g.DefineFunction(string(FunctionSimpleBatch), []string{}, g.vimstate.simpleBatch)
g.DefineFunction(string(FunctionCancelBatch), []string{}, g.vimstate.cancelBatch)
g.DefineFunction(string(FunctionBadBatch), []string{}, g.vimstate.badBatch)
g.DefineFunction(string(FunctionAssertFailedBatch), []string{}, g.vimstate.assertFailedBatch)
}
func (v *vimstate) hello(args ...json.RawMessage) (interface{}, error) {
return "Hello from function", nil
}
func (v *vimstate) helloComm(flags govim.CommandFlags, args ...string) error {
msg := "Hello from command"
if len(args) == 1 {
msg += "; special note: " + args[0]
}
v.ChannelExf("echom %q", msg)
return nil
}
func (v *vimstate) dumpPopups(args ...json.RawMessage) (interface{}, error) {
var bufInfo []struct {
BufNr int `json:"bufnr"`
Popups []int `json:"popups"`
}
bi := v.ChannelExpr("getbufinfo()")
v.Parse(bi, &bufInfo)
sort.Slice(bufInfo, func(i, j int) bool {
return bufInfo[i].BufNr < bufInfo[j].BufNr
})
var sb strings.Builder
for _, b := range bufInfo {
if len(b.Popups) == 0 {
continue
}
sb.WriteString(v.ParseString(v.ChannelExprf(`join(getbufline(%v, 0, '$'), "\n")."\n"`, b.BufNr)))
}
return sb.String(), nil
}
func (v *vimstate) simpleBatch(args ...json.RawMessage) (interface{}, error) {
v.BatchStart()
defer v.BatchCancelIfNotEnded()
v.BatchChannelCall("eval", "5")
v.BatchChannelExprf("4")
res := v.BatchEnd()
return res, nil
}
func (v *vimstate) cancelBatch(args ...json.RawMessage) (interface{}, error) {
v.BatchStart()
defer v.BatchCancelIfNotEnded()
return "did not run", nil
}
func (v *vimstate) badBatch(args ...json.RawMessage) (interface{}, error) {
v.BatchStart()
defer v.BatchCancelIfNotEnded()
v.BatchChannelCall("execute", "throw \"failed\"")
res := v.BatchEnd()
return res, nil
}
func (v *vimstate) assertFailedBatch(args ...json.RawMessage) (interface{}, error) {
v.BatchStart()
defer v.BatchCancelIfNotEnded()
v.BatchAssertChannelExprf(AssertIsZero, "1")
res := v.BatchEnd()
return res, nil
}