### ----------------------------------------------------
### -- Identity::Utils
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;

use Identity::Utils;

plan 149;

# Check all of the default exports
isa-ok OUTER::MY::{"&$_"}, Sub, "&$_ was exported" for <
  api auth build bytecode bytecode-io compunit dependency-specification
  ecosystem from is-short-name is-pinned latest-successors meta nick
  sanitize short-name source source-io ver version
  without-api without-auth without-from without-ver
>;

# Check String::Utils subs *not* being exported
ok OUTER::MY::{"&$_"}:!exists, "&$_ has not been exported" for <
  after before between between-included
>;

my $short-name = "Foo::Bar";
my $ver        = "0.0.42";
my $version    = $ver.Version;
my $ecosystem  = "zef";
my $nick       = "lizmat";
my $auth       = "$ecosystem:$nick";
my $api        = "2.0";
my $from       = "Perl5";
my $sanitized  = $short-name ~ ":ver<$ver>:auth<$auth>:api<$api>:from<$from>";

is build($short-name, :$ver, :$auth, :$api, :$from), $sanitized, 'is build ok?';

for
    (":ver<$ver>",   ":auth<$auth>",   ":api<$api>",   ":from<$from>"),
    (":ver('$ver')", ":auth('$auth')", ":api('$api')", ":from('$from')")
-> @parts {
    my $identity = $short-name ~ @parts.join;

    my $sver  := @parts[0];
    my $sauth := @parts[1];
    my $sapi  := @parts[2];
    my $sfrom := @parts[3];

    for @parts.combinations -> @combinations {
        my $long-name = "$short-name@combinations.join('')";
        is short-name($long-name), $short-name,
          "is short-name of $long-name ok?";
    }

    for @parts.permutations -> @permuted {
        my $long-name = "$short-name@permuted.join('')";
        is sanitize($long-name), $sanitized, "is sanitize of $long-name ok?";
    }

    is ver($identity),            $ver,       'is ver ok?';
    is-deeply version($identity), $version,   'is version ok?';
    is auth($identity),           $auth,      'is auth ok?';
    is ecosystem($identity),      $ecosystem, 'is ecosystem ok?';
    is nick($identity),           $nick,      'is nick ok?';
    is api($identity),            $api,       'is api ok?';
    is from($identity),           $from,      'is from ok?';

    isa-ok dependency-specification($identity),
      CompUnit::DependencySpecification;

    subtest 'without-ver' => {
        plan 2;
        my $without := $short-name ~ $sauth ~ $sapi ~ $sfrom;
        is without-ver($identity), $without, 'is without-ver (1) ok?';
        is without-ver($without),  $without, 'is without-ver (2) ok?';
    }

    subtest 'without-auth' => {
        plan 2;
        my $without := $short-name ~ $sver ~ $sapi ~ $sfrom;
        is without-auth($identity), $without, 'is without-auth (1) ok?';
        is without-auth($without),  $without, 'is without-auth (2) ok?';
    }

    subtest 'without-api' => {
        plan 2;
        my $without := $short-name ~ $sver ~ $sauth ~ $sfrom;
        is without-api($identity), $without, 'is without-api (1) ok?';
        is without-api($without),  $without, 'is without-api (2) ok?';
    }

    subtest 'without-api' => {
        plan 2;
        my $without := $short-name ~ $sver ~ $sauth ~ $sapi;
        is without-from($identity), $without, 'is without-from (1) ok?';
        is without-from($without),  $without, 'is without-from (2) ok?';
    }

    is-deeply is-short-name($identity),  False, 'not is-short-name?';
}

is-deeply is-short-name($short-name), True, 'is-short-name ok?';

for <Perl6 Raku> {
    is build($short-name, :from($_)), $short-name, "is from<$_> ignored?";
}

is-deeply is-pinned($sanitized),   True, 'is is-pinned (1) ok?';
is-deeply is-pinned($short-name), False, 'is is-pinned (2) ok?';

my $class := "Identity::Utils";
my $dir   := $*PROGRAM.parent(2);
my $path  := $dir.absolute;
isa-ok compunit($class, $dir), CompUnit;
isa-ok (my $io := bytecode-io($class, $dir)), IO::Path;
if $io.e {
    isa-ok bytecode($class, $dir), Buf;
}
else {
    is $io.parent(4).absolute, $path, 'dir looks ok, but no bytecode yet';
}

my %meta = meta($class, $dir);
%meta<ver version>:delete;  # too finicky
is-deeply %meta, %(
 api          => "",
 auth         => "zef:lizmat",
 authors      => ["Elizabeth Mattijsen"],
 build-depends => [],
 depends      => ["String::Utils:ver<0.0.32+>:auth<zef:lizmat>"],
 description  => "Provide utility functions related to distribution identities",
 files        => %(),
 license      => "Artistic-2.0",
 name         => "Identity::Utils",
 perl         => "6.d",
 provides     => %("Identity::Utils" => "lib/Identity/Utils.rakumod"),
 resources    => [],
 source-url   => "https://github.com/lizmat/Identity-Utils.git",
 tags         => [<DISTRIBUTION META IDENTITY ECOSYSTEM>],
 test-depends => [],
), "is meta ok";

is source-io($class, $dir).absolute,
  $dir.add("lib/Identity/Utils.rakumod").absolute,
  "is the source IO correct?";
isa-ok source($class, $dir), Str;

ok source("Test").contains("unit module Test"), "Test source found";
ok source("String::Utils").contains("my sub between"),
  "String::Utils source found";

my @identities = q:to/IDENTITIES/.lines;
AccountableBagHash:ver<0.0.3>:auth<cpan:ELIZABETH>
AccountableBagHash:ver<0.0.6>:auth<zef:lizmat>
Acme::Addslashes:ver<0.1.2>:auth<github:xfix>
IDENTITIES

is-deeply latest-successors(@identities), @identities.skip,
  'did we get the correct successors';

# vim: expandtab shiftwidth=4


