aboutsummaryrefslogtreecommitdiff
path: root/test/serialization.tm
blob: fd1ac743a27b846a7eef41a692d12311ecadddf4 (plain)
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

struct Foo(name:Text, next:@Foo?=none)

enum MyEnum(Zero, One(x:Int), Two(x:Num, y:Text))

func main()
    do
        >> obj := Int64(123)
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Int64) == obj

    do
        >> obj := 5
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Int) == obj

    do
        >> obj := 9999999999999999999999999999999999999999999999999999
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Int) == obj

    do
        >> obj := "Héllo"
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Text) == obj

    do
        >> obj := [Int64(10), Int64(20), Int64(30)].reversed()
        >> bytes := obj.serialized()
        assert deserialize(bytes -> [Int64]) == obj

    do
        >> obj := yes
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Bool) == obj

    do
        >> obj := @[10, 20]
        >> bytes := obj.serialized()
        >> roundtrip := deserialize(bytes -> @[Int])
        >> roundtrip == obj
        = no
        assert roundtrip[] == obj[]

    do
        >> obj := {"A":10, "B":20; fallback={"C":30}}
        >> bytes := obj.serialized()
        >> roundtrip := deserialize(bytes -> {Text:Int})
        assert roundtrip == obj
        assert roundtrip.fallback == obj.fallback

    do
        >> obj := @Foo("root")
        >> obj.next = @Foo("abcdef", next=obj)
        >> bytes := obj.serialized()
        >> roundtrip := deserialize(bytes -> @Foo)
        assert "$roundtrip" == "$obj"

    do
        >> obj := MyEnum.Two(123, "OKAY")
        >> bytes := obj.serialized()
        assert deserialize(bytes -> MyEnum) == obj

    do
        >> obj := "Hello"?
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Text?) == obj

    do
        >> obj := |10, 20, 30|
        >> bytes := obj.serialized()
        assert deserialize(bytes -> |Int|) == obj

    do
        >> obj : Num? = none
        >> bytes := obj.serialized()
        assert deserialize(bytes -> Num?) == obj

    do
        cases := [0, -1, 1, 10, 100000, 999999999999999999999999999]
        for i in cases
            >> bytes := i.serialized()
            assert deserialize(bytes -> Int) == i