### ----------------------------------------------------
### -- Pakku
### -- Licenses: Artistic-2.0
### -- Authors: Haytham Elganiny
### -- File: t/spec.rakutest
### ----------------------------------------------------
#!/usr/bin/env raku

use Test;

use Pakku::Spec;

my @rakuspec = (
  'Spec',
  'Spec::Spec',
  'Spec::Spec:ver<0.42>',
  'Spec::Spec:ver<0.42>:auth<github:jojo>',
  'Spec::Spec:auth<github:jojo>:ver<0.42>',
  'Spec::Spec:ver<0.1>:auth<github:jojo>:api<0.42>',
  { :from<raku>, :name<Spec::Spec> },
);


for @rakuspec -> $s {
   my $spec = Pakku::Spec.new: $s;
   isa-ok $spec, Pakku::Spec::Raku, ~$spec;
}

my @binspec = (
  'spec:from<bin>',
  { :from<bin>, :name<spec> },
);

for @binspec -> $s {
   my $spec = Pakku::Spec.new: $s;
   isa-ok $spec, Pakku::Spec::Bin, ~$spec;
}

my @nativespec = (
  'spec:from<native>',
  { :from<native>, :name<spec> },
  { :from<native>, :name<spec>, :ver<0.42> },
);

for @nativespec -> $s {
   my $spec = Pakku::Spec.new: $s;
   isa-ok $spec, Pakku::Spec::Native, ~$spec;
}


my @junction-spec = (
  { any => [ 'spec1:ver<1>', 'spec2' ] },
  { any => [ 'spec:ver<1>:from<bin>', { :from<Bin>, :name<spec> } ] },
  { any => [ { :from<Native>, :name<spec> }, { :from<bin>, :name<spec> } ] },
);

for @junction-spec -> %spec {
   lives-ok { Pakku::Spec.new: %spec }, %spec.raku;
}


my @invalidspec = (

  'Spec:',
  'Spec::',
  'Spec::Spec:ver',
  'Spec::Spec:ver<',
  'Spec::Spec:ver<0.1>:auth<github:jojo>:api42>',

);


for @invalidspec -> $s {

  dies-ok { Pakku::Spec.new( $s ) }, "$s is not valid spec";

}

my $meta = q:to/META/;
{
    "perl" : "6.*",
    "name" : "dist",
    "auth" : "github:hythm7",
    "api" : "0",
    "version" : "0.0.1",
    "description" : "Description.",
    "authors" : [ "Haytham Elganiny" ],
    "license" : "Artistic-2.0",
    "provides" : {
        "Module" : "lib/Module.rakumod"
    },
    "depends" : [
      "perl:from<native>"
    ],
    "build-depends" : [ ],
    "test-depends" : [ ],
    "resources" : [
    ],
   "tags": [
   ],
    "source-url" : "https://github.com/hythm7/dist.git"
}

META

my @pathspec = (
  $*TMPDIR.add( 'pakku-test-' ~ now.Num ).add( 'META6.json' ),
);

for @pathspec -> $path {

   next if $*DISTRO.is-win; # fails on windows for some reason

   $path.dirname.IO.mkdir;
   $path.spurt: $meta;
   isa-ok Pakku::Spec.new( $path.dirname.IO ), Pakku::Spec::Raku, 'Spec from META6 file';
   $path.unlink;
   $path.dirname.IO.rmdir;
}

my $spec1 = Pakku::Spec.new: 'foo';
my $spec2 = Pakku::Spec.new: 'foo:ver<0.0.2+>';
my @spec3 = Pakku::Spec.new: { any => [ 'bar:ver<0.0.1>', 'foo:ver<0.0.3>' ] };
my $spec4 = Pakku::Spec.new: 'foo:ver<0.0.4+>';
my $spec5 = Pakku::Spec.new: 'foo:api<5>';

my $dist-spec = Pakku::Spec.new: 'foo:ver<0.0.3>:auth<bar>:api<3>'; 

ok  $dist-spec ~~ $spec1, "$dist-spec matches $spec1";
ok  $dist-spec ~~ $spec2, "$dist-spec matches $spec2";
ok  $dist-spec ~~ any(@spec3), "$dist-spec matches { @spec3.map(~*).join: ' | ' }";
nok $dist-spec ~~ $spec4, "$dist-spec does not match $spec4";
nok $dist-spec ~~ $spec5, "$dist-spec does not match $spec5";


done-testing;


