-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic.js
774 lines (626 loc) · 39.8 KB
/
basic.js
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
var assert = require('assert')
var bracery = require('../index')
var extend = bracery.ParseTree.extend
// initial grammar
var initJson = { abc: 'def',
hello: '[hello|hi]',
world: ['world', 'planet'],
show_user: function (config) { return 'user=' + config.node.user },
dynamo: function (config) { return [config.random() < .5 ? 'dynamik' : 'DYNAMIC'] },
lambda: function (config, x, y) { return x + 'world' + y },
json: function (config, x, y, z) { return 'x=' + JSON.stringify(x) + ' y=' + JSON.stringify(y) + ' z=' + JSON.stringify(z) },
coinflip: '&prob{.5}{heads}{tails}',
test1: 'testing',
test2: '~TEST1',
test3: 'x~test3',
test4: '"e{~TEST1}' }
var nTestSymbols = Object.keys(initJson).length // number of symbols in initial grammar
var b // the Bracery object
// tests
function doTests (testRunner) {
// test parameters
var maxTries = 100
// Theo's alternation test
expectExpand ('$Theo:={[dad|mom]} $theo $theo come to [Africa|our house|nowhere] [just kidding |seriously] ha ha ha ha ha ',
'dad dad dad come to nowhere just kidding ha ha ha ha ha ',
{ maxTries: maxTries })
// the tests themselves
expectExpand ('~hello ~world', 'hello world', {maxTries:maxTries})
expectExpand ('~hello ~world', 'hello planet', {maxTries:maxTries})
expectExpand ('~hello ~world', 'hi world', {maxTries:maxTries})
expectExpand ('~hello ~world', 'hi planet', {maxTries:maxTries})
expectExpand ('~hello ~world', 'yo earth', {maxTries:maxTries,fail:true})
// simple expansions
expectExpand ('~test1', 'testing')
expectExpand ('~test1', 'testings', {fail:true})
expectExpand ('~test1', 'TESTING', {fail:true})
expectExpand ('~test2', 'TESTING')
// default is to expand ~abc (to 'def'), as that is the alphabetically earliest symbol
expectExpand (undefined, 'def')
expectExpand ('', '')
// look out! recursion
expectExpand ('~test3', 'xxx')
expectExpand ('~test3', 'xxx', { maxDepth: 5 })
expectExpand ('~test3', 'xxxxx', { maxDepth: 5, maxRecursion: 10 })
expectExpand ('~test3', 'xxxx', { maxRecursion: 4 })
expectExpand ('~test3', 'xxxxxxxxxx', { maxRecursion: 10 })
expectExpand ('~test3 ~test3 ~test3', 'xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx', { maxRecursion: 10 })
expectExpand ('~test3', 'xxxxxxxxxx', { maxRecursion: 10, maxLength: 5 })
expectExpand ('~test3 ~test3 ~test3', 'xxxxxxxxxx', { maxRecursion: 10, maxLength: 5 })
expectExpand ('~test3 ~test3 ~test3', 'xxxxxxxxxx xxxxxxxxxx', { maxRecursion: 10, maxLength: 15 })
expectExpand ('~test3 ~test3 ~test3', 'xxxxxxxxxx', { maxRecursion: 10, maxNodes: 5 })
// quoting
expectExpand ('~test4', '~TEST1')
expectExpand ('&eval{~test4}', 'TESTING')
expectExpandQuote ('"e{~test1}', '~test1')
expectExpand ('\\~test1', '~test1')
expectExpand ('~', '~')
expectExpandQuote ('"e{~}', '\\~')
expectExpandQuote ('"e{~}test1', '\\~test1')
expectExpandQuote ('"e{~te}st1', '~test1')
expectExpandQuote ('"e{~test1}', '~test1')
expectExpandQuote ('&eval{"e{~}}', '~')
expectExpandQuote ('&eval{"e{~test1}}', 'testing')
expectExpandQuote ('&eval{"e{~te}st1}', 'testing')
expectExpandQuote ('&eval{"e{~}test1}', 'testing', {fail:true})
expectExpandQuote ('&eval{"e{~}test1}', '~test1')
expectExpandQuote ('&eval{&eval{"e{~}test1}}', 'testing')
expectExpand ('\\~test1', '~test1')
expectExpand ('&eval{\\~test1}', 'testing')
expectExpandQuote ('"e{#heroPet#}', '#heropet#')
expectExpandQuote ('"e[a=>b]', '$a={"e{b}}')
expectExpandQuote ('"e[a=>b|c]', '$a={"e[b|c]}')
expectExpandQuote ('$heropet={freddy}&eval"e{#heroPet#}', 'freddy')
expectExpandQuote ('"e{&match/a/{cat}{$$0}}', '&match/a/{cat}$$0')
expectExpand ('$x=3 "e{x(&unquote$x)="e{&unquote$x}}', 'x(3)="e&unquote{$x}')
expectExpand ('$x=3 &eval"e{x(&unquote$x)="e{&unquote$x}}', 'x(3)=3')
expectExpand ('"e{a|b}', '[a|b]')
expectExpand ('"e&unquote{a|b}', 'a', {maxTries:maxTries})
expectExpand ('"e&unquote{a|b}', 'b', {maxTries:maxTries})
expectExpand ('"e"e&unquote{a|b}', '"e&unquote{[a|b]}')
expectExpandQuote ('"e{&~hello{abc}{def}}', '&~hello{abc}{def}')
expectExpand ('"e"e{&~hello{abc}{def}}', '"e{&~hello{abc}{def}}')
expectExpandQuote ('"e{&xapply~{ lambda }{$y}}', '&xapply~lambda$y')
expectExpandQuote ('"e{&xcall~a{b}{cd}{e}}', '&~a{b}{cd}{e}')
// user namespaces
expectExpand ('~{testuser/show_user}', 'user=testuser',)
expectExpand ('~testuser/show_user', 'user=testuser',)
expectExpand ('~{testuser/abc}', 'def',)
expectExpand ('"e~{testuser/abc}', '~{testuser/abc}',)
expectExpand ('"e{~testuser/abc}', '~{testuser/abc}',)
// case manipulation
expectExpand ('"e{~TEST1}', '~TEST1')
expectExpand ('"e{~Test1}', '~Test1')
expectExpand ('~TEST1', 'TESTING')
expectExpand ('~Test1', 'Testing')
expectExpand ('&uc{abc}', 'ABC')
expectExpand ('&lc{AbC}', 'abc')
expectExpand ('&cap{&lc{AbC}}', 'Abc')
// compromise
expectExpand ('&plural{child}', 'children')
expectExpand ('&singular{children}', 'child')
expectExpand ('&adjective{love}', 'loveable', {sorry_blame_compromise:true})
expectExpand ('&future{love}', 'will love')
expectExpand ('&past{love}', 'loved')
// arithmetic & numbers
expectExpand ('&add{2}{2}', '4')
expectExpand ('&add{two}{two}', 'four')
expectExpand ('&add{two cats}{4}', 'six cats')
expectExpand ('&add{two cats three dogs}{2}', 'four cats five dogs')
expectExpand ('&subtract{3}{3}', '0')
expectExpand ('&subtract{three}{three}', '0')
expectExpand ('&subtract{three}{four}', 'negative one')
expectExpand ('&subtract{3}{4}', '-1')
expectExpand ('&add{two}{&subtract{three}{four}}', 'one')
expectExpand ('&multiply{two}{three}', '6')
expectExpand ('÷{three}{two}', '1.5')
expectExpand ('&pow{three}{two}', '9')
expectExpand ('&pow{2}{3}', '8')
expectExpand ('>{three cats}{2}', 'three cats')
expectExpand ('>{three cats}{4}', '')
expectExpand ('<{three cats}{2}', '')
expectExpand ('<{three cats}{4}', 'three cats')
expectExpand ('&eq{three cats}{3}', 'three cats')
expectExpand ('&eq{three cats}{4}', '')
expectExpand ('&eq{zero}{}', 'zero')
expectExpand ('&eq{}{zero}', 'zero')
expectExpand ('&eq{}{}', 'eq')
expectExpand ('&neq{one}{}', 'one')
expectExpand ('&neq{}{one}', 'one')
expectExpand ('&neq{three cats}{3}', '')
expectExpand ('&neq{three cats}{4}', 'three cats')
expectExpand ('&leq{three cats}{3}', 'three cats')
expectExpand ('&leq{three cats}{4}', 'three cats')
expectExpand ('&leq{three cats}{2}', '')
expectExpand ('&geq{three cats}{3}', 'three cats')
expectExpand ('&geq{three cats}{4}', '')
expectExpand ('&geq{three cats}{2}', 'three cats')
expectExpand ('&round{3.141}', '3')
expectExpand ('&round{three point one four one}', '3')
expectExpand ('&round{3.6}', '4')
expectExpand ('&floor{3.6}', '3')
expectExpand ('&ceil{3.6}', '4')
expectExpand ('&ceil{&random{6}}', '6', {maxTries:maxTries})
expectExpand ('&ceil{&random{6}}', '1', {maxTries:maxTries})
expectExpand ('&wordnum{3}', 'three')
expectExpand ('&wordnum{32}', 'thirty two')
expectExpand ('&wordnum{three}', 'three')
expectExpand ('&dignum{3}', '3')
expectExpand ('&dignum{thirty two}', '32')
expectExpand ('&dignum{three}', '3')
expectExpand ('&ordinal{3}', '3rd')
expectExpand ('&ordinal{three}', 'third')
expectExpand ('&cardinal{3rd}', '3')
expectExpand ('&cardinal{third}', 'three')
expectExpand ('&abs{-3}', '3')
expectExpand ('&max{four}{6}', '6')
expectExpand ('&max{four}{2}', 'four')
expectExpand ('&min{-10}{twenty}', '-10')
expectExpand ('&percent{.5}', '50%')
expectExpand ('&percent{1.01}', '101%')
// test workaround for nlp's floating-point parser, that doesn't recognize '.5' as '0.5'
expectExpand ('&add{.25}{.3}', '0.55')
// variables
expectExpand ('$x={aha}$x', 'aha')
expectExpand ('$x=aha $x', 'aha')
expectExpand ('$x:=aha', 'aha')
expectExpand ('$x:=o h$x', 'oho')
expectExpand ('[x:aha]$x', 'aha')
expectExpand ('[x:aha]\n\n$x', 'aha')
expectExpand ('[x:aha]\n\n$x\n\n$x', 'aha\n\naha')
expectExpand ('[x=>aha]\n\n$x', 'aha')
expectExpand ('[x=>aha|aha]\n\n$x', '[aha|aha]')
expectExpand ('[x=>aha|aha]\n\n#x#', 'aha')
expectExpand ('$x=aha\n\n$x', 'aha')
expectExpand ('$x=aha\n\n$x\n\n$x', 'aha\n\naha')
expectExpand ('$z={zebedee}$zeb={zebadiah}$Zeb $Z', 'Zebadiah ZEBEDEE')
expectExpand ('$AbC={air}$aBC={hair}$abC={lair}$abc$Abc$ABC', 'lairLairLAIR')
expectExpand ('$x=3 $y=8 &map&vars{\[$_:&eval{\$$_}\]}', '[x:3][y:8]') // how to quote the environment...
expectExpand ('&set$x{oho}$x', 'oho')
// Game-specific extensions: &accept, &reject, &meter, &status, &footer, &tag
expectExpand ('&accept$x $accept', '$x')
expectExpand ('&reject{123}$reject', '123')
expectExpand ('&footer$y $footer', '$y')
expectExpand ('&meter{a}{$b} &meter{c}{$d/100} &json$meters', '[[["a","&math{$b}"],["c","&math{$d/100}"]]]')
expectExpand ('&meter{a}{$b}{$c} &json$meters', '[[["a","&math{$b}","$c"]]]')
expectExpand ('&status{$blah}$status', '$blah')
expectExpand ('$tags=abc &tag{def}&tag{ghi} $tags', 'abc def ghi')
expectExpandQuote ('"e&accept{$x}', '&accept{$x}')
expectExpandQuote ('"e&reject{$y $z}', '&reject{$y $z}')
expectExpandQuote ('"e&status{$blah}', '&status{$blah}')
expectExpandQuote ('"e&tag{newtag}', '&tag{newtag}')
expectExpandQuote ('"e&meter{icon}{$a + $b}', '&meter{icon}{$a + $b} ')
expectExpandQuote ('"e&meter{icon}{$a + $b}{status}', '&meter{icon}{$a + $b}{status}')
// syntax edge cases involving dummy alternations
expectExpand ('$abc=[ABC]', '=[ABC]')
expectExpand ('$abc={[DEF]}', '')
expectExpand ('$abc={[DEF]}$abc', '[DEF]')
expectExpand ('$x={a\\|b}$x', 'a|b')
expectExpand ('$dummy=[c\\|d]', '=[c|d]')
expectExpand ('$x={a\\|b}$r=&eval{[$x]}$r', 'a', {maxTries:maxTries})
expectExpand ('$x={a\\|b}$r=&eval{[$x]}$r', 'b', {maxTries:maxTries})
// Tracery variables
expectExpand ('[myvar:myval]', '')
expectExpand ('[myvar:myval]$myvar', 'myval')
expectExpand ('[myvar:myval]#myvar#', 'myval')
expectExpand ('[myvar=>myval]$myvar', 'myval')
expectExpand ('[myvar=>myval]#myvar#', 'myval')
expectExpand ('[myvar=>myval1|myval2]$myvar', '[myval1|myval2]')
expectExpand ('[myvar=>myval1|myval2]#myvar#', 'myval1', {maxTries:maxTries})
expectExpand ('[myvar=>myval1|myval2]#myvar#', 'myval2', {maxTries:maxTries})
// Tracery modifiers
expectExpand ('#test1#', 'testing')
expectExpand ('#test1.capitalize#', 'Testing')
expectExpand ('#test1.capitalizeAll#', 'TESTING')
// Tracery-style overriding
expectExpand ('$test1={OVERLOAD}#test1#', 'OVERLOAD')
expectExpand ('[test1:OVERLOAD]#test1#', 'OVERLOAD')
expectExpand ('$test1={OVERLOAD}~test1', 'testing')
expectExpand ('$test1={~test4}#test1#', 'TESTING')
// local scope
expectExpand ('$a={A}$b={B}$a$b&let$a={x}$b={y}{$a$b}$a$b', 'ABxyAB')
expectExpand ('$a={A}$b={B}$a$b&let $a={x} $b={y} {$a$b}$a$b', 'ABxyAB')
expectExpand ('$a={a}$b={${a}b$a}$ab="e{$a$b}#[a:3$b][b:5$a]ab#$a$b', '3aba53abaaaba')
// variable persistence
var vars = {}
expectExpand ('$a={x}$a', 'x', {vars:vars})
expectExpand ('$a', '')
expectExpand ('$a', 'x', {vars:vars,a_equals_x:true})
// lists
expectExpand ('{}', '{}')
expectExpand ('&{}', '')
expectExpand ('&cat{&{}}{xyz}', 'xyz')
expectExpand ('&cat{xyz}{abc}', 'xyzabc')
expectExpand ('&cat{123}&cat{xyz}{abc}', '123xyzabc')
expectExpand ('&first{&cat{123}&cat{xyz}{abc}}', '123')
expectExpand ('¬first{&cat{123}&cat{xyz}{abc}}', 'xyzabc')
expectExpand ('&last{&cat{123}&cat{xyz}{abc}}', 'abc')
expectExpand ('¬last{&cat{123}&cat{xyz}{abc}}', '123xyz')
expectExpand ('&prepend{123}&cat{xyz}{abc}', '123xyzabc')
expectExpand ('&prepend&cat{xyz}{abc}{123}', 'xyzabc123')
expectExpand ('$list={&prepend{123}&cat{xyz}{abc}}&first$list', '123')
expectExpand ('$list={&prepend&cat{xyz}{abc}{123}}&first$list', 'xyzabc')
expectExpand ('$list={&prepend{123}&cat{xyz}{abc}}&last$list', 'abc')
expectExpand ('$list={&prepend&cat{xyz}{abc}{123}}&last$list', '123')
expectExpand ('&append{123}&cat{xyz}{abc}', '123xyzabc')
expectExpand ('&first&append{123}&cat{xyz}{abc}', '123')
expectExpand ('&last&append{123}&cat{xyz}{abc}', 'xyzabc')
expectExpand ('&join{&prepend{123}&cat{xyz}{abc}}{, }', '123, xyz, abc')
expectExpand ('&islist{&{}}', '[]')
expectExpand ('&islist{}', '')
expectExpand ('&same{&{}}{}', '')
expectExpand ('&same{&{}}{&{}}', '1')
expectExpand ('&join{&{}x&{}y}{,}', 'x,y')
expectExpand ('&join{x&{}y&{}}{,}', 'xy')
expectExpand ('$x=&list{"e{abc}"e{def}}&map$a$x{$a!}', 'abc!def!')
expectExpand ('$x=&list{"e{abc}"e{def}}&join&map$a$x{$a!}{ }', 'abc! def!')
expectExpand ('$x=&list{"e{2}"e{4}"e{6}"e{0}}&filter$n$x>{$n}{3}', '46')
expectExpand ('$x=&list{"e{2}"e{4}"e{6}"e{0}}&reduce$n$x$r={0}&add$n$r', '12')
expectExpand ('$x=&list{"e{2}"e{4}"e{6}"e{0}}&reduce$n:$x$r={zero dogs}&add$r$n', 'twelve dogs')
expectExpand ('$x=&{A&,B&,C&,D}&indexof{C}{$x}', '2')
expectExpand ('$x=&{A&,B&,C&,D}&indexof{E}{$x}', '')
expectExpand ('$x={1}$a1={verily}$a2={in troth}&eval"e{$a&unquote$x indeed}', 'verily indeed')
expectExpand ('"e&unquote"e&infinitive$y', '&infinitive$y')
expectExpand ('$x={&{}abc"e{def}}&q$x', '&{abc&,def}')
expectExpand ('&push$x{a}&push$x{b}&uc&push$x{c}&push$x{...}&join$x{,} &shift$x $dots:={&pop$x} $quirk:=uh, &shift$x $dots &unshift$x&cat{x}{t} &uc&join$x$dots',
'a,b,c,... a ... uh,b ... X...T...C') // a lot going on in this one. Spaces must be exactly correct (of course)
expectExpand ('$x=&split{1 2 3}&eval&shift$x', '1') // check &shift$x is only called once within &eval
expectExpand ('$x=5 &inc$x x=$x $x=10 &dec$x x=$x', 'x=6 x=9') // note exact spaces
expectExpand ('$a=10 $b=20 $c=30 $d=40 ++$a --$b $c++ $d-- a=$a b=$b c=$c d=$d', '11 19 30 40 a=11 b=19 c=31 d=39') // note exact spaces
expectExpand ('$a=ten $a $a+=3 $a $a-=5 $a $a*=2 $a $a/=4 $a $a.=2 $a', 'ten thirteen eight 16 4 42')
expectExpand ('$x=&split{fresh word salad} &join{&shuffle{$x}}{ }', 'salad word fresh', {maxTries:maxTries})
expectExpand ('$x=&{1&,3&,2&,11}&json$x $y=&numsort{$x}{$_} &json$y', '[["1","3","2","11"]] [["1","2","3","11"]]')
expectExpand ('$x=&{1&,3&,2&,11}&json$x $y=&lexsort{$x}{$_} &json$y', '[["1","3","2","11"]] [["1","11","2","3"]]')
expectExpand ('&numsort$x', '')
expectExpand ('$x=hello &push$x $x=well $x $x=&pop $x', 'well hello') // default arguments to &push and &pop
expectExpand ('&join&split/,/{hello,world}', 'hello world') // default argument to &join
expectExpand ('&lexsort&split{a c d b}', 'abcd') // default argument to &lexsort
expectExpand ('&revstr{abcde}', 'edcba')
expectExpand ('&reverse{abcde}', 'abcde')
expectExpand ('&reverse&split//{abcde}', 'edcba') // empty regex in &split
expectExpand ('&reverse&split/b/{abcde}', 'cdea')
expectExpand ('&join&iota{5}{,}', '0,1,2,3,4')
expectExpand ('&map$x&iota{11}{$y+=$x}$y', '55')
expectExpand ('&for$x&iota{11}{$y+=$x}$y', '55')
// &makelist, "elist, &rotate, &cycle, &queue, &shuffle, &bump, &playlist
expectExpand ('$x=&split{1 2 3} &rotate$x', '231')
expectExpand ('[y=>&cycle$x&split{1 2 3}] #y# #y# #y# #y# #y# #y# #y#', '1 2 3 1 2 3 1')
expectExpand ('[y=>&queue$x&split{1 2 3}] #y# #y#', '1 2')
expectExpand ('[y=>&queue$x&split{1 2 3}] #y# #y# #y# #y#', '1 2 3 ')
expectExpand ('[y=>&cycle$x&list{"e{++$a}"e{++$b}"e{++$c}}] $a=$b:=$c:=0 $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c', '000 1 100 1 110 1 111 2 211 2 221 2 222 3 322')
expectExpand ('[y=>&queue$x&list{"e{++$a}"e{++$b}"e{++$c}}] $a=$b:=$c:=0 $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c #y# $a$b$c', '000 1 100 1 110 1 111 111 111 111 111')
expectExpand ('&rotate&makelist{1}{2}{3}', '231')
expectExpand ('&rotate"elist{$x}{$y}{$z}', '$y$z$x')
expectExpand ('[tick=>You feel &cycle$mood&makelist{happy}{sad}{bored}.] #tick# #tick# #tick# #tick#',
'You feel happy. You feel sad. You feel bored. You feel happy.') // example from README.md
expectExpand ('&shuffle&split{1 2 3}', '321', {maxTries:maxTries})
expectExpand ('&shuffle&split{1 2 3}', '213', {maxTries:maxTries})
expectExpand ('&shuffle&split{1 2 3}', '132', {maxTries:maxTries})
expectExpand ('&bump&split{1 2 3}', '231', {maxTries:maxTries})
expectExpand ('&bump&split{1 2 3}', '213', {maxTries:maxTries,fail:true})
expectExpand ('&bump&split{1 2 3 4}', '2314', {maxTries:maxTries})
expectExpand ('&bump&split{1 2 3 4}', '2341', {maxTries:maxTries})
expectExpand ('&bump&split{1 2 3 4}', '2134', {maxTries:maxTries,fail:true})
expectExpand ('[y=>&playlist$x{&split{1 2 3 4}}] &$y &$y', '1 2', {maxTries:maxTries})
expectExpand ('[y=>&playlist$x{&split{1 2 3 4}}] &$y &$y', '2 1', {maxTries:maxTries})
expectExpand ('[y=>&playlist$x{&split{1 2 3 4}}] &$y &$y', '4 2', {maxTries:maxTries})
expectExpand ('[y=>&playlist$x{&split{1 2 3 4}}] &$y &$y', '1 1', {maxTries:maxTries,fail:true})
expectExpand ('[y=>&playlist$x{&split{1 2 3 4}}] &$y &$y', '2 2', {maxTries:maxTries,fail:true})
// these used to be tests of &strip, now obsoleted by &replace (and thus &strip has been stripped and replaced by &replace)
expectExpand ('&replace/hello/g{hello world hello}{}', ' world ')
expectExpand ('&replace/(hell)(o)/g{hello world hello}{$$2 well $$1}', 'o well hell world o well hell')
expectExpand ('&replace/&unquote{~abc}/{defcon}{}', 'con')
expectExpand ('~{abc}', 'def')
expectExpand ('~{abc}con', 'defcon')
expectExpand ('~{abc}con', 'defcon')
expectExpand ('&replace/&unquote{~abc}/{~{abc}con}{}', 'con')
expectExpand ('&replace/&unquote{~abc}/{~{abc}con defcon}{}', 'con defcon')
expectExpand ('&replace/&unquote{~abc}/g{~{abc}con defcon}{}', 'con con')
expectExpand ('$b={b}$x={Batch}$y=&replace/&unquote&replace/&unquote{$b}/{abc}{}/&replace/t/$x{}{}$y', 'Bh')
// strlen, length, comment
expectExpand ('$x=hello &strlen$x', '5')
expectExpand ('$x=hello &length$x', '1')
expectExpand ('$x=&split{hello world} &length$x', '2')
expectExpand ('$x=&split{hello world} &strlen$x', '10')
expectExpand ('&comment{hello world}', '')
expectExpandQuote ('"e&comment{hello world}', '&comment{hello world}')
// same, and, or, not
expectExpand ('&same{abc}{def}', '')
expectExpand ('&same{abc}{abc}', 'abc')
expectExpand ('&and{ }{world}', '')
expectExpand ('&and{hello}{ }', '')
expectExpand ('&and{hello}{world}', 'helloworld')
expectExpand ('&or{a}{b}', 'a')
expectExpand ('&or{ }{b}', 'b')
expectExpand ('$mood=lazy &and{ }{world$mood=busy} mood=$mood', ' mood=lazy')
expectExpand ('$mood=lazy &and{hello}{world$mood=busy} mood=$mood', 'helloworld mood=busy')
expectExpand ('$mood=lazy &or{ }{world$mood=busy} mood=$mood', 'world mood=busy')
expectExpand ('$mood=lazy &or{hello}{world$mood=busy} mood=$mood', 'hello mood=lazy')
expectExpand ('$x=10 $y=20 or=&or$y{++$x} x=$x and=&and{}{++$y} y=$y', 'or=20 x=10 and= y=20')
expectExpand ('¬{}', '1')
expectExpand ('¬{ }', '1')
expectExpand ('¬{1}', '')
// repetition
expectExpand ('&rep{Test}{3}', 'TestTestTest')
expectExpand ('&rep{Test}{3,5}', 'TestTestTest', {maxTries:maxTries})
expectExpand ('&rep{Test}{3,5}', 'TestTestTestTest', {maxTries:maxTries})
expectExpand ('&rep{Test}{3,5}', 'TestTest', {maxTries:maxTries,fail:true})
expectExpand ('&rep{Test}{3,5}', 'TestTestTestTestTestTest', {maxTries:maxTries,fail:true})
expectExpand ('&rep{Test}{6}', 'TestTest', {maxReps:2})
// eval
expectExpand ('$a={~}$b={test}$c={1}&eval{$a&cap{$b}$c}', 'Testing')
expectExpand ('$a={1}$b={2}$c={3}&let$a={~}$b={test}$c={1}&eval{$a&cap{$b}$c}$a&cap{$b}$c', 'Testing123')
expectExpand ('$a={1}$b={2}$c={3}&let$a={~}$b={test}$c={1}{&eval{$a&cap{$b}$c}}$a&cap{$b}$c', 'Testing123')
expectExpand ('$a={1}$b={2}$c={3}&let$a={~}$b={test}$c={1}{&eval{$a&cap{$b}$c}$a&cap{$b}$c}', 'Testing~Test1')
expectExpand ('$x=3 &preserve{x}', '$x=3')
expectExpand ('$x=3 $y=&preserve{x} $x=5 x=$x &$y x=$x', 'x=5 x=3')
expectExpand ('$x=3 $y=5 $s=&map&vars&preserve{$_} $x=10 $y=20 x=$x y=$y &$s x=$x y=$y', 'x=10 y=20 x=3 y=5')
expectExpand ('$x=&makelist{1}{2}{3} $y=&preserve{x} $x=&makelist{6}{2} x=&json$x &$y x=&json$x', 'x=[["6","2"]] x=[["1","2","3"]]')
// syntax, parse, grammar, tree
expectExpand ('&json&syntax"e{$x=[a|b]}', '[[["$","x","=",["{",[["[",[[["a"],"|"],[["b"]]],"]"]],"}"]]]]')
expectExpand ('&q&parsejson{\["a","b",\["c","d"\],\{"x":3,"w":\["abc","def"\]\}\]}', '&{a&,b&{c&,d}&{&{w&{abc&,def}}&{x&,3}}}')
expectExpand ('[a=>cat|#a# #a#]&json&parse{#a#}{cat cat cat}', '[["root",["#a#",["alt",["#a#",["alt",["#a#",["alt","cat"]]," ",["#a#",["alt","cat"]]]]," ",["#a#",["alt","cat"]]]]]]', {maxTries:maxTries})
expectExpand ('[a=>cat|#a# #a#]&json&parse{#a#}{cat cat cat}', '[["root",["#a#",["alt",["#a#",["alt","cat"]]," ",["#a#",["alt",["#a#",["alt","cat"]]," ",["#a#",["alt","cat"]]]]]]]]', {maxTries:maxTries})
expectExpand ('[a=>cat|#a# #a#]&json&parse{#a#}{cat cat dog}', '[""]')
expectExpand ('[a=>cat|#a# #a#]&json&parse{#a#}{cat cat cat}', '[""]', {maxParseLength:5})
expectExpand ('[a=>$animal|#a# #a#][animal:cat]&json&parse{#a#}{cat cat cat}', '[["root",["#a#",["alt",["#a#",["alt",["$animal","cat"]]]," ",["#a#",["alt",["#a#",["alt",["$animal","cat"]]]," ",["#a#",["alt",["$animal","cat"]]]]]]]]]', {maxTries:maxTries})
expectExpand ('[a=>~abc|#a# #a#]&json&parse{#a#}{def def def}', '[["root",["#a#",["alt",["#a#",["alt",["#a#",["alt",["~abc","def"]]]," ",["#a#",["alt",["~abc","def"]]]]]," ",["#a#",["alt",["~abc","def"]]]]]]]', {maxTries:maxTries})
expectExpand ('[a=>cat|dog]&json&parse{#a# $a}"e{cat [cat|dog]}', '[["root",["#a#",["alt","cat"]]," ",["$a","[cat|dog]"]]]')
expectExpand ('[sentence=>#plural_noun# #plural_verb# #prep_or_noun#|#singular_noun# #singular_verb# #prep_or_noun#][prep_or_noun=>#noun#|like #noun#][noun=>#plural_noun#|#singular_noun#][plural_noun=>fruit flies][singular_noun=>fruit|a banana][plural_verb=>fly|like][singular_verb=>flies|likes]&json&parse#sentence#{fruit flies like a banana}', '[["root",["#sentence#",["alt",["#plural_noun#","fruit flies"]," ",["#plural_verb#",["alt","like"]]," ",["#prep_or_noun#",["alt",["#noun#",["alt",["#singular_noun#",["alt","a banana"]]]]]]]]]]', {maxTries:maxTries})
expectExpand ('[sentence=>#plural_noun# #plural_verb# #prep_or_noun#|#singular_noun# #singular_verb# #prep_or_noun#][prep_or_noun=>#noun#|like #noun#][noun=>#plural_noun#|#singular_noun#][plural_noun=>fruit flies][singular_noun=>fruit|a banana][plural_verb=>fly|like][singular_verb=>flies|likes]&json&parse#sentence#{fruit flies like a banana}', '[["root",["#sentence#",["alt",["#singular_noun#",["alt","fruit"]]," ",["#singular_verb#",["alt","flies"]]," ",["#prep_or_noun#",["alt","like ",["#noun#",["alt",["#singular_noun#",["alt","a banana"]]]]]]]]]]', {maxTries:maxTries})
expectExpand ('[a=>#a#x|x]&json&parse#a#{xxxx}', '[["root",["#a#",["alt",["#a#",["alt",["#a#",["alt",["#a#",["alt","x"]],"x"]],"x"]],"x"]]]]')
expectExpand ('[a=>xo#a#|x]&json&parse#a#{xoxoxox}', '[""]', {maxSubsequenceLength:1})
expectExpand ('[a=>xo#a#|x]&json&parse#a#{xoxoxox}', '[["root",["#a#",["alt","xo",["#a#",["alt","xo",["#a#",["alt","xo",["#a#",["alt","x"]]]]]]]]]]', {maxSubsequenceLength:3})
expectExpand ('[a=>#a#ox|x]&json&parse#a#{xoxoxox}', '[""]', {maxSubsequenceLength:1})
expectExpand ('[a=>#a#ox|x]&json&parse#a#{xoxoxox}', '[["root",["#a#",["alt",["#a#",["alt",["#a#",["alt",["#a#",["alt","x"]],"ox"]],"ox"]],"ox"]]]]', {maxSubsequenceLength:3})
expectExpand ('[a=>#b#|#c#][b=>cat|dog#a#][c=>horse|cow][d=>whatever|why]&grammar#a#', '[_start=>#_1#][_1=>#_2#][_2=>#_3#|#_4#][_3=>#_5#][_4=>#_6#][_5=>cat|dog#_1#][_6=>horse|cow]')
expectExpand ('[a=>#b#|#c#][b=>cat|dog#a#][c=>horse|cow][d=>whatever|why]&grammar#d#', '[_start=>#_1#][_1=>#_2#][_2=>whatever|why]')
expectExpand ('[a=>#b#|#c#][b=>cat|dog#a#][c=>horse|cow][d=>whatever|why]&grammar{#a# #a#}', '[_start=>#_1##_2#][_1=>#_3#][_2=> #_1#][_3=>#_4#|#_5#][_4=>#_6#][_5=>#_7#][_6=>cat|dog#_1#][_7=>horse|cow]')
// test equivalency of &parse and &tree for a parse involving the elements our syntactic analyzer recognizes
// these elements include:
// variable lookup ($c)
// variable expansion without bindings (&eval{$z})
// symbol get &xget{~c}
// symbol expansion &eval{&xget{~b}} which should be the same as remote call ~b
// Tracery-style #x# which is equivalent to &if{$x}{&eval{$x}}{~x}. Both variable and symbol expansion are tested with #x# and #a#, respectively.
var defcatText = 'defcat'
var defcatRules = {a:"d#x#",b:"e&eval$z",c:"c$c"}
var defcatBracery = '[z:f&xget{~c}] [x=>~{b}t] $c=a'
var defcatParse = JSON.stringify ([["root",["#a#","d",["#x#",["~b","e",["&$z","fc",["$c","a"]]],"t"]]]])
expectExpand (defcatBracery + '#a#', defcatText, {rules:defcatRules})
expectExpand (defcatBracery + '&json&tree{#a#}', defcatParse, {rules:defcatRules})
expectExpand (defcatBracery + '&json&parse#a#{#a#}', defcatParse, {rules:defcatRules})
// a more parse-tree varied example of the same test, exposes a different test case
var defcatRules2 = {a:"d#x#t",b:"&eval{$z}c",c:"e$c"}
var defcatBracery2 = '[z:&xget{~c}] [x=>~{b}a] $c=f'
var defcatParse2 = JSON.stringify ([["root",["#a#","d",["#x#",["~b",["&$z","e",["$c","f"]],"c"],"a"],"t"]]])
expectExpand (defcatBracery2 + '#a#', defcatText, {rules:defcatRules2})
expectExpand (defcatBracery2 + '&json&tree{#a#}', defcatParse2, {rules:defcatRules2})
expectExpand (defcatBracery2 + '&json&parse#a#{#a#}', defcatParse2, {rules:defcatRules2})
// narrowed down to this case
expectExpand ('[z:z] &json&parse#za##za#', '[["root",["#za#",["&$z","z"],"a"]]]', {rules:{za:"&eval{$z}a"}})
expectExpand ('"e{&eval{$z}a}', '&eval$za', {fail:true}) // bug
expectExpand ('"e{&eval{$z}a}', '&eval{$z}a') // bug fixed
// in &parse expressions, &call$x and &apply$x{} should be handled the same as &eval
expectExpand ('[a=>cat|dog][b=>walk|whistle]&json&parse{&$a &apply{$b}{}}{cat walk}', '[["root",["&$a",["alt","cat"]]," ",["&$b",["alt","walk"]]]]')
// down with fixed nonterminals
expectExpand ('[hello:"e[yo|oy]][world:"e[earthling|human]]#hello# #world#', 'yo earthling', {maxTries:maxTries})
expectExpand ('[hello:"e[yo|oy]][world:"e[earthling|human]]#hello# #world#', 'oy earthling', {maxTries:maxTries})
expectExpand ('[hello:"e[yo|oy]][world:"e[earthling|human]]#hello# #world#', 'yo human', {maxTries:maxTries})
expectExpand ('[hello:"e[yo|oy]][world:"e[earthling|human]]#hello# #world#', 'oy human', {maxTries:maxTries})
expectExpand ('[hello:"e[yo|oy]][world:"e[earthling|human]]#hello# #world#', 'hello world', {maxTries:maxTries,fail:true})
// dynamic function binding
expectExpand ('~dynamo', 'dynamik', {maxTries:maxTries})
expectExpand ('~dynamo', 'DYNAMIC', {maxTries:maxTries})
expectExpand ('&~lambda{hi, }{!!!}', 'hi, world!!!')
expectExpand ('$y=&cat{hi, }{!!!}&xapply~lambda$y', 'hi, world!!!')
expectExpand ('$a=3 &~x{A}{B}', 'In x: a=3 1=A 2=B 0=AB', {rules:{x:["In x: a=$a 1=$$1 2=$$2 0=$$0"]}})
expectExpand ('$x="e{In x: a=$a 1=$$1 2=$$2 0=$$0} $a=3 &$x{A}{B}', 'In x: a=3 1=A 2=B 0=AB')
expectExpand ('~lambda', 'undefinedworldundefined')
expectExpand ('&xapply~lambda{}', 'undefinedworldundefined')
expectExpand ('&~lambda{}', 'worldundefined')
expectExpand ('&~lambda{$undef}', 'worldundefined')
// shorthands for &value and &list (precise generators)
expectExpand ('&xapply~json{&{1}&{2}&{3}}', 'x="1" y="2" z="3"')
expectExpand ('&xapply~json{1&{2}&{3}}', 'x="123" y=undefined z=undefined')
expectExpand ('&xapply~json{&{1&{2}&{&{3}}}}', 'x="1" y=["2"] z=[["3"]]')
expectExpand ('&xapply~json{&{1}&{&{2}}&{&{&{3}}}}', 'x="1" y=["2"] z=[["3"]]')
expectExpand ('&xapply~json&list{1&{2}&{3}}', 'x="1" y=["2"] z=["3"]')
expectExpand ('&json{&{&,a&,b&{&,c&,d}e&,f&,}}', '[["","a","b",["","c","d"],"e","f",""]]')
expectExpand ('&json{&{a&,b&{&,c&,d}e&,f&,}}', '[["a","b",["","c","d"],"e","f",""]]')
expectExpand ('&json{&{a&,b&{&,c&,d}e&,f}}', '[["a","b",["","c","d"],"e","f"]]')
expectExpand ('"ify{&{&,a&,b&{&,c&,d}&,e&,f&,}}', '&{&,a&,b&{&,c&,d}e&,f&,}')
expectExpand ('&q{&{&,a&,b&{&,c&,d}&,e&,f&,}}', '&{&,a&,b&{&,c&,d}e&,f&,}')
expectExpand ('$a=3 &value{{{[$a]}}}', '{{[3]}}')
expectExpand ('$x=&{&{&q{ab}&q{cd}&q{ef}}&{&q{gh}&q{ij}&q{kl}}&{&q{mn}&q{op}&q{qr}}} $x', 'abcdefghijklmnopqr')
expectExpand ('$x=&{&{&q{ab}&q{cd}&q{ef}}&{&q{gh}&q{ij}&q{kl}}&{&q{mn}&q{op}&q{qr}}} &nth{0}$x', 'abcdef')
expectExpand ('$x=&{&{&q{ab}&q{cd}&q{ef}}&{&q{gh}&q{ij}&q{kl}}&{&q{mn}&q{op}&q{qr}}} &nth{1}&nth{0}$x', 'cd')
// xget
expectExpandQuote ('"e&xget{~abc}', '&xget~abc')
expectExpand ('&xget~abc', 'def')
expectExpand ('&xget~world', '[world|planet]')
// call, apply, function
expectExpand ('$func=&function$first$second{0="ify$$0 1=$first 2=$second} &call{$func}{A}{B}', '0=&{A&,B} 1=A 2=B')
expectExpand ('$func=&function{$first$second}{0="ify$$0 1=$first 2=$second} &call{$func}{A}{B}', '0=&{A&,B} 1=A 2=B')
expectExpand ('$func=&function{$first,$second}{0="ify$$0 1=$first 2=$second} &call{$func}{A}{B}', '0=&{A&,B} 1=A 2=B')
expectExpand ('$func=&function{}{here we go} &$func &$func', 'here we go here we go')
expectExpand ('$func=&function{here we go} &$func &$func', '=&function{here we go} ')
expectExpand ('$func=&function$first$second{0="ify$$0 1=$first 2=$second} $y=&{one&,two} &apply{$func}$y', '0=&{one&,two} 1=one 2=two')
expectExpand ('&function$first$second{1=$first 2=$second}', '&let$first={$$1}{&let$second={$$2}{1=$first 2=$second}}')
expectExpand ('$x=99 &function$first$second{1=$first 2=$second x=$x}', '&let$first={$$1}{&let$second={$$2}{1=$first 2=$second x=$x}}')
expectExpand ('$x=99 &function$first$second{1=$first 2=$second x=&unquote$x}', '&let$first={$$1}{&let$second={$$2}{1=$first 2=$second x=99}}')
expectExpand ('[a=>cat|cat]&apply$a{}', 'cat')
expectExpand ('&apply"e{x=$$1 y=$$2}&split{3 4}', 'x=3 y=4')
expectExpand ('&apply"e{x=$$1 y=$$2}{3 4}', 'x=3 4 y=')
expectExpand ('"e{&call$f{A}{B}}', '&call$f{A}{B}')
// regexes
expectExpand ('&match/a/{cat}{$$0$$0}', 'aa')
expectExpand ('"ify&match/[aeiou]/g{generic}{&uc$$0}', '&{E&,E&,I}')
expectExpand ('&replace/a/g{catamaran}{u|o}', 'cutomoron', {maxTries:10*maxTries}) // bumping up maxTries because this has failed... should probably do the basic probability calculation required to set this with a given probability of failure (or specify this probability at the level of the whole test suite), given that the probability of success on each try is 1/16 in this case
expectExpand ('&join&split/[aeiou]+/{felicitous}{..}', 'f..l..c..t..s')
expectExpand ('&join&split{a bc d}{,}', 'a,bc,d')
expectExpand ('&join&map&split{a bc def}{"$_"}{, }', '"a", "bc", "def"')
expectExpand ('&grep/a/&split{cat dog man lizard}', 'catmanlizard')
// math
expectExpand ('$a=4 $d=3 $b=5 $c=2 &math{($a * $d) - $b + $c}', '9')
expectExpand ('$a=4 $d=3 $b=5 $c=2 &math{&value{zero} + ($a * $d) - $b + $c}', 'nine')
expectExpand ('$a=4 $d=3 $b=5 $c=2 &math{0.2*$a+$b}', '5.8')
expectExpand ('"e&math{ ( $a * $d )-$b+$c }', '&math{($a * $d) - $b + $c}')
expectExpand ('&math{.5}', '0.5')
expectExpand ('&math{0.5}', '0.5')
// links
expectExpand ('$x=3 &link{test$x}{$x}', '&link{test3}{$x}')
expectExpand ('"e&link{test}{$x}', '&link{test}{$x}')
expectExpand ('[x=>&link{test}{#x#}] #x#', '&link{test}{#x#}')
expectExpand ('[x=>&link{test}{Indeed, this is #x#}] #x#', '&link{test}{Indeed, this is #x#}')
expectExpand ('"e{&link{test}{$x}}', '&link{test}{$x}')
expectExpand ('"e{&link{test}{x=$x}}', '&link{test}{x=$x}')
expectExpand ('"e&reveal{hello}{world}', '&reveal{hello}{world}')
expectExpand ('$x=3 &reveal{test$x}{$x}', '&reveal{test3}{3}')
expectExpand ('"e&reveal{test}{$x}', '&reveal{test}{$x}')
expectExpand ('$x=3 &link{test}{$x}', 'test==>(link)==>$x', {makeLink:function(text,link,type){return text.text+'==>('+type+')==>'+link.text}})
expectExpand ('$x=3 &reveal{test}{$x}', 'test==>(reveal)==>3', {makeLink:function(text,link,type){return text.text+'==>('+type+')==>'+link.text}})
// [[Text with spaces, punctuation and caps.] shorthand for &link{Text with spaces, punctuation and caps.}{#text_with_spaces_punctuation_and_caps#}
expectExpand ('[[]]', '[[]]')
expectExpand ('[[ ]]', '[[ ]]')
expectExpand ('[[x]]', '&link{x}{#x#}')
expectExpand ('[[xYz]]', '&link{xYz}{#xyz#}')
expectExpand ('[[ X! ... ]]', '&link{ X! ... }{#x#}')
expectExpand ('[[Next room.]]', '&link{Next room.}{#next_room#}')
expectExpand ('[[Next room.]]', '&link{Next room.}{#next_room#}')
expectExpand ("[[Apostrophe's OK.]]", "&link{Apostrophe's OK.}{#apostrophes_ok#}")
// layout
expectExpand ('"e&layout{+001,-0002}{hello}', '&layout{1,-2}{hello}')
expectExpand ('&layout{1,2}{hi}', 'hi')
expectExpand ('&placeholder{}{1,2}', '')
expectExpand ('&placeholder$x{1,2}', '')
expectExpand ('&placeholder~test1{1,2}', '')
expectExpand ('"e&placeholder{}{1,2}', '&placeholder{}{1,2}')
expectExpand ('"e&placeholder$x{1,2}', '&placeholder$x{1,2}')
expectExpand ('"e&placeholder~test1{1,2}', '&placeholder~test1{1,2}')
// @x,y notation for layout, and [text]{target} shorthand for &link{text}{target}
expectExpand ('"e[a@2,3=>hello|there]', '[a@2,3=>hello|there]')
expectExpand ('"e[a@(2,3)=>hello|there]', '[a@2,3=>hello|there]')
expectExpand ('@1,2:START', '')
expectExpand ('"e{@1,2:START}', '&placeholder{}{1,2}')
expectExpand ('@1,2:START', '')
expectExpand ('"e{@1,2:START}', '&placeholder{}{1,2}')
expectExpand ('"e{@1,2$x}', '&placeholder$x{1,2}')
expectExpand ('"e{@1,2~test1}', '&placeholder~test1{1,2}')
expectExpand ('[hello]{world}', '&link{hello}{world}')
expectExpand ('$x=hi [$x]{$x world}', '&link{hi}{$x world}')
expectExpand ('[hello]@100,200{world}', '&link{hello}{world}')
expectExpand ('"e{[hello]@100,200{world}}', '&layout{100,200}{&link{hello}{world}}')
// Some weird infinite-loop bug that only showed up with certain self-referential recursions
// Turned out to be a missing property in the object returned by the maxDepth terminator.
maxDepthConfig = { maxDepth: 5 };
expectExpand ('[x=>&link@{a}{b #x#} [[y]]]#x#', '&link@{a}{b &link@{a}{b } &link{}{}} &link{y}{&if{$y}then{&eval{}}else{~y}}', maxDepthConfig)
expectExpand ('[x=>&link{a}{#b#}#x#]#x#', '&link{a}{&if{$b}then{&eval{}}else{~b}}&link{}{}', maxDepthConfig)
expectExpand ('$x="e{&link{a}{#b#}&eval$x}&eval$x', '&link{a}{&if{$b}then{&eval{}}else{~b}}&link{a}{&if{$b}then{&eval{}}else{~b}}&link{a}{&if{$b}then{}else{}}&link{}{}&link{}{}', maxDepthConfig)
expectExpand ('$x="e{&link{a}{&if{b}{c}{d}}&eval$x}&eval$x', '&link{a}{&if{b}then{c}else{d}}&link{a}{&if{b}then{c}else{d}}&link{a}{&if{b}then{}else{}}&link{}{}&link{}{}', maxDepthConfig)
expectExpand ('$x="e{&link{a}{&if{b}then{c}else{d}}&eval$x}&eval$x', '&link{a}{&if{b}then{c}else{d}}&link{a}{&if{b}then{c}else{d}}&link{a}{&if{b}then{}else{}}&link{}{}&link{}{}', maxDepthConfig)
// charclass, alt
expectExpand ('&charclass{abc}', '[a|b|c]')
expectExpand ('&charclass{a-e}', '[a|b|c|d|e]')
expectExpand ('&charclass{a-ej}', '[a|b|c|d|e|j]')
expectExpand ('&charclass{a\\\\-j\\|\\\\}', '[a|-|j|\\||\\\\]')
expectExpand ('&eval&charclass{a\\\\-j\\|\\\\}', 'a', {maxTries:maxTries})
expectExpand ('&eval&charclass{a\\\\-j\\|\\\\}', '-', {maxTries:maxTries})
expectExpand ('&eval&charclass{a\\\\-j\\|\\\\}', 'j', {maxTries:maxTries})
expectExpand ('&eval&charclass{a\\\\-j\\|\\\\}', '|', {maxTries:maxTries})
expectExpand ('&eval&charclass{a\\\\-j\\|\\\\}', '\\', {maxTries:maxTries})
expectExpand ('&alt&list{&split{all your base}&split{are belong to us}}', '[[all|your|base]|[are|belong|to|us]]')
expectExpand ('$b=&charclass{ -x}[a=>#a##b##a#|cat]&json&parse#a#{cat cat}', '[["root",["#a#",["alt",["#a#",["alt","cat"]],["#b#",["alt"," "]],["#a#",["alt","cat"]]]]]]')
expectExpand ('[a=>#a#&unquote&charclass{ -x}#a#|cat]&json&parse#a#{cat cat}', '[["root",["#a#",["alt",["#a#",["alt","cat"]],["alt"," "],["#a#",["alt","cat"]]]]]]')
// if
expectExpand ('$x=1 &if$x{hello} there', 'hello there')
expectExpand ('$x=1 &if¬$x{hello} there', ' there')
expectExpand ('$x=1 &if¬$x{hello}{hi} there', 'hi there')
// prob
expectExpand ('&prob{.5}{a}{b}', 'a', {maxTries:maxTries})
expectExpand ('&prob{.5}{a}{b}', 'b', {maxTries:maxTries})
expectExpand ('~coinflip', 'heads', {maxTries:maxTries}) // test bug that occurred when &random was permanently cached when called from a user-defined symbol, so future results were always the same
expectExpand ('~coinflip', 'tails', {maxTries:maxTries})
// wrapper for individual 'for a given input (lhs), expect the following output (rhs)'-style tests
// (lhs/rhs = left/right hand side)
function expectExpand (lhs, rhs, config) {
var fail = config && config.fail
function verify (text, done) {
text = bracery.ParseTree.makeString (text)
if (fail)
assert.notEqual (text, rhs)
else
assert.equal (text, rhs)
done()
}
it('should expand ' + (lhs || 'the empty string').replace(/\n/g,'\\n') + ' to ' + (rhs || 'the empty string').replace(/\n/g,'\\n')
+ (config ? (' with ' + JSON.stringify(config)) : ''),
testRunner (lhs, rhs, extend ({ enableParse: true }, config), verify))
}
// wrapper for quote/quasiquote equivalence tests
function expectExpandQuote (lhs, rhs, config) {
expectExpand (lhs, rhs, config)
expectExpand (lhs.replace(/"e/g,'&strictquote'), rhs, config)
}
}
// initialization test
describe('initialization', function() {
it('should initialize', function (done) {
b = new bracery.Bracery (initJson)
done()
})
it('should have ' + nTestSymbols + ' rules', function (done) {
assert.equal (Object.keys(b.rules).length, nTestSymbols)
done()
})
})
// wrappers for different groups of tests
describe('synchronous tests', function() {
doTests (function (lhs, rhs, config, verify) {
var maxTries = (config && config.maxTries) || 1
return function (done) {
var text
for (var n = 0; text !== rhs && n < maxTries; ++n)
text = b.expand (lhs, config).text
verify (text, done)
}
})
})
describe('asynchronous tests', function() {
doTests (function (lhs, rhs, config, verify) {
var maxTries = (config && config.maxTries) || 1
return function (done) {
function tryExpand (n) {
n = n || 0
b.expand (lhs,
extend
({},
config,
{ callback: function (expansion) {
var text = bracery.ParseTree.makeString (expansion.text)
if (text !== rhs && n < maxTries)
tryExpand (n + 1)
else
verify (text, done)
} }))
}
tryExpand()
}
})
})
describe('idempotent double expansion tests', function() {
doTests (function (lhs, rhs, config, verify) {
var maxTries = (config && config.maxTries) || 1
return function (done) {
var expand
var b2 = config && config.rules ? new bracery.Bracery(config.rules) : b
var config2 = extend ({}, config, {rules:null})
for (var n = 0; !(expand && expand.text === rhs) && n < maxTries; ++n)
expand = b2.expand (lhs, config2)
var text = bracery.ParseTree.makeRhsExpansionText (extend ({ rhs: expand.tree,
vars: {} },
b2.makeConfig (config2)))
verify (text, done)
}
})
})