-
Notifications
You must be signed in to change notification settings - Fork 83
/
parser_test.go
148 lines (144 loc) · 2.6 KB
/
parser_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
package gval
import (
"testing"
"text/scanner"
"unicode"
)
func TestParser_Scan(t *testing.T) {
tests := []struct {
name string
input string
Language
do func(p *Parser)
wantScan rune
wantToken string
wantPanic bool
}{
{
name: "camouflage",
input: "$abc",
do: func(p *Parser) {
p.Scan()
p.Camouflage("test")
},
wantScan: '$',
wantToken: "$",
},
{
name: "camouflage with next",
input: "$abc",
do: func(p *Parser) {
p.Scan()
p.Camouflage("test")
p.Next()
},
wantPanic: true,
},
{
name: "camouflage scan camouflage",
input: "$abc",
do: func(p *Parser) {
p.Scan()
p.Camouflage("test")
p.Scan()
p.Camouflage("test2")
},
wantScan: '$',
wantToken: "$",
},
{
name: "camouflage with peek",
input: "$abc",
do: func(p *Parser) {
p.Scan()
p.Camouflage("test")
p.Peek()
},
wantPanic: true,
},
{
name: "next and peek",
input: "$#abc",
do: func(p *Parser) {
p.Scan()
p.Next()
p.Peek()
},
wantScan: scanner.Ident,
wantToken: "abc",
},
{
name: "scan token camouflage token",
input: "abc",
do: func(p *Parser) {
p.Scan()
p.TokenText()
p.Camouflage("test")
},
wantScan: scanner.Ident,
wantToken: "abc",
},
{
name: "scan token peek camouflage token",
input: "abc",
do: func(p *Parser) {
p.Scan()
p.TokenText()
p.Peek()
p.Camouflage("test")
},
wantScan: scanner.Ident,
wantToken: "abc",
},
{
name: "tokenize all whitespace",
input: "foo\tbar\nbaz",
do: func(p *Parser) {
p.SetWhitespace()
p.Scan()
},
wantScan: '\t',
wantToken: "\t",
},
{
name: "custom ident",
input: "$#foo",
do: func(p *Parser) {
p.SetIsIdentRuneFunc(func(ch rune, i int) bool { return unicode.IsLetter(ch) || ch == '#' })
p.Scan()
},
wantScan: scanner.Ident,
wantToken: "#foo",
},
{
name: "do not scan idents",
input: "abc",
do: func(p *Parser) {
p.SetMode(scanner.GoTokens ^ scanner.ScanIdents)
p.Scan()
},
wantScan: 'b',
wantToken: "b",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
err := recover()
if err != nil && !tt.wantPanic {
t.Fatalf("unexpected panic: %v", err)
}
}()
p := newParser(tt.input, tt.Language)
tt.do(p)
if tt.wantPanic {
return
}
scan := p.Scan()
token := p.TokenText()
if scan != tt.wantScan || token != tt.wantToken {
t.Errorf("Parser.Scan() = %v (%v), want %v (%v)", scan, token, tt.wantScan, tt.wantToken)
}
})
}
}