v2 / vlib / json / tests / json_struct_option_test.v
46 lines · 39 sloc · 772 bytes · 8ebbacecd60366ac4ba68aa35f9b0e7a0e56ff61
Raw
1import json
2
3pub struct MyStruct[T] {
4pub mut:
5 result ?T
6 id string
7}
8
9fn test_gn_struct_string() ! {
10 a := MyStruct[string]{
11 result: 'test'
12 id: 'some id'
13 }
14
15 encoded_string := json.encode(a)
16 dump(encoded_string)
17 test := json.decode(MyStruct[string], encoded_string)!
18 dump(test)
19 assert a == test
20}
21
22fn test_gn_struct_int() ! {
23 a := MyStruct[int]{
24 result: 1
25 id: 'some id'
26 }
27
28 encoded_string := json.encode(a)
29 dump(encoded_string)
30 test := json.decode(MyStruct[int], encoded_string)!
31 dump(test)
32 assert a == test
33}
34
35fn test_gn_struct_f64() ! {
36 a := MyStruct[f64]{
37 result: 1.2
38 id: 'some id'
39 }
40
41 encoded_string := json.encode(a)
42 dump(encoded_string)
43 test := json.decode(MyStruct[f64], encoded_string)!
44 dump(test)
45 assert a == test
46}
47