v / vlib / json
Raw file | 127 loc (110 sloc) | 2.42 KB | Latest commit hash 992b50219
1import json
2
3struct TestTwin {
4 id int
5 seed string
6 pubkey string
7}
8
9struct TestTwins {
10mut:
11 twins []TestTwin [required]
12}
13
14fn test_json_decode_fails_to_decode_unrecognised_array_of_dicts() {
15 data := '[{"twins":[{"id":123,"seed":"abcde","pubkey":"xyzasd"},{"id":456,"seed":"dfgdfgdfgd","pubkey":"skjldskljh45sdf"}]}]'
16 json.decode(TestTwins, data) or {
17 assert err.msg() == "expected field 'twins' is missing"
18 return
19 }
20 assert false
21}
22
23fn test_json_decode_works_with_a_dict_of_arrays() {
24 data := '{"twins":[{"id":123,"seed":"abcde","pubkey":"xyzasd"},{"id":456,"seed":"dfgdfgdfgd","pubkey":"skjldskljh45sdf"}]}'
25 res := json.decode(TestTwins, data) or {
26 assert false
27 exit(1)
28 }
29 assert res.twins[0].id == 123
30 assert res.twins[0].seed == 'abcde'
31 assert res.twins[0].pubkey == 'xyzasd'
32 assert res.twins[1].id == 456
33 assert res.twins[1].seed == 'dfgdfgdfgd'
34 assert res.twins[1].pubkey == 'skjldskljh45sdf'
35}
36
37struct Mount {
38 size u64
39}
40
41fn test_decode_u64() {
42 data := '{"size": 10737418240}'
43 m := json.decode(Mount, data)!
44 assert m.size == 10737418240
45 // println(m)
46}
47
48//
49
50pub struct Comment {
51pub mut:
52 id string
53 comment string
54}
55
56pub struct Task {
57mut:
58 description string
59 id int
60 total_comments int
61 file_name string [skip]
62 comments []Comment [skip]
63 skip_field string [json: '-']
64}
65
66fn test_skip_fields_should_be_initialised_by_json_decode() {
67 data := '{"total_comments": 55, "id": 123}'
68 mut task := json.decode(Task, data)!
69 assert task.id == 123
70 assert task.total_comments == 55
71 assert task.comments == []
72}
73
74fn test_skip_should_be_ignored() {
75 data := '{"total_comments": 55, "id": 123, "skip_field": "foo"}'
76 mut task := json.decode(Task, data)!
77 assert task.id == 123
78 assert task.total_comments == 55
79 assert task.comments == []
80 assert task.skip_field == ''
81}
82
83//
84
85struct DbConfig {
86 host string
87 dbname string
88 user string
89}
90
91fn test_decode_error_message_should_have_enough_context_empty() {
92 json.decode(DbConfig, '') or {
93 assert err.msg().len < 2
94 return
95 }
96 assert false
97}
98
99fn test_decode_error_message_should_have_enough_context_just_brace() {
100 json.decode(DbConfig, '{') or {
101 assert err.msg() == '{'
102 return
103 }
104 assert false
105}
106
107fn test_decode_error_message_should_have_enough_context_trailing_comma_at_end() {
108 txt := '{
109 "host": "localhost",
110 "dbname": "alex",
111 "user": "alex",
112}'
113 json.decode(DbConfig, txt) or {
114 assert err.msg() == ' "user": "alex",\n}'
115 return
116 }
117 assert false
118}
119
120fn test_decode_error_message_should_have_enough_context_in_the_middle() {
121 txt := '{"host": "localhost", "dbname": "alex" "user": "alex", "port": "1234"}'
122 json.decode(DbConfig, txt) or {
123 assert err.msg() == 'ost", "dbname": "alex" "user":'
124 return
125 }
126 assert false
127}