Flesh out the repository with example and tests
This commit is contained in:
parent
546eaa5d4d
commit
05c45bbc81
8 changed files with 165 additions and 57 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -3,3 +3,6 @@ result*
|
||||||
|
|
||||||
# Misc
|
# Misc
|
||||||
.direnv
|
.direnv
|
||||||
|
|
||||||
|
# Build
|
||||||
|
build
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,34 @@ project(
|
||||||
C
|
C
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 23)
|
add_library(
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
|
||||||
set(CMAKE_C_EXTENSIONS OFF)
|
|
||||||
|
|
||||||
add_executable(
|
|
||||||
${PROJECT_NAME}
|
${PROJECT_NAME}
|
||||||
main.c
|
|
||||||
json.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()
|
||||||
|
|
||||||
|
|
|
||||||
22
example/CMakeLists.txt
Normal file
22
example/CMakeLists.txt
Normal file
|
|
@ -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}
|
||||||
|
)
|
||||||
|
|
||||||
26
example/main.c
Normal file
26
example/main.c
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <json.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
flake-utils.lib.eachDefaultSystem (system:
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
let
|
let
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
pname = "hello-world"; #package name
|
pname = "json"; #package name
|
||||||
version = "0.0.1";
|
version = "0.0.1";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
|
|
|
||||||
50
main.c
50
main.c
|
|
@ -1,50 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
||||||
|
|
||||||
34
tests/CMakeLists.txt
Normal file
34
tests/CMakeLists.txt
Normal 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
52
tests/json_test.c
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue