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
|
use Test;
use JSON::Unmarshal;
plan 2;
class Foo {
has Int $.count;
has Bool $.check;
}
my $json = '{"count": 42, "check": true, "description": "about something", "name": "fubar" }';
{
my $warn-msg;
CONTROL {
when CX::Warn {
$warn-msg = .message;
.resume
}
}
my Foo $foo = unmarshal $json, Foo, :warn;
my $msg = "a warning produced for unsued JSON keys with :warn";
with $warn-msg {
like $_, /"No attributes found " .* "'description', 'name'"/, $msg;
}
else {
flunk $msg;
}
}
throws-like
{ my Foo $foo = unmarshal $json, Foo, :throw; },
X::UnusedKeys,
:unused-keys(<description name>.Set),
"an exception is thrown for unsued JSON keys with :throw (or :die)";
|