Skip to content

Commit d18fd46

Browse files
maass-hamburgnashif
authored andcommitted
tests: json: add a test for JSON_TOK_ENCODED_OBJ
add a test for JSON_TOK_ENCODED_OBJ Signed-off-by: Miika Karanki <[email protected]> Signed-off-by: Fin Maaß <[email protected]>
1 parent 01c40dd commit d18fd46

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/lib/json/src/main.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ static const struct json_obj_descr array_2dim_extra_named_descr[] = {
143143
ARRAY_SIZE(obj_array_descr)),
144144
};
145145

146+
struct test_json_tok_encoded_obj {
147+
const char *encoded_obj;
148+
int ok;
149+
};
150+
151+
static const struct json_obj_descr test_json_tok_encoded_obj_descr[] = {
152+
JSON_OBJ_DESCR_PRIM(struct test_json_tok_encoded_obj, encoded_obj, JSON_TOK_ENCODED_OBJ),
153+
JSON_OBJ_DESCR_PRIM(struct test_json_tok_encoded_obj, ok, JSON_TOK_NUMBER),
154+
};
155+
146156
ZTEST(lib_json_test, test_json_encoding)
147157
{
148158
struct test_struct ts = {
@@ -1209,4 +1219,23 @@ ZTEST(lib_json_test, test_large_descriptor)
12091219
zassert_true(ret & ((int64_t)1 << 39), "Field int39 not decoded");
12101220
}
12111221

1222+
ZTEST(lib_json_test, test_json_encoded_object_tok_encoding)
1223+
{
1224+
static const char encoded[] =
1225+
"{\"encoded_obj\":{\"test\":{\"nested\":\"yes\"}},\"ok\":1234}";
1226+
const struct test_json_tok_encoded_obj obj = {
1227+
.encoded_obj = "{\"test\":{\"nested\":\"yes\"}}",
1228+
.ok = 1234,
1229+
};
1230+
char buffer[sizeof(encoded)];
1231+
int ret;
1232+
1233+
ret = json_obj_encode_buf(test_json_tok_encoded_obj_descr,
1234+
ARRAY_SIZE(test_json_tok_encoded_obj_descr), &obj, buffer,
1235+
sizeof(buffer));
1236+
1237+
zassert_equal(ret, 0, "Encoding function failed");
1238+
zassert_mem_equal(buffer, encoded, sizeof(encoded), "Encoded contents not consistent");
1239+
}
1240+
12121241
ZTEST_SUITE(lib_json_test, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)