### ----------------------------------------------------
### -- Listicles
### -- Licenses: MIT
### -- Authors: masukomi <masukomi@masukomi.org>
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use lib 'lib';
use Test;
use Listicles;

my @test_array     = [1,2,"4","hi", 3/4];
my @array_with_nil = [1, Nil, 3];
my @nested_arrays  = [[1, 2], [3, 4], [5, [6, [7]]]];
my @nested_seqs = ((1,2), (3,4), (5, (6, (7))));
my @nested_combo = [ (1, 2), [3, 4], (5, [6, (7)]) ];


# all on Array
ok( [2,4,6].all-are(->$x {$x mod 2 == 0}) , "Array.all-are produced unexpected False");
is( [1,3].all-are(->$x {$x mod 2 == 0}), False, "Array.all-are produced unexpected True for false set");
is( [1,2].all-are(->$x {$x mod 2 == 0}), False, "Array.all-are produced unexpected True for mixed set");
# all on Seq
ok( [2,4,6].Seq.all-are(->$x {$x mod 2 == 0}) , "Seq.all-are produced unexpected False");
is( [1,3].Seq.all-are(->$x {$x mod 2 == 0}), False, "Seq.all-are produced unexpected True for false set");
is( [1,2].Seq.all-are(->$x {$x mod 2 == 0}), False, "Seq.all-are produced unexpected True for mixed set");
# all on Seq
ok( [2,4,6].List.all-are(->$x {$x mod 2 == 0}) , "List.all-are produced unexpected False");
is( [1,3].List.all-are(->$x {$x mod 2 == 0}), False, "List.all-are produced unexpected True for false set");
is( [1,2].List.all-are(->$x {$x mod 2 == 0}), False, "List.all-are produced unexpected True for mixed set");
# any on Array
ok( [2,4,6].any-are(->$x {$x mod 2 == 0}) , "Array.any-are produced unexpected False");
is( [1,3].any-are(->$x {$x mod 2 == 0}), False, "Array.any-are produced unexpected True");
ok( [1,2].any-are(->$x {$x mod 2 == 0}), "Array.any-are produced unexpected False");
is( [1,3].any-are(->$x {$x mod 2 == 0}), False , "Array.any-are produced unexpected True");
# any on Seq
ok( [2,4,6].Seq.any-are(->$x {$x mod 2 == 0}) , "Seq.any-are produced unexpected False");
is( [1,3].Seq.any-are(->$x {$x mod 2 == 0}), False, "Seq.any-are produced unexpected True");
ok( [1,2].Seq.any-are(->$x {$x mod 2 == 0}), "Seq.any-are produced unexpected False");
is( [1,3].Seq.any-are(->$x {$x mod 2 == 0}), False , "Seq.any-are produced unexpected True");
# any on List
ok( [2,4,6].List.any-are(->$x {$x mod 2 == 0}) , "List.any-are produced unexpected False");
is( [1,3].List.any-are(->$x {$x mod 2 == 0}), False, "List.any-are produced unexpected True");
ok( [1,2].List.any-are(->$x {$x mod 2 == 0}), "List.any-are produced unexpected False");
is( [1,3].List.any-are(->$x {$x mod 2 == 0}), False , "List.any-are produced unexpected True");
# car
is( @test_array.car, 1 );
dies-ok({[].car()}, "can't call car on empty arrays");
# cdr
is( @test_array.cdr, [2, "4","hi", 3/4] );
is([1].cdr(), []);
dies-ok({[].cdr()}, "can't call cdr on empty arrays");
# drop positive
is( @test_array.drop(2), ["4","hi", 3/4] );
# drop negative
is( @test_array.drop(-2), [1,2,"4"] );
# drop too much, but flexible
is( [].drop(4), [] );
# drop too much inflexible
dies-ok({ @test_array.drop(20, all_or_nothing=>True) }, "should die if insuficient elements");
ok(@test_array.drop(1).isa(Array));
# flatten
is(@nested_arrays.flatten.elems, 7);
ok(@nested_arrays.flatten.isa(Seq), ".flatten should return a Seq (like .flat)");
is(@nested_seqs.flatten.elems, 7);
is(@nested_combo.flatten.elems, 7);
# includes
is(@test_array.includes(Nil), False, "should not find Nil");
is(@array_with_nil.includes(Nil), True, "should find Nil");
is(@test_array.includes("4"), True, "should find \"4\"");
is(@test_array.includes(4), False, 'should NOT find 4');
is(@test_array.includes(3/4), True, 'should find 3/4');
# NOTE: raku's handling of rationals may lead to unexpected results
is(@test_array.includes(0.75), True, 'should find 0.75');
# includes ducktyped
is(@test_array.includes(4, ducktyped=>True), True, 'should find ducktyped 4');
# is-empty
is(@test_array.is-empty(), False);
is([].is-empty(), True);
# is-empty Seq version
is(@nested_seqs.is-empty(), False);
is(().Seq.is-empty(), True);
# rest
is( @test_array.rest(), [2, "4","hi", 3/4] );
is( [].rest(), [] );
#
# split-by
# note the "too-small" entry at th eend
is(@test_array.split-by(2).raku, [[1,2],["4","hi"], [3/4]].raku);
# split-by (perfectly)
dies-ok({@test_array.split-by(2, only_perfect_splits=>True)}, "Should die if not evenly splittable");
is([1,2,3,4].split-by(2, only_perfect_splits=>True), [[1,2],[3,4]]);
# take positive
is(@test_array.take(2), [1,2]);
# take negative
is(@test_array.take(-2), ["hi", 3/4]);
# # take too much
is(@test_array.take(20), @test_array);
dies-ok({@test_array.take(20, :all_or_nothing=>True)}, "Should die if insufficent elements");
# to-hash
is(@test_array.to-hash(),  {0=>1, 1=>2, 2=>"4", 3=>"hi", 4=>3/4});
# to-hash one_based
is(@test_array.to-hash(one_based=>True),  {1=>1, 2=>2, 3=>"4", 4=>"hi", 5=>3/4});
# to-hash empty
is([].to-hash(), {});


done-testing;


