-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_test.go
88 lines (73 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
package sql_test
import (
"strings"
"testing"
"github.com/syst3mw0rm/sql-lexer-parser"
)
// Ensure the scanner can scan tokens correctly.
func TestSingleTokenScan(t *testing.T) {
var testCases = []struct {
s string
tok sql.Token
lit string
}{
// Special tokens (EOF, ILLEGAL, WS)
{s: ``, tok: sql.EOF, lit: ""},
{s: `#`, tok: sql.ILLEGAL, lit: `#`},
{s: ` `, tok: sql.WS, lit: " "},
{s: "\t", tok: sql.WS, lit: "\t"},
{s: "\n", tok: sql.WS, lit: "\n"},
// Misc characters
{s: `*`, tok: sql.ASTERISK, lit: "*"},
// Identifiers
{s: `foo`, tok: sql.IDENT, lit: `foo`},
{s: `Zx12_3U_-`, tok: sql.IDENT, lit: `Zx12_3U_`},
// Keywords
{s: `FROM`, tok: sql.FROM, lit: "FROM"},
{s: `SELECT`, tok: sql.SELECT, lit: "SELECT"},
}
for i, tt := range testCases {
s := sql.NewScanner(strings.NewReader(tt.s))
tok, lit := s.Scan()
if tt.tok != tok {
t.Errorf("%d. %q token mismatch: exp=%q got %q <%q>", i, tt.s, tt.tok, tok, lit)
} else if tt.lit != lit {
t.Errorf("%d. %q literal mismatch: exp=%q got=%q", i, tt.s, tt.lit, lit)
}
}
}
func TestMultiTokenScan(t *testing.T) {
var testCases = []struct {
s string
ans []struct {
tok sql.Token
lit string
}
}{
{s: `SELECT FROM`, ans: []struct {
tok sql.Token
lit string
}{
{tok: sql.SELECT, lit: "SELECT"},
{tok: sql.WS, lit: " "},
{tok: sql.FROM, lit: "FROM"},
},
},
}
for i, tt := range testCases {
s := sql.NewScanner(strings.NewReader(tt.s))
j := 0
for {
tok, lit := s.Scan()
if tok == sql.EOF {
break
}
if tok != tt.ans[j].tok {
t.Errorf("%d. %q token mismatch: exp=%q got %q <%q>", i, tt.s, tt.ans[j].tok, tok, lit)
} else if lit != tt.ans[j].lit {
t.Errorf("%d. %q literal mismatch: exp=%q got=%q", i, tt.s, tt.ans[j].lit, lit)
}
j++
}
}
}