-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlex_test.go
116 lines (111 loc) · 3.02 KB
/
lex_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
package lexer
import (
"reflect"
"strings"
"testing"
)
func testLexer(t *testing.T, script string, tokens []Token) {
t.Run("case", func(t *testing.T) {
actualTokens, err := Lex(strings.NewReader(script), &Options{})
if err != nil {
if tokens == nil {
return
}
t.Error("Unexpected error:", err)
return
}
if tokens == nil {
t.Error("Unexpected success:", actualTokens)
return
}
if !reflect.DeepEqual(tokens, actualTokens) {
t.Log("Wrong lexer output:")
t.Logf("Actual: %#v", actualTokens)
t.Logf("Expected: %#v", tokens)
t.Fail()
}
})
}
func TestLex(t *testing.T) {
testLexer(t, ``, []Token{})
testLexer(t, `[]`, []Token{ListStart{Position: LineCol(1, 1)}, ListEnd{Position: LineCol(1, 2)}})
testLexer(t, `[ "hello1" , "hello2" ]`, []Token{
ListStart{Position: LineCol(1, 1)},
String{Text: "hello1", Position: LineCol(1, 3)},
Comma{Position: LineCol(1, 12)},
String{Text: "hello2", Position: LineCol(1, 14)},
ListEnd{LineCol(1, 23)},
})
testLexer(t, `"multi
line
string"`, []Token{String{Text: "multi\r\nline\r\nstring", Position: LineCol(1, 1)}})
testLexer(t, `" and so it goes... `, nil) // lexer error
testLexer(t, `[ "hello" ] id`, []Token{
ListStart{Position: LineCol(1, 1)},
String{Text: "hello", Position: LineCol(1, 3)},
ListEnd{Position: LineCol(1, 11)},
Identifier{Text: "id", Position: LineCol(1, 13)},
})
testLexer(t, `[ "hello" ]
/* also a comment
whatever # aaaa
"still a comment"
{}
*/
{ identifier :size 123K }`, []Token{
ListStart{Position: LineCol(1, 1)},
String{Text: "hello", Position: LineCol(1, 3)},
ListEnd{Position: LineCol(1, 11)},
BlockStart{Position: LineCol(7, 1)},
Identifier{Text: "identifier", Position: LineCol(7, 3)},
Colon{Position: LineCol(7, 14)},
Identifier{Text: "size", Position: LineCol(7, 15)},
Number{Value: 123, Quantifier: Kilo, Position: LineCol(7, 20)},
BlockEnd{Position: LineCol(7, 25)},
})
testLexer(t, `set "message" text:
From: [email protected]
Subject: Frop!
Frop!
.
`, []Token{
Identifier{Text: "set", Position: LineCol(1, 1)},
String{Text: "message", Position: LineCol(1, 5)},
String{Text: "From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Subject: Frop!\r\n" +
"\r\n" +
"Frop!\r\n", Position: LineCol(1, 15)},
})
testLexer(t, `set "message" text:
From: [email protected]
Subject: Frop!
..
Frop!
.
`, []Token{
Identifier{Text: "set", Position: LineCol(1, 1)},
String{Text: "message", Position: LineCol(1, 5)},
String{Text: "From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Subject: Frop!\r\n" +
"\r\n" +
".\r\n" +
"Frop!\r\n", Position: LineCol(1, 15)},
})
testLexer(t, `set "text" text: # Comment
Line 1
.Line 2
..Line 3
.Line 4
Line 5
.
;`, []Token{
Identifier{Text: "set", Position: LineCol(1, 1)},
String{Text: "text", Position: LineCol(1, 5)},
String{Text: "Line 1\r\n.Line 2\r\n.Line 3\r\n.Line 4\r\nLine 5\r\n", Position: LineCol(1, 12)},
Semicolon{Position: LineCol(8, 1)},
})
}