forked from PaesslerAG/gval
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
117 lines (113 loc) · 1.98 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
package gval
import (
"testing"
"text/scanner"
)
func TestParser_Scan(t *testing.T) {
tests := []struct {
name string
input string
Language
do func(p *Parser)
wantScan rune
wantToken string
wanPanic 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()
},
wanPanic: 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()
},
wanPanic: 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",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
err := recover()
if err != nil && !tt.wanPanic {
t.Fatalf("unexpected panic: %v", err)
}
}()
p := newParser(tt.input, tt.Language)
tt.do(p)
if tt.wanPanic {
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)
}
})
}
}