-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer_test.go
378 lines (367 loc) · 10.3 KB
/
lexer_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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package cajun
import (
"fmt"
"testing"
)
var itemName = map[itemType]string{
itemUnset: "unset",
itemError: "error",
itemAsterisks: "asterisks",
itemBold: "bold",
itemEOF: "EOF",
itemFreeLink: "freelink",
itemHeading1: "heading1",
itemHeading2: "heading2",
itemHeading3: "heading3",
itemHeading4: "heading4",
itemHeading5: "heading5",
itemHeading6: "heading6",
itemHorizontalRule: "heading7",
itemImage: "image",
itemItalics: "italics",
itemLink: "link",
itemLineBreak: "linebreak",
itemListUnordered: "listunordered",
itemListOrdered: "listordered",
itemTable: "table",
itemText: "text",
itemNewLine: "newline",
itemSpaceRun: "spaces",
itemNoWiki: "nowiki",
itemWikiLineBreak: "wikilinebreak",
}
func (i itemType) String() string {
s := itemName[i]
if s == "" {
return fmt.Sprintf("item%d", int(i))
}
return s
}
type lexTest struct {
name string
input string
items []item
}
var (
tEOF = item{itemEOF, 0, ""}
tNewLine = item{itemNewLine, 0, "\n"}
)
var lexTests = []lexTest{
{"empty", "", []item{tEOF}},
{"spaces", " \t\n", []item{{itemSpaceRun, 0, " \t"}, tNewLine, tEOF}},
{"new lines", "\n\n\n\n", []item{tNewLine, tNewLine, tNewLine, tNewLine, tEOF}},
{"text", `now is the time`, []item{{itemText, 0, "now is the time"}, tEOF}},
{"escaped bold", `~**now is the time`, []item{{itemEscape, 0, "~"}, {itemEscapeText, 0, "*"}, {itemText, 0, "*now is the time"}, tEOF}},
{"escaped bold", `~~now is the time`, []item{{itemEscape, 0, "~"}, {itemEscapeText, 0, "~"}, {itemText, 0, "now is the time"}, tEOF}},
{"text with link", "hello-[[blah]]-world", []item{
{itemText, 0, "hello-"},
{itemLink, 0, "[[blah]]"},
{itemText, 0, "-world"},
tEOF,
}},
{"text with bold asterisks", "hello-**blah**-world", []item{
{itemText, 0, "hello-"},
{itemBold, 0, "**"},
{itemText, 0, "blah"},
{itemBold, 0, "**"},
{itemText, 0, "-world"},
tEOF,
}},
{"text with asterisks", "**blah**-world", []item{
{itemBold, 0, "**"},
{itemText, 0, "blah"},
{itemBold, 0, "**"},
{itemText, 0, "-world"},
tEOF,
}},
{"text with asterisks", "* start unordered list\n", []item{
{itemListUnorderedIncrease, 0, "*"},
{itemListUnordered, 0, ""},
{itemText, 0, " start unordered list"},
tNewLine,
tEOF,
}},
{"text with asterisks", "** start unordered list\n", []item{
{itemBold, 0, "**"},
{itemText, 0, " start unordered list"},
tNewLine,
tEOF,
}},
{"text with asterisks", "*** start unordered list\n", []item{
{itemBold, 0, "**"},
{itemText, 0, "* start unordered list"},
tNewLine,
tEOF,
}},
{"text with pound", "# start ordered list\n", []item{
{itemListOrderedIncrease, 0, "#"},
{itemListOrdered, 0, ""},
{itemText, 0, " start ordered list"},
tNewLine,
tEOF,
}},
{"text with pound, no space follows", "#start ordered list\n", []item{
{itemText, 0, "#start ordered list"},
tNewLine,
tEOF,
}},
{"text with pound, newline, space", "\n # start ordered list\n text", []item{
tNewLine,
{itemSpaceRun, 0, " "},
{itemListOrderedIncrease, 0, "#"},
{itemListOrdered, 0, ""},
{itemText, 0, " start ordered list"},
tNewLine,
{itemText, 0, " text"},
tEOF,
}},
{"text with wiki line break", "wiki wiki\\\\ break\n", []item{
{itemText, 0, "wiki wiki"},
{itemWikiLineBreak, 0, "\\\\"},
{itemText, 0, " break"},
tNewLine,
tEOF,
}},
{"text with wiki horizontal rule", "wiki wiki\n----\n break\n", []item{
{itemText, 0, "wiki wiki"},
tNewLine,
{itemHorizontalRule, 0, "----"},
tNewLine,
{itemText, 0, " break"},
tNewLine,
tEOF,
}},
{"text with wiki horizontal rule spaces around", "wiki wiki\n ---- \n break\n", []item{
{itemText, 0, "wiki wiki"},
tNewLine,
{itemSpaceRun, 0, " "},
{itemHorizontalRule, 0, "----"},
{itemSpaceRun, 0, " "},
tNewLine,
{itemText, 0, " break"},
tNewLine,
tEOF,
}},
{"text with text before horizontal rule", "wiki wiki ---- \n", []item{
{itemText, 0, "wiki wiki ---- "},
tNewLine,
tEOF,
}},
{"text with italics", "hello-//blah//-world", []item{
{itemText, 0, "hello-"},
{itemItalics, 0, "//"},
{itemText, 0, "blah"},
{itemItalics, 0, "//"},
{itemText, 0, "-world"},
tEOF,
}},
{"text with heading", "= start heading\n", []item{
{itemHeading1, 0, "="},
{itemText, 0, " start heading"},
tNewLine,
tEOF,
}},
{"text with heading", "== start heading\n", []item{
{itemHeading2, 0, "=="},
{itemText, 0, " start heading"},
tNewLine,
tEOF,
}},
{"text with heading open and close", "== start heading==\n", []item{
{itemHeading2, 0, "=="},
{itemText, 0, " start heading"},
{itemHeadingCloseRun, 0, "=="},
tNewLine,
tEOF,
}},
{"text with heading open and close", "== start heading== \n", []item{
{itemHeading2, 0, "=="},
{itemText, 0, " start heading"},
{itemHeadingCloseRun, 0, "=="},
{itemText, 0, " "},
tNewLine,
tEOF,
}},
{"text with free link", "hello-http://www.blah.com/whatever?asdf, -world", []item{
{itemText, 0, "hello-"},
{itemFreeLink, 0, "http://www.blah.com/whatever?asdf"},
{itemText, 0, ", -world"},
tEOF,
}},
{"text with link", "hello- [[http://www.blah.com/whatever?asdf]], -world", []item{
{itemText, 0, "hello- "},
{itemLink, 0, "[[http://www.blah.com/whatever?asdf]]"},
{itemText, 0, ", -world"},
tEOF,
}},
{"text with link", "hello- [[http://www.blah.com/whatever?asdf|blah whatever]], -world", []item{
{itemText, 0, "hello- "},
{itemLink, 0, "[[http://www.blah.com/whatever?asdf|blah whatever]]"},
{itemText, 0, ", -world"},
tEOF,
}},
{"text with link", "hello- [[somepage|blah whatever]], -world", []item{
{itemText, 0, "hello- "},
{itemLink, 0, "[[somepage|blah whatever]]"},
{itemText, 0, ", -world"},
tEOF,
}},
{"text with image", "hello- {{http://www.blah.com/whatever?asdf}}, -world", []item{
{itemText, 0, "hello- "},
{itemImage, 0, "{{http://www.blah.com/whatever?asdf}}"},
{itemText, 0, ", -world"},
tEOF,
}},
{"text with image w/ alt", "hello- {{somepage|blah whatever}}, -world", []item{
{itemText, 0, "hello- "},
{itemImage, 0, "{{somepage|blah whatever}}"},
{itemText, 0, ", -world"},
tEOF,
}},
// {"text with image", "hello- {{http://www.blah.com/whatever?asdf}}, -world", []item{
// {itemText, 0, "hello- "},
// {itemImageLeftDelimiter, 0, "{{"},
// {itemImageLocation, 0, "http://www.blah.com/whatever?asdf"},
// {itemImageRightDelimiter, 0, "}}"},
// {itemText, 0, ", -world"},
// tEOF,
// }},
// {"text with image full src with alt", "hello- {{http://www.blah.com/whatever?asdf|blah whatever}}, -world", []item{
// {itemText, 0, "hello- "},
// {itemImageLeftDelimiter, 0, "{{"},
// {itemImageLocation, 0, "http://www.blah.com/whatever?asdf"},
// {itemImageDelimiter, 0, "|"},
// {itemImageText, 0, "blah whatever"},
// {itemImageRightDelimiter, 0, "}}"},
// {itemText, 0, ", -world"},
// tEOF,
// }},
{"no wiki", "hello- {{{ test ** blah ** test }}} -world", []item{
{itemText, 0, "hello- "},
{itemNoWikiOpen, 0, "{{{"},
{itemNoWikiText, 0, " test ** blah ** test "},
{itemNoWikiClose, 0, "}}}"},
{itemText, 0, " -world"},
tEOF,
}},
{"no wiki", "hello- {{{ test \n test }}} -world", []item{
{itemText, 0, "hello- "},
{itemNoWikiOpen, 0, "{{{"},
{itemNoWikiText, 0, " test \n test "},
{itemNoWikiClose, 0, "}}}"},
{itemText, 0, " -world"},
tEOF,
}},
}
// collect gathers the emitted items into a slice.
func collect(t *lexTest, left, right string) (items []item) {
l := lex(t.name, t.input)
for {
item := l.nextItem()
items = append(items, item)
if item.typ == itemEOF || item.typ == itemError {
break
}
}
return
}
func equal(i1, i2 []item, checkPos bool) bool {
if len(i1) != len(i2) {
return false
}
for k := range i1 {
if i1[k].typ != i2[k].typ {
return false
}
if i1[k].val != i2[k].val {
return false
}
if checkPos && i1[k].pos != i2[k].pos {
return false
}
}
return true
}
func TestLex(t *testing.T) {
for _, test := range lexTests {
items := collect(&test, "", "")
if !equal(items, test.items, false) {
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, items, test.items)
}
}
}
//// Some easy cases from above, but with delimiters $$ and @@
//var lexDelimTests = []lexTest{
// {"punctuation", "$$,@%{{}}@@", []item{
// tLeftDelim,
// {itemChar, 0, ","},
// {itemChar, 0, "@"},
// {itemChar, 0, "%"},
// {itemChar, 0, "{"},
// {itemChar, 0, "{"},
// {itemChar, 0, "}"},
// {itemChar, 0, "}"},
// tRightDelim,
// tEOF,
// }},
// {"empty action", `$$@@`, []item{tLeftDelim, tRightDelim, tEOF}},
// {"for", `$$for@@`, []item{tLeftDelim, tFor, tRightDelim, tEOF}},
// {"quote", `$$"abc \n\t\" "@@`, []item{tLeftDelim, tQuote, tRightDelim, tEOF}},
// {"raw quote", "$$" + raw + "@@", []item{tLeftDelim, tRawQuote, tRightDelim, tEOF}},
//}
//
//var (
// tLeftDelim = item{itemLeftDelim, 0, "$$"}
// tRightDelim = item{itemRightDelim, 0, "@@"}
//)
//
//func TestDelims(t *testing.T) {
// for _, test := range lexDelimTests {
// items := collect(&test, "$$", "@@")
// if !equal(items, test.items, false) {
// t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
// }
// }
//}
//
//var lexPosTests = []lexTest{
// {"empty", "", []item{tEOF}},
// {"punctuation", "{{,@%#}}", []item{
// {itemLeftDelim, 0, "{{"},
// {itemChar, 2, ","},
// {itemChar, 3, "@"},
// {itemChar, 4, "%"},
// {itemChar, 5, "#"},
// {itemRightDelim, 6, "}}"},
// {itemEOF, 8, ""},
// }},
// {"sample", "0123{{hello}}xyz", []item{
// {itemText, 0, "0123"},
// {itemLeftDelim, 4, "{{"},
// {itemIdentifier, 6, "hello"},
// {itemRightDelim, 11, "}}"},
// {itemText, 13, "xyz"},
// {itemEOF, 16, ""},
// }},
//}
//
//// The other tests don't check position, to make the test cases easier to construct.
//// This one does.
//func TestPos(t *testing.T) {
// for _, test := range lexPosTests {
// items := collect(&test, "", "")
// if !equal(items, test.items, true) {
// t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
// if len(items) == len(test.items) {
// // Detailed print; avoid item.String() to expose the position value.
// for i := range items {
// if !equal(items[i:i+1], test.items[i:i+1], true) {
// i1 := items[i]
// i2 := test.items[i]
// t.Errorf("\t#%d: got {%v %d %q} expected {%v %d %q}", i, i1.typ, i1.pos, i1.val, i2.typ, i2.pos, i2.val)
// }
// }
// }
// }
// }
//}