-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer_test.go
168 lines (154 loc) · 4.14 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
package sqlparse
import (
"fmt"
"testing"
)
func tokseq(expecting, got []Token) bool {
if len(got) != len(expecting) {
return false
}
for i := range got {
if !got[i].eq(expecting[i]) {
return false
}
}
return true
}
func compmsg(msg string, expecting, got []Token) string {
return fmt.Sprintf(`%s
expecting: %s
got: %s`, msg, expecting, got)
}
func TestLexer_SkipSpaces(t *testing.T) {
toks := lex(`
`)
if len(toks) != 0 {
t.Error("expected spaces to be skipped")
}
}
func TestLexer_Symbols(t *testing.T) {
got := lex(`(),. < > = <= >= + - * /`)
expecting := []Token{
tokenOpenParenthesis,
tokenCloseParenthesis,
tokenComma,
tokenPeriod,
tokenLt,
tokenGt,
tokenEq,
tokenLe,
tokenGe,
tokenPlus,
tokenMinus,
tokenMul,
tokenDiv,
}
if !tokseq(expecting, got) {
t.Errorf(compmsg("lexing symbols did not return expected tokens.",
expecting, got))
}
}
func TestLexer_Identifiers(t *testing.T) {
got := lex(`one two_three four__ __five six6`)
expecting := []Token{
{Tok: tokIdentifier, Lexeme: "one"},
{Tok: tokIdentifier, Lexeme: "two_three"},
{Tok: tokIdentifier, Lexeme: "four__"},
{Tok: tokIdentifier, Lexeme: "__five"},
{Tok: tokIdentifier, Lexeme: "six6"},
}
if !tokseq(expecting, got) {
t.Errorf(compmsg("lexing identifiers did not return expected tokens.",
expecting, got))
}
}
func TestLexer_Numbers(t *testing.T) {
got := lex(`1 23 4 5`)
expecting := []Token{
{Tok: tokNumber, Lexeme: "1"},
{Tok: tokNumber, Lexeme: "23"},
{Tok: tokNumber, Lexeme: "4"},
{Tok: tokNumber, Lexeme: "5"},
}
if !tokseq(expecting, got) {
t.Errorf(compmsg("lexing numbers did not return expected tokens.",
expecting, got))
}
}
func TestLexer_QuotedValues(t *testing.T) {
got := lex(`'' 'one' 'two three' "four" "" "five six"`)
expecting := []Token{
{Tok: tokSingleQuoteString, Lexeme: ""},
{Tok: tokSingleQuoteString, Lexeme: "one"},
{Tok: tokSingleQuoteString, Lexeme: "two three"},
{Tok: tokDoubleQuoteString, Lexeme: "four"},
{Tok: tokDoubleQuoteString, Lexeme: ""},
{Tok: tokDoubleQuoteString, Lexeme: "five six"},
}
if !tokseq(expecting, got) {
t.Errorf(compmsg("lexing quoted values did not return expected tokens.",
expecting, got))
}
}
func TestLexer_SampleQuery(t *testing.T) {
got := lex(`
select max(w.weight) as max_weight,
max(min(w.weight)) as min_max_weight,
distinct w.exercise,
w.weight
from workouts as w
where w.exercise like 'bench press' or w.exercise like 'bicep curl'
`)
expecting := []Token{
{Tok: tokIdentifier, Lexeme: "select"},
{Tok: tokIdentifier, Lexeme: "max"},
tokenOpenParenthesis,
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "weight"},
tokenCloseParenthesis,
{Tok: tokIdentifier, Lexeme: "as"},
{Tok: tokIdentifier, Lexeme: "max_weight"},
tokenComma,
{Tok: tokIdentifier, Lexeme: "max"},
tokenOpenParenthesis,
{Tok: tokIdentifier, Lexeme: "min"},
tokenOpenParenthesis,
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "weight"},
tokenCloseParenthesis,
tokenCloseParenthesis,
{Tok: tokIdentifier, Lexeme: "as"},
{Tok: tokIdentifier, Lexeme: "min_max_weight"},
tokenComma,
{Tok: tokIdentifier, Lexeme: "distinct"},
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "exercise"},
tokenComma,
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "weight"},
{Tok: tokIdentifier, Lexeme: "from"},
{Tok: tokIdentifier, Lexeme: "workouts"},
{Tok: tokIdentifier, Lexeme: "as"},
{Tok: tokIdentifier, Lexeme: "w"},
{Tok: tokIdentifier, Lexeme: "where"},
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "exercise"},
{Tok: tokIdentifier, Lexeme: "like"},
{Tok: tokSingleQuoteString, Lexeme: "bench press"},
{Tok: tokIdentifier, Lexeme: "or"},
{Tok: tokIdentifier, Lexeme: "w"},
tokenPeriod,
{Tok: tokIdentifier, Lexeme: "exercise"},
{Tok: tokIdentifier, Lexeme: "like"},
{Tok: tokSingleQuoteString, Lexeme: "bicep curl"},
}
if !tokseq(expecting, got) {
t.Errorf(compmsg("lexing sample query did not return expected tokens.",
expecting, got))
}
}