### ----------------------------------------------------
### -- Sub::Memoized
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;

use Sub::Memoized;

plan 13 + 2 * 3 * 12;

my $executed = 0;
my $ft = 42;
role Foo { } # to check if mixins don't break things

for
    sub ($a) is memoized                   { ++$executed; $a + $a },
    'is memoized',
    sub ($a) is memoized( my %h )          { ++$executed; $a + $a },
    'is memoized(my %h)',
    sub ($a) is memoized( my %h does Foo ) { ++$executed; $a + $a },
    'is memoized(my %h does Foo)'
-> &code, $what {
    $executed = 0;

    is code(42), 84, "$what: positional, did we get right result";
    is $executed, 1, "$what: positional, did we execute the body once";

    is code(42), 84, "$what: positional, did we get right result";
    is $executed, 1, "$what: positional, did we not execute the body this time";

    is code($ft), 84, "$what: positional, did we get right result";
    is $executed, 1, "$what: positional, did we not execute the body this time";

    is code(666), 1332, "$what: positional, did we get right result";
    is $executed, 2, "$what: positional, did we execute the body again";

    is code(42), 84, "$what: positional, did we get right result";
    is $executed, 2, "$what: positional, did we not execute the body this time";

    is code(666), 1332, "$what: positional, did we get right result";
    is $executed, 2, "$what: positional, did we not execute the body this time";
}

for
    sub (:$a) is memoized                   { ++$executed; $a + $a + 1 },
    'is memoized',
    sub (:$a) is memoized( my %h )          { ++$executed; $a + $a + 1 },
    'is memoized(my %h)',
    sub (:$a) is memoized( my %h does Foo ) { ++$executed; $a + $a + 1 },
    'is memoized(my %h does Foo)'
-> &code, $what {
    $executed = 0;

    is code(a => 42), 85, "$what: named, did we get right result";
    is $executed, 1, "$what: named, did we execute the body once";

    is code(a => 42), 85, "$what: named, did we get right result";
    is $executed, 1, "$what: named, did we not execute the body this time";

    is code(a => $ft), 85, "$what: positional, did we get right result";
    is $executed, 1, "$what: positional, did we not execute the body this time";

    is code(a => 666), 1333, "$what: named, did we get right result";
    is $executed, 2, "$what: named, did we execute the body again";

    is code(a => 42), 85, "$what: named, did we get right result";
    is $executed, 2, "$what: named, did we not execute the body this time";

    is code(a => 666), 1333, "$what: named, did we get right result";
    is $executed, 2, "$what: named, did we not execute the body this time";
}

my int $seen;
sub foo($a) is memoized({ ++$seen; .[0].Str }) { $a }
is-deeply foo($_), $_, "foo check $_" for ^5;
is $seen, 5, 'foo did different keys call keymaker';

$seen = 0;
my %done;
sub bar($a) is memoized(%done, { ++$seen; .[0] + 1 }) { $a }
is-deeply bar($_), $_, "bar check $_" for ^5;
is $seen, 5, 'bar did different keys call keymaker';
is-deeply %done, { "1" => 0, "2" => 1, "3" => 2, "4" => 3, "5" => 4 },
  'did the cache get filled with the right keys';

# vim: expandtab sw=4


