File: 040-types.rakutest

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 (194 lines) | stat: -rw-r--r-- 4,020 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!raku

use v6;
use Test;

use JSON::Unmarshal;

sub test-typed($json, Mu \obj-class, Mu $expected, Str :$message is copy, Bool :$is-null) {
    my $type-name = $expected.^name;
    my $ret;
    $message //= "$type-name attribute from " ~ ($json ~~ Str ?? "JSON string" !! "a " ~ $json.^name);
    subtest $message => {
        plan 4;
        $ret = unmarshal($json, obj-class);
        lives-ok { $ret = unmarshal($json, obj-class) }, "unmarshal with $type-name typed attribute";
        isa-ok $ret, obj-class.WHAT, "it's the right object type";
        if $is-null {
            nok $ret.attr.defined, "and unefined";
            isa-ok $ret.attr, $expected.WHAT, "and the correct type";
        }
        else {
            ok $ret.attr.defined, "and defined";
            is $ret.attr, $expected, "and the correct value";
        }
        done-testing;
    }
}

class RatClass {
    has $.attr;
}
class NumClass {
    has Num $.attr;
}
class IntClass {
    has Int $.attr;
}
class BoolClass {
    has Bool $.attr;
}
class StrClass {
    has Str $.attr;
}
class IntDClass {
    has Int:D $.attr is required;
}
class IntCoerced {
    has Int() $.attr;
}
class IntDCoerced {
    has Int:D() $.attr is required;
}
class VersionDCoerced {
    has Version:D() $.attr is required;
}
class DateTimeDClass {
    has DateTime:D $.attr is required;
}

my @tests =
    [
        '{ "attr" : 4.2 }',
        RatClass,
        4.2,
    ],
    [
        '{ "attr" : 4.2 }',
        NumClass,
        4.2,
    ],
    [
        '{ "attr" : 42 }',
        IntClass,
        42,
    ],
    [
        '{ "attr" : 42 }',
        IntDClass,
        42,
        :message("Int:D attribute"),
    ],
    [
        '{ "attr" : 42 }',
        IntCoerced,
        42,
        :message("Int() attribute"),
    ],
    [
        '{ "attr" : 42 }',
        IntDCoerced,
        42,
        :message("Int:D() attribute"),
    ],
    [
        '{ "attr" : "0.12.42" }',
        VersionDCoerced,
        v0.12.42,
        :message("Version:D() attribute")
    ],
    [
        '{ "attr" : true }',
        BoolClass,
        True,
    ],
    [
        '{ "attr" : false }',
        BoolClass,
        False,
        :message("Bool attribute with False")
    ],
    [
        '{ "attr" : "foo" }',
        StrClass,
        "foo",
    ],
    [
        '{ "attr" : null }',
        StrClass,
        Str,
        message => "Str attribute with 'null' in JSON",
        :is-null,
    ],
    [
        '{ "attr": { "daycount": 59770, "month": 7, "timezone": -14400, "formatter": null, "hour": 20, "minute": 32, "year": 2022, "day": 10, "second": 12.345e0 } }',
        DateTimeDClass,
        DateTime.new(2022,7,10,20,32,12.345e0,:timezone(-14400)),
        :message("DateTime as a definite"),
    ],
    [
        { attr => 4.2 },
        RatClass,
        4.2,
    ],
    [
        { attr => 4.2 },
        NumClass,
        4.2,
    ],
    [
        { attr => 42 },
        IntClass,
        42,
    ],
    [
        { attr => True },
        BoolClass,
        True,
    ],
    [
        { attr => False },
        BoolClass,
        False,
        :message("Bool attribute with False in JSON hash")
    ],
    [
        { attr => "foo" },
        StrClass,
        "foo",
    ],
    [
        { attr => Nil },
        StrClass,
        Str,
        message => "Str attribute with Nil in JSON hash",
        :is-null,
    ],
    ;

plan 2+@tests;

for @tests -> @test {
    my @pos = @test.grep( * !~~ Pair );
    my %named = |@test.grep( * ~~ Pair );
    test-typed |@pos, |%named;
}

throws-like
    { unmarshal('{"attr": null}', IntDClass ) },
    X::TypeCheck::Assignment,
    "null into a definite dies";

class NastyCoercive {
    has Int(Rat) $.attr;
}

throws-like
    { unmarshal('{"attr": "13"}', NastyCoercive) },
    X::CannotUnmarshal,
    :message(q«Cannot unmarshal "13" into type 'Int' for attribute $!attr of 'NastyCoercive'»),
    "unmarshalling into a wrong type throws";

done-testing;

# vim: expandtab shiftwidth=4 ft=raku