Skip to content
This repository
branch: non-bootstrap
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 88 lines (70 sloc) 2.647 kb
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
use Test;

{
    "fooxbar" ~~ /x/;
    is $/.prematch, "foo", ".prematch works";
    is $/.postmatch, "bar", ".postmatch works";

    is ("foo" ~~ /(f)/).kv.join("|"), '0|f', '.kv sees positional';
    is ("foo" ~~ /$<x>=[f]/).kv.join("|"), 'x|f', '.kv sees names';
    is ("foo" ~~ /(f)/).keys.join("|"), '0', '.keys sees positional';
    is ("foo" ~~ /$<x>=[f]/).keys.join("|"), 'x', '.keys sees names';
    is ("foo" ~~ /(f)/).values.join("|"), 'f', '.values sees positional';
    is ("foo" ~~ /$<x>=[f]/).values.join("|"), 'f', '.values sees names';

    "abcdefg" ~~ / b $<x>=[c] d (e) f /;
    is $/.caps».key.join('|'), 'x|0', '.caps works (keys)';
    is $/.caps».value.join('|'), 'c|e', '.caps works (values)';
    is $/.chunks».key.join('|'), '~|x|~|0|~', '.chunks works (keys)';
    is $/.chunks».value.join('|'), 'b|c|d|e|f', '.chunks works (values)';
}

{
    "foo" ~~ /./;
    is "+a+".subst(/\w/, { uc $/ }), '+A+', 'can use $/ in subst() arg';
    is ~$/, 'f', '$/ restored after subst';
    $_ = "baa5ar";
    s/\d//;
    is ~$/, '5', 's/// resets $/';
}

{
    my $hello = "'/'hello'/'(<-[\\\\/\\\\.]>+)";
    my $reg = / ^ <hello=$hello> $/;
    "/hello/bug" ~~ $reg;
    is $/<hello>[0], 'bug', '<foo=$bar> works';
}

# http://irclog.perlgeek.de/perl6/2011-12-29#i_4894154
{
    "ab" ~~ / $<a>=[ <alpha>+ ] /;
    is $<alpha>[1], "b", "<foo>+ inside string capture works";
}

# the Rakudo-inherited algorithm failed on this one.
{
    my class A { }
    my class B is A { }
    my class C is B { }

    multi foo(A $, A $) { "AA" }
    multi foo(A $, B $) { "AB" }
    multi foo(A $, C $) { "AC" }
    multi foo(B $, A $) { "BA" }
    multi foo(C $, A $) { "CA" }

    dies_ok { foo(B, C) }, "hard case of catching ambiguous dispatch";
}

{
    sub foo(Any:U $) { } #OK
    sub bar(Any:D $) { } #OK
    sub moo(Any:_ $) { } #OK
    sub cow(Any:T $) { } #OK

    multi qux(Any:U $) { "U" }
    multi qux(Any:D $) { "D" }

    lives_ok { eval "foo(Int)" }, ":U allows type objects";
    dies_ok { eval "foo(5)" }, ":U denies concrete objects";

    lives_ok { eval "bar(5)" }, ":D allows concrete objects";
    dies_ok { eval "bar(Int)" }, ":D denies type objects";

    lives_ok { eval "moo(5)" }, ":_ allows concrete objects";
    lives_ok { eval "moo(Int)" }, ":_ allows type objects";

    lives_ok { eval "cow(Int)" }, ":T allows type objects";
    dies_ok { eval "cow(5)" }, ":T denies concrete objects";

    is qux(Int), 'U', 'multi can discriminate on :U/:D (1)';
    is qux(5), 'D', 'multi can discriminate on :U/:D (2)';
}

{
    sub foo(%x) { } #OK
    dies_ok { foo(3) }, '%x needs Associative';
}
Something went wrong with that request. Please try again.