<commit_before>#! nqp

# for string_to_int
use QRegex;

my @files := [
    'rx_basic',
    'rx_metachars',
# contains /<xaaaay @ 0>/ - style things, NYI
#    'rx_quantifiers',
#    'rx_backtrack',
#    'rx_charclass',
# contains /mob <alnum>: <0 @ 35>/ - style things, NYI
#    'rx_subrules',
#    'rx_lookarounds',
#    'rx_captures',
#    'rx_modifiers',
# loops:
#    'rx_goal',
];

my %expansions;
%expansions<n> := "\n";
%expansions<r> := "\r";
%expansions<e> := "\e";
%expansions<t> := "\t";
%expansions<f> := "\f";
sub unescape($s) {
    $s := subst($s, /\\(<[nretf]>)/, -> $m { %expansions{$m[0]} }, :global);
    subst($s, /\\x(<?xdigit>**4)/, -> $m { HLL::Actions::string_to_int(~$m[0], 16) }, :global);
}


sub test_line($line) {
    my @parts  := match($line, /\T+/, :global);
    my $regex  := @parts[0];
    my $target := @parts[1];
    my $expect := @parts[2];
    my $desc   := @parts[3];

    $target := '' if $target eq "''";
    $target := unescape($target);

    my $expect_error := nqp::substr($expect, 0, 1) eq '/';

    my $rxcomp := pir::compreg__Ps('QRegex::P6Regex');
    try {
        my $rxsub  := $rxcomp.compile($regex);
        my $cursor := NQPCursor."!cursor_init"($target, :c(0));
        my $match  := $rxsub($cursor).MATCH;
        if $expect_error {
            ok(0, $desc);
            say("# expected $expect but no exception caught ");
        }
        else {
            ok($expect eq 'y' ?? $match !! !$match, $desc);
        }
        CATCH {
            if $expect_error {
                my $m := nqp::index($_, pir::chopn__Ssi(nqp::substr($expect, 1),1)) >= 0;
                ok($m, $desc);
                say("#      got: $_\n# expected: $expect") unless $m;
            }
            else {
                ok(0, $desc);
                say("# ERROR: $_");
            }
       }
   }
}


my $tests := 0;
for @files -> $fn {
    say("# file: $fn");
    my $contents := slurp('t/p6regex/' ~ $fn);
    my @lines    := pir::split("\n", $contents);

    for @lines -> $l {
        next if $l ~~ /^\s*\# | ^\s*$ /;
        test_line($l);
        $tests := $tests + 1;
    }
    say("# done with file $fn");
}
say("plan 1..$tests");


# vim: ft=perl6<commit_msg>Improve qregex-testing a bit more.<commit_after>#! nqp

# for string_to_int
use QRegex;

my @files := [
    'rx_basic',
    'rx_metachars',
# contains /<xaaaay @ 0>/ - style things, NYI
#    'rx_quantifiers',
#    'rx_backtrack',
#    'rx_charclass',
# contains /mob <alnum>: <0 @ 35>/ - style things, NYI
#    'rx_subrules',
#    'rx_lookarounds',
#    'rx_captures',
#    'rx_modifiers',
# loops:
#    'rx_goal',
];

my %expansions;
%expansions<n> := "\n";
%expansions<r> := "\r";
%expansions<e> := "\e";
%expansions<t> := "\t";
%expansions<f> := "\f";
sub unescape($s) {
    $s := subst($s, /\\(<[nretf]>)/, -> $m { %expansions{$m[0]} }, :global);
    subst($s, /\\x(<?xdigit>**4)/, -> $m { HLL::Actions::string_to_int(~$m[0], 16) }, :global);
}


sub test_line($line) {
    my @parts  := match($line, /\T+/, :global);
    my $regex  := @parts[0];
    my $target := @parts[1];
    my $expect := @parts[2];
    my $desc   := @parts[3];

    $target := '' if $target eq "''";
    $target := unescape($target);

    my $expect_error := nqp::substr($expect, 0, 1) eq '/';

    my $rxcomp := pir::compreg__Ps('QRegex::P6Regex');
    try {
        my $rxsub  := $rxcomp.compile($regex);
        my $cursor := NQPCursor."!cursor_init"($target, :c(0));
        my $match  := $rxsub($cursor).MATCH;
        if $expect_error {
            ok(0, $desc);
            say("# expected $expect but no exception caught ");
        }
        else {
            ok($expect eq 'y' ?? $match !! !$match, $desc);
        }
        CATCH {
            if $expect_error {
                my $errpat := pir::chopn__Ssi(nqp::substr($expect, 1), 1);
                my $m := "$_" ~~ /<$errpat>/;
                ok($m, $desc);
                say("#      got: $_\n# expected: $expect") unless $m;
            }
            else {
                ok(0, $desc);
                say("# ERROR: $_");
            }
       }
   }
}


my $tests := 0;
for @files -> $fn {
    say("# file: $fn");
    my $contents := slurp('t/p6regex/' ~ $fn);
    my @lines    := pir::split("\n", $contents);

    for @lines -> $l {
        my $m := $l ~~ /'# todo ' .*? ':pge<' (.*?) '>'/;
        if $m {
            todo($m[0], 1);
        }
        else {
            next if $l ~~ /^\s*\# | ^\s*$ /;
            test_line($l);
            $tests := $tests + 1;
        }
    }
    say("# done with file $fn");
}
say("plan 1..$tests");


# vim: ft=perl6
{'fraction_non_alphanumeric': 0.22850335715832792, 'fraction_numerical': 0.011912497292614252, 'mean_word_length': 2.0421607378129116, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2841630', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1749, 'n_words': 381}