### ----------------------------------------------------
### -- JSON::Unmarshal
### -- Licenses: Artistic-2.0
### -- Authors: Tadeusz Sośnierz, Raku Community
### -- File: t/100-issue-7.rakutest
### ----------------------------------------------------
use Test;

use JSON::Unmarshal;

plan 4;

subtest 'where subset with Junction type' => {
    my class TestClass {
        has $.attribute where Positional|Associative;
    }

    my $obj;

    my $with-object-attr = '{ "attribute": { "foo" : "bar" } }';

    lives-ok { $obj = unmarshal($with-object-attr, TestClass) }, "with object attribute";

    is-deeply $obj.attribute, { foo => 'bar' }, "attribute got set correctly";

    my $with-list-attr = '{ "attribute": [ "foo", "bar" ] }';

    lives-ok { $obj = unmarshal($with-list-attr, TestClass) }, "with list attribute";

    is-deeply $obj.attribute, [ "foo", "bar" ], "attribute got set correctly";

}

subtest 'positional vs associative mismatch' => {
    my class TestClass {
        has Str @.attribute;
    }

    my $with-object-attr = '{ "attribute": { "foo" : "bar" } }';


    throws-like { unmarshal($with-object-attr, TestClass) }, X::CannotUnmarshal, "object passed for positional attribute";

}

subtest 'class that does positional with pairwise constructor at top level' => {
    my class TestClass does Positional {
        has Str $.string;
    }

    my $string-attribute = '{ "string" : "test value" }';

    my $obj;

    lives-ok { $obj = unmarshal($string-attribute, TestClass) }, "unmarshal";

    is $obj.string, 'test value', "got the attribute back";

}

subtest 'class that does positional with pairwise constructor as attribute' => {
    my class TestClassPos does Positional {
        has Str $.string;
    }

    my class TestClass {
        has TestClassPos $.pos;
    }

    my $string-attribute = '{"pos" : { "string" : "test value" }}';

    my $obj;

    lives-ok { $obj = unmarshal($string-attribute, TestClass) }, "unmarshal";

    is $obj.pos.string, 'test value', "got the attribute back";
}

# vim: expandtab shiftwidth=4 ft=raku


