-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser_test.go
356 lines (311 loc) · 9.4 KB
/
parser_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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package toml
import (
"github.com/achun/testing-want"
"io/ioutil"
"testing"
)
func assertBadParse(wt want.Want, src string, msg string) {
scan := NewScanner([]byte(src))
p := &parse{Scanner: scan, testMode: true}
p.Handler(func(Token, string) error { return nil })
stagePlay(p, openStage())
nwt := wt
nwt.Skip = 3
if msg == "" {
nwt.Error(p.err, "TOML want an error: ", src)
} else {
nwt.Equal(p.err.Error(), msg, func() string {
l, c, s := p.LastLine()
return want.String("Line ", l, ", Column ", c, "\n"+s, "\n"+arrowCol(s, c))
})
}
}
func arrowCol(line string, col int) string {
ret := ""
for i, r := range line {
if i+1 == col {
break
}
if r < 256 {
ret += " "
} else {
ret += " "
}
}
return ret + "^"
}
// outs =["token","value"]
func assertParse(wt want.Want, str string, outs ...string) {
scan := NewScanner([]byte(str))
p := &parse{Scanner: scan, testMode: true}
i := 0
l := len(outs)
last := ""
if l > 0 {
last = outs[l-1]
}
p.Handler(func(token Token, str string) (err error) {
if tokenWhitespace == token || token == tokenNewLine || token == tokenEOF {
return
}
if i == l {
wt.False(true, "outputs more: ", i, " > ", l-1, "\nlast: ", last, "\ngot token: ", token.String()+"\nFetch: "+str, func() string {
l, c, s := p.LastLine()
return want.String("Line ", l, ", Column ", c, "\n"+s, "\n"+arrowCol(s, c))
})
}
wt.Equal(token.String()+" "+str, outs[i], i, func() string {
l, c, s := p.LastLine()
return want.String("Line ", l, ", Column ", c, "\n"+s, "\n"+arrowCol(s, c))
})
i++
return
})
stagePlay(p, openStage())
wt = wt
wt.Skip = 3
wt.Nil(p.err, func() string {
l, c, s := p.LastLine()
return want.String("Line ", l, ", Column ", c, "\n"+s, "\n"+arrowCol(s, c))
})
if l > 0 {
j := i
if j >= l {
j = l - 1
}
wt.Equal(i, l, "loss: ", outs[j])
}
}
func TestScanner(t *testing.T) {
if skipTest {
return
}
wt := want.T(t)
s := NewScanner([]byte("0123456789"))
wt.Equal(s.Fetch(true), "0")
r := s.Rune()
for i := 0; i < 10; i++ {
wt.Equal(r, rune('0'+i))
r = s.Next()
}
wt.Equal(s.Next(), rune(EOF))
wt.True(s.Eof())
wt.Equal(s.Fetch(true), "123456789")
wt.Equal(s.Next(), rune(EOF))
wt.Equal(s.Next(), rune(EOF))
wt.Equal(s.Fetch(true), "")
}
func TestEmpty(tt *testing.T) {
t := want.Want{tt, 7}
if skipTest {
return
}
assertParse(t, ``)
assertParse(t, ` `)
assertParse(t, ` `)
assertParse(t, `
`)
}
func TestToken(tt *testing.T) {
const (
al = `ArrayLeftBrack [`
ar = `ArrayRightBrack ]`
eq = `Equal =`
i = `Integer`
ca = `Comma ,`
)
t := want.Want{tt, 7}
if skipTest {
return
}
assertParse(t, `string = "is string \n newline"`, `Key string`, eq, `String "is string \n newline"`)
assertParse(t, `#`, `Comment #`)
assertParse(t, `#
# 1
`, `Comment #`, `Comment # 1`)
assertParse(t, `k = true # 1.1`, `Key k`, eq, `Boolean true`, `Comment # 1.1`)
assertParse(t, `k = false# ""`, `Key k`, eq, `Boolean false`, `Comment # ""`)
assertParse(t, `key = 1`, `Key key`, eq, `Integer 1`)
assertParse(t, `key = []`, `Key key`, eq, al, ar)
assertParse(t, `key = [1]`, `Key key`, eq, al, `Integer 1`, ar)
assertParse(t, `ia = [1 , 2]`, `Key ia`, eq, al, `Integer 1`, ca, `Integer 2`, ar)
assertParse(t, `ia = [[1],[2,3],["A","B"]]`, `Key ia`, eq,
al,
al, `Integer 1`, ar, ca,
al, `Integer 2`, ca, `Integer 3`, ar, ca,
al, `String "A"`, ca, `String "B"`, ar,
ar)
assertParse(t, `ia = [[[ 0,1 ],["A","B"],[["D"],["E"]]],[ 2,3]]`, `Key ia`, eq,
al,
al,
al, `Integer 0`, ca, `Integer 1`, ar, ca,
al, `String "A"`, ca, `String "B"`, ar, ca,
al,
al, `String "D"`, ar, ca,
al, `String "E"`, ar, ar,
ar, ca,
al, `Integer 2`, ca, `Integer 3`, ar,
ar)
assertParse(t, `str = ""`, `Key str`, eq, `String ""`)
const noEqual = `roles does not match one of stageEqual`
assertBadParse(t, `key`, "invalid Key")
assertBadParse(t, `key 1`, noEqual)
assertBadParse(t, `ke y = name`, noEqual)
assertBadParse(t, `key # comment`, noEqual)
const noValues = `roles does not match one of stageValues`
assertBadParse(t, `key = name`, noValues)
assertBadParse(t, `key = # comment`, noValues)
const noInteger = `roles does not match one of stageIntegerArray`
assertBadParse(t, `key = [1,ent]`, noInteger)
assertBadParse(t, `key = [1,"ent"]`, noInteger)
const noArrayVlaues = `roles does not match one of stageArray`
assertBadParse(t, `key = [`, noArrayVlaues)
assertBadParse(t, `[]`, "invalid TableName")
assertBadParse(t, `[table ]`, "invalid TableName")
assertBadParse(t, `[tab le]`, "invalid TableName")
assertBadParse(t, `[ table]`, "invalid TableName")
assertBadParse(t, `[ table]`, "invalid TableName")
assertBadParse(t, `[[tables]`, "invalid ArrayOfTables")
assertBadParse(t, `[[ tables]]`, "invalid ArrayOfTables")
assertBadParse(t, `[[tab les]]`, "invalid ArrayOfTables")
assertBadParse(t, `[[tables ]]`, "invalid ArrayOfTables")
assertBadParse(t, `[[ tables]]`, "invalid ArrayOfTables")
assertBadParse(t, `[[tab les]]`, "invalid ArrayOfTables")
assertParse(t, `[name]`, "TableName [name]")
assertParse(t, `[name] #`, "TableName [name]", `Comment #`)
assertParse(t, `[[name]]`, "ArrayOfTables [[name]]")
assertParse(t, `[[name]]#`, "ArrayOfTables [[name]]", `Comment #`)
assertParse(t, `
[[name]] # 世界,
id = 1
[[name]] # Template, 尝试独立的 _layouts
id =2
`,
`ArrayOfTables [[name]]`, `Comment # 世界,`,
`Key id`, eq, `Integer 1`,
`ArrayOfTables [[name]]`, `Comment # Template, 尝试独立的 _layouts`,
`Key id`, eq, `Integer 2`,
)
}
func TestParserOK(t *testing.T) {
if skipTest {
return
}
assertParse(want.Want{t, 7},
`
# comment 1
# comment 2
[table#] # comment 3
[[arrayoftable]]
# comment 4
key = -111# comment 5
float= 123.22
int = 11234
datetime = 2012-01-02T13:11:14Z
string = "is string \n newline"
intger = [
1,# comment 6
2# comment 7
]
strings=[# comment 8
"a",
"b",
]# comment 9
# comment 10`,
`Comment # comment 1`,
`Comment # comment 2`,
`TableName [table#]`, `Comment # comment 3`,
`ArrayOfTables [[arrayoftable]]`,
`Comment # comment 4`,
`Key key`, `Equal =`, `Integer -111`, `Comment # comment 5`,
`Key float`, `Equal =`, `Float 123.22`,
`Key int`, `Equal =`, `Integer 11234`,
`Key datetime`, `Equal =`, `Datetime 2012-01-02T13:11:14Z`,
`Key string`, `Equal =`, `String "is string \n newline"`,
`Key intger`, `Equal =`,
`ArrayLeftBrack [`,
`Integer 1`, `Comma ,`, `Comment # comment 6`, `Integer 2`, `Comment # comment 7`,
`ArrayRightBrack ]`,
`Key strings`, `Equal =`,
`ArrayLeftBrack [`, `Comment # comment 8`,
`String "a"`, `Comma ,`, `String "b"`, `Comma ,`,
`ArrayRightBrack ]`, `Comment # comment 9`, `Comment # comment 10`,
)
}
func TestFile(t *testing.T) {
if skipTest {
return
}
buf, err := ioutil.ReadFile("tests/example.toml")
if err != nil {
t.Fatal(err)
}
assertParse(want.Want{t, 7}, string(buf),
`Comment # This is a TOML document. Boom.`,
`Key title`, `Equal =`, `String "TOML Example"`,
`TableName [owner]`,
`Comment # owner information`,
`Key name`, `Equal =`, `String "Tom Preston-Werner"`,
`Key organization`, `Equal =`, `String "GitHub"`,
`Key bio`, `Equal =`, `String "GitHub Cofounder & CEO\nLikes tater tots and beer."`,
`Key dob`, `Equal =`, `Datetime 1979-05-27T07:32:00Z`, `Comment # First class dates? Why not?`,
`TableName [database]`,
`Key server`, `Equal =`, `String "192.168.1.1"`,
`Key ports`, `Equal =`,
`ArrayLeftBrack [`,
`Integer 8001`, `Comma ,`, `Integer 8001`, `Comma ,`, `Integer 8002`,
`ArrayRightBrack ]`,
`Key connection_max`, `Equal =`, `Integer 5000`,
`Key enabled`, `Equal =`, `Boolean true`,
`TableName [servers]`,
`Comment # You can indent as you please. Tabs or spaces. TOML don't care.`,
`TableName [servers.alpha]`,
`Key ip`, `Equal =`, `String "10.0.0.1"`,
`Key dc`, `Equal =`, `String "eqdc10"`,
`TableName [servers.beta]`,
`Key ip`, `Equal =`, `String "10.0.0.2"`,
`Key dc`, `Equal =`, `String "eqdc10"`,
`Key country`, `Equal =`, `String "中国"`, `Comment # This should be parsed as UTF-8`,
`TableName [clients]`,
`Key data`, `Equal =`,
`ArrayLeftBrack [`,
`ArrayLeftBrack [`,
`String "gamma"`, `Comma ,`, `String "delta"`,
`ArrayRightBrack ]`,
`Comma ,`,
`ArrayLeftBrack [`,
`Integer 1`, `Comma ,`, `Integer 2`,
`ArrayRightBrack ]`,
`ArrayRightBrack ]`,
`Comment # just an update to make sure parsers support it`,
`Comment # Line breaks are OK when inside arrays`,
`Key hosts`, `Equal =`,
`ArrayLeftBrack [`,
`String "alpha"`, `Comma ,`, `String "omega"`,
`ArrayRightBrack ]`,
`Comment # Products`,
`ArrayOfTables [[products]]`,
`Key name`, `Equal =`, `String "Hammer"`,
`Key sku`, `Equal =`, `Integer 738594937`,
`ArrayOfTables [[products]]`,
`Key name`, `Equal =`, `String "Nail"`,
`Key sku`, `Equal =`, `Integer 284758393`,
`Key color`, `Equal =`, `String "gray"`,
`Comment # nested`,
`ArrayOfTables [[fruit]]`,
`Key name`, `Equal =`, `String "apple"`,
`TableName [fruit.physical]`,
`Key color`, `Equal =`, `String "red"`,
`Key shape`, `Equal =`, `String "round"`,
`ArrayOfTables [[fruit.variety]]`,
`Key name`, `Equal =`, `String "red delicious"`,
`ArrayOfTables [[fruit.variety]]`,
`Key name`, `Equal =`, `String "granny smith"`,
`ArrayOfTables [[fruit]]`,
`Key name`, `Equal =`, `String "banana"`,
`ArrayOfTables [[fruit.variety]]`,
`Key name`, `Equal =`, `String "plantain"`,
`Comment # last comments for`,
`Comment # TOML document`,
)
}