-
Notifications
You must be signed in to change notification settings - Fork 29
/
tokenizer_test.go
168 lines (143 loc) · 4.19 KB
/
tokenizer_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 toolbox_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
)
func TestNewTokenizer(t *testing.T) {
tokenizer := toolbox.NewTokenizer("Z Abcf",
0,
-1,
map[int]toolbox.Matcher{
101: toolbox.KeywordMatcher{"Abc", true},
201: toolbox.CharactersMatcher{Chars: " \n\t"},
102: toolbox.LiteralMatcher{},
},
)
assert.Equal(t, 102, tokenizer.Nexts(101, 201, 102).Token)
assert.Equal(t, 201, tokenizer.Nexts(101, 201, 102).Token)
assert.Equal(t, 101, tokenizer.Nexts(101, 201, 102).Token)
assert.Equal(t, 102, tokenizer.Nexts(101, 201, 102).Token)
}
func Test_NewCustomIdMatcher(t *testing.T) {
{
matcher := toolbox.NewCustomIdMatcher("$")
assert.Equal(t, 5, matcher.Match("Z $Abcf", 2))
assert.Equal(t, 1, matcher.Match("Z Abcf", 0))
assert.Equal(t, 0, matcher.Match("### ##", 0))
}
matcher := toolbox.NewCustomIdMatcher("_", "(", ")")
assert.Equal(t, 1, matcher.Match(" v_sc()", 6))
}
func Test_NewSequenceMatcher(t *testing.T) {
matcher := toolbox.NewSequenceMatcher("&&", "||")
assert.Equal(t, 2, matcher.Match("123", 1))
assert.Equal(t, 4, matcher.Match("123 && 123", 0))
}
func Test_NewSingleSequenceMatcher(t *testing.T) {
matcher := toolbox.NewSequenceMatcher("&")
assert.Equal(t, 0, matcher.Match("123", 1))
assert.Equal(t, 5, matcher.Match("12345&3", 0))
}
func TestMatchKeyword(t *testing.T) {
matcher := toolbox.KeywordMatcher{"Abc", true}
assert.Equal(t, 3, matcher.Match("Z Abcf", 2))
assert.Equal(t, 0, matcher.Match("Z Abcf", 0))
assert.Equal(t, 3, matcher.Match("Z Abc", 2))
}
func TestMatchWhitespace(t *testing.T) {
matcher := toolbox.CharactersMatcher{Chars: " \n\t"}
assert.Equal(t, 0, matcher.Match("1, 2, 3", 0))
assert.Equal(t, 2, matcher.Match("1, \t2, 3", 2))
}
func TestLiteralMatcher(t *testing.T) {
matcher := toolbox.LiteralMatcher{}
assert.Equal(t, 0, matcher.Match(" abc ", 0))
assert.Equal(t, 4, matcher.Match(" a1bc", 1))
}
func TestEOFMatcher(t *testing.T) {
matcher := toolbox.EOFMatcher{}
assert.Equal(t, 0, matcher.Match(" abc ", 0))
assert.Equal(t, 1, matcher.Match(" a1bc", 4))
}
func TestKeywordsMatcher(t *testing.T) {
{
matcher := toolbox.KeywordsMatcher{Keywords: []string{"ab", "xy"},
CaseSensitive: false}
assert.Equal(t, 2, matcher.Match(" abcde", 1))
assert.Equal(t, 0, matcher.Match(" abcde", 0))
}
{
matcher := toolbox.KeywordsMatcher{Keywords: []string{"AB", "xy"},
CaseSensitive: true}
assert.Equal(t, 2, matcher.Match(" ABcde", 1))
assert.Equal(t, 0, matcher.Match("abcde", 0))
}
}
func TestBodyMatcher(t *testing.T) {
{
matcher := toolbox.BodyMatcher{Begin: "{", End: "}"}
var text = " { { \n} } "
pos := matcher.Match(text, 1)
assert.Equal(t, 16, pos)
}
{
matcher := toolbox.BodyMatcher{Begin: "begin", End: "end"}
var text = " begin { \n} end "
pos := matcher.Match(text, 1)
assert.Equal(t, 20, pos)
}
}
func TestBlockMatcher(t *testing.T) {
{
matcher := toolbox.NewBlockMatcher(false, "begin", "end;", []string{"CASE"}, []string{"END IF"})
text := ` TRIGGER users_before_insert
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SELECT users_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
INSERT INTO DUMMY(ID, NAME) VALUES(2, 'xyz');
`
matcher.Match(text, 65)
}
{
matcher := toolbox.BlockMatcher{
CaseSensitive: false,
SequenceStart: "begin",
SequenceTerminator: "end",
NestedSequences: []string{"case"},
IgnoredTerminators: []string{"end if"},
}
text := "\n\n" +
"BEgin\n" +
"IF get_version()=20\n" +
"select *\n" +
"from table\n" +
"where color = case inventory when 1 then 'brown' when 2 then 'red' END;\n" +
"END IF\n" +
"END;"
pos := matcher.Match(text, 2)
assert.Equal(t, 128, pos)
}
{
matcher := toolbox.BlockMatcher{
CaseSensitive: false,
SequenceStart: "begin",
SequenceTerminator: "end;",
NestedSequences: []string{"case"},
}
text := "bEgIn case 123deabc then 22 End; End;"
pos := matcher.Match(text, 0)
assert.Equal(t, 37, pos)
matcher.CaseSensitive = true
pos = matcher.Match(text, 0)
assert.Equal(t, 0, pos)
matcher.SequenceTerminator = "End;"
matcher.SequenceStart = "bEgIn"
pos = matcher.Match(text, 0)
assert.Equal(t, 37, pos)
}
}