### ----------------------------------------------------
### -- Iter::Able
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/16-insert-at.rakutest
### ----------------------------------------------------
use Test;
use Iter::Able::Insert-At;

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

plan 20;

my $s;
$s := [2, 3];
id insert-at($s, 0 => 1), [1, 2, 3].Seq, "Random list with 1 pair";

$s := (1, 2, 0, 16);
id insert-at($s, 2 => 4, 3 => 9), [1, 2, 4, 0, 9, 16].Seq, "Random list with 2 pairs";

$s := [5, 7];
id $s.&insert-at(2 => 9), $s.Seq, "Random list with out-of-bounds index";

$s := ["e", "g"];
id $s.&insert-at(0 => "d", 2 => "f"), ("d", "e", "g").Seq, "pairs == iterable in length; at least one of them is ignored";

$s := [0 => 0, 2 => 2].Seq;
id $s.&insert-at(1 => 1 => 1), [0 => 0, 1 => 1, 2 => 2].Seq, "Seq with pairs in it";

$s := [3];
id $s.&insert-at(1 => 8), $s.Seq, "Past the end is not in effect";

$s := "xyw".comb;
id $s.&insert-at(2 => "z"), "xyzw".comb, "String insertion to a Seq";

$s := [];
id insert-at($s, 0 => 0), [].Seq,  "Empty list";

my $t = ();
id insert-at($t, 3 => 2), [].Seq,  "Empty itemized list";

$s := "aise";
id insert-at($s, 1 => "r"), "arise", "String with 1 pair";

$s := "sing";
id insert-at($s, 1 => "tr"), "string", "String with an expanding pair";

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

# Invalid calls
throws-like { [].&insert-at }, Exception, "No pair", message => /:i 'Must pass at least one pair' /;
throws-like { [].&insert-at(^Inf X=> 1) }, Exception, "Possibly nonfinite number of pairs", message => /:i 'lazy' /;
throws-like { "".&insert-at(*) }, Exception, "Nonpair translator", message => /:i 'should all be pairs' /;
throws-like { "".&insert-at("yes" => 4) }, Exception, "Noninteger key-having translator", message => /:i 'keys should be all nonnegative integers' /;
throws-like { "".&insert-at(-1 => 4) }, Exception, "Noninteger key-having translator", message => /:i 'keys should be all nonnegative integers' /;

done-testing;


