### ----------------------------------------------------
### -- Iter::Able
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/01-group-conseq.rakutest
### ----------------------------------------------------
use Test;
use Iter::Able::Group-Conseq;

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

plan 11;

my $s;
$s := [2, 2, 3, 4, 5, -5, 25, 3, 3];
id group-conseq($s, as => &abs), [2 => (2, 2), 3 => (3,), 4 => (4,), 5 => (5, -5), 25 => (25,), 3 => (3, 3)].Seq, "with `&abs`";

$s := [];
id group-conseq($s, as => * > 0),  [].Seq, "with `* > 0`";

$s := [0, 0, 0, 0];
id group-conseq($s, as => * == 0), [Bool::True => (0, 0, 0, 0)].Seq, "with `* == 0`";

$s := [-1, -2, 0, 8, -3];
id group-conseq($s, as => * < 0), [Bool::True => (-1, -2), Bool::False => (0, 8), Bool::True => (-3,)].Seq, "with `* < 0`";

$s := (1, 1, 2, 2, 1, 1, 2, 2).Seq;
id group-conseq($s),  [1 => (1, 1), 2 => (2, 2), 1 => (1, 1), 2 => (2, 2)].Seq, "with `*`";

$s := [1, 0, -1, 2];
id group-conseq($s, as => &{False}), [Bool::False => $s.List].Seq, "with `\{False\}`";

$s := "neither AM I!";
id group-conseq($s, as => /<.lower>/), [Bool::True => ('n', 'e', 'i', 't', 'h', 'e', 'r'),
                                        Bool::False => (' ', 'A', 'M', ' ', 'I', '!')].Seq, "String with `/<lower>/`";

$s := (3, 3, 3, 4, 4, 4, 4, 5, 3);
my \it = $s.iterator;
it.pull-one for ^2;
my \newit = group-conseq(it);
it.pull-one;  # should affect `newit`s behaviour
does-ok newit, Iterator, "Iterator (returned an Iterator, not a Seq)";
id Seq.new(newit), [4 => (4, 4, 4, 4), 5 => (5,), 3 => (3,)].Seq, "Iterator (values)";
ok it.pull-one =:= IterationEnd, "Iterator (original, exhausted)";
ok newit.pull-one =:= IterationEnd, "Iterator (new, exhausted)";

done-testing;


