GCC Code Coverage Report


Directory: ./
File: test/parse_objects.c
Date: 2021-09-04 00:13:15
Exec Total Coverage
Lines: 166 166 100.0%
Branches: 64 122 52.5%

Line Branch Exec Source
1 /***************************************************************************/ /**
2
3 @file parse_objects.c
4
5 @author Stephen Brennan
6
7 @date Created Tuesday, 24 November 2015
8
9 @brief Test object parsing.
10
11 @copyright Copyright (c) 2015, Stephen Brennan. Released under the
12 Revised BSD License. See LICENSE.txt for details.
13
14 *******************************************************************************/
15
16 #include <unity.h>
17
18 #include "nosj.h"
19
20 16 void setUp(void)
21 {
22 // set stuff up here
23 16 }
24
25 16 void tearDown(void)
26 {
27 // clean stuff up here
28 16 }
29
30 1 static void test_empty_object(void)
31 1 {
32 1 char input[] = "{}";
33 1 size_t ntok = 1;
34 1 struct json_token tokens[ntok];
35 1 struct json_parser p = json_parse(input, tokens, ntok);
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == ntok);
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].type == JSON_OBJECT);
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 1);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 0);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].child == 0);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].next == 0);
45 1 }
46
47 1 static void test_single_element(void)
48 1 {
49 1 char input[] = "{\"a\": 1}";
50 1 size_t ntok = 3, i;
51 1 struct json_token tokens[ntok];
52 1 struct json_token expected[] = {
53 { .type = JSON_OBJECT,
54 .start = 0,
55 .end = 7,
56 .length = 1,
57 .child = 1,
58 .next = 0 },
59 { .type = JSON_STRING,
60 .start = 1,
61 .end = 3,
62 .length = 1,
63 .child = 2,
64 .next = 0 },
65 { .type = JSON_NUMBER,
66 .start = 6,
67 .end = 6,
68 .length = 0,
69 .child = 0,
70 .next = 0 },
71 };
72 1 struct json_parser p = json_parse(input, tokens, ntok);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == ntok);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
76
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < ntok; i++) {
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].type == expected[i].type);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].start == expected[i].start);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].end == expected[i].end);
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].length == expected[i].length);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].child == expected[i].child);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].next == expected[i].next);
83 }
84 1 }
85
86 1 static void test_multiple_elements(void)
87 1 {
88 1 char input[] = "{\"a\": 1, \"b\": 2}";
89 1 size_t ntok = 5, i;
90 1 struct json_token tokens[ntok];
91 1 struct json_token expected[] = {
92 { .type = JSON_OBJECT,
93 .start = 0,
94 .end = 15,
95 .length = 2,
96 .child = 1,
97 .next = 0 },
98 { .type = JSON_STRING,
99 .start = 1,
100 .end = 3,
101 .length = 1,
102 .child = 2,
103 .next = 3 },
104 { .type = JSON_NUMBER,
105 .start = 6,
106 .end = 6,
107 .length = 0,
108 .child = 0,
109 .next = 0 },
110 { .type = JSON_STRING,
111 .start = 9,
112 .end = 11,
113 .length = 1,
114 .child = 4,
115 .next = 0 },
116 { .type = JSON_NUMBER,
117 .start = 14,
118 .end = 14,
119 .length = 0,
120 .child = 0,
121 .next = 0 },
122 };
123 1 struct json_parser p = json_parse(input, tokens, ntok);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == ntok);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
127
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (i = 0; i < ntok; i++) {
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].type == expected[i].type);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].start == expected[i].start);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].end == expected[i].end);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].length == expected[i].length);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].child == expected[i].child);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 TEST_ASSERT(tokens[i].next == expected[i].next);
134 }
135 1 }
136
137 1 static void test_extra_comma(void)
138 1 {
139 1 char input[] = "{\"a\": 1,}";
140 1 size_t ntok = 3, i;
141 1 struct json_token tokens[ntok];
142 1 struct json_token expected[] = {
143 { .type = JSON_OBJECT,
144 .start = 0,
145 .end = 8,
146 .length = 1,
147 .child = 1,
148 .next = 0 },
149 { .type = JSON_STRING,
150 .start = 1,
151 .end = 3,
152 .length = 1,
153 .child = 2,
154 .next = 0 },
155 { .type = JSON_NUMBER,
156 .start = 6,
157 .end = 6,
158 .length = 0,
159 .child = 0,
160 .next = 0 },
161 };
162 1 struct json_parser p = json_parse(input, tokens, ntok);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == ntok);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
166
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < ntok; i++) {
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].type == expected[i].type);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].start == expected[i].start);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].end == expected[i].end);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].length == expected[i].length);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].child == expected[i].child);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 TEST_ASSERT(tokens[i].next == expected[i].next);
173 }
174 1 }
175
176 1 static void test_no_end(void)
177 {
178 1 char input[] = "{\"a\": 1,";
179 1 struct json_parser p = json_parse(input, NULL, 0);
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF);
181 1 }
182
183 1 static void test_no_colon(void)
184 {
185 1 char input[] = "{\"blah\" 2}";
186 1 struct json_parser p = json_parse(input, NULL, 0);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_EXPECTED_TOKEN);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.errorarg == ':');
189 1 }
190
191 1 static void test_missing_value(void)
192 {
193 1 char input[] = "{\"blah\":}";
194 1 struct json_parser p = json_parse(input, NULL, 0);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
196 1 }
197
198 1 static void test_no_key(void)
199 {
200 1 char input[] = "{:2}";
201 1 struct json_parser p = json_parse(input, NULL, 0);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
203 1 }
204
205 1 static void test_number_key(void)
206 {
207 1 char input[] = "{1:2}";
208 1 struct json_parser p = json_parse(input, NULL, 0);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
210 1 }
211
212 1 static void test_true_key(void)
213 {
214 1 char input[] = "{true:2}";
215 1 struct json_parser p = json_parse(input, NULL, 0);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
217 1 }
218
219 1 static void test_false_key(void)
220 {
221 1 char input[] = "{false:2}";
222 1 struct json_parser p = json_parse(input, NULL, 0);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
224 1 }
225
226 1 static void test_null_key(void)
227 {
228 1 char input[] = "{null:2}";
229 1 struct json_parser p = json_parse(input, NULL, 0);
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
231 1 }
232
233 1 static void test_list_key(void)
234 {
235 1 char input[] = "{[]:2}";
236 1 struct json_parser p = json_parse(input, NULL, 0);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
238 1 }
239
240 1 static void test_object_key(void)
241 {
242 1 char input[] = "{{}:2}";
243 1 struct json_parser p = json_parse(input, NULL, 0);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_UNEXPECTED_TOKEN);
245 1 }
246
247 1 static void test_no_comma(void)
248 {
249 1 char input[] = "{\"a\":2 \"b\":\"blah\"}";
250 1 struct json_parser p = json_parse(input, NULL, 0);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_EXPECTED_TOKEN);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.errorarg == ',');
253 1 }
254
255 1 static void test_get_object(void)
256 {
257 1 char input[] = "{\"a\":2, \"b\":\"blah\"}";
258 struct json_token tokens[5];
259 1 struct json_parser p = json_parse(input, tokens, 5);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 5);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
263 1 size_t value = json_object_get(input, tokens, 0, "a");
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(value == 2);
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[value].type == JSON_NUMBER);
266 1 value = json_object_get(input, tokens, 0, "b");
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(value == 4);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[value].type == JSON_STRING);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[value].start == 12);
270 1 value = json_object_get(input, tokens, 0, "c");
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(value == 0);
272 1 }
273
274 1 int main(void)
275 {
276 1 UNITY_BEGIN();
277
278 1 RUN_TEST(test_empty_object);
279 1 RUN_TEST(test_single_element);
280 1 RUN_TEST(test_multiple_elements);
281 1 RUN_TEST(test_extra_comma);
282 1 RUN_TEST(test_no_end);
283 1 RUN_TEST(test_no_colon);
284 1 RUN_TEST(test_missing_value);
285 1 RUN_TEST(test_no_key);
286 1 RUN_TEST(test_number_key);
287 1 RUN_TEST(test_true_key);
288 1 RUN_TEST(test_false_key);
289 1 RUN_TEST(test_null_key);
290 1 RUN_TEST(test_list_key);
291 1 RUN_TEST(test_object_key);
292 1 RUN_TEST(test_no_comma);
293 1 RUN_TEST(test_get_object);
294
295 1 return UNITY_END();
296 }
297