Flesh out the repository with example and tests

This commit is contained in:
ktkk 2025-10-09 15:07:32 +00:00
parent 546eaa5d4d
commit 05c45bbc81
8 changed files with 165 additions and 57 deletions

34
tests/CMakeLists.txt Normal file
View file

@ -0,0 +1,34 @@
include(FetchContent)
FetchContent_Declare(
Unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
GIT_TAG v2.6.1
)
FetchContent_MakeAvailable(Unity)
add_executable(
json_test
json_test.c
)
set_target_properties(
json_test
PROPERTIES
C_STANDARD 23
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
)
target_link_libraries(
json_test
PRIVATE
json
unity::framework
)
target_include_directories(
json_test
PRIVATE
${CMAKE_SOURCE_DIR}
)
add_test(NAME json_test COMMAND json_test)

52
tests/json_test.c Normal file
View file

@ -0,0 +1,52 @@
#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_add_string()
{
json_add_string(&json, "string");
fflush(tmp);
rewind(tmp);
char buffer[256];
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
buffer[n] = '\0';
TEST_ASSERT_EQUAL_STRING("\"string\"", buffer);
}
int main()
{
UNITY_BEGIN();
{
RUN_TEST(test_json_add_string);
}
return UNITY_END();
}