-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialize_test.c3
More file actions
111 lines (98 loc) · 2.7 KB
/
Copy pathserialize_test.c3
File metadata and controls
111 lines (98 loc) · 2.7 KB
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
module serialize_test;
import std::collections;
import std::core::test;
import json;
fn void test_basic() @test {
test::eq(json::to_json(true, allocator::temp())!!, "true");
test::eq(json::to_json("one", allocator::temp())!!, `"one"`);
test::eq(json::to_json(1, allocator::temp())!!, "1");
}
struct Foo {
bool is_bar;
int baz;
}
fn void test_struct() @test {
Foo foo = { .is_bar = true, .baz = 1 };
test::eq(json::to_json(foo, allocator::temp())!!, `{"is_bar":true,"baz":1}`);
}
fn void test_array() @test {
int[] xs = { 1, 2 };
test::eq(json::to_json(xs, allocator::temp())!!, "[1,2]");
}
struct Bar {
int[] bar;
}
fn void test_soa() @test {
Bar bar = { .bar = { 1, 2 } };
test::eq(json::to_json(bar, allocator::temp())!!, `{"bar":[1,2]}`);
}
fn void test_aos() @test {
Foo[] foos = { { .is_bar = true, .baz = 1 }, { .is_bar = false, .baz = 2 } };
String expected = `[{"is_bar":true,"baz":1},{"is_bar":false,"baz":2}]`;
test::eq(json::to_json(foos, allocator::temp())!!, expected);
}
fn void test_maybe() @test {
Maybe{int} x = maybe::value{int}(1);
test::eq(json::to_json(x, allocator::temp())!!, "1");
}
struct MaybeSkip {
Maybe{int} foo @tag("json", "skip_empty");
Maybe{int} bar @tag("json", "skip_empty");
Maybe{int} foo_noskip;
Maybe{int} bar_noskip;
}
fn void test_maybe_skip() @test {
MaybeSkip maybe_skip = {
.foo = maybe::value{int}(1),
.bar = maybe::EMPTY{int},
.foo_noskip = maybe::value{int}(1),
.bar_noskip = maybe::EMPTY{int},
};
test::eq(json::to_json(maybe_skip, allocator::temp())!!, `{"foo":1,"foo_noskip":1,"bar_noskip":null}`);
}
enum MyEnum {
FOO,
BAR,
}
fn void test_enum() @test {
MyEnum my = FOO;
test::eq(json::to_json(my, allocator::temp())!!, `"FOO"`);
}
enum MyEnumAssoc : char (String json_repr) {
FOO = "foo",
BAR = "bar",
}
fn void test_enum_assoc() @test {
MyEnumAssoc my = BAR;
test::eq(json::to_json(my, allocator::temp())!!, `"bar"`);
}
struct StructWithUnion {
union {
Foo foo;
}
}
fn void test_struct_with_union() @test {
StructWithUnion s = {};
if (catch err = json::to_json(s, allocator::temp())) {
test::eq(err, serialize::UNION_NOT_SUPPORTED);
}
}
struct StructWithUnionCustom {
union {
Foo foo;
}
}
fn void? StructWithUnionCustom.to_json(self, DString* dest, int indent, int indent_level) {
// Unions not supported, must handle each variant manually
serialize::add_json(self.foo, dest, indent, indent_level)!;
}
fn void test_struct_with_union_custom() @test {
StructWithUnionCustom s = { .foo = { .is_bar = true} };
test::eq(json::to_json(s, allocator::temp())!!, `{"is_bar":true,"baz":0}`);
}
fn void test_hashmap() @test {
HashMap{String,int} s;
s.tinit();
s.set("one", 1);
test::eq(json::to_json(s, allocator::temp())!!, `{"one":1}`);
}