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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
package lexical
import (
"encoding/json"
"fmt"
"testing"
spec "urubu/spec/grammar"
)
func TestLexSpec_Validate(t *testing.T) {
// We expect that the spelling inconsistency error will occur.
spec := &LexSpec{
Entries: []*LexEntry{
{
Modes: []spec.LexModeName{
// 'Default' is the spelling inconsistency because 'default' is predefined.
"Default",
},
Kind: "foo",
Pattern: "foo",
},
},
}
err := spec.Validate()
if err == nil {
t.Fatalf("expected error didn't occur")
}
}
func TestSnakeCaseToUpperCamelCase(t *testing.T) {
tests := []struct {
snake string
camel string
}{
{
snake: "foo",
camel: "Foo",
},
{
snake: "foo_bar",
camel: "FooBar",
},
{
snake: "foo_bar_baz",
camel: "FooBarBaz",
},
{
snake: "Foo",
camel: "Foo",
},
{
snake: "fooBar",
camel: "FooBar",
},
{
snake: "FOO",
camel: "FOO",
},
{
snake: "FOO_BAR",
camel: "FOOBAR",
},
{
snake: "_foo_bar_",
camel: "FooBar",
},
{
snake: "___foo___bar___",
camel: "FooBar",
},
}
for _, tt := range tests {
c := SnakeCaseToUpperCamelCase(tt.snake)
if c != tt.camel {
t.Errorf("unexpected string; want: %v, got: %v", tt.camel, c)
}
}
}
func TestFindSpellingInconsistencies(t *testing.T) {
tests := []struct {
ids []string
duplicated [][]string
}{
{
ids: []string{"foo", "foo"},
duplicated: nil,
},
{
ids: []string{"foo", "Foo"},
duplicated: [][]string{{"Foo", "foo"}},
},
{
ids: []string{"foo", "foo", "Foo"},
duplicated: [][]string{{"Foo", "foo"}},
},
{
ids: []string{"foo_bar_baz", "FooBarBaz"},
duplicated: [][]string{{"FooBarBaz", "foo_bar_baz"}},
},
{
ids: []string{"foo", "Foo", "bar", "Bar"},
duplicated: [][]string{{"Bar", "bar"}, {"Foo", "foo"}},
},
{
ids: []string{"foo", "Foo", "bar", "Bar", "baz", "bra"},
duplicated: [][]string{{"Bar", "bar"}, {"Foo", "foo"}},
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("#%v", i), func(t *testing.T) {
duplicated := FindSpellingInconsistencies(tt.ids)
if len(duplicated) != len(tt.duplicated) {
t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated, duplicated)
}
for i, dupIDs := range duplicated {
if len(dupIDs) != len(tt.duplicated[i]) {
t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated[i], dupIDs)
}
for j, id := range dupIDs {
if id != tt.duplicated[i][j] {
t.Fatalf("unexpected IDs; want: %#v, got: %#v", tt.duplicated[i], dupIDs)
}
}
}
})
}
}
func TestCompile(t *testing.T) {
tests := []struct {
Caption string
Spec string
Err bool
}{
{
Caption: "allow duplicates names between fragments and non-fragments",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "a2z",
"pattern": "\\f{a2z}"
},
{
"fragment": true,
"kind": "a2z",
"pattern": "[a-z]"
}
]
}
`,
},
{
Caption: "don't allow duplicates names in non-fragments",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "a2z",
"pattern": "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
},
{
"kind": "a2z",
"pattern": "[a-z]"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow duplicates names in fragments",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "a2z",
"pattern": "\\f{a2z}"
},
{
"fragments": true,
"kind": "a2z",
"pattern": "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
},
{
"fragments": true,
"kind": "a2z",
"pattern": "[a-z]"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow kind names in the same mode to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "foo_1",
"pattern": "foo_1"
},
{
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow kind names across modes to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"modes": ["default"],
"kind": "foo_1",
"pattern": "foo_1"
},
{
"modes": ["other_mode"],
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow mode names to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"modes": ["foo_1"],
"kind": "a",
"pattern": "a"
},
{
"modes": ["foo1"],
"kind": "b",
"pattern": "b"
}
]
}
`,
Err: true,
},
{
Caption: "allow fragment names in the same mode to contain spelling inconsistencies because fragments will not appear in output files",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "a",
"pattern": "a"
},
{
"fragment": true,
"kind": "foo_1",
"pattern": "foo_1"
},
{
"fragment": true,
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
},
{
Caption: "allow fragment names across modes to contain spelling inconsistencies because fragments will not appear in output files",
Spec: `
{
"name": "test",
"entries": [
{
"modes": ["default"],
"kind": "a",
"pattern": "a"
},
{
"modes": ["default"],
"fragment": true,
"kind": "foo_1",
"pattern": "foo_1"
},
{
"modes": ["other_mode"],
"fragment": true,
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("#%v %s", i, tt.Caption), func(t *testing.T) {
lspec := &LexSpec{}
err := json.Unmarshal([]byte(tt.Spec), lspec)
if err != nil {
t.Fatalf("%v", err)
}
clspec, err, _ := Compile(lspec, CompressionLevelMin)
if tt.Err {
if err == nil {
t.Fatalf("expected an error")
}
if clspec != nil {
t.Fatalf("Compile function mustn't return a compiled specification")
}
} else {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if clspec == nil {
t.Fatalf("Compile function must return a compiled specification")
}
}
})
}
}
|