Skip to content

Commit

Permalink
Refactored json test for re-use in another project
Browse files Browse the repository at this point in the history
Ticket: ENT-11450
Changelog: none
  • Loading branch information
craigcomstock committed Apr 17, 2024
1 parent 412f6b3 commit 51859a3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 61 deletions.
61 changes: 0 additions & 61 deletions tests/unit/json_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,67 +1908,6 @@ static void test_json_null_not_null(void)
JsonDestroy(json);
}

static bool compare_json_object_merge_deep(const JsonElement *a, const JsonElement *b);

static bool compare_array_json_object_merge_deep(const JsonElement *const a, const JsonElement *const b)
{
const size_t len = JsonLength(a);
if (len != JsonLength(b))
{
return false;
}

for (size_t i = 0; i < len; i++)
{
const JsonElement *const a_child = JsonArrayGet(a, i);
const JsonElement *const b_child = JsonArrayGet(b, i);
if (!compare_json_object_merge_deep(a_child, b_child)) {
return false;
}
}
return true;
}

static bool compare_object_json_object_merge_deep(const JsonElement *const a, const JsonElement *const b)
{
if (JsonLength(a) != JsonLength(b))
{
return false;
}

JsonIterator iter = JsonIteratorInit(a);
while (JsonIteratorHasMore(&iter))
{
const char *const key = JsonIteratorNextKey(&iter);
const JsonElement *const a_child = JsonObjectGet(a, key);
const JsonElement *const b_child = JsonObjectGet(b, key);
if (b_child == NULL || !compare_json_object_merge_deep(a_child, b_child))
{
return false;
}
}
return true;
}

static bool compare_json_object_merge_deep(const JsonElement *const a, const JsonElement *const b)
{
const JsonType type = JsonGetType(a);
if (type != JsonGetType(b))
{
return false;
}

switch (type)
{
case JSON_TYPE_OBJECT:
return compare_object_json_object_merge_deep(a, b);
case JSON_TYPE_ARRAY:
return compare_array_json_object_merge_deep(a, b);
default:
return JsonCompare(a, b) == 0;
}
}

static bool check_json_object_merge_deep(const char *base_raw, const char *extra_raw, const char *expected_raw)
{
JsonElement *base = NULL;
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <math.h>
#include <alloc.h>
#include <json.h>

char *file_read_string(FILE *in)
{
Expand Down Expand Up @@ -63,3 +64,70 @@ void test_progress_end()
putchar('\n');
fflush(stdout);
}

bool compare_json_object_merge_deep(
const JsonElement *a, const JsonElement *b);

bool compare_array_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b)
{
const size_t len = JsonLength(a);
if (len != JsonLength(b))
{
return false;
}

for (size_t i = 0; i < len; i++)
{
const JsonElement *const a_child = JsonArrayGet(a, i);
const JsonElement *const b_child = JsonArrayGet(b, i);
if (!compare_json_object_merge_deep(a_child, b_child))
{
return false;
}
}
return true;
}

bool compare_object_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b)
{
if (JsonLength(a) != JsonLength(b))
{
return false;
}

JsonIterator iter = JsonIteratorInit(a);
while (JsonIteratorHasMore(&iter))
{
const char *const key = JsonIteratorNextKey(&iter);
const JsonElement *const a_child = JsonObjectGet(a, key);
const JsonElement *const b_child = JsonObjectGet(b, key);
if (b_child == NULL
|| !compare_json_object_merge_deep(a_child, b_child))
{
return false;
}
}
return true;
}

bool compare_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b)
{
const JsonType type = JsonGetType(a);
if (type != JsonGetType(b))
{
return false;
}

switch (type)
{
case JSON_TYPE_OBJECT:
return compare_object_json_object_merge_deep(a, b);
case JSON_TYPE_ARRAY:
return compare_array_json_object_merge_deep(a, b);
default:
return JsonCompare(a, b) == 0;
}
}
10 changes: 10 additions & 0 deletions tests/unit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <setjmp.h>
#include <cmockery.h>
#include <stdio.h>
#include <json.h>


/* Use this define for specific overrides inside all our source tree. */
Expand Down Expand Up @@ -55,4 +56,13 @@ void test_progress_end(void);
}\
}\

// shared json test helpers, used in nova/unit/tests for cf-reactor
// group_data_test
bool compare_array_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b);
bool compare_object_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b);
bool compare_json_object_merge_deep(
const JsonElement *const a, const JsonElement *const b);

#endif

0 comments on commit 51859a3

Please sign in to comment.