-
Notifications
You must be signed in to change notification settings - Fork 15
/
test.pl
2321 lines (1945 loc) · 66 KB
/
test.pl
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim: ft=perl6
use Test;
plan 1006;
ok 1, "one is true";
ok 2, "two is also true";
ok !(0), "zero is false";
ok 2 + 2 == 4, "two and two makes four";
{
my $x = 2 + 2;
ok $x == 4, "two and two can be stored in a variable";
}
ok 2 < 3, "two is less than three";
ok !(3 < 1), "three is not less than one";
ok 42 / 3 == 14, "division comes out in the right order";
{
my $x = 2;
ok (--$x) == 1, "predecrement returns new value";
ok $x == 1, "value clearly decremented";
ok ($x++) == 1, "postincrement returns old";
ok $x == 2, "but value increased";
}
{
my $x = 2;
my $y := $x;
ok $y == 2, "binding shares old value";
$x = 5;
ok $y == 5, "changing old changes new";
$y = 4;
ok $y == 4, "changing new changes old";
}
{
sub fib($n) {
if $n <= 2 {
1
} else {
fib($n - 1) + fib($n - 2);
}
}
ok fib(10) == 55, "recursion works";
}
{
my $n = 9;
my $a = 1;
my $b = 1;
while --$n >= 0 {
my $c = $a + $b;
$a = $b;
$b = $c;
}
ok $a == 55, "looping works";
}
{
sub foo() { 42 }
ok (foo) == 42, "can call argless function without parens";
}
ok !Cool, "undefined type objects are false";
ok !Cool.defined, "type objects are undefined";
ok "Foo".defined, "strings are defined";
ok !Str.defined, "derived type objects are still undefined";
ok "foo" eq "foo", "equal strings are equal";
ok !("foo" ne "foo"), "equal strings are not not equal";
ok "foo" ne "bar", "unequal strings are unequal";
ok Cool === Cool, "identical objects are identical";
ok !(Cool === Any), "unidentical objects are unidentical";
ok 12 eq "12", "eq stringifies";
ok ("a" ~ "b") eq "ab", "a + b = ab";
ok ("a" ~ "b" ~ "c") eq "abc", "a + b + c = abc";
ok (?1) eq "True", "True strings to True";
ok (?0) eq "False", "False strings to False";
ok False ~~ Bool && !False, "False is the right constant";
ok True ~~ Bool && True, "True is the right constant";
ok Bool::False ~~ Bool && !Bool::False, "Bool::False is the right constant";
ok Bool::True ~~ Bool && Bool::True, "Bool::True is the right constant";
ok Any.gist eq "(Any)", "Any.gist is (Any)";
ok (2.WHAT === 3.WHAT), "different objects get the same WHAT";
ok !(2.WHAT.defined), "WHATs are undefined type objects";
ok (sub foo() {}).WHAT.gist eq '(Sub)', 'WHAT of a Sub is (Sub)';
ok "Foo".WHAT === Str, 'WHAT of a Str *is* Str';
ok "Foo".HOW.WHAT.gist eq '(ClassHOW)', 'anything.HOW is a ClassHOW';
ok "Foo".HOW === "Cow".HOW, 'objects of the same class have the same HOW';
ok !("Foo".HOW === Any.HOW), 'objects of different classes have different HOWs';
{
my class Foo {
method zow() {
"A";
}
method !moo() { "Z" }
method moop() { self!moo }
method zilch() {
"B";
}
method crow($x) {
$x * $x;
}
method pie() {
self.zow
}
}
my class Bar is Foo {
method zow() {
"C";
}
}
ok !Foo.defined, "class type objects are undefined";
ok !Bar.defined, "even derived ones";
ok Foo.zow eq 'A', "can call defined methods";
ok Foo.moop eq 'Z', "can call 'private' methods";
ok Bar.zow eq 'C', "subclasses can override methods";
ok Bar.zilch eq 'B', "not overriden methods are inherited";
ok Foo.crow(13) == 169, "can call methods with arguments";
ok Foo.pie eq 'A', "can call methods through self";
ok Bar.pie eq 'C', "calls through self are virtual";
}
{
my $x = 0;
{
START { $x = 1 };
ok $x, "START blocks are run";
}
}
{
my $x = '';
{
$x = $x ~ '1';
START { $x = $x ~ '2'; }
$x = $x ~ '3';
}
ok $x eq '123', "START blocks are run in order with other code";
}
{
my $x = '';
my $y = 0;
while $y < 3 {
$x = $x ~ '1';
START { $x = $x ~ '2'; }
$x = $x ~ '3';
$y++;
}
ok $x eq '1231313', "START blocks are only run once";
}
{
my $x = '';
my $z = 0;
while $z < 2 {
my $y = 0;
while $y < 3 {
$x = $x ~ '1';
START { $x = $x ~ '2'; }
$x = $x ~ '3';
$y++;
}
$z++;
}
ok $x eq '12313131231313', "START blocks reset on clone";
}
{
my $x = sub () { 5 };
ok $x() == 5, 'Anonymous functions can be called';
sub const($y) { sub { $y } }
my $z = const(42);
ok $z, "subs are true";
ok $z() == 42, "subs close over lexicals";
my $w = const(81);
ok !($w === $z), "sub returns different values in different clonings";
ok $w() == 81, "new sub captures new values";
ok $z() == 42, "old sub keeps old value";
}
{
sub accum() {
anon sub go() {
state $x = 0;
$x++;
}
}
my $f = accum;
my $g = accum;
ok $f() == 0, "state variables can be initialized";
ok $f() == 1, "state variables preserve values";
ok $g() == 0, "different clones have different state vars";
}
{
my class A { }
my class B is A { }
my class C is B { }
ok A.^isa(Any), "a new class is Any";
ok B.^isa(A), "a subclass is the superclass";
ok C.^isa(A), "isa is transitive";
ok !(A.^isa(B)), "a superclass is not the subclass";
ok B.^does(A), "a subclass does the superclass";
ok !(A.^does(B)), "a superclass not-does the subclass";
}
ok "Foo".^isa(Str), "strings are Str";
ok (?1).^isa(Bool), "booleans are Bool";
ok (1.HOW).^isa(ClassHOW), "class objects are ClassHOW";
{
my $canary = 1;
ok 1 || ($canary = 0), "1 || ? returns true";
ok $canary, "without touching the rhs";
ok !(0 && ($canary = 0)), "0 && ? returns false";
ok $canary, "without touching the rhs";
ok (0 // ($canary = 0)) eq '0', "0 // ? returns 0";
ok $canary, "without touching the rhs";
ok (12 && 34) == 34, "12 && 34 -> 34";
ok (2 andthen "three") eq "three", '2 andthen three -> three';
ok (12 || 34) == 12, '12 || 34 -> 34';
ok (0 || 34) == 34, '0 || 34 -> 34';
}
{
my $x = 42;
ok (do $x) == 42, "do <statement> sees a lexical";
ok (do { $x }) == 42, "do <block> sees a lexical";
ok (do { my $x = 42; $x }) == 42, "do <block> sees a new lexical";
}
{
ok 42.ACCEPTS(42), "accepts can match stuff";
ok 12 ~~ "12", "strings match string value";
ok "Hi" ~~ Str, "types match identity";
ok (?0) ~~ (?1), "Bool.ACCEPTS ignores its argument";
}
{
constant foo = 42;
constant $bar = 51;
ok foo == 42, "constants without sigils work";
ok $bar == 51, "constants with sigils work";
}
{
ok "Hello".substr(1,3) eq "ell", "substr works";
ok "Hello".chars == 5, ".chars works";
}
{
my $buf = '';
sub cat(*@x) {
while @x {
$buf = $buf ~ @x.shift;
}
$buf;
}
ok cat(1, (2, 3), ((4, 5), ((), 7))) eq "123457", "parcels flatten";
}
{
my package Foo { our $x = 42; }
ok $Foo::x == 42, "can access our vars";
my module Bar { our $x = 42; }
ok $Bar::x == 42, "module accepted too";
my class Baz { our $x = 42; }
ok $Baz::x == 42, "and class";
ok !$Cow::x.defined, "package variables autoviv to undef";
$Cow::x = 51;
ok $Cow::x == 51, "but can still hold values";
}
{
our $kluw = 99; #OK
ok $GLOBAL::kluw == 99, "GLOBAL:: works";
ok $OUR::kluw == 99, "OUR:: works";
}
{
my class Foo {
has $.a;
has $!b;
method test() {
ok self.a == 33, "values accessible as self.a";
ok $.a == 33, 'values accessible as $.a';
$!b = 44;
ok $!b == 44, 'private values accessible as $!b';
ok $!a == 33, 'public values so accessible too';
}
}
my class Bar is Foo {
has $.c;
}
my $x = Foo.new;
my $y = Bar.new;
$x.a = 33;
$x.test;
$y.a = 33;
$y.test;
$y.c = 55;
ok $y.c == 55, "subclass attributes work";
}
{
sub foo($v) {
ok $*FOO == $v, "contextuals accessible";
}
my $*FOO = 99;
foo(99);
ok !$*BAR.defined, "contextuals undefined are undef";
$PROCESS::qaax = 555;
ok $*qaax == 555, "contextuals default to PROCESS";
$GLOBAL::qeex = 5;
$PROCESS::qeex = 3;
ok $*qeex == 5, "GLOBAL takes precedence";
$GLOBAL::quux = 111;
ok $*quux == 111, "contextuals default to GLOBAL too";
{
my $*quux = 222;
ok $*quux == 222, "but can be overriden";
}
ok $*quux == 111, "but only in the one scope";
}
{
my $whatever = *;
ok $whatever.WHAT.gist eq '(Whatever)', "can call methods on a specific Whatever";
my $wwhat = *.WHAT;
ok !($wwhat.^isa(Whatever)), "method calls against * curry, though";
ok (* + 5)(2) == 7, "can curry simple binops";
is ((*) eq "foo"), Bool::False, "parens defeat Whatever directly";
ok (1 + 2 * *)(5) == 11, "nested Whatever works";
ok (2 * (1 + *))(5) == 12, "parens for grouping do not kill WhateverCode";
ok (* + *)(5,12) == 17, "multiple *, multiple args";
ok ((2 * *) + (3 * *))(13,19) == 83, "even from groups";
ok (1,*).^isa(Parcel), "infix:<,> doesn't curry";
}
{
my $x1; my $x2; my $x3; my $x4;
$x1 = 1 if 0;
$x2 = 1 if 1;
$x3 = 1 unless 0;
$x4 = 1 unless 1;
ok !$x1, "if 0 doesn't execute";
ok $x2, "if 1 does execute";
ok $x3, "unless 0 does execute";
ok !$x4, "unless 1 doesn't execute";
}
{
ok ({ $_ * $_ })(20) == 400, '$_ treated as placeholder';
ok ({ $^a - $^b })(5,3) == 2, '$^x treated as such';
ok ({ $^b - $^a })(3,5) == 2, '... in the right order';
}
{
my $canary = 1;
ok !(1 > 2 > ($canary = 0)), "1 > 2 > ... is false";
ok $canary, "short circuitally";
ok !(1 < 3 < 2), "1 < 3 < 2 is false";
ok (1 < 2 < 3), "1 < 2 < 3 is true";
}
ok (-42) + 42 == 0, "unary minus works";
{
ok Q:to/EOA/.substr(0,5) eq 'Hello', "heredocs work";
Hello
EOA
ok Q:to/EOB/ eq Q:to/EOC/, "multiple heredocs in a line work";
Foo
EOB
Foo
EOC
}
{
my @x;
ok @x ~~ Array, '@x isa Array';
ok @x.elems == 0, 'no elements';
ok +@x == 0, 'no elements (+)';
@x.push(5);
ok @x.elems == 1, 'one element now';
ok @x.shift == 5, 'element removed is 5';
@x.push(7,8);
ok @x.elems == 2, "added two elements";
ok @x.shift == 7, "removed first correctly";
ok @x.shift == 8, "removed second correctly";
ok @x.elems == 0, "no elements again";
my $k = 2;
@x.push($k);
$k = 3;
ok @x.shift == 2, "push copies";
@x.push(11,12);
ok @x.pop == 12, "pop is LIFO (1)";
ok @x.pop == 11, "pop is LIFO (2)";
ok +@x == 0, "pop removed all elements";
my @y = 15;
ok @y.elems == 1, "assigning a single value makes a single-item list";
ok @y.shift == 15, "and the value came right!";
@y = 1, 2, 3;
ok @y.elems == 3, "list assignment gets correct item count";
is @y.join("|"), '1|2|3', "assignment of multiple values works properly";
@y = 1, (2, 3), 4;
ok @y.elems == 4, "list assignment flattens (1)";
is @y.join("|"), '1|2|3|4', "list assignment flattens (2)";
@y = 5, @y;
ok @y.elems == 5, "self assignment works (1)";
is @y.join("|"), '5|1|2|3|4', "self assignment works (2)";
}
{
my $a = 0;
my $b = 0;
my $c = 0;
my $d = 0;
my $e = 0;
augment class Any { trusts GLOBAL }
(Any!Any::butWHENCE({ $a = 1 }));
my $x := (Any!Any::butWHENCE({ $b = 1 })); #OK not used
my $y ::= (Any!Any::butWHENCE({ $c = 1 })); #OK not used
my $z = (Any!Any::butWHENCE({ $d = 1 })); #OK not used
(Any!Any::butWHENCE({ $e = 1 })) = 2;
ok !$a, "no autovivification in void context";
ok $b, "autovivification after rw bind";
ok !$c, "no autovivification after ro bind";
ok !$d, "no autovivification after rvalue context";
ok $e, "autovivification after lvalue context";
}
{
sub postcircumfix:<[ ]>($a, $b) { $a ~ "|" ~ $b }
is 1[2], "1|2", "can call postcircumfix [ ]";
}
{
sub postcircumfix:<{ }>($a, $b) { $a ~ "|" ~ $b }
is 1{2}, "1|2", 'can call postcircumfix { }';
}
{
my @arr = <a b c>;
is @arr.join("|"), 'a|b|c', "word splitter works";
my @narr;
@narr[0];
ok +@narr == 0, "rvalue reference to out of range value does not add";
@narr[2] = 5;
ok +@narr == 3, "assigning to element 2 makes length 3";
ok !(@narr[0].defined), "first element undefined";
ok !(@narr[1].defined), "second element undefined";
ok @narr[2] == 5, "third element properly assigned";
my @darr;
@darr[1][1];
ok +@darr == 0, "rvalue nested reference, no effect";
@darr[2][2] = 'pie';
ok +@darr == 3, "outer level vivifies elements";
ok @darr[2] ~~ Array, "inner Array created";
is @darr[2][2], 'pie', "inner value retained";
}
{
sub postcircumfix:<{ }> { @_.join("|") }
is 1<2 3>, '1|2|3', "angle bracket postcircumfix works";
}
{
my $foo;
ok !($foo<x>.defined), "fetch from hash, no value";
ok !($foo.defined), "no autoviv for rvalue";
$foo<x> = 'foo';
is $foo<x>, 'foo', "values are retained";
ok !($foo<y>.defined), "no cross-slot leakage";
ok $foo ~~ Hash, "foo isa hash now";
$foo<z><a> = 'pie';
is $foo<z><a>, 'pie', "can autoviv deeply";
$foo<y>[2] = 'zed';
is $foo<y>[2], 'zed', "can mix array and hash viv";
$foo<12> = 'fry';
is $foo{12}, 'fry', "keys are strings";
}
{
ok ("a" ~~ /a/), "letter matches itself";
ok !("a" ~~ /b/), "letter does not match other";
ok ("xxa" ~~ /a/), "leading garbage ignored";
ok ("axx" ~~ /a/), "trailing garbage ignored";
ok ("ab" ~~ /ab/), "sequence matches sequence";
ok !("ab" ~~ /ba/), "sequence requires order";
ok ("abc" ~~ /ab?c/), "conditional can match";
ok ("ac" ~~ /ab?c/), "conditional can match nothing";
ok !("adc" ~~ /ab?c/), "conditional cannot match something else";
ok ("ac" ~~ /ab*c/), "kleene closure can match none";
ok ("abbc" ~~ /ab*c/), "kleene closure can match many";
ok !("adc" ~~ /ab*c/), "kleene closure cannot match other";
ok ("abc" ~~ /ab+c/), "plus can match one";
ok ("abbbc" ~~ /ab+c/), "plus can match many";
ok !("adc" ~~ /ab+c/), "plus cannot match other";
ok !("ac" ~~ /ab+c/), "plus cannot match none";
grammar Bob {
rule TOP {ab*c}
}
ok Bob.parse("abbc"), "grammars work (1)";
ok !Bob.parse("adc"), "grammars work (2)";
ok !Bob.parse("xac"), "grammars anchor (1)";
ok !Bob.parse("acx"), "grammars anchor (2)";
}
{
my $x = False;
my $y = False;
my @l1 := gather do { $x = True; take 1; $y = True };
ok !$x, "gather does not run block immediately";
ok @l1.shift == 1, "first value pulled";
ok $x, "pull started block";
ok !$y, "but did not finish it";
ok !@l1, "no more values";
ok $y, "querying that fact finished the block";
}
{
my grammar G1 {
regex TOP { <.foo> }
regex foo { x }
}
ok G1.parse("x"), "subrules work (positive)";
ok !G1.parse("y"), "subrules work (negative)";
my grammar G2 {
regex TOP { y <.foo> <.foo> y }
regex foo { x }
}
ok G2.parse("yxxy"), "subrule position tracking works";
ok !G2.parse("yxy"), "subrule position tracking works (2)";
my grammar G3 {
regex TOP { <moo> }
regex moo { x }
}
ok G3.parse("x"), "capturing subrules work (positive)";
ok !G3.parse("y"), "capturing subrules work (negative)";
}
{
ok (&infix:<+>)(2,2) == 4, '&infix:<+> syntax works';
}
{
sub foo($x = 5) { $x }
ok foo() == 5, "defaults operate";
ok foo(19) == 19, "can override defaults";
ok !foo(Any).defined, "defaults do not misfire";
my $y = 0;
sub bar($x = $y++) { $x }
ok bar() == 0, "expressional defaults operate";
ok bar() == 1, "expressional defaults are called each time";
ok bar(3) == 3, "expressional defaults can be overridden";
ok bar() == 2, "when not used, expressional defaults are not called";
}
{
sub foo($x?) { $x }
ok !foo().defined, "defaults to undef";
ok foo(19) == 19, "can override optionals";
sub bar($x?, $y?) { ($x // 5), ($y // 9) }
is bar().join("|"), "5|9", "2x defaulting works";
is bar(10,20).join("|"), "10|20", "2x overriding works";
is bar(10).join("|"), "10|9", "one value hits the right parameter";
}
{
is :foo.value, Bool::True, ":foo is true";
is :!foo.value, Bool::False, ":!foo is false";
is :foo<12>.value, '12', ":foo<12> is 12";
is :foo.key, 'foo', ":foo is foo";
is (foo => 1).key, 'foo', "foo => 1 keeps key";
is (foo => 1).value, '1', "foo => 1 keeps value";
is ("foo" => 1).key, 'foo', '"foo" => 1 keeps key';
is ("foo" => 1).value, '1', '"foo" => 1 keeps value';
my %hash;
ok %hash ~~ Hash, '%-vars are Hash';
}
{
ok ("aab" ~~ /a* ab/), "a*ab backtracks";
ok !("aab" ~~ /a*: ab/), "a*: ab doesn't";
ok ("aab" ~~ /a*! ab/), "a*! ab backtracks";
ok !("aab" ~~ /:r a* ab/), "ratcheting a* ab does not";
ok !("aab" ~~ /:r a*: ab/), "ratcheting a*: ab does not";
ok ("aab" ~~ /:r a*! ab/), "ratcheting a*! ab does";
ok !("aab" ~~ token { a* ab }), "a* ab in a token does not";
ok ("ab ab" ~~ / ab <.ws> ab /), "ws matches a space";
ok (q:to/end/ ~~ / ab <.ws> ab /), "ws matches a newline";
ab
ab
end
ok ("ab ab" ~~ / ab <.ws> ab /), "ws matches several spaces";
ok !("abab" ~~ / ab <.ws> ab /), "ws does not match nothing";
ok ("ab ab" ~~ rule { ab ab }), "rule gives space";
}
{
sub meow(*@x) {
is @x[0], 'a', "can index [0] slurpies";
is @x[1], 'b', "can index [1] slurpies";
}
meow('a', 'b');
# doing a more reasonable test will probably require embedded blocks
ok "foobarx" ~~ / [ foo | foobar ]: x /, "LTM picks longest even if second";
ok "foobarx" ~~ / [ foobar | foo ]: x /, "LTM picks longest even if first";
}
{
my $x = '';
ok !("a" ~~ / a { $x = 1; } b /), '{} does not terminate regex';
is $x, 1, '{} is run even if regex fails';
$x = '';
ok !("" ~~ / a { $x = 1; } b /), '{} does not affect regex that ends before it';
is $x, '', '{} is only run if reached';
$x = 0;
ok ("aab" ~~ / a* { $x++ } ab /), '{} does not block backtracking';
is $x, 2, '{} is run multiple times when backtracking';
$x = '';
ok ("foo" ~~ / foo { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ foo|foo";
is $x, 1, "with no other constraints, first item is used";
$x = '';
ok ("foo" ~~ / fo* { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ fo*|foo";
is $x, 2, "longer literal prefix wins over seniority";
$x = '';
ok ("fooo" ~~ / fo* { $x = $x ~ 1 } | foo { $x = $x ~ 2 } /),
"foo ~~ fo*|foo";
is $x, 1, "longer length wins over prefix";
$x = '';
ok !("fooo" ~~ / [ fo*: { $x = $x ~ 1 } | foo { $x = $x ~ 2 } ] x /),
"foo !~~ [fo*:|foo]x";
is $x, '12', "will backtrack into shorter token";
our $x6474;
my grammar G5 {
token a { foo }
token b { foobar }
token c { <a> | <b> }
token d { <c> x }
token e { x <e> x | y }
token TOP { A <d> | E <e> }
}
ok G5.parse('Afoobarx'), 'LTM works even through subrules';
ok G5.parse('Exxyxx'), 'recursivity does not crash LTM';
my grammar G6 {
token a { fo* { $x6474 = 1 } }
token b { foo { $x6474 = 2 } }
token TOP { <a> | <b> }
}
G6.parse("foo");
is $x6474, 2, "prefix length testing works in subrules";
}
{
is "\x63", "c", '\x works';
is "Y\x63", "Yc", 'can put stuff before escapes';
is "\x63Y", "cY", 'can put stuff after escapes';
is "Y\x63Y", "YcY", 'can put stuff before and after escapes';
is "\x[63,69]", "ci", 'bracketed \x works';
is "\x4E03", "七", '\x with >2 characters works';
is "七".chars, 1, "nana is one kanji";
is "\\", "\x5C", 'can backslash backslashes';
is "\"", "\x22", 'can backslash quotes';
is '\'', "\x27", 'can backslash single quotes';
is "\b", "\x08", '\b works';
is "\a", "\x07", '\a works';
# punt named forms for now
is "\e", "\x1B", '\e works';
is "\f", "\x0C", '\f works';
is "\n", "\x0A", '\n works';
is "\r", "\x0D", '\r works';
is "\t", "\x09", '\t works';
is "\o[61,63,65]", '135', '\o works (bracketed)';
is "\o67", '7', '\o works (bare)';
is "\0", "\x00", '\0 works';
is "foo { 2 + 2 } bar", "foo 4 bar", "code interpolation works";
my $cow = 'hi';
is "foo $cow bar", "foo hi bar", '$-interpolation works';
is "foo $cow.substr(0,1) bar", "foo h bar", 'methodcall interpolation works';
}
{
my grammar G7 {
proto token tok {*}
token tok:sym<+> { <sym> }
token tok:foo { <sym> }
rule TOP { <tok> }
}
ok G7.parse('+'), "can parse :sym<> symbols";
ok G7.parse('foo'), "can parse : symbols";
}
{
sub t1(*@k, :$x, :y($)) { $x } #OK
sub t2(*@k, :y($x), :x($)) { $x } #OK
sub t3(*@k, :y(:$x)) { $x } #OK
ok !t1.defined, "no arg, no value";
ok !t1(12).defined, "positional is not enough";
ok t1(x => 5) == 5, "can pass argument (fatarrow)";
ok !t1("x" => 5), "quoted fatarrow doesn't work";
ok !t1((x => 5)), "parenned fatarrow doesn't work";
ok t1(:x(6)) == 6, "colonpair works";
ok !t1(:y(7)).defined, "wrong name, no cigar";
ok t2(y => 9) == 9, ":y(\$x) syntax picks out y as name";
ok !t2(x => 10).defined, "x is NOT a usable name";
ok t3(:x(11)) == 11, ":y(:\$x) works for both (1)";
ok t3(:y(11)) == 11, ":y(:\$x) works for both (2)";
}
{
ok "\n" ~~ /\n/, '\n in regex matches literal NL';
ok !('\n' ~~ /\n/), '\n in regex does not match literal \n';
ok '+' ~~ /\+/, '\+ in regex matches literal +';
ok '\\' ~~ /\\/, '\\\\ in regex matches literal \\';
ok 'a' ~~ /\x61/, '\x61 in regex matches literal a';
ok 'xy' ~~ /x <?> y/, '<?> matches null string';
my $a = 2;
my $b ::= $a;
$a = 5;
is $b, 2, "ro binding loses original container";
ok 'xxy' ~~ /x { $a = $/.pos } /, "can match with \$/ stuff";
is $a, 1, '$/.pos is the right sort of thing';
'xxy' ~~ /x { $a = ($¢ ~~ Cursor) }/;
is $a, True, '$¢ isa Cursor';
}
{
sub infix:<@>($x, $y, :$z) { $x, $y, $z }
is (1 @ 2 :z(3)).join("|"), "1|2|3", "adverbs on infix ops work";
}
{
my $x = 4;
$x += 3;
is $x, 7, "metaop += works";
sub testx:sym«foo bar»() { 42 }
is &testx:sym<foo bar>(), 42, "can use french quotes in declarations";
sub foo(Str $x) { $x ~ $x }
is foo("bar"), "barbar", "can parse type constraints";
}
rxtest /x.y/, "x.y", ("xay", "x y"), ("xy", "xaay");
rxtest /<!>/, '<!>', (), ("", "x");
rxtest /\s/, '\s', (" ", ("\n" => '\n'), ("\r" => '\r'), "\x3000"),
("x", "1", "+");
rxtest /\S/, '\S', ("x", "1", "+"),
(" ", ("\n" => '\n'), ("\r" => '\r'), ("\x3000" => 'id space'));
rxtest /\w/, '\w', ("x", "1", "_", "\x4E00"), ("+", " ");
rxtest /<[ y ]>/, '<[ y ]>', ("y",), (" ", "x", "z");
rxtest /<[ i .. k ]>/, '<[ i .. k ]>', ("i", "j", "k"), ("h", "l");
rxtest /<[ \W a..z ]>/, '<[\W a..z]>', ("a", "z", "+"), ("\x4E00",);
rxtest /a || b/, 'a || b', ("a", "b"), ("c", "");
rxtest /x [a || aa]: c/, 'x[a||b]:c', ("xac",), ("xaac",);
{
my $obj ::= (class {
method item() { "item" }
method list() { "list" }
method hash() { "hash" }
}).new;
is $($obj), "item", '$() calls item';
is @($obj), "list", '@() calls list';
is %($obj), "hash", '%() calls hash';
is $$obj, "item", '$$ truncated context';
is @$obj, "list", '@$ truncated context';
is %$obj, "hash", '%$ truncated context';
is "x$$obj", "xitem", '$$ interpolation';
is "x@$obj.Str()", "xlist", '@$ interpolation';
is "x%$obj.Str()", "xhash", '%$ interpolation';
}
ok "axy" ~~ / a <before x> \w y / , "before is zero-width";
ok "axy" ~~ / a <?before x> \w y / , "?before is zero-width";
ok "azy" ~~ / a <!before x> \w y / , "!before is zero-width";
ok !("azy" ~~ / a <?before x> \w y /) , "?before x needs x";
ok !("axy" ~~ / a <!before x> \w y /) , "!before x needs !x";
ok '{}' ~~ / \{ <.ws> \} /, 'ws matches between \W';
{
rxtest /z .* y [ a :: x | . ]/, "z.*y[a::x|.]",
("zyax", "zyb", "zyaxya"), ("zya",);
# no ::> until STD gets here...
rxtest /z .* y [ a ::: x || . ]/, "z.*y[a:::x||.]",
("zyax", "zyb"), ("zya", "zyaxya");
my grammar G7 {
proto regex TOP {*}
regex TOP:foo { a :: x }
regex TOP:bar { . }
}
ok G7.parse("ax"), ":: does not block forward";
ok G7.parse("b"), ":: does not affect other paths";
ok !G7.parse("a"), "cannot backtrack past :: in proto ltm";
}
rxtest /y [ [ foo || bar ] | . ]: y/, "|| hides right side from LTM",
("yky","yfooy"), ("ybary",);
rxtest /y [ [a||b] | c ]: y/, "|| exposes a declarative prefix for left only",
("yay","ycy"), ("yby",);
{
# one CgOp bug manifested as a failure to compile this
ok (/ <?before x>: <ws>: /).defined, "unnamed regression";
my $ma = ("ab29x" ~~ /\d+/);
ok $ma.defined, "match is defined";
ok $ma.WHAT === Match, "matches are Match";
is $ma.from, 2, '$ma.from works';
is $ma.to, 4, '$ma.to works';
is $ma.Str, '29', '$ma.Str works';
}
{
use MONKEY_TYPING;
my class Foo {
method foo { 1 }
}
is Foo.foo, 2, "augments run early";
augment class Foo {
method foo { 2 }
}
}
{
my $ma = (grammar {
token TOP { <foo> <bar> }
token foo { \d+ }
token bar { \D+ }
}).parse("123abc");
ok $ma<foo> ~~ Match, "<foo> sub is a Match";
is $ma<foo>, "123", "<foo> sub got 123";
ok $ma<bar> ~~ Match, "<bar> sub is a Match";
is $ma<bar>, "abc", "<bar> sub got 123";
ok !$ma<quux>, "no <quux> sub";
my $mb = (grammar {
token TOP { <foo> <foo> }
token foo { \D+ | \d+ }
}).parse("def456");
ok $mb.defined, "grammar b matched";
ok $mb<foo> ~~ List, "<foo> sub isa List";
is +$mb<foo>, 2, "2 matches";
ok $mb<foo>[0] ~~ Match, "<foo>[0] sub isa Match";
is $mb<foo>[0], "def", "<foo>[0] got def";
is $mb<foo>[1], "456", "<foo>[1] got 456";
my $mc = (grammar {
token TOP { <foo>+ }
token foo { \D+ | \d+ }
}).parse("def");
ok $mc<foo> ~~ List, "<foo>+ makes a list";
is +$mc<foo>, 1, "despite only one element";
my $md = (grammar {
token TOP { \D+ <foo> \D+ }
token foo { \d+ <bar> \d+ }
token bar { \D+ }
}).parse("a1b2c");
is $md<foo><bar>, "b", "<foo><bar> works";
my $mf = (grammar {
proto token TOP {*}
token TOP:x { <foo> }
token foo { \w+ }
}).parse("abc");
is $mf<foo>, "abc", "protoregex captures work";
my $me = (grammar {
token TOP { <tok>+ }
proto token tok {*}
token tok:num { <sign>? <digit>+ }
token tok:ws { \s+ }
token sign { \+ | \- }
token digit { \d | _ }
}).parse("2 -34 5");
ok $me<tok> ~~ List, "a list of tokens";
is +$me<tok>, 5, "5 of them";
is ?($me<tok>[0]<sign>), False, "first no sign";
is ?($me<tok>[2]<sign>), True, "third sign";
is ?($me<tok>[4]<sign>), False, "fifth no sign";
is $me<tok>[2], '-34', "3rd token '-34'";
}
{
rxtest / . << . /, ".<<.", (" x",), ("x "," ","xx");
rxtest / . << /, ".<<", (), ("x", " ");
rxtest / << . /, "<<.", ("x",), (" ",);
rxtest / << /, "<<", (), ("",);
rxtest / . >> . /, ".>>.", ("x ",), (" x"," ","xx");
rxtest / . >> /, ".>>", ("x",), (" ",);
rxtest / >> . /, ">>.", (), ("x"," ");
rxtest / >> /, ">>", (), ("",);
rxtest / . « . /, ".«.", (" x",), ("x "," ","xx");
rxtest / . « /, ".«", (), ("x", " ");
rxtest / « . /, "«.", ("x",), (" ",);
rxtest / « /, "«", (), ("",);
rxtest / . » . /, ".».", ("x ",), (" x"," ","xx");
rxtest / . » /, ".»", ("x",), (" ",);
rxtest / » . /, "».", (), ("x"," ");
rxtest / » /, "»", (), ("",);
rxtest / . ^ . /, ".^.", (), ("x",);
rxtest / . ^ /, ".^", (), ("x",);
rxtest / ^ . /, "^.", ("x",), ();
rxtest / ^ /, "^", ("",), ();
rxtest / . $ . /, '.$.', (), ("x",);
rxtest / . $ /, '.$', ("x",), ();
rxtest / $ . /, '$.', (), ("x",);
rxtest / $ /, '$', ("",), ();
rxtest / . ^^ . /, '.^^.', ("\nx","\n\n"), ("x\n","xx");
rxtest / . ^^ /, '.^^', (), ("x","\n");
rxtest / ^^ . /, '^^.', ("x","\n"), ();
rxtest / ^^ /, '^^', ("",), ();