diff --git a/.gitignore b/.gitignore index f5adaf3..c1c5439 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ result* # Misc .direnv + +# Build +build diff --git a/CMakeLists.txt b/CMakeLists.txt index bc7edb2..d723887 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,13 +6,34 @@ project( C ) -set(CMAKE_C_STANDARD 23) -set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_C_EXTENSIONS OFF) - -add_executable( +add_library( ${PROJECT_NAME} - main.c json.c ) +target_include_directories( + ${PROJECT_NAME} + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} +) +set_target_properties( + ${PROJECT_NAME} + PROPERTIES + C_STANDARD 23 + C_STANDARD_REQUIRED ON + C_EXTENSIONS OFF +) + +install(TARGETS ${PROJECT_NAME} DESTINATION lib) +install(FILES json.h DESTINATION include) + +option(BUILD_EXAMPLE "Build the example" OFF) +if(BUILD_EXAMPLE) + add_subdirectory(example) +endif() + +option(BUILD_TESTS "Build unit tests" OFF) +if(BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..ce84f81 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,22 @@ +add_executable( + example + main.c +) +set_target_properties( + example + PROPERTIES + C_STANDARD 23 + C_STANDARD_REQUIRED ON + C_EXTENSIONS OFF +) +target_link_libraries( + example + PRIVATE + json +) +target_include_directories( + example + PRIVATE + ${CMAKE_SOURCE_DIR} +) + diff --git a/example/main.c b/example/main.c new file mode 100644 index 0000000..529b86f --- /dev/null +++ b/example/main.c @@ -0,0 +1,26 @@ +#include + +#include + +int main() +{ + auto json = json_init(stdout, INDENT_2); + + json_begin_object(&json); + { + json_add_object_field(&json, "array"); + json_begin_array(&json); + { + json_add_string(&json, "element"); + json_add_long(&json, 5l); + json_add_double(&json, 3.14); + json_add_bool(&json, true); + json_add_null(&json); + } + json_end_array(&json); + } + json_end_object(&json); + + json_free(&json); +} + diff --git a/flake.nix b/flake.nix index 4cc5a94..b2df353 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,7 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; - pname = "hello-world"; #package name + pname = "json"; #package name version = "0.0.1"; src = ./.; buildInputs = with pkgs; [ diff --git a/main.c b/main.c deleted file mode 100644 index 3a8a6f6..0000000 --- a/main.c +++ /dev/null @@ -1,50 +0,0 @@ -#include - -#include "json.h" - -typedef struct Info { - int id; - const char* description; - bool enabled; -} Info; - -#define INFO_LEN 2 -static const Info infos[INFO_LEN] = { - (Info){ - .id = 0, - .description = "test", - .enabled = true, - }, - (Info){ - .id = 1, - .description = "Hello, World!", - .enabled = false, - }, -}; - -int main() -{ - auto json = json_init(stdout, INDENT_2); - - json_begin_array(&json); - for (int i = 0; i < INFO_LEN; i++) { - auto info = infos[i]; - - json_begin_object(&json); - { - json_add_object_field(&json, "id"); - json_add_long(&json, info.id); - - json_add_object_field(&json, "description"); - json_add_string(&json, info.description); - - json_add_object_field(&json, "enabled"); - json_add_bool(&json, info.enabled); - } - json_end_object(&json); - } - json_end_array(&json); - - json_free(&json); -} - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..39b5967 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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) + diff --git a/tests/json_test.c b/tests/json_test.c new file mode 100644 index 0000000..37cc98f --- /dev/null +++ b/tests/json_test.c @@ -0,0 +1,52 @@ +#include +#include + +#include + +#include + +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(); +} +