-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.v
More file actions
196 lines (173 loc) · 3.93 KB
/
encoder_test.v
File metadata and controls
196 lines (173 loc) · 3.93 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
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
195
196
import xmlencoder
fn test_encode_primitives() {
assert xmlencoder.encode[int](15) == '<int>15</int>'
assert xmlencoder.encode[[]int]([1, 2, 3]) == '<int>1</int><int>2</int><int>3</int>'
assert xmlencoder.encode[string]('R&D') == '<string>R&D</string>'
assert xmlencoder.encode[[]string](['a', 'b', 'c']) == '<string>a</string><string>b</string><string>c</string>'
}
@[xml: employee]
struct Employee {
name string
age int
department string
}
fn test_encode_struct() {
doc := Employee{
name: 'John Doe'
age: 24
department: 'R&D'
}
doc_str := '<employee><name>John Doe</name><age>24</age><department>R&D</department></employee>'
doc_str_pretty := '
<employee>
<name>John Doe</name>
<age>24</age>
<department>R&D</department>
</employee>'.trim_indent()
assert xmlencoder.encode[Employee](doc) == doc_str
assert xmlencoder.encode[Employee](doc, pretty: true) == doc_str_pretty
}
fn test_encode_array_of_structs() {
doc := [
Employee{
name: 'John Doe'
age: 24
department: 'R&D'
},
Employee{
name: 'Jane Doe'
age: 21
department: 'Sales'
},
]
doc_str_pretty := '
<employee>
<name>John Doe</name>
<age>24</age>
<department>R&D</department>
</employee>
<employee>
<name>Jane Doe</name>
<age>21</age>
<department>Sales</department>
</employee>'.trim_indent()
assert xmlencoder.encode[[]Employee](doc, pretty: true) == doc_str_pretty
}
@[xml: doc]
struct Doc {
EmbedStruct
name string @[xml: documentName]
nested NestedOne @[xml: nestedOne]
status Status
}
struct EmbedStruct {
document_id int @[xml: documentId]
}
struct NestedOne {
foo int
bar []string
baz Baz @[xml: baZ]
empty Empty
}
struct Baz {
bytearray []u8
}
struct Empty {}
enum Status {
unknown
open
closed
}
fn test_encode_complex_struct() {
doc := Doc{
document_id: 228
name: 'Top Secret'
nested: NestedOne{
baz: Baz{[u8(0), 1, 2, 3, 4]}
}
}
doc_str_pretty := '
<doc>
<EmbedStruct>
<documentId>228</documentId>
</EmbedStruct>
<documentName>Top Secret</documentName>
<nestedOne>
<foo>0</foo>
<baZ>
<bytearray>0</bytearray>
<bytearray>1</bytearray>
<bytearray>2</bytearray>
<bytearray>3</bytearray>
<bytearray>4</bytearray>
</baZ>
<empty/>
</nestedOne>
<status>unknown</status>
</doc>'.trim_indent()
assert xmlencoder.encode[Doc](doc, pretty: true) == doc_str_pretty
}
@[xml: foo]
struct Foo {
bar struct {
hello int
world int
}
baz struct {
hello int
world int
} @[xml: baZ]
}
fn test_encode_anon_struct() {
foo := Foo{}
assert xmlencoder.encode(foo) == '<foo><bar><hello>0</hello><world>0</world></bar><baZ><hello>0</hello><world>0</world></baZ></foo>'
}
@[xml: opt]
struct WithOptions {
hello int
maybe ?string
}
fn test_encode_options() {
// assert xmlencoder.encode(WithOptions{ hello: 1 }) == '<opt><hello>1</hello></opt>'
}
@[xml: topology]
struct DocWithAttrs {
sockets int @[xml: ',attr']
cores int @[xml: ',attr']
threads int @[xml: 'threads,attr']
}
fn test_encode_xml_attrs() {
assert xmlencoder.encode(DocWithAttrs{1, 4, 1}) == "<topology sockets='1' cores='4' threads='1'/>"
}
@[xml: memory]
struct Memory {
unit string @[xml: ',attr']
value u64 @[xml: ',chardata']
}
fn test_encode_chardata() {
doc_str := "<memory unit='MiB'>10240</memory>"
assert xmlencoder.encode(Memory{ unit: 'MiB', value: 10240 }) == doc_str
}
struct CData {
data string @[xml: ',cdata']
}
fn test_encode_cdata() {
d := CData{
data: 'R&D'
}
assert xmlencoder.encode(d) == '<CData><![CDATA[R&D]]></CData>'
}
@[xmlns: 'meta,https://example.com/xmlns/meta']
@[xml: 'metadata']
struct Metadata {
key string
}
fn test_encode_xmlns() {
m := Metadata{
key: 'foobar'
}
assert xmlencoder.encode(m, pretty: true) == "
<meta:metadata xmlns:meta='https://example.com/xmlns/meta'>
<meta:key>foobar</meta:key>
</meta:metadata>".trim_indent()
}