-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_test.go
267 lines (236 loc) · 8.46 KB
/
test_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package uci
import (
"os"
"strings"
)
// test helper and common test cases for lexer/parser
//
// XXX: This file is named test_test.go, because `go test` ignores
// files with prefix "_", including "_test.go"... I'm open for less
// stupid names.
// control via DUMP env var, which details should be printed out. Use
// something like
//
// DUMP="lex,token" go test -v ./...
var dump = func() map[string]bool {
m := make(map[string]bool)
for _, field := range strings.Split(os.Getenv("DUMP"), ",") {
if field == "all" {
m["json"] = true
m["token"] = true
m["lex"] = true
m["serialized"] = true
} else {
m[field] = true
}
}
return m
}()
func (t scanToken) mk(items ...item) token {
return token{t, items}
}
func (t itemType) mk(val string) item {
return item{t, val, -1}
}
const tcEmptyInput1 = ""
const tcEmptyInput2 = " \n\t\n\n \n "
const tcSimpleInput = `config sectiontype 'sectionname'
option optionname 'optionvalue'
`
const tcExportInput = `package "pkgname"
config empty
config squoted 'sqname'
config dquoted "dqname"
config multiline 'line1\
line2'
`
const tcUnquotedInput = "config foo bar\noption answer 42\n"
const tcUnnamedInput = `
config foo named
option pos '0'
option unnamed '0'
list list 0
config foo
option pos '1'
option unnamed '1'
list list 10
config foo
option pos '2'
option unnamed '1'
list list 20
config foo named
option pos '3'
option unnamed '0'
list list 30
`
const tcHyphenatedInput = `
config wifi-device wl0
option type 'broadcom'
option channel '6'
config wifi-iface wifi0
option device 'wl0'
option mode 'ap'
`
const tcComment = `
# heading
# another heading
config foo
option opt1 1
# option opt1 2
option opt2 3 # baa
option opt3 hello
# a comment block spanning
# multiple lines, surrounded
# by empty lines
# eof
`
const tcInvalid = `
<?xml version="1.0">
<error message="not a UCI file" />
`
const tcIncompletePackage = `
package
`
const tcUnterminatedQuoted = `
config foo "bar
`
const tcUnterminatedUnquoted = `
config foo
option opt opt\
`
var lexerTests = []struct {
name, input string
expected []item
}{
{"empty1", tcEmptyInput1, []item{}},
{"empty2", tcEmptyInput2, []item{}},
{"simple", tcSimpleInput, []item{
itemConfig.mk("config"), itemIdent.mk("sectiontype"), itemString.mk("sectionname"),
itemOption.mk("option"), itemIdent.mk("optionname"), itemString.mk("optionvalue"),
}},
{"export", tcExportInput, []item{
itemPackage.mk("package"), itemString.mk("pkgname"),
itemConfig.mk("config"), itemIdent.mk("empty"),
itemConfig.mk("config"), itemIdent.mk("squoted"), itemString.mk("sqname"),
itemConfig.mk("config"), itemIdent.mk("dquoted"), itemString.mk("dqname"),
itemConfig.mk("config"), itemIdent.mk("multiline"), itemString.mk("line1\\\n\tline2"),
}},
{"unquoted", tcUnquotedInput, []item{
itemConfig.mk("config"), itemIdent.mk("foo"), itemString.mk("bar"),
itemOption.mk("option"), itemIdent.mk("answer"), itemString.mk("42"),
}},
{"unnamed", tcUnnamedInput, []item{
itemConfig.mk("config"), itemIdent.mk("foo"), itemString.mk("named"),
itemOption.mk("option"), itemIdent.mk("pos"), itemString.mk("0"),
itemOption.mk("option"), itemIdent.mk("unnamed"), itemString.mk("0"),
itemList.mk("list"), itemIdent.mk("list"), itemString.mk("0"),
itemConfig.mk("config"), itemIdent.mk("foo"), // unnamed
itemOption.mk("option"), itemIdent.mk("pos"), itemString.mk("1"),
itemOption.mk("option"), itemIdent.mk("unnamed"), itemString.mk("1"),
itemList.mk("list"), itemIdent.mk("list"), itemString.mk("10"),
itemConfig.mk("config"), itemIdent.mk("foo"), // unnamed
itemOption.mk("option"), itemIdent.mk("pos"), itemString.mk("2"),
itemOption.mk("option"), itemIdent.mk("unnamed"), itemString.mk("1"),
itemList.mk("list"), itemIdent.mk("list"), itemString.mk("20"),
itemConfig.mk("config"), itemIdent.mk("foo"), itemString.mk("named"),
itemOption.mk("option"), itemIdent.mk("pos"), itemString.mk("3"),
itemOption.mk("option"), itemIdent.mk("unnamed"), itemString.mk("0"),
itemList.mk("list"), itemIdent.mk("list"), itemString.mk("30"),
}},
{"hyphenated", tcHyphenatedInput, []item{
itemConfig.mk("config"), itemIdent.mk("wifi-device"), itemString.mk("wl0"),
itemOption.mk("option"), itemIdent.mk("type"), itemString.mk("broadcom"),
itemOption.mk("option"), itemIdent.mk("channel"), itemString.mk("6"),
itemConfig.mk("config"), itemIdent.mk("wifi-iface"), itemString.mk("wifi0"),
itemOption.mk("option"), itemIdent.mk("device"), itemString.mk("wl0"),
itemOption.mk("option"), itemIdent.mk("mode"), itemString.mk("ap"),
}},
{"commented", tcComment, []item{
itemConfig.mk("config"), itemIdent.mk("foo"), // unnamed
itemOption.mk("option"), itemIdent.mk("opt1"), itemString.mk("1"),
itemOption.mk("option"), itemIdent.mk("opt2"), itemString.mk("3"),
itemOption.mk("option"), itemIdent.mk("opt3"), itemString.mk("hello"),
}},
{"invalid", tcInvalid, []item{
itemError.mk(`expected keyword (package, config, option, list) or eof, got "<?xml vers…"`),
}},
{"pkg invalid", tcIncompletePackage, []item{
itemPackage.mk("package"),
itemError.mk("incomplete package name"),
}},
{"unterminated quoted string", tcUnterminatedQuoted, []item{
itemConfig.mk("config"), itemIdent.mk("foo"), itemError.mk("unterminated quoted string"),
}},
{"unterminated unquoted string", tcUnterminatedUnquoted, []item{
itemConfig.mk("config"), itemIdent.mk("foo"), // unnamed
itemOption.mk("option"), itemIdent.mk("opt"), itemError.mk("unterminated unquoted string"),
}},
}
var parserTests = []struct {
name, input string
expected []token
}{
{"empty1", "", []token{}},
{"empty2", " \n\t\n\n \n ", []token{}},
{"simple", tcSimpleInput, []token{
tokSection.mk(itemIdent.mk("sectiontype"), itemString.mk("sectionname")),
tokOption.mk(itemIdent.mk("optionname"), itemString.mk("optionvalue")),
}},
{"export", tcExportInput, []token{
tokPackage.mk(itemString.mk("pkgname")),
tokSection.mk(itemIdent.mk("empty")),
tokSection.mk(itemIdent.mk("squoted"), itemString.mk("sqname")),
tokSection.mk(itemIdent.mk("dquoted"), itemString.mk("dqname")),
tokSection.mk(itemIdent.mk("multiline"), itemString.mk("line1\\\n\tline2")),
}},
{"unquoted", tcUnquotedInput, []token{
tokSection.mk(itemIdent.mk("foo"), itemString.mk("bar")),
tokOption.mk(itemIdent.mk("answer"), itemString.mk("42")),
}},
{"unnamed", tcUnnamedInput, []token{
tokSection.mk(itemIdent.mk("foo"), itemString.mk("named")),
tokOption.mk(itemIdent.mk("pos"), itemString.mk("0")),
tokOption.mk(itemIdent.mk("unnamed"), itemString.mk("0")),
tokList.mk(itemIdent.mk("list"), itemString.mk("0")),
tokSection.mk(itemIdent.mk("foo")), // unnamed
tokOption.mk(itemIdent.mk("pos"), itemString.mk("1")),
tokOption.mk(itemIdent.mk("unnamed"), itemString.mk("1")),
tokList.mk(itemIdent.mk("list"), itemString.mk("10")),
tokSection.mk(itemIdent.mk("foo")), // unnamed
tokOption.mk(itemIdent.mk("pos"), itemString.mk("2")),
tokOption.mk(itemIdent.mk("unnamed"), itemString.mk("1")),
tokList.mk(itemIdent.mk("list"), itemString.mk("20")),
tokSection.mk(itemIdent.mk("foo"), itemString.mk("named")),
tokOption.mk(itemIdent.mk("pos"), itemString.mk("3")),
tokOption.mk(itemIdent.mk("unnamed"), itemString.mk("0")),
tokList.mk(itemIdent.mk("list"), itemString.mk("30")),
}},
{"hyphenated", tcHyphenatedInput, []token{
tokSection.mk(itemIdent.mk("wifi-device"), itemString.mk("wl0")),
tokOption.mk(itemIdent.mk("type"), itemString.mk("broadcom")),
tokOption.mk(itemIdent.mk("channel"), itemString.mk("6")),
tokSection.mk(itemIdent.mk("wifi-iface"), itemString.mk("wifi0")),
tokOption.mk(itemIdent.mk("device"), itemString.mk("wl0")),
tokOption.mk(itemIdent.mk("mode"), itemString.mk("ap")),
}},
{"commented", tcComment, []token{
tokSection.mk(itemIdent.mk("foo")),
tokOption.mk(itemIdent.mk("opt1"), itemString.mk("1")),
tokOption.mk(itemIdent.mk("opt2"), itemString.mk("3")),
tokOption.mk(itemIdent.mk("opt3"), itemString.mk("hello")),
}},
{"invalid", tcInvalid, []token{
tokError.mk(itemError.mk(`expected keyword (package, config, option, list) or eof, got "<?xml vers…"`)),
}},
{"pkg invalid", tcIncompletePackage, []token{
tokError.mk(itemError.mk("incomplete package name")),
}},
{"unterminated quoted string", tcUnterminatedQuoted, []token{
tokSection.mk(itemIdent.mk("foo")),
tokError.mk(itemError.mk("unterminated quoted string")),
}},
{"unterminated unquoted string", tcUnterminatedUnquoted, []token{
tokSection.mk(itemIdent.mk("foo")),
tokError.mk(itemError.mk("unterminated unquoted string")),
}},
}