-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion_test.go
121 lines (98 loc) · 2.38 KB
/
completion_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type testcase struct {
path string
lines []string
cursorX int
cursorY int
expected []string
}
func TestCompletion(t *testing.T) {
test := assert.New(t)
scenarios, err := filepath.Glob("unit_tests/*")
if err != nil {
panic(err)
}
only := os.Getenv("ONLY")
if only != "" {
log.Printf("filter for testcases: *%s*", only)
}
testcases := []testcase{}
for _, scenario := range scenarios {
// ignore dirs
if isFileExists(scenario) {
if strings.Contains(scenario, only) {
testcases = append(testcases, getTestcase(scenario))
}
}
}
if len(scenarios) == 0 {
panic("no tests found")
}
for _, testcase := range testcases {
id, err := getIdentifierToComplete(
defaultRegexpCursor,
testcase.lines,
testcase.cursorX,
testcase.cursorY,
)
if err != nil {
test.Errorf(err, "unable to get completion identifier: %s", testcase.path)
continue
}
if !test.NotNilf(id, "invalid prompt (identifier = nil) in %s", testcase.path) {
continue
}
candidates, err := getCompletionCandidates(defaultRegexpCandidate, testcase.lines, id)
if err != nil {
test.Errorf(err, "unable to get completion candidates: %s", testcase.path)
continue
}
candidates = getUniqueCandidates(candidates)
values := []string{}
for _, candidate := range candidates {
values = append(values, candidate.Value)
log.Printf("candidate=%s y=%d x=%d", candidate.Value, candidate.Y, candidate.X)
}
test.EqualValues(testcase.expected, values, "%s", testcase.path)
}
}
func getTestcase(filename string) testcase {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
src := strings.Split(strings.TrimSpace(string(data)), "\n")
pane := src[0]
prompt := src[1]
candidates := src[2:]
// no cache because zfs already has arc
paneData, err := ioutil.ReadFile("unit_tests/" + pane)
if err != nil {
panic(err)
}
lines := strings.Split(string(paneData), "\n")
prefix := "$ "
lines = append(lines, prefix+prompt)
cursorY := len(lines) - 1
cursorX := len(prefix + prompt)
return testcase{
path: filename,
lines: lines,
cursorX: cursorX,
cursorY: cursorY,
expected: candidates,
}
}
func isFileExists(path string) bool {
stat, err := os.Stat(path)
return !os.IsNotExist(err) && !stat.IsDir()
}