Add even more tests

This commit is contained in:
ktkk 2025-10-13 12:18:44 +00:00
parent 905251e586
commit af8fde1222
4 changed files with 338 additions and 0 deletions

View file

@ -11,6 +11,8 @@ list(
APPEND JSON_TESTS
json_tests_simple
json_tests_array
json_tests_object
json_tests_complex
)
foreach(JSON_TEST IN LISTS JSON_TESTS)
add_executable(

View file

@ -31,6 +31,8 @@ void tearDown()
void test_json_array_empty()
{
json_begin_array(&json);
{
}
json_end_array(&json);
fflush(tmp);
rewind(tmp);
@ -141,6 +143,25 @@ void test_json_array_mixed()
TEST_ASSERT_EQUAL_STRING("[\"string\",42,3.14,true,null]", buffer);
}
void test_json_array_nested()
{
json_begin_array(&json);
{
json_begin_array(&json);
{
}
json_end_array(&json);
}
json_end_array(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("[[]]", buffer);
}
int main()
{
UNITY_BEGIN();
@ -152,6 +173,7 @@ int main()
RUN_TEST(test_json_array_bool);
RUN_TEST(test_json_array_null);
RUN_TEST(test_json_array_mixed);
RUN_TEST(test_json_array_nested);
}
return UNITY_END();
}

119
tests/json_tests_complex.c Normal file
View file

@ -0,0 +1,119 @@
#include <stdio.h>
#include <string.h>
#include <unity.h>
#include <json.h>
static FILE* tmp = nullptr;
static char buffer[1024];
static Json json;
void setUp()
{
tmp = tmpfile();
TEST_ASSERT_NOT_NULL(tmp);
json = json_init(tmp, INDENT_TAB);
}
void tearDown()
{
json_free(&json);
if (tmp) {
fclose(tmp);
tmp = nullptr;
}
}
struct Person
{
const char* name;
int age;
};
struct Status
{
const char* description;
bool is_success;
};
struct PersonsResult
{
struct Status status;
int length;
struct Person* persons;
};
void test_json_complex()
{
struct Person persons[4] = {
{ .name = "Alice", .age = 35 },
{ .name = "Bob", .age = 67 },
{ .name = "Patricia", .age = 54 },
{ .name = "Jake", .age = 16 },
};
struct PersonsResult persons_result = {
.status = { .description = "ok", .is_success = true },
.length = sizeof(persons) / sizeof(struct Person),
.persons = persons,
};
json_begin_object(&json);
{
json_add_object_field(&json, "status");
json_begin_object(&json);
{
json_add_object_field(&json, "description");
json_add_string(&json, persons_result.status.description);
json_add_object_field(&json, "is_success");
json_add_bool(&json, persons_result.status.is_success);
}
json_end_object(&json);
json_add_object_field(&json, "length");
json_add_long(&json, persons_result.length);
json_add_object_field(&json, "persons");
json_begin_array(&json);
{
for (auto i = 0; i < persons_result.length; ++i)
{
auto person = persons_result.persons[i];
json_begin_object(&json);
{
json_add_object_field(&json, "name");
json_add_string(&json, person.name);
json_add_object_field(&json, "age");
json_add_long(&json, person.age);
}
json_end_object(&json);
}
}
json_end_array(&json);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\n\t\"status\": {\n\t\t\"description\": \"ok\",\n\t\t\"is_success\": true\n\t},\n\t\"length\": 4,\n\t\"persons\": [\n\t\t{\n\t\t\t\"name\": \"Alice\",\n\t\t\t\"age\": 35\n\t\t},\n\t\t{\n\t\t\t\"name\": \"Bob\",\n\t\t\t\"age\": 67\n\t\t},\n\t\t{\n\t\t\t\"name\": \"Patricia\",\n\t\t\t\"age\": 54\n\t\t},\n\t\t{\n\t\t\t\"name\": \"Jake\",\n\t\t\t\"age\": 16\n\t\t}\n\t]\n}", buffer);
}
int main()
{
UNITY_BEGIN();
{
RUN_TEST(test_json_complex);
}
return UNITY_END();
}

195
tests/json_tests_object.c Normal file
View file

@ -0,0 +1,195 @@
#include <stdio.h>
#include <string.h>
#include <unity.h>
#include <json.h>
static FILE* tmp = nullptr;
static char buffer[256];
static Json json;
void setUp()
{
tmp = tmpfile();
TEST_ASSERT_NOT_NULL(tmp);
json = json_init(tmp, MINIFIED);
}
void tearDown()
{
json_free(&json);
if (tmp) {
fclose(tmp);
tmp = nullptr;
}
}
void test_json_object_empty()
{
json_begin_object(&json);
{
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{}", buffer);
}
void test_json_object_string_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_add_string(&json, "string");
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":\"string\"}", buffer);
}
void test_json_object_long_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_add_long(&json, 42);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":42}", buffer);
}
void test_json_object_double_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_add_double(&json, 3.14);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":3.14}", buffer);
}
void test_json_object_bool_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_add_bool(&json, true);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":true}", buffer);
}
void test_json_object_null_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_add_null(&json);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":null}", buffer);
}
void test_json_object_multiple_fields()
{
json_begin_object(&json);
{
json_add_object_field(&json, "string field");
json_add_string(&json, "string");
json_add_object_field(&json, "long field");
json_add_long(&json, 42);
json_add_object_field(&json, "double field");
json_add_double(&json, 3.14);
json_add_object_field(&json, "bool field");
json_add_bool(&json, true);
json_add_object_field(&json, "null field");
json_add_null(&json);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"string field\":\"string\",\"long field\":42,\"double field\":3.14,\"bool field\":true,\"null field\":null}", buffer);
}
void test_json_object_nested_field()
{
json_begin_object(&json);
{
json_add_object_field(&json, "field");
json_begin_object(&json);
{
}
json_end_object(&json);
}
json_end_object(&json);
fflush(tmp);
rewind(tmp);
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("{\"field\":{}}", buffer);
}
int main()
{
UNITY_BEGIN();
{
RUN_TEST(test_json_object_empty);
RUN_TEST(test_json_object_string_field);
RUN_TEST(test_json_object_long_field);
RUN_TEST(test_json_object_double_field);
RUN_TEST(test_json_object_bool_field);
RUN_TEST(test_json_object_null_field);
RUN_TEST(test_json_object_multiple_fields);
RUN_TEST(test_json_object_nested_field);
}
return UNITY_END();
}