use v6-alpha; use Test; =pod This file was originally derived from the perl5 CPAN module Perl6::Rules, version 0.3 (12 Apr 2004), file t/hash_cap.t. =cut plan 116; if !eval('("a" ~~ /a/)') { skip_rest "skipped tests - rules support appears to be missing"; } else { force_todo(1..49,51..99,101..108,111..116); ok(" a b\tc" ~~ m/%:=( \s+ \S+ )/, 'Named unrepeated hash capture'); ok(exists($/,<' a'>), 'One key captured'); ok(eval(q{!defined($/{' a'})}), 'One value undefined'); ok($/.keys == 1, 'No extra unrepeated captures'); ok(" a b\tc" ~~ m/%:=( \s+ \S+ )+/, 'Named simple hash capture'); ok(exists($/,<' a'>), 'First simple key captured'); ok(eval(q{!defined($/{' a'})}), 'First simple value undefined'); ok(exists($/,<' b'>), 'Second simple key captured'); ok(eval(q{!defined($/{' b'})}), 'Second simple value undefined'); ok(exists($/,<"\tc">), 'Third simple key captured'); ok(eval(q{!defined($/{"\tc"})}), 'Third simple value undefined'); ok($/.keys == 3, 'No extra simple captures'); ok(" a b\tc" ~~ m/%:=( \s+ \S+ )+ %:=( \s+ \S+)+/, 'Sequential simple hash capture'); ok(exists($/,<' a'>), 'First sequential key captured'); ok(eval(q{!defined($/{' a'})}), 'First sequential value undefined'); ok(exists($/,<' b'>), 'Second sequential key captured'); ok(eval(q{!defined($/{' b'})}), 'Second sequential value undefined'); ok(exists($/,<"\tc">), 'Third sequential key captured'); ok(eval(q{!defined($/{"\tc"})}), 'Third sequential value undefined'); ok($/.keys == 2, 'No extra first sequential captures'); ok($/.keys == 1, 'No extra last sequential captures'); ok("abcxyd" ~~ m/a %:=(.(.))+ d/, 'Repeated nested hash capture'); ok(exists($/,), 'Nested key 1 captured'); ok(eval(q{!defined($/)}), 'No nested value 1 captured'); ok(exists($/,), 'Nested key 2 captured'); ok(eval(q{!defined($/)}), 'No nested value 2 captured'); ok($/.keys == 2, 'No extra nested captures'); ok("abcd" ~~ m/a %:=(.(.)) d/, 'Unrepeated nested hash capture'); ok(exists($/,), 'Unrepeated key captured'); ok(eval(q{!defined($/)}), 'Unrepeated value not captured'); ok($/.keys == 1, 'No extra unrepeated nested captures'); ok("abcd" ~~ m/a %:=((.)(.)) d/, 'Unrepeated nested hash multicapture'); ok(exists($/,), 'Unrepeated key multicaptured'); ok(eval(q{$/}), 'c', 'Unrepeated value not multicaptured'); ok($/.keys == 1, 'No extra unrepeated nested multicaptures'); ok("abcxyd" ~~ m/a %:=((.)(.))+ d/, 'Repeated nested hash multicapture'); ok(exists($/,), 'Nested key 1 multicaptured'); ok(eval(q{$/}), 'c', 'Nested value 1 multicaptured'); ok(exists($/,), 'Nested key 2 multicaptured'); ok(eval(q{$/}), 'y', 'Nested value 2 multicaptured'); ok($/.keys == 2, 'No extra nested multicaptures'); our %foo; ok("abcxyd" ~~ m/a %foo:=(.(.))+ d/, 'Package hash capture'); ok(exists(%foo,), 'Package hash key 1 captured'); ok(eval(q{!defined(%foo{c})}), 'Package hash value 1 not captured'); ok(exists(%foo,), 'Package hash key 2 captured'); ok(eval(q{!defined(%foo{y})}), 'Package hash value 2 not captured'); ok(%foo.keys == 2, 'No extra package hash captures'); rule two {..} ok("abcd" ~~ m/a %:=[] d/, 'Compound hash capture'); is($/, "bc", 'Implicit subrule variable captured'); ok($/.keys == 0, 'Explicit hash variable not captured'); ok(" a b\tc" ~~ m/%:=( %:=[\s+] (\S+))+/, 'Nested multihash capture'); ok(exists($/,), 'Outer hash capture key 1'); ok(eval(q{!defined($/)}), 'Outer hash no capture value 1'); ok(exists($/,), 'Outer hash capture key 2'); ok(eval(q{!defined($/)}), 'Outer hash no capture value 2'); ok(exists($/,), 'Outer hash capture key 3'); ok(eval(q{!defined($/)}), 'Outer hash no capture value 3'); ok($/.keys == 3, 'Outer hash no extra captures'); ok(exists($/,<' '>), 'Inner hash capture key 1'); ok(eval(q{!defined($/{' '})}), 'Inner hash no capture value 1'); ok(exists($/,<' '>), 'Inner hash capture key 2'); ok(eval(q{!defined($/{' '})}), 'Inner hash no capture value 2'); ok(exists($/,<"\t">), 'Inner hash capture key 3'); ok(eval(q{!defined($/{"\t"})}), 'Inner hash no capture value 3'); ok($/.keys == 3, 'Inner hash no extra captures'); rule spaces { @:=[\s+] } ok(" a b\tc" ~~ m/%:=( (\S+))+/, 'Subrule hash capture'); ok(exists($/,), 'Outer subrule hash capture key 1'); ok(eval(q{!defined($/)}), 'Outer subrule hash no capture value 1'); ok(exists($/,), 'Outer subrule hash capture key 2'); ok(eval(q{!defined($/)}), 'Outer subrule hash no capture value 2'); ok(exists($/,), 'Outer subrule hash capture key 3'); ok(eval(q{!defined($/)}), 'Outer subrule hash no capture value 3'); ok($/.keys == 3, 'Outer subrule hash no extra captures'); is($/, "\t", 'Final subrule hash capture'); ok(" a b\tc" ~~ m/%:=( %:=[] (\S+))+/, 'Nested subrule hash multicapture'); ok(exists($/,), 'Outer rule nested hash key multicapture'); ok(eval(q{!defined($/)}), 'Outer rule nested hash value multicapture'); ok(exists($/,), 'Outer rule nested hash key multicapture'); ok(eval(q{!defined($/)}), 'Outer rule nested hash value multicapture'); ok(exists($/,), 'Outer rule nested hash key multicapture'); ok(eval(q{!defined($/)}), 'Outer rule nested hash value multicapture'); ok($/.keys == 3, 'Outer subrule hash no extra multicaptures'); ok(exists($/,<' '>), 'Inner rule nested hash key multicapture'); ok(eval(q{!defined($/{' '})}), 'Inner rule nested hash value multicapture'); ok(exists($/,<' '>), 'Inner rule nested hash key multicapture'); ok(eval(q{!defined($/{' '})}), 'Inner rule nested hash value multicapture'); ok(exists($/,<"\t">), 'Inner rule nested hash key multicapture'); ok(eval(q{!defined($/{"\t"})}), 'Inner rule nested hash value multicapture'); ok($/.keys == 3, 'Inner subrule hash no extra multicaptures'); ok(" a b\tc" ~~ m/%:=( () (\S+))+/, 'Nested multiple hash capture'); ok(eval(q{$/{' '} eq 'a'}), 'Outer rule nested hash value multicapture'); ok(eval(q{$/{' '} eq 'b'}), 'Outer rule nested hash value multicapture'); ok(eval(q{$/{"\t"}}), 'c', 'Outer rule nested hash value multicapture'); ok(eval(q{$/.keys == 3}), 'Outer subrule hash no extra multicaptures'); my %bases = (); ok("Gattaca" ~~ m:i/ %bases:=(A|C|G|T)+ /, 'All your bases...'); ok(exists(%bases,), 'a key'); ok(eval(q{!defined(%bases{a})}), 'No a value'); ok(exists(%bases,), 'c key'); ok(eval(q{!defined(%bases{c})}), 'No c value'); ok(!exists(%bases,), 'No g key'); ok(exists(%bases,), 'G key'); ok(eval(q{!defined(%bases{G})}), 'No G value'); ok(exists(%bases,), 't key'); ok(eval(q{!defined(%bases{t})}), 'No t value'); ok(%bases.keys == 4, 'No other bases'); %bases = (); my %aca = ('aca' => 1);; ok("Gattaca" ~~ m:i/ %bases:=(A|C|G|T)**{4} (%aca) /, 'Hash interpolation'); ok(exists(%bases,), 'a key'); ok(eval(q{!defined(%bases{a})}), 'No a value'); ok(!exists(%bases,), 'No c key'); ok(!exists(%bases,), 'No g key'); ok(exists(%bases,), 'G key'); ok(eval(q{!defined(%bases{G})}), 'No G value'); ok(exists(%bases,), 't key'); ok(eval(q{!defined(%bases{t})}), 'No t value'); ok(%bases.keys == 3, 'No other bases'); is("$1", "aca", 'Trailing aca'); }