|
| 1 | +#! nqp |
| 2 | + |
| 3 | +use NQPP5QRegex; |
| 4 | + |
| 5 | +my @files := [ |
| 6 | + 'rx_basic' |
| 7 | +]; |
| 8 | + |
| 9 | +my %expansions; |
| 10 | +%expansions<n> := "\n"; |
| 11 | +%expansions<r> := "\r"; |
| 12 | +%expansions<e> := "\e"; |
| 13 | +%expansions<t> := "\t"; |
| 14 | +%expansions<f> := "\f"; |
| 15 | +sub unescape($s) { |
| 16 | + $s := subst($s, /\\(<[nretf]>)/, -> $m { %expansions{$m[0]} }, :global); |
| 17 | + subst($s, /\\x(<[a..fA..F0..9]>**4)/, -> $m { nqp::chr(HLL::Actions::string_to_int(~$m[0], 16)) }, :global); |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +sub test_line($line) { |
| 22 | + my @parts := match($line, /\T+/, :global); |
| 23 | + my $regex := @parts[0]; |
| 24 | + my $target := @parts[1]; |
| 25 | + my $expect := @parts[2]; |
| 26 | + my $desc := @parts[3]; |
| 27 | + |
| 28 | + $target := '' if $target eq "''"; |
| 29 | + $target := unescape($target); |
| 30 | + |
| 31 | + my $expect_substr := nqp::substr($expect, 0, 1) eq '<' |
| 32 | + ?? pir::chopn__Ssi(nqp::substr($expect, 1), 1) |
| 33 | + !! ''; |
| 34 | + |
| 35 | + my $rxcomp := pir::compreg__Ps('QRegex::P5Regex'); |
| 36 | + try { |
| 37 | + my $rxsub := $rxcomp.compile($regex); |
| 38 | + my $cursor := NQPCursor."!cursor_init"($target, :c(0)); |
| 39 | + my $match := $rxsub($cursor).MATCH; |
| 40 | + if $expect_substr { |
| 41 | + my $got := ~$match."!dump_str"('mob'); |
| 42 | + my $m := nqp::index($got, $expect_substr) >= 0; |
| 43 | + ok($m, $desc); |
| 44 | + say("# got: $got\n# expected: $expect_substr") unless $m; |
| 45 | + } |
| 46 | + else { |
| 47 | + ok($expect eq 'y' ?? $match !! !$match, $desc); |
| 48 | + } |
| 49 | + CATCH { |
| 50 | + if $expect_substr { |
| 51 | + my $m := nqp::index(~$_, $expect_substr) >= 0; |
| 52 | + ok($m, $desc); |
| 53 | + say("# got: $_\n# expected: $expect") unless $m; |
| 54 | + } |
| 55 | + else { |
| 56 | + ok(0, $desc); |
| 57 | + say("# ERROR: $_"); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +my $tests := 0; |
| 65 | +for @files -> $fn { |
| 66 | + say("# file: $fn"); |
| 67 | + my $contents := slurp('t/qregex/' ~ $fn); |
| 68 | + my @lines := nqp::split("\n", $contents); |
| 69 | + |
| 70 | + for @lines -> $l { |
| 71 | + my $m := $l ~~ /'# todo ' .*? ':pge<' (.*?) '>'/; |
| 72 | + if $m { |
| 73 | + todo($m[0], 1); |
| 74 | + } |
| 75 | + else { |
| 76 | + next if $l ~~ /^\s*\# | ^\s*$ /; |
| 77 | + test_line($l); |
| 78 | + $tests := $tests + 1; |
| 79 | + } |
| 80 | + } |
| 81 | + say("# done with file $fn"); |
| 82 | +} |
| 83 | +say("1..$tests"); |
| 84 | +
|
| 85 | +
|
| 86 | +# vim: ft=perl6 |
0 commit comments