tests: lib: move json to new ztest API

Move test json to use new ztest API.

Signed-off-by: li biao <biao1x.li@intel.com>
This commit is contained in:
li biao 2022-07-05 12:30:00 +08:00 committed by Carles Cufí
parent f5932657dd
commit ddaee674e3
2 changed files with 24 additions and 51 deletions

View file

@ -1,3 +1,4 @@
CONFIG_JSON_LIBRARY=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ZTEST=y
CONFIG_ZTEST_STACK_SIZE=2048

View file

@ -106,7 +106,7 @@ static const struct json_obj_descr array_array_descr[] = {
ARRAY_SIZE(array_descr)),
};
static void test_json_encoding(void)
ZTEST(lib_json_test, test_json_encoding)
{
struct test_struct ts = {
.some_string = "zephyr 123\uABCD",
@ -164,7 +164,7 @@ static void test_json_encoding(void)
zassert_equal(ret, 0, "Encoded contents not consistent");
}
static void test_json_decoding(void)
ZTEST(lib_json_test, test_json_decoding)
{
struct test_struct ts;
char encoded[] = "{\"some_string\":\"zephyr 123\\uABCD456\","
@ -228,7 +228,7 @@ static void test_json_decoding(void)
"Named nested string not decoded correctly");
}
static void test_json_limits(void)
ZTEST(lib_json_test, test_json_limits)
{
int ret = 0;
char encoded[] = "{\"int_max\":2147483647,"
@ -255,7 +255,7 @@ static void test_json_limits(void)
"Integer limits not decoded correctly");
}
static void test_json_decoding_array_array(void)
ZTEST(lib_json_test, test_json_decoding_array_array)
{
int ret;
struct obj_array_array obj_array_array_ts;
@ -290,7 +290,7 @@ static void test_json_decoding_array_array(void)
"Usain Bolt height not decoded correctly");
}
static void test_json_obj_arr_encoding(void)
ZTEST(lib_json_test, test_json_obj_arr_encoding)
{
struct obj_array oa = {
.elements = {
@ -329,7 +329,7 @@ static void test_json_obj_arr_encoding(void)
"Encoded array of objects is not consistent");
}
static void test_json_arr_obj_decoding(void)
ZTEST(lib_json_test, test_json_arr_obj_decoding)
{
int ret;
struct obj_array obj_array_array_ts;
@ -362,7 +362,7 @@ static void test_json_arr_obj_decoding(void)
"Usain Bolt height not decoded correctly");
}
static void test_json_arr_obj_encoding(void)
ZTEST(lib_json_test, test_json_arr_obj_encoding)
{
struct obj_array oa = {
.elements = {
@ -400,7 +400,7 @@ static void test_json_arr_obj_encoding(void)
"Encoded array of objects is not consistent");
}
static void test_json_obj_arr_decoding(void)
ZTEST(lib_json_test, test_json_obj_arr_decoding)
{
struct obj_array oa;
char encoded[] = "{\"elements\":["
@ -469,7 +469,7 @@ static void parse_harness(struct encoding_test encoded[], size_t size)
}
}
static void test_json_invalid_string(void)
ZTEST(lib_json_test, test_json_invalid_string)
{
struct encoding_test encoded[] = {
{ "{\"some_string\":\"\\u@@@@\"}", -EINVAL },
@ -482,7 +482,7 @@ static void test_json_invalid_string(void)
parse_harness(encoded, ARRAY_SIZE(encoded));
}
static void test_json_invalid_bool(void)
ZTEST(lib_json_test, test_json_invalid_bool)
{
struct encoding_test encoded[] = {
{ "{\"some_bool\":truffle }", -EINVAL},
@ -492,7 +492,7 @@ static void test_json_invalid_bool(void)
parse_harness(encoded, ARRAY_SIZE(encoded));
}
static void test_json_invalid_null(void)
ZTEST(lib_json_test, test_json_invalid_null)
{
struct encoding_test encoded[] = {
/* Parser will recognize 'null', but refuse to decode it */
@ -504,7 +504,7 @@ static void test_json_invalid_null(void)
parse_harness(encoded, ARRAY_SIZE(encoded));
}
static void test_json_invalid_number(void)
ZTEST(lib_json_test, test_json_invalid_number)
{
struct encoding_test encoded[] = {
{ "{\"some_int\":xxx }", -EINVAL},
@ -513,7 +513,7 @@ static void test_json_invalid_number(void)
parse_harness(encoded, ARRAY_SIZE(encoded));
}
static void test_json_missing_quote(void)
ZTEST(lib_json_test, test_json_missing_quote)
{
struct test_struct ts;
char encoded[] = "{\"some_string";
@ -524,7 +524,7 @@ static void test_json_missing_quote(void)
zassert_equal(ret, -EINVAL, "Decoding has to fail");
}
static void test_json_wrong_token(void)
ZTEST(lib_json_test, test_json_wrong_token)
{
struct test_struct ts;
char encoded[] = "{\"some_string\",}";
@ -535,7 +535,7 @@ static void test_json_wrong_token(void)
zassert_equal(ret, -EINVAL, "Decoding has to fail");
}
static void test_json_item_wrong_type(void)
ZTEST(lib_json_test, test_json_item_wrong_type)
{
struct test_struct ts;
char encoded[] = "{\"some_string\":false}";
@ -546,7 +546,7 @@ static void test_json_item_wrong_type(void)
zassert_equal(ret, -EINVAL, "Decoding has to fail");
}
static void test_json_key_not_in_descr(void)
ZTEST(lib_json_test, test_json_key_not_in_descr)
{
struct test_struct ts;
char encoded[] = "{\"key_not_in_descr\":123456}";
@ -557,7 +557,7 @@ static void test_json_key_not_in_descr(void)
zassert_equal(ret, 0, "No items should be decoded");
}
static void test_json_escape(void)
ZTEST(lib_json_test, test_json_escape)
{
char buf[42];
char string[] = "\"abc"
@ -589,7 +589,7 @@ static void test_json_escape(void)
}
/* Edge case: only one character, which must be escaped. */
static void test_json_escape_one(void)
ZTEST(lib_json_test, test_json_escape_one)
{
char buf[3] = {'\t', '\0', '\0'};
const char *expected = "\\t";
@ -605,7 +605,7 @@ static void test_json_escape_one(void)
"Escaped value is not correct");
}
static void test_json_escape_empty(void)
ZTEST(lib_json_test, test_json_escape_empty)
{
char empty[] = "";
size_t len = sizeof(empty) - 1;
@ -617,7 +617,7 @@ static void test_json_escape_empty(void)
zassert_equal(empty[0], '\0', "Empty string does not remain empty");
}
static void test_json_escape_no_op(void)
ZTEST(lib_json_test, test_json_escape_no_op)
{
char nothing_to_escape[] = "hello,world:!";
const char *expected = "hello,world:!";
@ -632,7 +632,7 @@ static void test_json_escape_no_op(void)
"Altered string with nothing to escape");
}
static void test_json_escape_bounds_check(void)
ZTEST(lib_json_test, test_json_escape_bounds_check)
{
char not_enough_memory[] = "\tfoo";
size_t len = sizeof(not_enough_memory) - 1;
@ -642,7 +642,7 @@ static void test_json_escape_bounds_check(void)
zassert_equal(ret, -ENOMEM, "Bounds check failed");
}
static void test_json_encode_bounds_check(void)
ZTEST(lib_json_test, test_json_encode_bounds_check)
{
struct number {
uint32_t val;
@ -662,32 +662,4 @@ static void test_json_encode_bounds_check(void)
zassert_equal(ret, -ENOMEM, "Bounds check failed");
}
void test_main(void)
{
ztest_test_suite(lib_json_test,
ztest_unit_test(test_json_encoding),
ztest_unit_test(test_json_decoding),
ztest_unit_test(test_json_decoding_array_array),
ztest_unit_test(test_json_obj_arr_encoding),
ztest_unit_test(test_json_obj_arr_decoding),
ztest_unit_test(test_json_invalid_string),
ztest_unit_test(test_json_invalid_bool),
ztest_unit_test(test_json_invalid_null),
ztest_unit_test(test_json_invalid_number),
ztest_unit_test(test_json_missing_quote),
ztest_unit_test(test_json_wrong_token),
ztest_unit_test(test_json_item_wrong_type),
ztest_unit_test(test_json_key_not_in_descr),
ztest_unit_test(test_json_escape),
ztest_unit_test(test_json_escape_one),
ztest_unit_test(test_json_escape_empty),
ztest_unit_test(test_json_escape_no_op),
ztest_unit_test(test_json_escape_bounds_check),
ztest_unit_test(test_json_encode_bounds_check),
ztest_unit_test(test_json_limits),
ztest_unit_test(test_json_arr_obj_encoding),
ztest_unit_test(test_json_arr_obj_decoding)
);
ztest_run_test_suite(lib_json_test);
}
ZTEST_SUITE(lib_json_test, NULL, NULL, NULL, NULL, NULL);