Skip to content

Commit

Permalink
test(common): test create/free-json-value
Browse files Browse the repository at this point in the history
  • Loading branch information
RiadhAdrani committed Jan 31, 2025
1 parent 77173dd commit 1c1cf8a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion common.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void free_json_error(JsonError *error)
error = NULL;
}

/**
* Create an initially errored json value struct
*/
JsonValue *create_json_value()
{
JsonValue *value = malloc(sizeof(JsonValue));
Expand All @@ -79,6 +82,10 @@ JsonValue *create_json_value()
return value;
}

/**
* Free a json value struct
* @param value struct
*/
void free_json_value(JsonValue *value)
{
if (value == NULL)
Expand All @@ -96,7 +103,7 @@ void free_json_value(JsonValue *value)
value->object = NULL;
}

if (value->error)
if (value->error != NULL)
{
free_json_error(value->error);
value->error = NULL;
Expand Down
33 changes: 33 additions & 0 deletions tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ void test_free_json_error()
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

void test_create_json_value()
{
JsonValue *val = create_json_value();

CU_ASSERT_EQUAL(val->end, 0);
CU_ASSERT_EQUAL(val->start, 0);
CU_ASSERT_EQUAL(val->type, JSON_NOT_AVAILABLE);
CU_ASSERT_EQUAL(val->number, 0);
CU_ASSERT_EQUAL(val->string, NULL);
CU_ASSERT_EQUAL(val->array, NULL);
CU_ASSERT_EQUAL(val->object, NULL);

CU_ASSERT_STRING_EQUAL(val->error->code, "unknown-json-value");

free_json_value(val);
}

void test_free_json_value()
{
JsonValue *val = create_json_value();
free_json_value(val);

CU_ASSERT_EQUAL(val->string, NULL);
CU_ASSERT_EQUAL(val->array, NULL);
CU_ASSERT_EQUAL(val->object, NULL);
}

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

CU_TestInfo common_tests[] = {
{"skip-empty__stop_on_non_empty", test_skip_empty__stop_on_non_empty},
{"skip-empty__skip_tabs", test_skip_empty__tab},
Expand All @@ -96,5 +126,8 @@ CU_TestInfo common_tests[] = {

{"create_json_error", test_create_json_error},
{"free_json_error", test_free_json_error},

{"create_json_value", test_create_json_value},
{"free_json_value", test_free_json_value},
CU_TEST_INFO_NULL,
};

0 comments on commit 1c1cf8a

Please sign in to comment.