Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 91 lines (69 sloc) 3.452 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 88 89 90
use v6;
use Test;
plan 42;

# L<S05/Match objects/"$/.caps">

sub ca(@x) {
    join '|', gather {
        for @x -> $p {
            take $p.key ~ ':' ~ $p.value;
        }
    }
}

ok 'a b c d' ~~ /(.*)/, 'basic sanity';
ok $/.caps ~~ Positional, '$/.caps returns something Positional';
ok $/.chunks ~~ Positional, '$/.chunks returns something Positional';
isa_ok $/.caps.[0], Pair, '.. and the items are Pairs (caps);';
isa_ok $/.chunks.[0], Pair, '.. and the items are Pairs (chunks);';
isa_ok $/.caps.[0].value, Match, '.. and the values are Matches (caps);';
isa_ok $/.chunks.[0].value, Match, '.. and the values are Matches (chunks);';

is ca($/.caps), '0:a b c d', '$/.caps is one item for (.*)';
is ca($/.chunks), '0:a b c d', '$/.chunks is one item for (.*)';

my token wc { \w };

ok 'a b c' ~~ /:s <wc=&wc> (\w) <wc=&wc> /, 'regex matches';
is ca($/.caps), 'wc:a|0:b|wc:c', 'named and positional captures mix correctly';
is ca($/.chunks), 'wc:a|~: |0:b|~: |wc:c',
                  'named and positional captures mix correctly (chunks)';

ok 'a b c d' ~~ /[(\w) \s*]+/, 'regex matches';
is ca($/.caps), '0:a|0:b|0:c|0:d', '[(\w)* \s*]+ flattens (...)* for .caps';
is ca($/.chunks), '0:a|~: |0:b|~: |0:c|~: |0:d',
                '[(\w)* \s*]+ flattens (...)* for .chunks';

ok 'a b c' ~~ /[ (\S) \s ] ** 2 (\S)/, 'regex matches';
is ca($/.caps), '0:a|0:b|1:c', '.caps distinguishes quantified () and multiple ()';
is ca($/.chunks), '0:a|~: |0:b|~: |1:c', '.chunks distinguishes quantified () and multiple ()';

ok 'a b c d' ~~ /:s [(\w) <wc=&wc> ]+/, 'regex matches';
#'RT 75484 (fails randomly) (noauto)'
is ca($/.caps), '0:a|wc:b|0:c|wc:d',
                      'mixed named/positional flattening with quantifiers';
is ca($/.chunks), '0:a|~: |wc:b|~: |0:c|~: |wc:d',
                      'mixed named/positional flattening with quantifiers';

# .caps and .chunks on submatches

ok ' abcdef' ~~ m/.*?(a(.).)/, 'Regex matches';
is ca($0.caps), '0:b', '.caps on submatches';
is ca($0.chunks), '~:a|0:b|~:c', '.chunks on submatches';

# RT117831 separator captures
ok 'a;b,c,' ~~ m/(<.alpha>) +% (<.punct>)/, 'Regex matches';
is ca($/.caps), '0:a|1:;|0:b|1:,|0:c', '.caps on % separator';
is ca($/.chunks), '0:a|1:;|0:b|1:,|0:c', '.chunks on % separator';

ok 'a;b,c,' ~~ m/(<.alpha>) +%% (<.punct>)/, 'Regex matches';
is ca($/.caps), '0:a|1:;|0:b|1:,|0:c|1:,', '.caps on %% separator';
is ca($/.chunks), '0:a|1:;|0:b|1:,|0:c|1:,', '.chunks on %% separator';

#?niecza skip 'conjunctive regex terms - nyi'
{
    ok 'a' ~~ m/a && <alpha>/, 'Regex matches';
    is ca($/.caps), 'alpha:a', '.caps && - first term';

    ok 'a' ~~ m/<alpha> && a/, 'Regex matches';
    is ca($/.caps), 'alpha:a', '.caps && - last term';

    ok 'a' ~~ m/<alpha> & <ident>/, 'Regex matches';
#?rakudo.jvm todo '& caps on jvm'
    is ca($/.caps), 'alpha:a|ident:a', '.caps & - multiple terms';

    ok 'a' ~~ m/<alpha> && <ident>/, 'Regex matches';
#?rakudo.jvm todo '&& caps on jvm'
    is ca($/.caps), 'alpha:a|ident:a', '.caps && - multiple terms';

    ok 'ab' ~~ m/([a|b] && <alpha>)**1..2/, 'Regex matches';
    is ca($/.caps), '0:a|0:b', '.caps on quantified &&';

    ok 'ab' ~~ m/[[a|b] && <alpha>]**1..2/, 'Regex matches';
#?rakudo todo 'RT117995 - quantified conjunctive capture'
    is ca($/.caps), 'alpha:a|alpha:b', '.caps on quantified &&';
}

done;

# vim: ft=perl6
Something went wrong with that request. Please try again.