### ----------------------------------------------------
### -- Configuration
### -- Licenses: Artistic 2.0
### -- Authors: 
### -- File: t/01-config.rakutest
### ----------------------------------------------------
use Test;
use Configuration;

class Conf1 does Configuration::Node {
  has     $.any;
  has Int $.int;
  has     @.array;
  has     %.hash;

  method set(:$any, :$int, :$array, :$hash) {
    $.any   = $_ with $any;
    $.int   = $_ with $int;
    $.array = $_ with $array;
    $.hash  = $_ with $hash;
  }
  method join { join " - ", $!any, $!int, @!array.raku, %!hash.raku }
  method gist { self.raku }
}

my %exports := generate-exports Conf1;

is %exports<&config>.({
  .any = 42
}).raku, Conf1.new(:42any).raku, "Passing Int to any";

is %exports<&config>.({
  .any = "bla"
}).raku, Conf1.new(:any<bla>).raku, "Passing Str to any";

is %exports<&config>.({
  .any = * + 1
}).any.(1), 2, "Passing WhateverCode to any";

is %exports<&config>.({
  .int = 42
}).raku, Conf1.new(:42int).raku, "Passing 42 to int";

throws-like {
  my $error = False;
  is %exports<&config>.({
    .int = "bla"
  }), Nil, "Passing wrong type";
  ok $error, "Called 'note'"
}, Exception, message => /Assignment/;

is %exports<&config>.({
  .array = 42
}).raku, Conf1.new(:array[42,]).raku, "Passing 42 to array";

is %exports<&config>.({
  .array.push: 13
}).raku, Conf1.new(:array[13,]).raku, "Push 13 on array";

is %exports<&config>.({
  .array = 3.14, 13, 42
}).raku, Conf1.new(:array[3.14, 13, 42]).raku, "Passing 3.14, 13, 42 to array";

is %exports<&config>.({
  .hash = answer => 42
}).raku, Conf1.new(:hash{answer => 42,}).raku, "Passing answer => 42 to hash";

is %exports<&config>.({
  .hash<luck> = 13
}).raku, Conf1.new(:hash{luck => 13,}).raku, "Push luck => 13 on hash";

is %exports<&config>.({
  .hash = pi => 3.14, luck => 13, answer => 42
}).raku, Conf1.new(:hash{ pi => 3.14, luck => 13, answer => 42 }).raku, "Passing pi => 3.14, luck => 13, answer => 42 to hash";

is %exports<&config>.({
  .set: :any<set>
}).raku, Conf1.new(:any<set>).raku, "using method";

is %exports<&config>.({
  .set: :any<set>, :1int
}).raku, Conf1.new(:any<set>, :1int).raku, "using method";

# is %exports<&config>.({
#   .set: :any<set>, :1int, :array[1,2,3]
# }).raku, Conf1.new(:any<set>, :1int).raku, "using method";

done-testing


