### ----------------------------------------------------
### -- Iter::Able
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/10-map-last.rakutest
### ----------------------------------------------------
use Test;
use Iter::Able::Map-Last;

my &id = -> \g, \e, Str $desc? { g | e ~~ Seq:D ?? cmp-ok(g, "eqv", e, $desc) !! is-deeply(g, e, $desc) };

plan 10;

my $s;
$s := [2, -3, 4, -6, 8];
id map-last($s, * < 0, -*), (2, -3, 4, 6, 8).Seq, "Random list with `* < 0` and `-*`";

$s := [];
id map-last($s, {;}, {;}), [].Seq,  "Empty-list";

$s := [57, 91, -13];
id $s.&map-last(*.is-prime, { 0 }), $s.Seq, "Random list with no match";

$s := [5, 6, 7].Seq;
id $s.&map-last({ True }, { 0 }), (5, 6, 0).Seq, "Seq with all-pass filter";

$s := [5, 6, 7];
id $s.&map-last({ False }, { 0 }), $s.Seq, "Random list with no-pass filter";

$s := "here we are!";
id map-last($s, / <.lower> /, &uc), "here we arE!", "String with ...";

$s := (0, -2, -7, 5, 13, 22);
my \it = $s.iterator;
it.pull-one for ^2;
my \newit = map-last(it, *.is-prime, *.sqrt);
it.pull-one;  # should *not* affect `newit`s behaviour because `&map-last` caches
does-ok newit, Iterator, "Iterator (returned an Iterator, not a Seq)";
id Seq.new(newit), [-7, 5, 13.sqrt, 22].Seq, "Iterator (values)";
ok it.pull-one =:= IterationEnd, "Iterator (original, exhausted)";
ok newit.pull-one =:= IterationEnd, "Iterator (new, exhausted)";

done-testing;


