File: 030-trait.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 (65 lines) | stat: -rw-r--r-- 1,962 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
#!raku

use v6;
use Test;

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

subtest {
    class VersionClassCode {
        has Version $.version is marshalled-by(-> Version $v { $v.defined ?? $v.Str !! Nil });
    }

    my VersionClassCode $obj = VersionClassCode.new(version => Version.new("0.0.1"));

    my $json;


    lives-ok { $json = marshal($obj) }, "marshall with attribute trait (code)";

    my $parsed;
    lives-ok { $parsed = from-json($json) }, "parse the resulting JSON";

    ok $parsed.defined, "got something back";
    is $parsed<version>, "0.0.1", "and has the right value";

    $obj = VersionClassCode.new;

    lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (code) but attribute not defined";
    lives-ok { $parsed = from-json($json) }, "got sensible JSON back";
    ok $parsed.defined, "got something back";
    ok $parsed<version>:exists, "got the key";
    ok !$parsed<version>.defined, "and has the right value (Nil)";

}, "marshalled-by trait with Code";
subtest {
    class VersionClassMethod {
        has Version $.version is marshalled-by('Str');
    }

    my VersionClassMethod $obj = VersionClassMethod.new(version => Version.new("0.0.1"));

    my $json;
    lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (method name)";

    my $parsed;

    lives-ok { $parsed = from-json($json) }, "got sensible JSON back";

    ok $parsed.defined, "got something back";
    is $parsed<version>, "0.0.1", "and has the right value";

    $obj = VersionClassMethod.new;

    lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (method name) but attribute not defined";
    lives-ok { $parsed = from-json($json) }, "got sensible JSON back";
    ok $parsed.defined, "got something back";
    ok $parsed<version>:exists, "got the key";
    ok !$parsed<version>.defined, "and has the right value (Nil)";


}, "marshalled-by trait with Method name";

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