-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdsl_stage_spec.rb
542 lines (472 loc) · 15.3 KB
/
dsl_stage_spec.rb
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
RSpec.describe Interscript::DSL::Stage do
each_compiler do |compiler|
describe compiler do
before :example do
$compiler = compiler
end
context "#sub" do
it "handles basic substitition" do
s = stage {
sub "b", "e"
}
expect(s.("abcd")).to eq("aecd")
expect(s.("aecd")).to eq("aecd")
expect(s.("bbbb")).to eq("eeee")
end
it "handles substitution with :before" do
s = stage {
sub "a", "A", before: space
}
expect(s.("abcda abcda abada")).to eq("abcda Abcda Abada")
end
it "handles substitution with :not_before" do
s = stage {
sub "a", "A", not_before: space
}
expect(s.("abcda abcda abada")).to eq("AbcdA abcdA abAdA")
end
it "handles substitution with :after" do
s = stage {
sub "a", "A", after: space
}
expect(s.("abcda abcda abada")).to eq("abcdA abcdA abada")
end
it "handles substitution with :not_after" do
s = stage {
sub "a", "A", not_after: space
}
expect(s.("abcda abcda abada")).to eq("Abcda Abcda AbAdA")
end
it "handles substitution with :not_before and :not_after" do
s = stage {
sub "a", "A", not_before: space, not_after: space
}
expect(s.("abcda abcda abada")).to eq("Abcda abcda abAdA")
end
it "handles characters inside BMP" do
s = stage {
sub "\u1234", "\u1235"
}
expect(s.("\u1234")).to eq("\u1235")
end
it "handles characters outside BMP" do
s = stage {
sub "\u{12345}", "\u{12346}"
}
expect(s.("\u{12345}")).to eq("\u{12346}")
end
it "works with upcase" do
s = stage {
sub "a", upcase
}
expect(s.("a")).to eq("A")
end
end
context "#parallel" do
it "finds the longest substrings" do
s = stage {
parallel {
sub "had", "B"
sub "little", "D"
sub "lamb", "E"
sub "mary", "A"
sub "a", "C"
sub " ", "X"
}
}
expect(s.("mary had a little lamb")).to eq("AXBXCXDXE")
expect(s.("lamborghini")).to eq("Eorghini")
expect(s.("hadahadahada")).to eq("BCBCBC")
end
it "works with any" do
s = stage {
parallel {
sub any("abcde"), "X"
sub any("f".."i"), "Y"
sub any(["AB", "CD"]), "Z"
}
}
expect(s.("Cameroon")).to eq("CXmXroon")
expect(s.("ABfghiabcdCD")).to eq("ZYYYYXXXXZ")
end
# The old behaviour was to take the LAST one. We may reintroduce this
# behavior in the future, but the maps will need to be rearranged.
#
# it "prefers the first given replacement" do
# s = stage {
# parallel {
# sub any("ab"), "X"
# sub any("ab"), "Y"
# }
# }
# expect(s.("abbacus")).to eq("XXXXcus")
# end
context "extended engine" do
it "falls back to an extended engine when needed" do
s = stage {
parallel {
sub "a", "A", not_before: space, not_after: space
sub "b", "B", not_before: space, not_after: space
}
}
expect(s.("abcda abcda abada")).to eq("ABcda aBcda aBAdA")
end
it "sorts arguments correctly with an extended engine" do
s = stage {
parallel {
sub "aaaa", "c", not_before: "doesntmatter"
sub "a", "d", not_before: "doesntmatter"
sub "aa", "e", not_before: "doesntmatter"
sub "aaa", "f", not_before: "doesntmatter"
}
}
expect(s.("aaaaa")).to eq("cd")
end
it "works with multiple replacements" do
s = stage {
parallel {
sub "a", any("bc"), not_before: "doesntmatter"
sub "d", any("ef"), not_before: "doesntmatter"
}
}
expect(s.("aaaddd")).to eq("bbbeee")
end
it "doesn't trigger a weird off-by-one error" do
s = stage {
parallel {
sub "\u064f", "u" # ُ damma
sub "\u0650" + boundary, "-e" # ِ kasra # needed to trigger the extended engine
sub "\u064f", any("uo") # ُ damma
sub "\u0627", "a" # ا
sub "\u0648", "o" # و
}
}
expect(s.("\u0627")).to eq("a")
end
end
end
context "#run" do
it "can run other stages" do
s = document {
stage(other) {
sub "a", "A"
}
stage {
run stage.other
}
}
expect(s.("aabaa")).to eq("AAbAA")
end
it "can run multiple stages" do
s = document {
stage(one) { sub "0", "1" }
stage(two) { sub "1", "2" }
stage(three) { sub "2", "3" }
stage {
run stage.one
run stage.two
run stage.three
}
}
expect(s.("0")).to eq("3")
end
it "can run stages inside other stages" do
s = document {
stage(one) { run stage.two }
stage(two) { run stage.three }
stage(three) { sub "0", "3" }
stage {
run stage.one
}
}
expect(s.("0")).to eq("3")
end
it "can run remote stages" do
document("remote-stages") {
stage(hello) { sub "0", "3" }
}
s = document {
dependency "remote-stages", as: remote
stage { run map.remote.stage.hello }
}
expect(s.("0")).to eq("3")
end
it "can run imported remote stages" do
document("imported-remote-stages") {
stage(hellohello) { sub "0", "3" }
}
s = document {
dependency "imported-remote-stages", import: true
stage { run stage.hellohello }
}
expect(s.("0")).to eq("3")
end
it "can run remote stages that run remote stages" do
document("remotest-stage") {
stage(seven) { sub "0", "3" }
}
document("remote-stage-running-remote-stage") {
dependency "remotest-stage", as: remotest
stage(six) { run map.remotest.stage.seven }
}
s = document {
dependency "remote-stage-running-remote-stage", as: remote
stage { run map.remote.stage.six }
}
expect(s.("0")).to eq("3")
end
end
context "items" do
context "#any" do
it "handles any with string" do
s = stage {
sub any("abc"), "X"
}
expect(s.("abcda abcda abada")).to eq("XXXdX XXXdX XXXdX")
end
it "handles any with range" do
s = stage {
sub any("a".."c"), "X"
}
expect(s.("abcda abcda abada")).to eq("XXXdX XXXdX XXXdX")
end
it "handles any with array" do
s = stage {
sub any(["a","b","c"]), "X"
}
expect(s.("abcda abcda abada")).to eq("XXXdX XXXdX XXXdX")
end
it "handles any with a complex array of anys" do
s = stage {
# Any of:
sub any([
any("a".."z"), # range a to z
any(["A", "B", "C"]), # A, B or C
"123" # string "123"
]), "X"
}
expect(s.("Mary had A littlë lámb")).to eq("MXXX XXX X XXXXXë XáXX")
expect(s.("1234512345")).to eq("X45X45")
end
it "handles any with concatenations" do
s = stage {
sub any([any("ab") + any("cd"), any("AB") + any("CD")]), "X"
}
expect(s.("ad AD ba ca Ad Da bd BD bD")).to eq("X X ba ca Ad Da X X bD")
end
end
context "#maybe" do
it "handles maybe with string concatenated to other string" do
s = stage {
sub maybe("a") + "b", "X"
}
expect(s.("abcdb")).to eq("XcdX")
end
it "handles maybe with multicharacter string" do
s = stage {
sub "X"+maybe("abcde")+"X", "Y"
}
expect(s.("XabcdeX")).to eq("Y")
expect(s.("XX")).to eq("Y")
end
end
context "#capture" do
it "captures a string and allows to reference it" do
s = stage {
sub capture("a"), "-"+ref(1)+"-"
}
expect(s.("bab")).to eq("b-a-b")
expect(s.("baab")).to eq("b-a--a-b")
end
it "captures multiple strings" do
s = stage {
sub capture("a")+capture("b"), ref(2)+ref(1)
}
expect(s.("mmabmm")).to eq("mmbamm")
end
it "allows for any to be used inside a captured string" do
s = stage {
sub capture(any("abc")), "["+ref(1)+"]"
}
expect(s.("abcde")).to eq("[a][b][c]de")
end
it "allows for #ref to be used in from part" do
s = stage {
sub capture("a")+ref(1), "X"
}
expect(s.("xax")).to eq("xax")
expect(s.("xaax")).to eq("xXx")
end
it "can be aliased" do
s = document {
aliases {
def_alias maybe_dash, capture(any(["-", ""]))
}
stage {
sub "a"+maybe_dash+"b", "X"+ref(1)+"Y"
}
}
expect(s.("abca-b-cabc")).to eq("XYcX-Y-cXYc")
end
end
context "concatenation" do
it "concatenates aliases with strings both ways" do
s = stage {
sub line_start + "a", "X"
sub "a" + line_end, "Y"
}
expect(s.("aaaaa")).to eq("XaaaY")
end
it "concatenates any with strings both ways" do
s = stage {
sub any("bc") + "a", "X"
sub "d" + any("ef"), "Y"
}
expect(s.("baca||dedf")).to eq("XX||YY")
end
it "concatenates multiple anys" do
s = stage {
sub any("ab") + any("cd") + any("ef") + any("gh"), "X"
}
expect(s.("adeg dd ab bcfh")).to eq("X dd ab X")
end
end
context "stdlib aliases" do
it "handles boundary correctly" do
s = stage {
sub boundary, "|"
}
expect(s.("Mary had A littlë lámb!")).to eq("|Mary| |had| |A| |littlë| |lámb|!")
end
it "handles non_word_boundary correctly" do
s = stage {
sub non_word_boundary, "|"
}
expect(s.("Mary had A littlë lámb!")).to eq("M|a|r|y h|a|d A | | l|i|t|t|l|ë l|á|m|b!|")
end
end
context "local aliases" do
it "handles local aliases correctly" do
s = document {
aliases {
def_alias hello, any(["hello", "Hello", "HELLO"])
}
stage {
sub hello, "Goodbye"
}
}
expect(s.("Hello world")).to eq("Goodbye world")
end
end
context "remote aliases" do
document("remote-aliases") {
aliases {
def_alias from_name, "404"
def_alias to_name, "500"
}
}
it "handles remote aliases correctly" do
s = document {
dependency "remote-aliases", as: remo
stage {
sub map.remo.from_name, map.remo.to_name
}
}
expect(s.("Error 404")).to eq("Error 500")
end
it "handles imported remote aliases correctly" do
s = document {
dependency "remote-aliases", import: true
stage {
sub from_name, to_name
}
}
expect(s.("Route 404")).to eq("Route 500")
end
end
context "multiple versions of replacement" do
it "works with any" do
s = stage {
sub any("a"), any("XY")
sub any("b"), any(["X", "Y"])
}
expect(s.("abbacus")).to eq("XXXXcus")
end
it "works with any + concatenation" do
s = stage {
sub any("ab"), "["+any("XY")+"]"
}
expect(s.("abbacus")).to eq("[X][X][X][X]cus")
end
it "works with any(any())" do
s = stage {
sub any("ab"), any([any("XY"), "Z"])
}
expect(s.("abbacus")).to eq("XXXXcus")
end
it "works with references" do
s = stage {
sub capture(any("a".."s")), any(["["+ref(1)+"]", "other"])
}
expect(s.("abbacus")).to eq("[a][b][b][a][c]u[s]")
end
it "works with stdlib aliases" do
s = stage {
sub any("ab"), any([none, space])
}
expect(s.("abbacus")).to eq("cus")
end
it "works with parallel" do
s = stage {
parallel {
sub any("ab"), any(["XA", "YB"])
sub "d", any(["88", "99", "00"])
sub any("ef"), any("ZT")
}
}
expect(s.("abcdefgh")).to eq("XAXAc88ZZgh")
end
end
end
context "stdlib calls" do
it "handles a title case stdlib call correctly" do
s = stage {
title_case
}
expect(s.("hello world hello hello")).to eq("Hello World Hello Hello")
end
it "handles a title case stdlib call with :word_separator correctly" do
s = stage {
title_case word_separator: ""
}
expect(s.("hello world hello hello")).to eq("Hello world hello hello")
expect(s.("hello world\nhello hello")).to eq("Hello world\nHello hello")
end
it "handles a separate stdlib call correctly" do
s = stage {
separate
}
expect(s.("こんいちは")).to eq("こ ん い ち は")
end
it "handles a separate stdlib call with :separator correctly" do
s = stage {
separate separator: "|"
}
expect(s.("こんいちは")).to eq("こ|ん|い|ち|は")
end
it "handles a compose call correctly" do
s = stage {
compose
}
expect(s.("ᄆ"+"ᅮ")).to eq("무")
end
it "handles a decompose call correctly" do
s = stage {
decompose
}
expect(s.("무")).to eq("ᄆ"+"ᅮ")
end
end
end
end
end