1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
use Test;
use JSON::Unmarshal;
plan 4;
subtest {
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";
}, 'where subset with Junction type';
subtest {
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";
}, 'positional vs associative mismatch';
subtest {
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";
}, 'class that does positional with pairwise constructor at top level';
subtest {
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";
}, 'class that does positional with pairwise constructor as attribute';
|