File: 090-unused-keys.t

package info (click to toggle)
raku-json-unmarshal 0.15-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 160 kB
  • sloc: makefile: 4
file content (34 lines) | stat: -rw-r--r-- 780 bytes parent folder | download
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)";