-
Notifications
You must be signed in to change notification settings - Fork 0
/
toknizer_test.go
189 lines (180 loc) · 3.21 KB
/
toknizer_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package reggenerator
import "testing"
func TestTokenize(t *testing.T) {
isEqualtokens := func(t *testing.T, expected, actual []*token) {
if len(expected) != len(actual) {
t.Fatalf("token len not equal: expected %d, got %d", len(expected), len(actual))
}
for i := range expected {
if len(expected[i].charRange) != len(actual[i].charRange) {
t.Fatalf("char range is not equal, expected %d, got %d", len(expected[i].charRange), len(actual[i].charRange))
}
for j := range expected[i].charRange {
if expected[i].charRange[j] != actual[i].charRange[j] {
t.Fatalf("char should be '%c' got '%c'", expected[i].charRange[j], actual[i].charRange[j])
}
}
if expected[i].quantifier != actual[i].quantifier {
t.Fatalf("quantifier should be '%s', got '%s'", expected[i].quantifier, actual[i].quantifier)
}
if expected[i].anyCharacter != actual[i].anyCharacter {
t.Fatalf("any characher doesn't match")
}
}
}
tests := []struct {
name string
str string
expected []*token
shouldErr bool
}{
{
name: "empty string",
str: "",
shouldErr: true,
},
{
name: "missing slash before",
str: "fds/",
shouldErr: true,
},
{
name: "missing slash after",
str: "fds/",
shouldErr: true,
},
{
name: "invalid regex",
str: "/[fds/",
shouldErr: true,
},
{
name: "invalid regex 2",
str: "/[fds]{/",
shouldErr: true,
},
{
name: "invalid regex 3",
str: "/[f-]{/",
shouldErr: true,
},
{
name: "single char",
str: "/f/",
expected: []*token{
{
charRange: "f",
},
},
},
{
name: "only characters",
str: "/f[.]/",
expected: []*token{
{
charRange: "f",
},
{
charRange: ".",
},
},
},
{
name: "only char special chars",
str: "/-[?.],!/",
expected: []*token{
{
charRange: "-",
},
{
charRange: "?.",
},
{
charRange: ",",
},
{
charRange: "!",
},
},
},
{
name: "char range",
str: "/[a-b]/",
expected: []*token{
{
charRange: "a-b",
},
},
},
{
name: "char range 2",
str: "/[a-bA-Z]/",
expected: []*token{
{
charRange: "a-bA-Z",
},
},
},
{
name: "char range with quantifier",
str: "/[a-bA-Z]{1,3}/",
expected: []*token{
{
charRange: "a-bA-Z",
quantifier: "1,3",
},
},
},
{
name: "char range with optional",
str: "/[ab]?/",
expected: []*token{
{
charRange: "ab",
quantifier: "?",
},
},
},
{
name: "negated chars",
str: "/[^ab]?/",
expected: []*token{
{
charRange: "^ab",
quantifier: "?",
},
},
},
{
name: "dot",
str: "/[.]/",
expected: []*token{
{
charRange: ".",
},
},
},
{
name: "any char",
str: "/./",
expected: []*token{
{
anyCharacter: true,
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tokens, err := tokenize(test.str)
if err != nil {
if test.shouldErr {
return
}
t.Errorf("unexpected error: %s", err)
return
}
isEqualtokens(t, test.expected, tokens)
})
}
}