Skip to content

Commit b311c53

Browse files
mrfuchscarlescufi
authored andcommitted
tests: lib: json: Add test for array of objects
Add tests for encoding and decoding nested arrays of objects located inside a parent object at a non-zero offset. Signed-off-by: Markus Fuchs <[email protected]>
1 parent 7a7a78b commit b311c53

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

tests/lib/json/src/main.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct test_struct {
2727
int another_array[10]; /* JSON: "another-array" */
2828
size_t another_array_len;
2929
struct test_nested xnother_nexx; /* JSON: "4nother_ne$+" */
30+
struct test_nested nested_obj_array[2];
31+
size_t obj_array_len;
3032
};
3133

3234
struct elt {
@@ -69,6 +71,8 @@ static const struct json_obj_descr test_descr[] = {
6971
JSON_TOK_NUMBER),
7072
JSON_OBJ_DESCR_OBJECT_NAMED(struct test_struct, "4nother_ne$+",
7173
xnother_nexx, nested_descr),
74+
JSON_OBJ_DESCR_OBJ_ARRAY(struct test_struct, nested_obj_array, 2,
75+
obj_array_len, nested_descr, ARRAY_SIZE(nested_descr)),
7276
};
7377

7478
static const struct json_obj_descr elt_descr[] = {
@@ -146,6 +150,11 @@ ZTEST(lib_json_test, test_json_encoding)
146150
.nested_bool = true,
147151
.nested_string = "no escape necessary",
148152
},
153+
.nested_obj_array = {
154+
{1, true, "true"},
155+
{0, false, "false"}
156+
},
157+
.obj_array_len = 2
149158
};
150159
char encoded[] = "{\"some_string\":\"zephyr 123\uABCD\","
151160
"\"some_int\":42,\"some_bool\":true,"
@@ -158,7 +167,10 @@ ZTEST(lib_json_test, test_json_encoding)
158167
"\"another-array\":[2,3,5,7],"
159168
"\"4nother_ne$+\":{\"nested_int\":1234,"
160169
"\"nested_bool\":true,"
161-
"\"nested_string\":\"no escape necessary\"}"
170+
"\"nested_string\":\"no escape necessary\"},"
171+
"\"nested_obj_array\":["
172+
"{\"nested_int\":1,\"nested_bool\":true,\"nested_string\":\"true\"},"
173+
"{\"nested_int\":0,\"nested_bool\":false,\"nested_string\":\"false\"}]"
162174
"}";
163175
char buffer[sizeof(encoded)];
164176
int ret;
@@ -193,7 +205,10 @@ ZTEST(lib_json_test, test_json_decoding)
193205
"\"another-array\":[2,3,5,7],"
194206
"\"4nother_ne$+\":{\"nested_int\":1234,"
195207
"\"nested_bool\":true,"
196-
"\"nested_string\":\"no escape necessary\"}"
208+
"\"nested_string\":\"no escape necessary\"},"
209+
"\"nested_obj_array\":["
210+
"{\"nested_int\":1,\"nested_bool\":true,\"nested_string\":\"true\"},"
211+
"{\"nested_int\":0,\"nested_bool\":false,\"nested_string\":\"false\"}]"
197212
"}\n";
198213
const int expected_array[] = { 11, 22, 33, 45, 299 };
199214
const int expected_other_array[] = { 2, 3, 5, 7 };
@@ -237,6 +252,20 @@ ZTEST(lib_json_test, test_json_decoding)
237252
zassert_true(!strcmp(ts.xnother_nexx.nested_string,
238253
"no escape necessary"),
239254
"Named nested string not decoded correctly");
255+
zassert_equal(ts.obj_array_len, 2,
256+
"Array of objects does not have correct number of items");
257+
zassert_equal(ts.nested_obj_array[0].nested_int, 1,
258+
"Integer in first object array element not decoded correctly");
259+
zassert_equal(ts.nested_obj_array[0].nested_bool, true,
260+
"Boolean value in first object array element not decoded correctly");
261+
zassert_true(!strcmp(ts.nested_obj_array[0].nested_string, "true"),
262+
"String in first object array element not decoded correctly");
263+
zassert_equal(ts.nested_obj_array[1].nested_int, 0,
264+
"Integer in second object array element not decoded correctly");
265+
zassert_equal(ts.nested_obj_array[1].nested_bool, false,
266+
"Boolean value in second object array element not decoded correctly");
267+
zassert_true(!strcmp(ts.nested_obj_array[1].nested_string, "false"),
268+
"String in second object array element not decoded correctly");
240269
}
241270

242271
ZTEST(lib_json_test, test_json_limits)

0 commit comments

Comments
 (0)