-
Notifications
You must be signed in to change notification settings - Fork 1
/
syntax_test.go
500 lines (438 loc) · 24.5 KB
/
syntax_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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package gogp
import (
"bytes"
"fmt"
"regexp"
"strings"
"testing"
)
const (
testPrintResult = false
)
func TestAllRegexpSyntax(t *testing.T) {
expAll := compileMultiRegexps(allSyntax...)
groups := expAll.SubexpNames()
if testPrintResult {
fmt.Printf("%#v\n", groups)
}
groups[0] = "0"
if matched := expAll.MatchString(tstExpSyntaxAll); !matched {
t.Errorf("test case not match")
return
}
var submatchesExpected = [][]string{
[]string{"match1", "0:\n\n\n", "EMPTY_LINE:\n\n\n"},
[]string{"match2", "0:// #GOGP_COMMENT", "COMMENT:// #GOGP_COMMENT"},
[]string{"match3", "0://#GOGP_COMMENT", "COMMENT://#GOGP_COMMENT"},
[]string{"match4", "0://#GOGP_IGNORE_BEGIN\n\t{ignore}\n//#GOGP_IGNORE_END\n\n", "IGNORE:\n\t{ignore}\n"},
[]string{"match5", "0:// #GOGP_REPLACE(<src>, <dst>)", "REPSRC:<src>", "REPDST:<dst>"},
[]string{"match6", "0://#GOGP_REPLACE(<src2>, <dst2>)", "REPSRC:<src2>", "REPDST:<dst2>"},
[]string{"match7", "0:// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content}\n// #GOGP_ELSE\n\t{if-else content}\n// #GOGP_ENDIF\n", "IFCOND,1,2:<key>", "IFCOND,2,1:!", "IFCOND,2,2:<key>", "IFCOND,3,2:<key>", "IFCOND,3,3:==", "IFCOND,3,4:xxx", "IFCOND,4,2:<key>", "IFCOND,4,3:!=", "IFCOND,4,4:xxx", "IFT:\t{if-true content}\n", "IFF:\t{if-else content}\n"},
[]string{"match8", "0:// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content2}\n// #GOGP_ENDIF\n", "IFCOND,1,2:<key>", "IFCOND,2,1:!", "IFCOND,2,2:<key>", "IFCOND,3,2:<key>", "IFCOND,3,3:==", "IFCOND,3,4:xxx", "IFCOND,4,2:<key>", "IFCOND,4,3:!=", "IFCOND,4,4:xxx", "IFT:\t{if-true content2}\n"},
[]string{"match9", "0://#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content}\n//#GOGP_ELSE\n\t{if-else content}\n//#GOGP_ENDIF\n", "IFCOND,1,2:<key>", "IFCOND,2,1:!", "IFCOND,2,2:<key>", "IFCOND,3,2:<key>", "IFCOND,3,3:==", "IFCOND,3,4:xxx", "IFCOND,4,2:<key>", "IFCOND,4,3:!=", "IFCOND,4,4:xxx", "IFT:\t{if-true content}\n", "IFF:\t{if-else content}\n"},
[]string{"match10", "0://#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content2}\n//#GOGP_ENDIF\n", "IFCOND,1,2:<key>", "IFCOND,2,1:!", "IFCOND,2,2:<key>", "IFCOND,3,2:<key>", "IFCOND,3,3:==", "IFCOND,3,4:xxx", "IFCOND,4,2:<key>", "IFCOND,4,3:!=", "IFCOND,4,4:xxx", "IFT:\t{if-true content2}\n"},
[]string{"match11", "0:// #GOGP_IFDEF x\n// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n// #GOGP_ELSE\n// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n// #GOGP_ENDIF\n", "IFCOND,1,2:x", "IFT:// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n", "IFF:// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n"},
[]string{"match12", "0:// #GOGP_IFDEF2 xx\n// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE //\n\t {if-else content}\n// #GOGP_ENDIF //\n// #GOGP_ELSE2\n// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE\n\t {if-else content}\n// #GOGP_ENDIF //\n// #GOGP_ENDIF2\n", "IFCOND2,1,2:xx", "IFT2:// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE //\n\t {if-else content}\n// #GOGP_ENDIF //\n", "IFF2:// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE\n\t {if-else content}\n// #GOGP_ENDIF //\n"},
[]string{"match13", "0:// #GOGP_SWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n// #GOGP_ENDSWITCH\n", "SWITCHKEY:<SwitchKey>", "SWITCHCONTENT,1:<SwitchKeyValue1>", "SWITCHCONTENT,2: {case content1}\n", "SWITCHCONTENT,1:<SwitchKeyValue2>", "SWITCHCONTENT,2: {case content2}\n", "SWITCHCONTENT,2: {default content1}\n"},
[]string{"match14", "0:// #GOGP_SWITCH\n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n// #GOGP_ENDSWITCH\n", "SWITCHCONTENT,1:<key>", "SWITCHCONTENT,2: {case content3}\n", "SWITCHCONTENT,1:<key>", "SWITCHCONTENT,2: {case content4}\n", "SWITCHCONTENT,2: {default content2}\n"},
[]string{"match15", "0://#GOGP_SWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n//#GOGP_ENDSWITCH\n", "SWITCHKEY:<SwitchKey>", "SWITCHCONTENT,1:<SwitchKeyValue1>", "SWITCHCONTENT,2: {case content1}\n", "SWITCHCONTENT,1:<SwitchKeyValue2>", "SWITCHCONTENT,2: {case content2}\n", "SWITCHCONTENT,2: {default content1}\n"},
[]string{"match16", "0://#GOGP_SWITCH \n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n//#GOGP_ENDSWITCH\n", "SWITCHCONTENT,1:<key>", "SWITCHCONTENT,2: {case content3}\n", "SWITCHCONTENT,1:<key>", "SWITCHCONTENT,2: {case content4}\n", "SWITCHCONTENT,2: {default content2}\n"},
[]string{"match17", "0:// #GOGP_MULTISWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n// #GOGP_ENDMULTISWITCH\n", "MULTISWITCHKEY:<SwitchKey>", "MULTISWITCHCONTENT,1:<SwitchKeyValue1>", "MULTISWITCHCONTENT,2: {case content1}\n", "MULTISWITCHCONTENT,1:<SwitchKeyValue2>", "MULTISWITCHCONTENT,2: {case content2}\n", "MULTISWITCHCONTENT,2: {default content1}\n"},
[]string{"match18", "0:// #GOGP_MULTISWITCH\n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n// #GOGP_ENDMULTISWITCH\n", "MULTISWITCHCONTENT,1:<key>", "MULTISWITCHCONTENT,2: {case content3}\n", "MULTISWITCHCONTENT,1:<key>", "MULTISWITCHCONTENT,2: {case content4}\n", "MULTISWITCHCONTENT,2: {default content2}\n"},
[]string{"match19", "0://#GOGP_MULTISWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n//#GOGP_ENDMULTISWITCH\n", "MULTISWITCHKEY:<SwitchKey>", "MULTISWITCHCONTENT,1:<SwitchKeyValue1>", "MULTISWITCHCONTENT,2: {case content1}\n", "MULTISWITCHCONTENT,1:<SwitchKeyValue2>", "MULTISWITCHCONTENT,2: {case content2}\n", "MULTISWITCHCONTENT,2: {default content1}\n"},
[]string{"match20", "0://#GOGP_MULTISWITCH \n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n//#GOGP_ENDMULTISWITCH\n", "MULTISWITCHCONTENT,1:<key>", "MULTISWITCHCONTENT,2: {case content3}\n", "MULTISWITCHCONTENT,1:<key>", "MULTISWITCHCONTENT,2: {case content4}\n", "MULTISWITCHCONTENT,2: {default content2}\n"},
[]string{"match21", "0:// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n", "CASEKEY,1,2:<key>", "CASECONTENT: {case content3}\n"},
[]string{"match22", "0:// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n", "CASEKEY,1,2:<key>", "CASECONTENT: {case content4}\n"},
[]string{"match23", "0:// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n\n", "CASECONTENT: {default content2}\n"},
[]string{"match24", "0://#GOGP_CASE <key>\n {case content3}\n//#GOGP_ENDCASE\n", "CASEKEY,1,2:<key>", "CASECONTENT: {case content3}\n"},
[]string{"match25", "0://#GOGP_CASE <key> != val\n {case content4}\n//#GOGP_ENDCASE\n", "CASEKEY,1,2:<key>", "CASECONTENT: {case content4}\n"},
[]string{"match26", "0://#GOGP_DEFAULT\n {default content2}\n//#GOGP_ENDCASE\n\n", "CASECONTENT: {default content2}\n"},
[]string{"match27", "0:// #GOGP_REQUIRE(<gp-path> , gpgSection)\n\n", "REQ:// #GOGP_REQUIRE(<gp-path> , gpgSection)", "REQP:<gp-path>", "REQN:gpgSection"},
[]string{"match28", "0:#GOGP_GPGCFG(<config-name>)", "GPGCFG:<config-name>"},
[]string{"match29", "0:// #GOGP_REPLACE(<src>, <dst>)", "REPSRC:<src>", "REPDST:<dst>"},
[]string{"match30", "0:// #GOGP_MAP(<src>, <dst>)", "MAPSRC:<src>", "MAPDST:<dst>"},
[]string{"match31", "0:// #GOGP_IGNORE_BEGIN \n {ignore content} \n// #GOGP_IGNORE_END\n\n", "IGNORE: \n {ignore content} \n// "},
[]string{"match32", "0:// #GOGP_GPONLY_BEGIN \n {gp-only content} \n// #GOGP_GPONLY_END\n\n", "GPONLY: \n {gp-only content} \n// "},
[]string{"match33", "0:// #GOGP_FILE_BEGIN\n\n", "FILEB:// #GOGP_FILE_BEGIN"},
[]string{"match34", "0:// #GOGP_FILE_END\n\n", "FILEE:// #GOGP_FILE_END"},
[]string{"match35", "0:// #GOGP_ONCE \n {only generate once from a gp file} \n// #GOGP_END_ONCE \n", "ONCE: \n {only generate once from a gp file} "},
[]string{"match36", "0:\n\n\n\n\n\n\n", "EMPTY_LINE:\n\n\n\n\n\n\n"},
[]string{"match37", "0:\n\n\n", "EMPTY_LINE:\n\n\n"},
}
var submatches [][]string
rep := expAll.ReplaceAllStringFunc(tstExpSyntaxAll, func(src string) string {
if testPrintResult {
fmt.Println("-----------------------------")
}
elem := expAll.FindAllStringSubmatch(src, -1)[0]
subs := make([]string, 0, 5)
subs = append(subs, fmt.Sprintf("match%d", len(submatches)+1))
for i, v := range groups {
if elem[i] != "" {
if testPrintResult {
fmt.Printf("%d %s-------\n%s\n", i, v, elem[i])
}
switch v {
case "CASEKEY", "IFCOND", "IFCOND2":
ss := strings.Split(elem[i], "||")
for j, vv := range ss {
c := gogpExpCondition.FindAllStringSubmatch(vv, -1)[0]
for k, v := range c {
if testPrintResult {
fmt.Println(j, k, v)
}
if v != "" && i > 0 && k > 0 {
subs = append(subs, fmt.Sprintf("%s,%d,%d:%s", groups[i], j+1, k, v))
}
}
}
case "SWITCHCONTENT", "MULTISWITCHCONTENT":
rep := gogpExpCases.ReplaceAllStringFunc(elem[i], func(src string) string {
cases := gogpExpCases.FindAllStringSubmatch(src, -1)[0]
if testPrintResult {
fmt.Printf("case: %#v\n", cases)
}
for j, v := range cases {
if v != "" && i > 0 && j > 0 {
subs = append(subs, fmt.Sprintf("%s,%d:%s", groups[i], j, v))
}
}
return ""
})
if testPrintResult {
fmt.Println(rep)
}
default:
subs = append(subs, fmt.Sprintf("%s:%s", groups[i], elem[i]))
}
}
}
submatches = append(submatches, subs)
return ""
})
if err := testCheckMatches(submatches, submatchesExpected); err != nil {
if !testPrintResult {
fmt.Println(testShowStringList(submatches))
}
t.Error(err)
}
if testPrintResult {
fmt.Println("replaced:----------------------------------------", rep)
fmt.Println(testShowStringList(submatches))
testShowAllSyntax()
}
}
func TestMultiRegexp(t *testing.T) {
var subNamesExpected = [][]string{
[]string{"", "IGNORE", "GPONLY", "MAPSRC", "MAPDST", "SWITCHKEY", "SWITCHCONTENT", "MULTISWITCHKEY", "MULTISWITCHCONTENT", "IFCOND", "IFT", "IFF", "IFCOND2", "IFT2", "IFF2"},
[]string{"", "IGNORE", "REQ", "REQP", "REQN", "REQGPG", "REQCONTENT", "GPGCFG", "ONCE", "REPSRC", "REPDST", "COMMENT"},
[]string{"", "REQ", "REQP", "REQN", "REQGPG", "REQCONTENT", "FILEB", "OPEN", "FILEE"},
[]string{"", "FILEB", "OPEN", "FILEE", "IGNORE"},
}
var subNames = [][]string{
gogpExpCodeSelector.SubexpNames(),
gogpExpPretreatAll.SubexpNames(),
gogpExpRequireAll.SubexpNames(),
gogpExpReverseIgnoreAll.SubexpNames(),
}
if err := testCheckMatches(subNames, subNamesExpected); err != nil {
t.Error(err)
}
if testPrintResult {
for i, v := range subNames {
fmt.Printf("expr-%d: %#v\n", i+1, v)
}
}
}
func TestSingleRegexps(t *testing.T) {
type testCase struct {
expect []string
syntax *regexp.Regexp
name string
}
var testCases = []*testCase{
&testCase{
expect: []string{"<src>", "<dst>", "<src2>", "<dst2>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<SwitchKey>", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "<key>", "<key>", "<SwitchKey>", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "<key>", "<key>", "<SwitchKey>", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "<key>", "<key>", "<SwitchKey>", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "<key>", "<key>", "<key>", "<key>", "<key>", "<key>", "<src>", "<dst>", "<src>", "<dst>"},
syntax: gogpExpTodoReplace,
name: "gogpExpTodoReplace",
},
&testCase{
expect: []string{"\n\t{ignore}\n", " \n {ignore content} \n// "},
syntax: gogpExpIgnore,
name: "gogpExpIgnore",
},
&testCase{
expect: []string{"<SwitchKeyValue1>", "<SwitchKeyValue2>", "", "<key>", "<key>", "", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "", "<key>", "<key>", "", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "", "<key>", "<key>", "", "<SwitchKeyValue1>", "<SwitchKeyValue2>", "", "<key>", "<key>", "", "<key>", "<key>", "", "<key>", "<key>", ""},
syntax: gogpExpCases,
name: "gogpExpCases",
},
&testCase{
expect: []string{"\n\n\n", "\n\n\n\n\n\n\n", "\n\n\n"},
syntax: gogpExpEmptyLine,
name: "gogpExpEmptyLine",
},
&testCase{
expect: []string{"head\n// #GOGP_COMMENT {comment code}\n//#GOGP_COMMENT {comment code2}\n\n//#GOGP_IGNORE_BEGIN\n\t{ignore}\n//#GOGP_IGNORE_END\n\n// #GOGP_REPLACE(<src>, <dst>)\n//#GOGP_REPLACE(<src2>, <dst2>)\n\n--------------------------------------\n\n// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content}\n// #GOGP_ELSE\n\t{if-else content}\n// #GOGP_ENDIF\n\n// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content2}\n// #GOGP_ENDIF\n\n//#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content}\n//#GOGP_ELSE\n\t{if-else content}\n//#GOGP_ENDIF\n\n//#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx\n\t{if-true content2}\n//#GOGP_ENDIF\n\n// #GOGP_IFDEF x\n// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n// #GOGP_ELSE\n// #GOGP_IFDEF2 yyy\n\t{if-true content}\n// #GOGP_ELSE2\n\t{if-else content}\n// #GOGP_ENDIF2\n// #GOGP_ENDIF\n\n// #GOGP_IFDEF2 xx\n// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE //\n\t {if-else content}\n// #GOGP_ENDIF //\n// #GOGP_ELSE2\n// #GOGP_IFDEF yyy\n\t {if-true content}\n// #GOGP_ELSE\n\t {if-else content}\n// #GOGP_ENDIF //\n// #GOGP_ENDIF2\n\n--------------------------------------\n\n// #GOGP_SWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n// #GOGP_ENDSWITCH\n\n// #GOGP_SWITCH\n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n// #GOGP_ENDSWITCH\n\n//#GOGP_SWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n//#GOGP_ENDSWITCH\n\n//#GOGP_SWITCH \n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n//#GOGP_ENDSWITCH\n\n--------------------------------------\n\n// #GOGP_MULTISWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n// #GOGP_ENDMULTISWITCH\n\n// #GOGP_MULTISWITCH\n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n// #GOGP_ENDMULTISWITCH\n\n//#GOGP_MULTISWITCH <SwitchKey>\n// #GOGP_CASE <SwitchKeyValue1>\n {case content1}\n// #GOGP_ENDCASE\n// #GOGP_CASE <SwitchKeyValue2>\n {case content2}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content1}\n// #GOGP_ENDCASE\n//#GOGP_ENDMULTISWITCH\n\n//#GOGP_MULTISWITCH \n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n//#GOGP_ENDMULTISWITCH\n\n// #GOGP_CASE <key>\n {case content3}\n// #GOGP_ENDCASE\n// #GOGP_CASE <key> != val\n {case content4}\n// #GOGP_ENDCASE\n// #GOGP_DEFAULT\n {default content2}\n// #GOGP_ENDCASE\n\n--------------------------------------\n\n//#GOGP_CASE <key>\n {case content3}\n//#GOGP_ENDCASE\n//#GOGP_CASE <key> != val\n {case content4}\n//#GOGP_ENDCASE\n//#GOGP_DEFAULT\n {default content2}\n//#GOGP_ENDCASE\n\n// #GOGP_REQUIRE(<gp-path> , gpgSection)\n\n--------------------------------------\n\n#GOGP_GPGCFG(<config-name>)\n\n// #GOGP_REPLACE(<src>, <dst>)\n\n// #GOGP_MAP(<src>, <dst>)\n\n// #GOGP_IGNORE_BEGIN \n {ignore content} \n// #GOGP_IGNORE_END\n\n// #GOGP_GPONLY_BEGIN \n {gp-only content} \n// #GOGP_GPONLY_END\n\n// #GOGP_FILE_BEGIN\n\n// #GOGP_FILE_END\n\n// #GOGP_ONCE \n {only generate once from a gp file} \n// #GOGP_END_ONCE \n\n---\n\n\n\n\n\n\ntail"},
syntax: gogpExpTrimEmptyLine,
name: "gogpExpTrimEmptyLine",
},
&testCase{
expect: []string{"// #GOGP_REQUIRE(<gp-path> , gpgSection)"},
syntax: gogpExpRequire,
name: "gogpExpRequire",
},
&testCase{
expect: []string{"", ""},
syntax: gogpExpCondition,
name: "gogpExpCondition",
},
&testCase{
expect: []string{"// #GOGP_COMMENT", "//#GOGP_COMMENT"},
syntax: gogpExpComment,
name: "gogpExpComment",
},
}
for i, v := range testCases {
match := v.syntax.FindAllStringSubmatch(tstExpSyntaxAll, -1)
got := testShowMatch(match)
if err := testCheckStrings(got, v.expect); err != nil {
fmt.Printf("%#v\n", testShowMatch(match))
t.Errorf("%d %s test error: %s", i+1, v.name, err)
return
}
}
}
func testShowMatch(match [][]string) []string {
ret := make([]string, 0, len(match))
for _, v := range match {
ret = append(ret, v[1])
}
return ret
}
func testShowAllSyntax() {
if testPrintResult {
for i, v := range allSyntax {
fmt.Printf("- **%02d/%d %s**<br>\n", i+1, len(allSyntax), v.name)
fmt.Printf(" {%s}\n", v.usage)
fmt.Printf("```go%s```\n", v.syntax)
}
}
}
func testCheckStrings(got, expect []string) error {
w := expect
if len(got) != len(w) {
return fmt.Errorf("regexp submatch-num mismatch, expect %d, got %d", len(w), len(got))
}
for j, vv := range got {
if vv != w[j] {
return fmt.Errorf(`regexp match %d submatch mismatch, expect "%s", got "%s"`, j+1, w[j], vv)
}
}
return nil
}
func testCheckMatches(got, expect [][]string) error {
if len(got) != len(expect) {
return fmt.Errorf("regexp num mismatch, expect %d, got %d", len(expect), len(got))
}
for i, v := range got {
w := expect[i]
if len(v) != len(w) {
return fmt.Errorf("regexp %d submatch-num mismatch, expect %d, got %d", i+1, len(w), len(v))
}
for j, vv := range v {
if vv != w[j] {
return fmt.Errorf(`regexp %d,%d submatch mismatch, expect "%s", got "%s"`, i+1, j+1, w[j], vv)
}
}
}
return nil
}
func testShowStringList(ss [][]string) string {
var b bytes.Buffer
b.WriteString("[][]string{\n")
for _, v := range ss {
b.WriteString(fmt.Sprintf(" %#v,\n", v))
}
b.WriteString("}")
return b.String()
}
const tstExpSyntaxAll = `
head
// #GOGP_COMMENT {comment code}
//#GOGP_COMMENT {comment code2}
//#GOGP_IGNORE_BEGIN
{ignore}
//#GOGP_IGNORE_END
// #GOGP_REPLACE(<src>, <dst>)
//#GOGP_REPLACE(<src2>, <dst2>)
--------------------------------------
// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx
{if-true content}
// #GOGP_ELSE
{if-else content}
// #GOGP_ENDIF
// #GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx
{if-true content2}
// #GOGP_ENDIF
//#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx
{if-true content}
//#GOGP_ELSE
{if-else content}
//#GOGP_ENDIF
//#GOGP_IFDEF <key> || ! <key> || <key> == xxx || <key> != xxx
{if-true content2}
//#GOGP_ENDIF
// #GOGP_IFDEF x
// #GOGP_IFDEF2 yyy
{if-true content}
// #GOGP_ELSE2
{if-else content}
// #GOGP_ENDIF2
// #GOGP_ELSE
// #GOGP_IFDEF2 yyy
{if-true content}
// #GOGP_ELSE2
{if-else content}
// #GOGP_ENDIF2
// #GOGP_ENDIF
// #GOGP_IFDEF2 xx
// #GOGP_IFDEF yyy
{if-true content}
// #GOGP_ELSE //
{if-else content}
// #GOGP_ENDIF //
// #GOGP_ELSE2
// #GOGP_IFDEF yyy
{if-true content}
// #GOGP_ELSE
{if-else content}
// #GOGP_ENDIF //
// #GOGP_ENDIF2
--------------------------------------
// #GOGP_SWITCH <SwitchKey>
// #GOGP_CASE <SwitchKeyValue1>
{case content1}
// #GOGP_ENDCASE
// #GOGP_CASE <SwitchKeyValue2>
{case content2}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content1}
// #GOGP_ENDCASE
// #GOGP_ENDSWITCH
// #GOGP_SWITCH
// #GOGP_CASE <key>
{case content3}
// #GOGP_ENDCASE
// #GOGP_CASE <key> != val
{case content4}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content2}
// #GOGP_ENDCASE
// #GOGP_ENDSWITCH
//#GOGP_SWITCH <SwitchKey>
// #GOGP_CASE <SwitchKeyValue1>
{case content1}
// #GOGP_ENDCASE
// #GOGP_CASE <SwitchKeyValue2>
{case content2}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content1}
// #GOGP_ENDCASE
//#GOGP_ENDSWITCH
//#GOGP_SWITCH
// #GOGP_CASE <key>
{case content3}
// #GOGP_ENDCASE
// #GOGP_CASE <key> != val
{case content4}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content2}
// #GOGP_ENDCASE
//#GOGP_ENDSWITCH
--------------------------------------
// #GOGP_MULTISWITCH <SwitchKey>
// #GOGP_CASE <SwitchKeyValue1>
{case content1}
// #GOGP_ENDCASE
// #GOGP_CASE <SwitchKeyValue2>
{case content2}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content1}
// #GOGP_ENDCASE
// #GOGP_ENDMULTISWITCH
// #GOGP_MULTISWITCH
// #GOGP_CASE <key>
{case content3}
// #GOGP_ENDCASE
// #GOGP_CASE <key> != val
{case content4}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content2}
// #GOGP_ENDCASE
// #GOGP_ENDMULTISWITCH
//#GOGP_MULTISWITCH <SwitchKey>
// #GOGP_CASE <SwitchKeyValue1>
{case content1}
// #GOGP_ENDCASE
// #GOGP_CASE <SwitchKeyValue2>
{case content2}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content1}
// #GOGP_ENDCASE
//#GOGP_ENDMULTISWITCH
//#GOGP_MULTISWITCH
// #GOGP_CASE <key>
{case content3}
// #GOGP_ENDCASE
// #GOGP_CASE <key> != val
{case content4}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content2}
// #GOGP_ENDCASE
//#GOGP_ENDMULTISWITCH
// #GOGP_CASE <key>
{case content3}
// #GOGP_ENDCASE
// #GOGP_CASE <key> != val
{case content4}
// #GOGP_ENDCASE
// #GOGP_DEFAULT
{default content2}
// #GOGP_ENDCASE
--------------------------------------
//#GOGP_CASE <key>
{case content3}
//#GOGP_ENDCASE
//#GOGP_CASE <key> != val
{case content4}
//#GOGP_ENDCASE
//#GOGP_DEFAULT
{default content2}
//#GOGP_ENDCASE
// #GOGP_REQUIRE(<gp-path> , gpgSection)
--------------------------------------
#GOGP_GPGCFG(<config-name>)
// #GOGP_REPLACE(<src>, <dst>)
// #GOGP_MAP(<src>, <dst>)
// #GOGP_IGNORE_BEGIN
{ignore content}
// #GOGP_IGNORE_END
// #GOGP_GPONLY_BEGIN
{gp-only content}
// #GOGP_GPONLY_END
// #GOGP_FILE_BEGIN
// #GOGP_FILE_END
// #GOGP_ONCE
{only generate once from a gp file}
// #GOGP_END_ONCE
---
tail
`