### ----------------------------------------------------
### -- Grammar::Profiler::Simple
### -- Licenses: Artistic-2.0
### -- Authors: Jonathan Scott Duff
### -- File: t/csv.rakutest
### ----------------------------------------------------
use Test;
use Grammar::Profiler::Simple;

grammar CSV {
    token TOP { ^ <line>+ % "\n" $ }
    token line { <value>+ % ',' }
    token value { <-[,]>+ }
}

my @tests = ( 
    # string,                   { rule1 => call1, rule2 => call2, ... }
    "",                       { TOP => 1, line => 1, value => 1 },
    "alpha",                  { TOP => 2, line => 2, value => 2 },
    "alpha,beta,gamma,delta", { TOP => 3, line => 3, value => 6 },
    "a\nb\nc",                { TOP => 4, line => 4, value => 7 },
    "a,b,c\ne,f\n,g,h,i",     { TOP => 5, line => 5, value => 14 },
);

plan @tests / 2;

for @tests -> $str, %tt {
    my $match := CSV.parse($str);
#    say (?$match ?? "MATCH" !! "no match") ~ " '$str'";
    my %t := get-timing;
    subtest "test consistency of '$str.subst("\n",:g)'", {
        plan +%tt.keys * 2;

        for %tt.kv -> $rule, $calls {
            is %t<CSV>{$rule}<calls>, %tt{$rule},
              "Rule ``$rule'' was called %tt{$rule} times (1)";
            is get-timing(CSV,$rule)<calls>, %tt{$rule},
              "Rule ``$rule'' was called %tt{$rule} times (2)";
        }
    }
}

# vim: expandtab shiftwidth=4


