File: 050-skip-null.t

package info (click to toggle)
raku-json-marshal 0.0.25-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 4
file content (62 lines) | stat: -rw-r--r-- 1,795 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
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
#!raku

use v6;
use Test;

use JSON::Marshal;
use JSON::Fast;

plan 15;

# test default global behaviour
class SkipTestClassOne {
    has Str $.id;
    has Str $.name;
    has Str %.stuff;
    has Str @.things;
}

my $res-default;

lives-ok { $res-default = marshal(SkipTestClassOne.new(name => "foo"), :skip-null) }, "apply skip-null to marshal";

my $out = from-json($res-default);

nok $out<id>:exists, "and the (null) id was skipped";
nok $out<stuff>:exists, "and the empty stuff was skipped";
nok $out<things>:exists, "and the empty things was skipped";
is  $out<name>, "foo", "but we still got the defined one";

class SkipTestClassTwo {
    has Str $.id is json-skip-null;
    has Str $.rev is json-skip-null;
    has Str $.name;
    has Str $.leave-blank;
    has Str %.empty-hash;
    has Str %.skip-hash is json-skip-null;
}

lives-ok { $res-default = marshal(SkipTestClassTwo.new(name => "foo", rev => "bar")) }, "apply skip-null trait to single attribute";

$out = from-json($res-default);

nok $out<id>:exists, "and the (null) id was skipped";
is  $out<name>, "foo", "but we still got the defined one";
ok $out<leave-blank>:exists, "one not defined but without trait still there";
nok $out<leave-blank>.defined, "and it isn't defined";
is $out<rev>, "bar", "one with the trait but with value is there";
ok $out<empty-hash>:exists, "the empty hash is there";
nok $out<skip-hash>:exists, "the skipped one isn't there";

class NestedStruct {
    has SkipTestClassOne:D $.stc1 .= new;
    has SkipTestClassTwo:D $.stc2 .= new;
}

lives-ok
    { $res-default = marshal(NestedStruct.new, :skip-null, :sorted-keys, :!pretty) },
    "skip-null with a deep struct";
is $res-default, '{"stc1":{},"stc2":{}}', "no empty keys are included";

done-testing;
# vim: expandtab shiftwidth=4 ft=raku