forked from Raku/roast
/
litvar.t
107 lines (78 loc) · 3.34 KB
/
litvar.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
use v6;
use Test;
=begin pod
This file was derived from the perl5 CPAN module Perl6::Rules,
version 0.3 (12 Apr 2004), file t/litvar.t.
It has (hopefully) been, and should continue to be, updated to
be valid perl6.
=end pod
plan 33;
# L<S05/Variable (non-)interpolation/The default way in which the engine handles a scalar>
my $var = "a*b";
my @var = <a b ab c>;
my $aref = @var;
# SCALARS
# just document ticket test below
ok($var ~~ m/$var/, 'Simple scalar interpolation');
ok("zzzzzz{$var}zzzzzz" ~~ m/$var/, 'Nested scalar interpolation');
ok(!( "aaaaab" ~~ m/$var/ ), 'Rulish scalar interpolation');
ok(!('a0' ~~ m/$aref[0]/), 'Array ref stringifies before matching'); #OK
ok('a b ab c0' ~~ m/$aref[0]/, 'Array ref stringifies before matching'); #OK
ok('a0' ~~ m/@$aref[0]/, 'Array deref ignores 0'); #OK
ok('bx0' ~~ m/@$aref.[0]/, 'Array deref ignores dot 0'); #OK
ok('c0' ~~ m/@var[0]/, 'Array ignores 0'); #OK
# ARRAYS
# L<S05/Variable (non-)interpolation/An interpolated array:>
ok("a" ~~ m/@var/, 'Simple array interpolation (a)');
ok("b" ~~ m/@var/, 'Simple array interpolation (b)');
ok("c" ~~ m/@var/, 'Simple array interpolation (c)');
ok(!( "d" ~~ m/@var/ ), 'Simple array interpolation (d)');
ok("ddddaddddd" ~~ m/@var/, 'Nested array interpolation (a)');
ok("abca" ~~ m/^@var+$/, 'Multiple array matching');
ok(!( "abcad" ~~ m/^@var+$/ ), 'Multiple array non-matching');
is("abc" ~~ m/ @var /, 'ab', 'Array using implicit junctive semantics');
is("abc" ~~ m/ | @var /, 'ab', 'Array using explicit junctive semantics');
is("abc" ~~ m/ || @var /, 'a', 'Array using explicit sequential semantics');
# contextializer $( )
# RT #115298
ok 'foobar' ~~ /$( $_ )/, '$( $_ ) will match';
is $/, 'foobar', '... $( $_ ) matched entire string';
is 'foobar' ~~ /$( $_.substr(3) )/, 'bar', 'Contextualizer with functions calls';
is 'foobar' ~~ /@( <a b c o> )+/, 'ooba', '@( <a b c o> )+';
# RT #117091
{
my $rex = 'rex';
ok 'Rex' ~~ m:i/$rex/, 'can case-insensitive match against interpolated var';
ok 'Rex' ~~ m:i/<$rex>/, 'can case-insensitive match against var in assertion';
}
# $i was picked here because it is an internal variable in rakudo that was
# visible in regex interpolations in the past.
$var = '$i';
throws-like { EVAL '"foo" ~~ /<$var>/' }, X::Undeclared, symbol => '$i',
'undeclared var in assertion in interpolated string throws';
{
no MONKEY-SEE-NO-EVAL;
my $i = 'f+o';
$var = '$i';
is "foo" ~~ /<$var>/, Nil,
'assertions only reinterpret one level deep';
$var = '<$i>';
dies-ok { "foo" ~~ /<$var>/},
'assertion in reinterpreted assertion is disallowed';
}
$var = 'fo+';
is "foo" ~~ /<$var>/, 'foo', 'string with metachars in assertion matches';
$var = 'fO+';
is "foo" ~~ /:i <$var>/, 'foo', 'string with metachars in assertion matches (:i)';
#?rakudo.jvm 3 skip ':ignoremark needs NFG RT #124500'
$var = 'fö+';
is "foo" ~~ /:m <$var>/, 'foo', 'string with metachars in assertion matches (:m)';
$var = 'fÖ+';
is "foo" ~~ /:i:m <$var>/, 'foo', 'string with metachars in assertion matches (:i:m)';
is "fo+" ~~ /:i:m $var/, 'fo+', 'string with metachars matches literally (:i:m)';
{
my Str $s;
my Regex $r = rx{ "a" | "b" | $s | $s };
nok "fooo" ~~ $r, 'Interpolating Str type object on typed variable fails to match';
}
# vim: ft=perl6