/
subst.t
831 lines (693 loc) · 31 KB
/
subst.t
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
use v6;
use lib $?FILE.IO.parent(2).add("packages");
use Test;
use Test::Util;
plan 192;
# L<S05/Substitution/>
my $str = 'hello';
is $str.subst(/h/,'f'), 'fello', 'We can use subst';
is $str, 'hello', '.. withouth side effect';
is $str.subst('h','f'), 'fello', '.. or using Str as pattern';
is $str.subst('.','f'), 'hello', '.. with literal string matching';
my $i=0;
is $str.subst(/l/,{$i++}), 'he0lo', 'We can have a closure as replacement';
is $str.=subst(/l/,'i'), 'heilo', '.. and with the .= modifier';
is $str, 'heilo', '.. it changes the receiver';
is 'hello'.subst('e', 3), 'h3llo', 'non-Str replacement works for string form too';
# not sure about this. Maybe '$1$0' should work.
$/ = ['nope'];
is 'a'.subst(/(.)/,"$0"), 'nope', '.. bare strings cannot see $/ because they are evaluated first';
is 'a'.subst(/(.)/,{$0~$0}),'aa', '.. you must wrap it in a closure to delay evaluation';
is '12'.subst(/(.)(.)/,{$()*2}),'24', '.. and do nifty things in closures';
# RT #116224
{
$/ = ('-'); # parens to avoid looking like a P5 irs directive
is 'a'.subst("a","b"), 'b', '"a".subst("a", "b") is "b"';
is $/, '-', '$/ is left untouched';
is 'a'.subst(/a/,"b"), 'b', '"a".subst(/a/, "b") is "b"';
is $/, 'a', '$/ matched "a"';
is 'a'.subst(/x/,"y"), 'a', '"a".subst(/x/, "y") is "a"';
nok $/, '$/ is a falsey';
$_ = 'a';
is s/a/b/, 'a', '$_ = "a"; s/a/b/ is "a"';
is $/, 'a', '$/ matched "a"';
$_ = 'a';
nok s/x/y/, '$_ = "a"; s/x/y/ is a falsey';
nok $/, '$/ is a falsey';
}
{
is 'a b c d'.subst(/\w/, 'x', :g), 'x x x x', '.subst and :g';
is 'a b c d'.subst(/\w/, 'x', :global), 'x x x x', '.subst and :global';
is 'a b c d'.subst(/\w/, 'x', :x(0)), 'a b c d', '.subst and :x(0)';
is 'a b c d'.subst(/\w/, 'x', :x(1)), 'x b c d', '.subst and :x(1)';
is 'a b c d'.subst(/\w/, 'x', :x(2)), 'x x c d', '.subst and :x(2)';
is 'a b c d'.subst(/\w/, 'x', :x(3)), 'x x x d', '.subst and :x(3)';
is 'a b c d'.subst(/\w/, 'x', :x(4)), 'x x x x', '.subst and :x(4)';
is 'a b c d'.subst(/\w/, 'x', :x(5)), 'a b c d', '.subst and :x(5)';
is 'a b c d'.subst(/\w/, 'x', :x(*)), 'x x x x', '.subst and :x(*)';
is 'a b c d'.subst(/\w/, 'x', :x(0..1)), 'x b c d', '.subst and :x(0..1)';
is 'a b c d'.subst(/\w/, 'x', :x(1..3)), 'x x x d', '.subst and :x(0..3)';
is 'a b c d'.subst(/\w/, 'x', :x(3..5)), 'x x x x', '.subst and :x(3..5)';
is 'a b c d'.subst(/\w/, 'x', :x(5..6)), 'a b c d', '.subst and :x(5..6)';
is 'a b c d'.subst(/\w/, 'x', :x(3..2)), 'a b c d', '.subst and :x(3..2)';
# string pattern versions
is 'a a a a'.subst('a', 'x', :g), 'x x x x', '.subst (str pattern) and :g';
is 'a a a a'.subst('a', 'x', :x(0)), 'a a a a', '.subst (str pattern) and :x(0)';
is 'a a a a'.subst('a', 'x', :x(1)), 'x a a a', '.subst (str pattern) and :x(1)';
is 'a a a a'.subst('a', 'x', :x(2)), 'x x a a', '.subst (str pattern) and :x(2)';
is 'a a a a'.subst('a', 'x', :x(3)), 'x x x a', '.subst (str pattern) and :x(3)';
is 'a a a a'.subst('a', 'x', :x(4)), 'x x x x', '.subst (str pattern) and :x(4)';
is 'a a a a'.subst('a', 'x', :x(5)), 'a a a a', '.subst (str pattern) and :x(5)';
is 'a a a a'.subst('a', 'x', :x(*)), 'x x x x', '.subst (str pattern) and :x(*)';
is 'a a a a'.subst('a', 'x', :x(0..1)), 'x a a a', '.subst (str pattern) and :x(0..1)';
is 'a a a a'.subst('a', 'x', :x(1..3)), 'x x x a', '.subst (str pattern) and :x(0..3)';
is 'a a a a'.subst('a', 'x', :x(3..5)), 'x x x x', '.subst (str pattern) and :x(3..5)';
is 'a a a a'.subst('a', 'x', :x(5..6)), 'a a a a', '.subst (str pattern) and :x(5..6)';
is 'a a a a'.subst('a', 'x', :x(3..2)), 'a a a a', '.subst (str pattern) and :x(3..2)';
}
# :nth
{
throws-like '"a b c d".subst(/\w/, "x", :nth(0))', Exception, message => rx/nth/; # RT #125815
is 'a b c d'.subst(/\w/, 'x', :nth(1)), 'x b c d', '.subst and :nth(1)';
is 'a b c d'.subst(/\w/, 'x', :nth(2)), 'a x c d', '.subst and :nth(2)';
is 'a b c d'.subst(/\w/, 'x', :nth(3)), 'a b x d', '.subst and :nth(3)';
is 'a b c d'.subst(/\w/, 'x', :nth(4)), 'a b c x', '.subst and :nth(4)';
is 'a b c d'.subst(/\w/, 'x', :nth(5)), 'a b c d', '.subst and :nth(5)';
# string pattern versions
throws-like '"a a a a".subst("a", "x", :nth(0))', Exception, message => rx/nth/; # RT #125815
is 'a a a a'.subst('a', 'x', :nth(1)), 'x a a a', '.subst (str pattern) and :nth(1)';
is 'a a a a'.subst('a', 'x', :nth(2)), 'a x a a', '.subst (str pattern) and :nth(2)';
is 'a a a a'.subst('a', 'x', :nth(3)), 'a a x a', '.subst (str pattern) and :nth(3)';
is 'a a a a'.subst('a', 'x', :nth(4)), 'a a a x', '.subst (str pattern) and :nth(4)';
is 'a a a a'.subst('a', 'x', :nth(5)), 'a a a a', '.subst (str pattern) and :nth(5)';
}
# combining :nth with :x
{
is 'a b c d e f g h'.subst(/\w/, 'x', :nth(1,2,3,4), :x(3)),
'x x x d e f g h',
'.subst with :nth(1,2,3,4)) and :x(3)';
is 'a b c d e f g h'.subst(/\w/, 'x', :nth(2,4,6,8), :x(2)),
'a x c x e f g h',
'.subst with :nth(2,4,6,8) and :x(2)';
throws-like '"a b c d e f g h".subst(/\w/, "x", :nth(2, 4, 1, 6), :x(3))',
Exception,
'.subst with :nth(2) and :x(3)';
}
{
# :p
is 'a b c d e f g h'.subst(/\w/, 'x', :p(0)),
'x b c d e f g h',
'.subst with :p(0)';
is 'a b c d e f g h'.subst(/\w/, 'x', :p(1)),
'a b c d e f g h',
'.subst with :p(1)';
is 'a b c d e f g h'.subst(/\w/, 'x', :p(2)),
'a x c d e f g h',
'.subst with :p(2)';
# :p and :g
is 'a b c d e f g h'.subst(/\w/, 'x', :p(0), :g),
'x x x x x x x x',
'.subst with :p(0) and :g';
is 'a b c d e f g h'.subst(/\w/, 'x', :p(1), :g),
'a b c d e f g h',
'.subst with :p(1) and :g';
is 'a b c d e f g h'.subst(/\w/, 'x', :p(2), :g),
'a x x x x x x x',
'.subst with :p(2) and :g';
}
{
# :c
is 'a b c d e f g h'.subst(/\w/, 'x', :c(0)),
'x b c d e f g h',
'.subst with :c(0)';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(1)),
'a x c d e f g h',
'.subst with :c(1)';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(2)),
'a x c d e f g h',
'.subst with :c(2)';
# :c and :g
is 'a b c d e f g h'.subst(/\w/, 'x', :c(0), :g),
'x x x x x x x x',
'.subst with :c(0) and :g';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(1), :g),
'a x x x x x x x',
'.subst with :c(1) and :g';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(2), :g),
'a x x x x x x x',
'.subst with :c(2) and :g';
# :c and :nth(3, 4)
is 'a b c d e f g h'.subst(/\w/, 'x', :c(0), :nth(3, 4)),
'a b x x e f g h',
'.subst with :c(0) and :nth(3, 4)';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(1), :nth(3, 4)),
'a b c x x f g h',
'.subst with :c(1) and :nth(3, 4)';
is 'a b c d e f g h'.subst(/\w/, 'x', :c(2), :nth(3, 4)),
'a b c x x f g h',
'.subst with :c(2) and :nth(3, 4)';
}
{
my $s = "ZBC";
my @a = ("A", 'ZBC');
$_ = q{Now I know my abc's};
is +s:g/Now/Wow/, 1, 'Constant substitution succeeds and returns correct count';
is($_, q{Wow I know my abc's}, 'Constant substitution produces correct result');
isa-ok s:global:i/ABC/$s/.WHAT, List, 'Global scalar substitution succeeds and returns a List';
is($_, q{Wow I know my ZBC's}, 'Scalar substitution produces correct result');
isa-ok s/BC/@a[]/.WHAT, Match, 'Single list replacement succeeds and returns a Match';
is($_, q{Wow I know my ZA ZBC's}, 'List replacement produces correct result');
dies-ok { 'abc' ~~ s/b/g/ },
"can't modify string literal (only variables)";
}
# L<S05/Modifiers/The :s modifier is considered sufficiently important>
# also RT #126679
{
dies-ok {"a b c" ~~ ss/a b c/x y z/}, 'Cannot ss/// string literal';
$_ = "a\nb\tc d";
#?rakudo.jvm 2 skip 'samemark NYI'
ok s:ss/a b c d/w x y z/, 'successful s:ss substitution returns truthy';
is $_, "w\nx\ty z", 's:ss/.../.../ preserves whitespace';
$_ = "a\nb\tc d";
# note, the ss here implies :samespace, not just :sigspace
#?rakudo.jvm 2 skip 'samemark NYI'
ok ss/a b c d/w x y z/, 'successful ss substitution returns truthy';
# RT #120526
is $_, "w\nx\ty z", 'ss/.../.../ preserves whitespace';
$_ = "a\nb\tc d";
#?rakudo.jvm 2 skip 'samemark NYI'
ok s:s/a b c d/w x y z/, 'successful s:s substitution returns truthy';
is $_, "w x y z", 's:s/.../.../ does not preserve whitespace';
$_ = "A\nb\tc D";
ok s:ss:ii/a B c D/w x y z/, 'successful s:ss:ii substitution returns truthy';
is $_, "W\nx\ty Z", 's:ss:ii/.../.../ preserves whitespace and case';
$_ = "A\nb\tC d";
#?rakudo.jvm 2 skip 'samemark NYI'
ok ss:i/A B c d/w x y z/, 'successful ss:i substitution returns truthy';
# RT #120526
is $_, "w\nx\ty z", 'ss:i/.../.../ preserves whitespace';
$_ = "A\nb\tC D";
ok s:s:ii/A B c D/w x y z/, 'successful s:s:ii substitution returns truthy';
is $_, "W x Y Z", 's:s:ii/.../.../ does not preserve whitespace but preserves case';
$_ = "Ä\nb\tć D";
#?rakudo.jvm 2 todo 'RT #125753'
ok s:ss:ii:mm/a ḇ? c D/w x y z/, 'successful s:ss:ii:mm substitution returns truthy';
is $_, "Ẅ\nx\tý Z", 's:ss:ii:mm/.../.../ preserves whitespace, case, and marks';
$_ = "a\nḇ\tĆ d";
#?rakudo.jvm 2 todo 'RT #125753'
ok ss:i:m/Å b C d/w x y z/, 'successful ss substitution returns truthy';
# RT #120526
is $_, "w\nx̱\tý z", 'ss/.../.../ preserves whitespace';
$_ = "Å\nḇ\tć d";
#?rakudo.jvm 2 todo 'RT #125753'
ok s:s:ii:mm/a B+ c D/w x y z/, 'successful s:s substitution returns truthy';
is $_, "W̊ x̱ ý z", 's:s/.../.../ does not preserve whitespace but preserves case and marks';
}
#L<S05/Substitution/As with Perl 5, a bracketing form is also supported>
{
my $a = 'abc';
ok $a ~~ s[b] = 'de', 's[...] = ... returns true on success';
is $a, 'adec', 'substitution worked';
$a = 'abc';
nok $a ~~ s[d] = 'de', 's[...] = ... returns false on failure';
is $a, 'abc', 'failed substitutions leaves string unchanged';
}
{
throws-like '$_ = "a"; s:unkonwn/a/b/', X::Syntax::Regex::Adverb,
's/// dies on unknown adverb';
throws-like '$_ = "a"; s:overlap/a/b/', X::Syntax::Regex::Adverb,
':overlap does not make sense on s///';
}
# note that when a literal is passed to 'given', $_ is bound read-only
{
given my $x = 'abc' {
ok (s[b] = 'de'), 's[...] = ... returns true on success';
is $_, 'adec', 'substitution worked';
}
given my $y = 'abc' {
s[d] = 'foo';
is $_, 'abc', 'failed substitutions leaves string unchanged';
}
}
{
my $x = 'foobar';
is +($x ~~ s:g[o] = 'u'), 2, 's:global[..] = returns correct count';
is $x, 'fuubar', 'and the substition worked';
}
{
$_ = 'a b c';
s[\w] = uc($/);
is $_, 'A b c', 'can use $/ on the RHS';
$_ = 'a b c';
s[(\w)] = uc($0);
is $_, 'A b c', 'can use $0 on the RHS';
$_ = 'a b c';
is +(s:g[ (\w) ] = $0 x 2), 3, 's:g[] returns proper count of matches';
is $_, 'aa bb cc', 's:g[...] and captures work together well';
}
{
my $x = 'ABCD';
$x ~~ s:x(2)/<.alpha>/x/;
is $x, 'xxCD', 's:x(2)';
}
# s///
{
my $x = 'ooooo';
ok $x ~~ s:1st/./X/, 's:1st return value';
is $x, 'Xoooo', 's:1st side effect';
$x = 'ooooo';
ok $x ~~ s:2nd/./X/, 's:2nd return value';
is $x, 'oXooo', 's:2nd side effect';
$x = 'ooooo';
ok $x ~~ s:3rd/./X/, 's:3rd return value';
is $x, 'ooXoo', 's:3rd side effect';
$x = 'ooooo';
ok $x ~~ s:4th/./X/, 's:4th return value';
is $x, 'oooXo', 's:4th side effect';
$x = 'ooooo';
ok $x ~~ s:nth(5)/./X/, 's:nth(5) return value';
is $x, 'ooooX', 's:nth(5) side effect';
$x = 'ooooo';
nok $x ~~ s:nth(6)/./X/, 's:nth(6) return value';
is $x, 'ooooo', 's:nth(6) no side effect';
}
# s///
{
my $x = 'ooooo';
$x ~~ s:x(2):nth(1,3)/o/A/;
is $x, 'AoAoo', 's:x(2):nth(1,3) works in combination';
$x = 'ooooo';
$x ~~ s:2x:nth(1,3)/o/A/;
is $x, 'AoAoo', 's:2x:nth(1,3) works in combination';
}
# RT #83484
# s// with other separators
{
my $x = 'abcde';
$x ~~ s!bc!zz!;
is $x, 'azzde', '! separator';
}
#L<S05/Substitution/Any scalar assignment operator may be used>
{
given 'a 2 3' -> $_ is copy {
ok (s[\d] += 5), 's[...] += 5 returns True';
is $_, 'a 7 3', 's[...] += 5 gave right result';
}
given 'a b c' -> $_ is copy {
s:g[\w] x= 2;
is $_, 'aa bb cc', 's:g[..] x= 2 worked';
}
}
{
multi sub infix:<fromplus>(Match $a, Int $b) {
$a.from + $b
}
given 'a b c' -> $_ is copy {
ok (s:g[\w] fromplus= 3), 's:g[...] customop= returned True';
is $_, '3 5 7', '... and got right result';
}
}
# RT #69044
{
sub s { 'sub s' }
$_ = "foo";
#?rakudo.jvm skip 'samemark NYI'
ok s:s,foo,bar, , 's with colon is always substitution';
is s(), 'sub s', 'can call sub s as "s()"';
is s, 'sub s', 'can call sub s as "s"';
$_ = "foo";
#?rakudo.jvm skip 'samemark NYI'
ok ss (foo) = 'bar', 'bare ss is substitution before whitespace then parens';
}
# Test for :samecase
{
is 'The foo and the bar'.subst('the', 'that', :samecase), 'The foo and that bar', '.substr and :samecase (1)';
is 'The foo and the bar'.subst('the', 'That', :samecase), 'The foo and that bar', '.substr and :samecase (2)';
is 'The foo and the bar'.subst(/:i the/, 'that', :samecase), 'That foo and the bar', '.substr (string pattern) and : samecase (1)';
is 'The foo and the bar'.subst(/:i The/, 'That', :samecase), 'That foo and the bar', '.substr (string pattern) and : samecase (2)';
is 'The foo and the bar'.subst(/:i the/, 'that', :g, :samecase), 'That foo and that bar', '.substr (string pattern) and :g and :samecase (1)';
is 'The foo and the bar'.subst(/:i The/, 'That', :g, :samecase), 'That foo and that bar', '.substr (string pattern) and :g and :samecase (2)';
my $str = "that";
is 'The foo and the bar'.subst(/:i the/, {++$str}, :samecase), 'Thau foo and the bar', '.substr and samecase, worked with block replacement';
is 'The foo and the bar'.subst(/:i the/, {$str++}, :g, :samecase), 'Thau foo and thav bar', '.substr and :g and :samecase, worked with block replacement';
}
{
$_ = 'foObar';
s:ii/oo/au/;
is $_, 'faUbar', ':ii implies :i';
$_ = 'foObar';
s:samecase/oo/au/;
is $_, 'faUbar', ':samecase implies :i';
}
# RT #66816
{
my $str = "a\nbc\nd";
is $str.subst(/^^/, '# ', :g), "# a\n# bc\n# d",
'Zero-width substitution does not make the GC recurse';
}
{
throws-like q[ $_ = "abc"; my $i = 1; s:i($i)/a/b/ ], X::Value::Dynamic,
'Value of :i must be known at compile time';
#?rakudo todo 'be smarter about constant detection'
eval-lives-ok q[ $_ = "abc";s:i(1)/a/b/ ],
':i(1) is OK';
}
{
$_ = 'foo';
s/f(.)/b$0/;
is $_, 'boo', 'can use $0 in RHS of s///';
}
# RT #76664
{
class SubstInsideMethod {
method ro($_ ) { s/c// }
}
dies-ok { SubstInsideMethod.new.ro('ccc') }, '(sanely) dies when trying to s/// a read-only variable';
}
# RT #83552
#?DOES 3
{
$_ = "foo"; s[f] = 'bar';
is $_, "baroo", 's[f] is parsed as a substitution op';
throws-like q{$_ = "foo"; s[] = "bar";}, X::Syntax::Regex::NullRegex;
}
# RT #119201
{
my $RT119201_s = 'abcdef';
my $RT119201_m = '';
$RT119201_s .= subst(/(\w)/, { $RT119201_m = $/[0] });
is($RT119201_m, 'a', 'get match variable in replacement of subst-mutator');
}
# RT #122349
{
eval-lives-ok '$_ = "a";s/a$/b/;s|b$|c|;s!c$!d!;', '$ anchor directly at the end of the search pattern works';
}
# RT #123168
{
my $foo = "bar";
$foo ~~ s:g [ r ] = 'z' if $foo.defined;
is $foo, 'baz', 's{}="" plus statement mod if is not parsed as /i';
}
{
$_ = 42;
my $match = s/\d+/xxx/;
isa-ok $match, Match, 's/// returns a Match object on non-strings';
is $_, 'xxx', 's/// can modify a container that contains a non-string';
}
# RT #123597
{
$_ = 0; s{^(\d+)$} = sprintf "%3d -", $_;
is $_, " 0 -", 's{}="" can modify a container that contains a non-string';
}
# RT #114388
{
$_ = "real";
s[ea] = "rea";
is $_, "rreal", 's[]="" works when $_ is set';
$_ = "";
throws-like { EVAL 's[] = "rea"' },
X::Syntax::Regex::NullRegex;
}
# RT #114388 -- part two
#?rakudo skip "RT #114388 -- No such method 'subst-mutate' for invocant of type 'Any'"
{
$_ = Any;
s[ea] = "rea";
is $_, "", 'can use s[]="" when $_ is not set';
}
# RT #126476
{
$_ = "foo";
is S/a/A/, "foo", "non-mutating single substitution works ($/)";
}
{
$_ = "foo";
is S/o/O/, "fOo", "non-mutating single substitution works ($/)";
$_ = "foo";
is S:g/o/O/, "fOO", "non-mutating global substitution works ($/)";
$_ = "foo";
is S[o] = 'O', "fOo", "non-mutating single substitution assignment works ($/)";
$_ = "foo";
is S:g[o] = 'O', "fOO", "non-mutating global substitution assignment works ($/)";
$_ = "foo";
is S/(o)/{$0.uc}/, "fOo", "non-mutating single substitution works ($0)";
$_ = "foo";
is S:g/(o)/{$0.uc}/, "fOO", "non-mutating global substitution works ($0)";
$_ = "foo";
is S[(o)] = $0.uc, "fOo", "non-mutating single substitution assignment works ($0)";
$_ = "foo";
is S:g[(o)] = $0.uc, "fOO", "non-mutating global substitution assignment works ($0)";
}
# RT #128809
{
is_run 'await ^30 .map: { start { S/.+/{$/.chars.print}/ given "abc"; } }', {
:err(''), :out('3' x 30)
}, 'code in replacement part of s/// has correct scoping';
}
#?rakudo.jvm skip 'at least one of the sub-tests leads to an UnwindException'
#?DOES 1
{ # coverage; 2016-09-27
subtest 'Cool.subst-mutate works same as on .Str' => {
my @tests = # args | returned stringified Match objects | result
[ \('', ''), '', "aabbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('', '', :g), ('' xx 20), "aabbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('', '', :global), ('' xx 20), "aabbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('a', ''), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('a', '', :g), ('a', 'a'), "bbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('a', '', :global), ('a', 'a'), "bbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('b', ''), 'b', "aabb\x[308]b\x[308]cc \t xz \t y", ],
[ \('b', '', :g), ('b', 'b'), "aab\x[308]b\x[308]cc \t xz \t y", ],
[ \('b', '', :global), ('b', 'b'), "aab\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, ''), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :g), ('a', 'a', 'b', 'b', 'c', 'c' ), "b\x[308]b\x[308] \t xz \t y", ],
[ \(/<[abc]>/, '', :global), ('a', 'a', 'b', 'b', 'c', 'c' ), "b\x[308]b\x[308] \t xz \t y", ],
[ \(/<[abc]>/, '', :1st), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :st{1;}), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :2nd), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :nd{2;}), 'a', "abbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :3rd), 'b', "aabb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :rd{3;}), 'b', "aabb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :4th), 'b', "aabb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :th{4;}), 'b', "aabb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[abc]>/, '', :5nth), 'c', "aabbb\x[308]b\x[308]c \t xz \t y", ],
[ \(/<[abc]>/, '', :nth{5;}), 'c', "aabbb\x[308]b\x[308]c \t xz \t y", ],
[ \(/<[abc]>/, '', :5x), ('a', 'a', 'b', 'b', 'c'), "b\x[308]b\x[308]c \t xz \t y", ], # RT # 129596
[ \(/<[abc]>/, '', :x(1..5)), ('a', 'a', 'b', 'b', 'c'), "b\x[308]b\x[308]c \t xz \t y", ], # RT # 129596
[ \(/<[cz]> \s+ <[xy]>/, 'Z P', :ss), "c \t x", "aabbb\x[308]b\x[308]cZ \t Pz \t y", ],
[ \(/<[cz]> \s+ <[xy]>/, 'Z P', :ss, :global), ( "c \t x", "z \t y" ), "aabbb\x[308]b\x[308]cZ \t PZ \t P", ],
[ \('a', 'Z', :ii), 'a', "zabbb\x[308]b\x[308]cc \t xz \t y", ],
[ \('a', 'Z', :ii, :global), ( 'a', 'a' ), "zzbbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[b]+[b\x[308]]>/, 'Z', :mm), 'b', "aaZbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[b]+[b\x[308]]>/, 'Z', :mm, :global), ( 'b', 'b', "b\x[308]", "b\x[308]" ), "aaZZZ\x[308]Z\x[308]cc \t xz \t y", ],
[ \(/<[b]+[b\x[308]]>/, 'Z', :ii, :mm), 'b', "aazbb\x[308]b\x[308]cc \t xz \t y", ],
[ \(/<[b]+[b\x[308]]>/, 'Z', :ii, :mm, :global), ( 'b', 'b', "b\x[308]", "b\x[308]" ), "aazzz\x[308]z\x[308]cc \t xz \t y", ],
;
plan 2*@tests;
for @tests -> $t {
# use IO::Path as our Cool object
my $obj = "aabbb\x[308]b\x[308]cc \t xz \t y".IO;
my $ret = $obj.subst-mutate(|$t[0]);
is-deeply ( $ret ~~ Iterable ?? $ret».Str !! $ret.Str ), $t[1],
"correct return value when using $t[0].gist()";
is-deeply $obj, $t[2], "correct modification when using $t[0].gist()";
}
}
}
{ # RT #129374
throws-like { "".subst: /\w/, "", :x(my class SomeInvalidXParam {}.new) },
X::Str::Match::x, 'giving .subst invalid args throws';
throws-like { ($ = "").subst-mutate: /\w/, "", :x(my class SomeInvalidXParam {}.new) },
X::Str::Match::x, 'giving .subst-mutate invalid args throws';
}
# https://irclog.perlgeek.de/perl6-dev/2016-11-26#i_13630780
throws-like { "".subst }, X::Multi::NoMatch, '.subst with no arguments throws';
# RT #130289
is-deeply (S:g/FAIL// with 'foo'), 'foo',
'S:g/// returns original string on failure to match';
# RT #130355
is-deeply (eager <a b c>.map: {S/a/x/}), <x b c>,
'S/// can be used in map (does not reuse a container)';
# RT #127135
try { ($ = 42).subst-mutate: Str, Str }; pass "Cool.subst-mutate with wrong args does not hang";
# https://irclog.perlgeek.de/perl6-dev/2017-03-22#i_14308172
subtest 'List/Match result adverb handling' => {
# Check that we can handle a bunch of adverb combinations that
# can result in either a Match or a List of Matches in $/
plan 3;
subtest ':2nd:g' => {
subtest 'S///' => {
plan 3;
is-deeply (S:2nd:g/./Z/ with 'abc'), 'aZc', 'return value';
isa-ok $/, Match, '$/ is Match';
is-deeply $/.Str, 'b', '$/.Str';
}
subtest 's///' => {
plan 4;
my $v = 'abc';
my $r = $v ~~ s:2nd:g/./Z/;
is-deeply $v, 'aZc', 'result';
cmp-ok $r, '===', $/, 'return value';
isa-ok $/, Match, '$/ is Match';
is-deeply $/.Str, 'b', '$/.Str';
}
}
subtest ':x(1..3)' => {
subtest 'S///' => {
plan 3;
is-deeply (S:x(1..3)/./Z/ with 'abcd'), 'ZZZd', 'return value';
isa-ok $/, List, '$/ is List';
is-deeply $/».Str, <a b c>, '$/».Str';
}
subtest 's///' => {
plan 4;
my $v = 'abcd';
my $r = $v ~~ s:x(1..3):g/./Z/;
is-deeply $v, 'ZZZd', 'result';
cmp-ok $r, '===', $/, 'return value';
isa-ok $/, List, '$/ is List';
is-deeply $/».Str, <a b c>, '$/».Str';
}
}
subtest ':th(1, 3)' => {
subtest 'S///' => {
plan 3;
is-deeply (S:th(1, 3)/./Z/ with 'abcd'), 'ZbZd', 'return value';
isa-ok $/, List, '$/ is List';
is-deeply $/».Str, <a c>, '$/».Str';
}
subtest 's///' => {
plan 4;
my $v = 'abcd';
my $r = $v ~~ s:th(1, 3):g/./Z/;
is-deeply $v, 'ZbZd', 'result';
cmp-ok $r, '===', $/, 'return value';
isa-ok $/, List, '$/ is List';
is-deeply $/».Str, <a c>, '$/».Str';
}
}
}
subtest '.subst(Str:D, Str:D)' => {
plan 6;
is-deeply 'abc'.subst('a', 'zo'), 'zobc', 'replace with longer';
is-deeply 'abc'.subst('ab', 'z'), 'zc', 'replace with shorter';
is-deeply 'abc'.subst('ab', 'xy'), 'xyc', 'replace with samelength';
is-deeply 'a♥bc'.subst('♥', 'zo'), 'azobc', 'replace with longer (2)';
is-deeply 'a♥bc'.subst('♥b', 'z'), 'azc', 'replace with shorter (2)';
is-deeply 'a♥bc'.subst('a♥', '♦z'), '♦zbc', 'replace with samelength (2)';
}
subtest '.subst-mutate returns a List for things .match return a List for' => {
plan 17 × 2 × my @matchers := /b/, 'b';
for @matchers -> \mt {
for "a", 123 -> $s is copy {
isa-ok $s.subst-mutate(:g, mt, ''), List,
":g with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:x(3), mt, ''), List,
":x with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nth(1, 2, 3), mt, ''), List,
":nth(List) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nth(1…3), mt, ''), List,
":nth(Seq) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nth(1..3), mt, ''), List,
":nth(Range) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:th(1, 2, 3), mt, ''), List,
":th(List) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:th(1…3), mt, ''), List,
":th(Seq) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:th(1..3), mt, ''), List,
":th(Range) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:st(1, 2, 3), mt, ''), List,
":st(List) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:st(1…3), mt, ''), List,
":st(Seq) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:st(1..3), mt, ''), List,
":st(Range) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nd(1, 2, 3), mt, ''), List,
":nd(List) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nd(1…3), mt, ''), List,
":nd(Seq) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:nd(1..3), mt, ''), List,
":nd(Range) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:rd(1, 2, 3), mt, ''), List,
":rd(List) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:rd(1…3), mt, ''), List,
":rd(Seq) with {mt.^name} matcher on {$s.^name}";
isa-ok $s.subst-mutate(:rd(1..3), mt, ''), List,
":rd(Range) with {mt.^name} matcher on {$s.^name}";
}
}
}
# https://github.com/rakudo/rakudo/issues/1523#issuecomment-365447388
subtest 'no-matches .subst-mutate: with multi-match opts = empty List' => {
plan 17 × 2 × my @matchers := /b/, 'b';
for @matchers -> \mt {
for "a", 123 -> $s is copy {
is-deeply $s.subst-mutate(:g, mt, ''), (),
":g with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:x(3), mt, ''), (),
":x with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nth(1, 2, 3), mt, ''), (),
":nth(List) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nth(1…3), mt, ''), (),
":nth(Seq) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nth(1..3), mt, ''), (),
":nth(Range) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:th(1, 2, 3), mt, ''), (),
":th(List) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:th(1…3), mt, ''), (),
":th(Seq) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:th(1..3), mt, ''), (),
":th(Range) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:st(1, 2, 3), mt, ''), (),
":st(List) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:st(1…3), mt, ''), (),
":st(Seq) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:st(1..3), mt, ''), (),
":st(Range) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nd(1, 2, 3), mt, ''), (),
":nd(List) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nd(1…3), mt, ''), (),
":nd(Seq) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:nd(1..3), mt, ''), (),
":nd(Range) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:rd(1, 2, 3), mt, ''), (),
":rd(List) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:rd(1…3), mt, ''), (),
":rd(Seq) with {mt.^name} matcher on {$s.^name}";
is-deeply $s.subst-mutate(:rd(1..3), mt, ''), (),
":rd(Range) with {mt.^name} matcher on {$s.^name}";
}
}
}
# RT #130688
subtest '.subst[-mutate] with multi-match args set $/ to a List of matches' => {
plan 2*2*(2+5);
for 1234567, '1234567' -> $type {
for <subst subst-mutate> -> $meth {
subtest "$type.^name().$meth: :g" => {
($ = $type)."$meth"(:g, /../, 'XX');
isa-ok $/, List, '$/ is a List…';
cmp-ok +$/, '==', 3, '…with 3 items…';
is-deeply $/.map({.WHAT}).unique, (Match,).Seq, '…all are Match…';
is-deeply $/.map(~*), <12 34 56>.map(*.Str), '…all have right values';
}
subtest ".$meth: :x" => {
($ = $type)."$meth"(:2x, /../, 'XX');
isa-ok $/, List, '$/ is a List…';
cmp-ok +$/, '==', 2, '…with 2 items…';
is-deeply $/.map({.WHAT}).unique, (Match,).Seq, '…all are Match…';
is-deeply $/.map(~*), <12 34>.map(*.Str), '…all have right values';
}
for <nth st nd rd th> -> $suffix {
subtest ".$meth: :$suffix" => {
($ = $type)."$meth"(|($suffix => 1..3), /../, 'XX');
isa-ok $/, List, '$/ is a List…';
cmp-ok +$/, '==', 3, '…with 3 items…';
is-deeply $/.map({.WHAT}).unique, (Match,).Seq, '…all are Match…';
is-deeply $/.map(~*), <12 34 56>.map(*.Str),
'…all have right values';
}
}
}
}
}
# vim: ft=perl6