3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-21 22:09:23 +01:00

unit: add strings to several json iteration tests

This commit is contained in:
James Prestwood 2022-01-06 14:52:55 -08:00 committed by Denis Kenzior
parent 790d431624
commit 380896d08a

View File

@ -315,7 +315,7 @@ static void test_json_arrays(const void *data)
"\"bool_array\":[true, false, true, false]," "\"bool_array\":[true, false, true, false],"
"\"null_array\":[null, null, null, null]," "\"null_array\":[null, null, null, null],"
"\"obj_array\":[{}, {\"key\":\"value\", \"key2\":\"value2\"}]," "\"obj_array\":[{}, {\"key\":\"value\", \"key2\":\"value2\"}],"
"\"mixed_array\":[1, -1, true, false, null]}"; "\"mixed_array\":[1, -1, true, false, null, \"string\"]}";
struct json_iter iter; struct json_iter iter;
struct json_iter i_array, ui_array, b_array, n_array, struct json_iter i_array, ui_array, b_array, n_array,
@ -372,25 +372,34 @@ static void test_json_arrays(const void *data)
count = 0; count = 0;
while (json_iter_next(&m_array)) { while (json_iter_next(&m_array)) {
assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE); _auto_(l_free) char *str = NULL;
switch (count) { switch (count) {
case 0: case 0:
assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
assert(json_iter_get_uint(&m_array, &ui)); assert(json_iter_get_uint(&m_array, &ui));
assert(ui == 1); assert(ui == 1);
break; break;
case 1: case 1:
assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
assert(json_iter_get_int(&m_array, &i)); assert(json_iter_get_int(&m_array, &i));
assert(i == -1); assert(i == -1);
break; break;
case 2: case 2:
case 3: case 3:
assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
assert(json_iter_get_boolean(&m_array, &b)); assert(json_iter_get_boolean(&m_array, &b));
assert(b == count % 2 ? false : true); assert(b == count % 2 ? false : true);
break; break;
case 4: case 4:
assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
assert(json_iter_get_null(&m_array)); assert(json_iter_get_null(&m_array));
break; break;
case 5:
assert(json_iter_get_type(&m_array) == JSON_STRING);
assert(json_iter_get_string(&m_array, &str));
assert(!strcmp(str, "string"));
break;
} }
count++; count++;
@ -425,7 +434,7 @@ static void test_json_arrays(const void *data)
static void test_json_nested_arrays(const void *data) static void test_json_nested_arrays(const void *data)
{ {
char json[] = "{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}]}"; char json[] = "{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}, [\"one\",\"two\"]]}";
int count = 0; int count = 0;
struct json_iter iter; struct json_iter iter;
struct json_iter array; struct json_iter array;
@ -442,8 +451,29 @@ static void test_json_nested_arrays(const void *data)
assert(json_iter_get_container(&array, &inner)); assert(json_iter_get_container(&array, &inner));
while (json_iter_next(&inner)) while (json_iter_next(&inner)) {
_auto_(l_free) char *str = NULL;
switch (count) {
case 0:
case 1:
assert(false);
break;
case 4:
assert(json_iter_get_type(&inner) ==
JSON_STRING);
assert(json_iter_get_string(&inner, &str));
if (count2 == 0)
assert(!strcmp("one", str));
else
assert(!strcmp("two", str));
break;
}
count2++; count2++;
}
/* /*
* TODO: add checks for object iteration. Currently these will * TODO: add checks for object iteration. Currently these will
@ -456,6 +486,9 @@ static void test_json_nested_arrays(const void *data)
case 2: case 2:
assert(count2 == 2); assert(count2 == 2);
break; break;
case 4:
assert(count2 == 2);
break;
default: default:
break; break;
} }
@ -463,7 +496,7 @@ static void test_json_nested_arrays(const void *data)
count++; count++;
} }
assert(count == 4); assert(count == 5);
json_contents_free(c); json_contents_free(c);
} }