Skip to content

Commit

Permalink
test(array): add more testing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RiadhAdrani committed Jan 24, 2025
1 parent d8bd65c commit c27bae0
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 0 deletions.
6 changes: 6 additions & 0 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ JsonValue *match_array(char *json, int *index, int size)

if (json[result->end] == ',')
{
if (item->error == 1)
{
// we didn't match any element
break;
}

comma = 1;
}

Expand Down
179 changes: 179 additions & 0 deletions tests/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,188 @@ void test_simple_array()
free(json);
}

void test_nested_empty_arrays_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_nested_empty_arrays.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->error, 0);
CU_ASSERT_EQUAL(res->array->size, 3);

// free_json_value(res);
free(res);
free(json);
}

void test_deeply_nested_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_deeply_nested.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->error, 0);
CU_ASSERT_EQUAL(res->array->size, 1);

// free_json_value(res);
free(res);
free(json);
}

void test_trailing_whitespaces_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_trailing_whitespace.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->error, 0);
CU_ASSERT_EQUAL(res->array->size, 3);

// free_json_value(res);
free(res);
free(json);
}

void test_no_ending_bracket_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_ending_bracket.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, len);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_trailing_comma_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_trailing_comma.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_invalid_element_lvl_0_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_invalid_element_lvl_0.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 1);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_invalid_element_lvl_1_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_invalid_element_lvl_1.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 1);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_invalid_element_lvl_2_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_invalid_element_lvl_2.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 1);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_missing_commas_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_commas.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 3);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_starting_comma_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_starting_comma.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 1);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

void test_no_consecutive_commas_array()
{
int index = 0;
char *json = get_json_file_content("tests/json/array_no_consecutive_commas.json");
int len = strlen(json);

JsonValue *res = match_array(json, &index, len);

CU_ASSERT_EQUAL(res->end, 3);
CU_ASSERT_EQUAL(res->error, 1);

free(res);
free(json);
}

CU_TestInfo array_tests[] = {
{"test_no_empty_json_array", test_no_empty_json_array},
{"test_empty_array", test_empty_array},
{"test_simple_array", test_simple_array},
{"test_nested_empty_arrays_array", test_nested_empty_arrays_array},
{"test_deeply_nested_array", test_deeply_nested_array},
{"test_trailing_whitespaces_array", test_trailing_whitespaces_array},

{"test_no_ending_bracket_array", test_no_ending_bracket_array},
{"test_no_trailing_comma_array", test_no_trailing_comma_array},
{"test_no_invalid_element_lvl_0_array", test_no_invalid_element_lvl_0_array},
{"test_no_invalid_element_lvl_1_array", test_no_invalid_element_lvl_1_array},
{"test_no_invalid_element_lvl_2_array", test_no_invalid_element_lvl_2_array},
{"test_no_missing_commas_array", test_no_missing_commas_array},
{"test_no_starting_comma_array", test_no_starting_comma_array},
{"test_no_consecutive_commas_array", test_no_consecutive_commas_array},
CU_TEST_INFO_NULL,
};
1 change: 1 addition & 0 deletions tests/json/array_deeply_nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[[[]]]]]]
1 change: 1 addition & 0 deletions tests/json/array_nested_empty_arrays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[], [], []]
1 change: 1 addition & 0 deletions tests/json/array_no_commas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1 2 3]
1 change: 1 addition & 0 deletions tests/json/array_no_consecutive_commas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,,,2]
1 change: 1 addition & 0 deletions tests/json/array_no_ending_bracket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, "two", false, null, true, [], {}
1 change: 1 addition & 0 deletions tests/json/array_no_invalid_element_lvl_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[bro]
1 change: 1 addition & 0 deletions tests/json/array_no_invalid_element_lvl_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[bro]]
1 change: 1 addition & 0 deletions tests/json/array_no_invalid_element_lvl_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[bro]]]
1 change: 1 addition & 0 deletions tests/json/array_no_starting_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[,1]
1 change: 1 addition & 0 deletions tests/json/array_no_trailing_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, "two", false, null, true, [], {},]
1 change: 1 addition & 0 deletions tests/json/array_trailing_whitespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ 1 , 2 , 3 ]

0 comments on commit c27bae0

Please sign in to comment.