forked from agl/xmpp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_test.go
161 lines (139 loc) · 3.4 KB
/
input_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"reflect"
"testing"
)
const (
opFind = iota
opNext
)
var priorityListTests = []struct {
op int
in, out string
}{
{opFind, "a", "anchor"},
{opFind, "a", "anchor"},
{opNext, "", "anvil"},
{opNext, "", "anchor"},
{opNext, "", "anvil"},
{opNext, "", "anchor"},
{opFind, "a", "anchor"},
{opNext, "", "anvil"},
{opFind, "a", "anvil"},
{opFind, "a", "anvil"},
{opFind, "bo", "bob"},
{opNext, "", "boom"},
{opNext, "", "bop"},
{opFind, "a", "anvil"},
{opFind, "b", "bop"},
{opFind, "c", "charlie"},
{opNext, "", "charlie"},
{opNext, "", "charlie"},
}
func TestPriorityList(t *testing.T) {
var pl priorityList
for _, word := range []string{"bop", "boom", "bob", "anvil", "anchor", "charlie"} {
pl.Insert(word)
}
for i, step := range priorityListTests {
var out string
switch step.op {
case opFind:
out, _ = pl.Find(step.in)
case opNext:
out = pl.Next()
default:
panic("unknown op")
}
if string(out) != step.out {
t.Fatalf("failed at step %d: got %s, want %s", i, string(out), step.out)
}
}
}
var testCommands = []uiCommand{
{"a", aCommand{}, "Desc"},
{"b", bCommand{}, "Desc"},
}
type aCommand struct {
}
type bCommand struct {
A string
B string "uid"
C string
}
var parseForCompletionTests = []struct {
in string
ok bool
before, prefix string
isCommand bool
}{
{"", false, "", "", false},
{"/", true, "/", "", true},
{"/a", true, "/", "a", true},
{"/ab", true, "/", "ab", true},
{"/a b", false, "", "", false},
{"/a b ", false, "", "", false},
{"/a ba", false, "", "", false},
{"/a ba c", false, "", "", false},
{"/a ba c d", false, "", "", false},
{"/b c", false, "", "", false},
{"/b c ", false, "", "", false},
{"/b c d", true, "/b c ", "d", false},
{"/b c de", true, "/b c ", "de", false},
{"/b c de ", false, "", "", false},
{"/b c de f", false, "", "", false},
}
func TestParseCommandForCompletion(t *testing.T) {
for i, test := range parseForCompletionTests {
before, prefix, isCommand, ok := parseCommandForCompletion(testCommands, test.in)
if ok != test.ok {
t.Errorf("#%d: result mismatch (should be %t)", i, test.ok)
continue
}
if !ok {
continue
}
if before != test.before {
t.Errorf("#%d: mismatch with 'before': got '%s', want '%s'", i, string(before), test.before)
}
if prefix != test.prefix {
t.Errorf("#%d: mismatch with 'prefix': got '%s', want '%s'", i, string(before), test.before)
}
if isCommand != test.isCommand {
t.Errorf("#%d: isCommand incorrect, wanted %t", i, test.isCommand)
}
}
}
var parseCommandTests = []struct {
in string
ok bool
out interface{}
}{
{"/", false, nil},
{"/bob", false, nil},
{"/a", true, aCommand{}},
{"/a b", false, nil},
{"/a b c", false, nil},
{"/b a", false, nil},
{"/b a b", false, nil},
{"/b a b ", false, nil},
{"/b a b c", true, bCommand{"a", "b", "c"}},
{"/b a b\\ c", true, bCommand{"a", "b ", "c"}},
{"/b a \"b b\" c", true, bCommand{"a", "b b", "c"}},
{"/b a \"b \\\"b\" c", true, bCommand{"a", "b \"b", "c"}},
}
func TestParseCommand(t *testing.T) {
for i, test := range parseCommandTests {
v, err := parseCommand(testCommands, []byte(test.in))
if (len(err) == 0) != test.ok {
t.Errorf("#%d: bad parse result, expected %t", i, test.ok)
continue
}
if !test.ok {
continue
}
if !reflect.DeepEqual(v, test.out) {
t.Errorf("#%d: bad result: got %v, want %v", i, v, test.out)
}
}
}