v / vlib / json / tests / json_generic_array_test.v
47 lines · 40 sloc · 1.12 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import json
2
3pub struct DocumentFindFilter[T] {
4pub:
5 selector ?T
6 selector2 ?[]T
7 selector3 ?[1]T
8 limit ?int
9 skip ?int
10 fields ?[]string // This breaks the compiler when encoding to JSON
11 conflicts ?bool
12 read_quorum ?int @[json: r]
13 update ?bool
14 stable ?bool
15 stale ?string
16 execution_stats ?bool
17}
18
19fn test_string() {
20 t := DocumentFindFilter[string]{
21 selector: 'aa'
22 selector2: ['a', 'b']
23 selector3: ['z']!
24 }
25
26 assert json.encode(t) == '{"selector":"aa","selector2":["a","b"],"selector3":["z"]}'
27 assert json.decode(DocumentFindFilter[string],
28 '{"selector":"aa","selector2":["a","b"],"selector3":["z"]}')! == t
29}
30
31fn test_int() ! {
32 t := DocumentFindFilter[int]{
33 selector: 1
34 selector2: [1, 2]
35 selector3: [3]!
36 }
37
38 assert json.encode(t) == '{"selector":1,"selector2":[1,2],"selector3":[3]}'
39 assert json.decode(DocumentFindFilter[int], '{"selector":1,"selector2":[1,2],"selector3":[3]}')! == t
40}
41
42fn test_none() ! {
43 t := DocumentFindFilter[int]{}
44
45 assert json.encode(t) == '{}'
46 assert json.decode(DocumentFindFilter[int], '{}')! == t
47}
48