### ----------------------------------------------------
### -- Iter::Able
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/15-clip.rakutest
### ----------------------------------------------------
use Test;
use Iter::Able::Clip;

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

plan 13;

my $s;
$s := [2, 2, 3, -4/9, 5];
id clip($s, from-below => 2), [2, 2, 3, 2, 5].Seq, "Random list with :2from-below";

$s := (2, 2, 3, -4, 5e3);
id clip($s, :2from-above), [2, 2, 2, -4, 2].Seq, "Random list with :2from-above";

$s := (2, 2, 3, -4, 5);
id clip($s, :2from-below, :4from-above), [2, 2, 3, 2, 4].Seq, "Random list with :2from-below and :4from-above";

$s := [];
id clip($s, :0from-below), [].Seq,  "Empty list";

my $t = ();
id clip($t, :3from-above), ().Seq,  "Empty itemized list";

$s := [0, 0, 0, 0];
id clip($s, :3from-above), $s.Seq, "All zeros with :3from-above";

$s := (0, -1, -2, -3).Seq;
id clip($s, from-below => pi), pi xx 4, "No nonpositives with from-below => pi";

$s := (0, -2, -7, 5, 13, -22);
my \it = $s.iterator;
it.pull-one for ^2;
my \newit = clip(it, :0from-below, :10from-above);
it.pull-one;  # should affect `newit`s behaviour
does-ok newit, Iterator, "Iterator (returned an Iterator, not a Seq)";
id Seq.new(newit), [5, 10, 0].Seq, "Iterator (values)";
ok it.pull-one =:= IterationEnd, "Iterator (original, exhausted)";
ok newit.pull-one =:= IterationEnd, "Iterator (new, exhausted)";

# Invalid calls
throws-like { [12, 3, 4].&clip }, Exception, "No bound provided", message => / 'Must supply at least lower or upper' /;
throws-like { [12, 3, 4].&clip(from-below => 5, from-above => 3) }, Exception, "Lower limit greater than upper limit", message => / 'Lower bound shouldn\'t be greater than the upper bound' /;

done-testing;


