-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer_test.go
94 lines (83 loc) · 1.79 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
package liquid
import (
"reflect"
"testing"
)
func checkLexerTokens(t *testing.T, raw string, want []Token) {
got, err := Lexer(raw)
if err != nil {
t.Error("Got an error", err)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Tokens did not match, want: %v, got: %v", want, got)
}
}
func TestStrings(t *testing.T) {
checkLexerTokens(t, ` 'this is a test""' "wat 'lol'"`, []Token{
{tSingleStringLiteral, `'this is a test""'`},
{tDoubleStringLiteral, `"wat 'lol'"`},
EndOfString,
})
}
func TestInteger(t *testing.T) {
checkLexerTokens(t, "hi 50", []Token{
{tIdentifier, "hi"},
{tNumberLiteral, "50"},
EndOfString,
})
}
func TestFloat(t *testing.T) {
checkLexerTokens(t, "hi 5.0", []Token{
{tIdentifier, "hi"},
{tNumberLiteral, "5.0"},
EndOfString,
})
}
func TestComparison(t *testing.T) {
checkLexerTokens(t, "== <> contains", []Token{
{tComparisonOperator, "=="},
{tComparisonOperator, "<>"},
{tComparisonOperator, "contains"},
EndOfString,
})
}
func TestSpecials(t *testing.T) {
checkLexerTokens(t, "| .:", []Token{
{tPipe, "|"},
{tDot, "."},
{tColon, ":"},
EndOfString,
})
checkLexerTokens(t, "[,]", []Token{
{tOpenSquare, "["},
{tComma, ","},
{tCloseSquare, "]"},
EndOfString,
})
}
func TestFancyIdentifiers(t *testing.T) {
checkLexerTokens(t, "hi five?", []Token{
{tIdentifier, "hi"},
{tIdentifier, "five?"},
EndOfString,
})
checkLexerTokens(t, "2foo", []Token{
{tNumberLiteral, "2"},
{tIdentifier, "foo"},
EndOfString,
})
}
func TestWhitespace(t *testing.T) {
checkLexerTokens(t, "five|\n\t ==", []Token{
{tIdentifier, "five"},
{tPipe, "|"},
{tComparisonOperator, "=="},
EndOfString,
})
}
func TestUnexpectedCharacter(t *testing.T) {
if _, err := Lexer("%"); err == nil {
t.Error(`Should raise an error for '%'`)
}
}