Compare commits
No commits in common. "905251e5864cb3079f4f5b75f1e9d86fe914fae8" and "05c45bbc8133805eace4070b949f250766c1c3ad" have entirely different histories.
905251e586
...
05c45bbc81
6 changed files with 75 additions and 292 deletions
|
|
@ -26,7 +26,6 @@
|
||||||
pkg-config
|
pkg-config
|
||||||
clang-tools
|
clang-tools
|
||||||
ctags
|
ctags
|
||||||
valgrind
|
|
||||||
];
|
];
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
|
|
||||||
2
json.c
2
json.c
|
|
@ -117,7 +117,7 @@ void json_add_long(Json* json, long value)
|
||||||
void json_add_double(Json* json, double value)
|
void json_add_double(Json* json, double value)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
fprintf(json->stream, "%g", value);
|
fprintf(json->stream, "%f", value);
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,35 +7,28 @@ FetchContent_Declare(
|
||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(Unity)
|
FetchContent_MakeAvailable(Unity)
|
||||||
|
|
||||||
list(
|
|
||||||
APPEND JSON_TESTS
|
|
||||||
json_tests_simple
|
|
||||||
json_tests_array
|
|
||||||
)
|
|
||||||
foreach(JSON_TEST IN LISTS JSON_TESTS)
|
|
||||||
add_executable(
|
add_executable(
|
||||||
${JSON_TEST}
|
json_test
|
||||||
${JSON_TEST}.c
|
json_test.c
|
||||||
)
|
)
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
${JSON_TEST}
|
json_test
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
C_STANDARD 23
|
C_STANDARD 23
|
||||||
C_STANDARD_REQUIRED ON
|
C_STANDARD_REQUIRED ON
|
||||||
C_EXTENSIONS OFF
|
C_EXTENSIONS OFF
|
||||||
)
|
)
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
${JSON_TEST}
|
json_test
|
||||||
PRIVATE
|
PRIVATE
|
||||||
json
|
json
|
||||||
unity::framework
|
unity::framework
|
||||||
)
|
)
|
||||||
target_include_directories(
|
target_include_directories(
|
||||||
${JSON_TEST}
|
json_test
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${CMAKE_SOURCE_DIR}
|
${CMAKE_SOURCE_DIR}
|
||||||
)
|
)
|
||||||
|
|
||||||
add_test(NAME ${JSON_TEST} COMMAND ${JSON_TEST})
|
add_test(NAME json_test COMMAND json_test)
|
||||||
endforeach()
|
|
||||||
|
|
||||||
|
|
|
||||||
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,158 +0,0 @@
|
||||||
#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_array_empty()
|
|
||||||
{
|
|
||||||
json_begin_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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_string()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_string(&json, "string");
|
|
||||||
}
|
|
||||||
json_end_array(&json);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("[\"string\"]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_long()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_long(&json, 42);
|
|
||||||
}
|
|
||||||
json_end_array(&json);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("[42]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_double()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_double(&json, 3.14);
|
|
||||||
}
|
|
||||||
json_end_array(&json);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("[3.14]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_bool()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_bool(&json, true);
|
|
||||||
}
|
|
||||||
json_end_array(&json);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("[true]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_null()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_null(&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("[null]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_array_mixed()
|
|
||||||
{
|
|
||||||
json_begin_array(&json);
|
|
||||||
{
|
|
||||||
json_add_string(&json, "string");
|
|
||||||
json_add_long(&json, 42);
|
|
||||||
json_add_double(&json, 3.14);
|
|
||||||
json_add_bool(&json, true);
|
|
||||||
json_add_null(&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("[\"string\",42,3.14,true,null]", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
{
|
|
||||||
RUN_TEST(test_json_array_empty);
|
|
||||||
RUN_TEST(test_json_array_string);
|
|
||||||
RUN_TEST(test_json_array_long);
|
|
||||||
RUN_TEST(test_json_array_double);
|
|
||||||
RUN_TEST(test_json_array_bool);
|
|
||||||
RUN_TEST(test_json_array_null);
|
|
||||||
RUN_TEST(test_json_array_mixed);
|
|
||||||
}
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
||||||
#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);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("\"string\"", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_add_long()
|
|
||||||
{
|
|
||||||
json_add_long(&json, 42);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("42", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_add_double()
|
|
||||||
{
|
|
||||||
json_add_double(&json, 3.14);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("3.14", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_add_bool()
|
|
||||||
{
|
|
||||||
json_add_bool(&json, true);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("true", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_json_add_null()
|
|
||||||
{
|
|
||||||
json_add_null(&json);
|
|
||||||
fflush(tmp);
|
|
||||||
rewind(tmp);
|
|
||||||
|
|
||||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
||||||
buffer[n] = '\0';
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_STRING("null", buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
{
|
|
||||||
RUN_TEST(test_json_add_string);
|
|
||||||
RUN_TEST(test_json_add_long);
|
|
||||||
RUN_TEST(test_json_add_double);
|
|
||||||
RUN_TEST(test_json_add_bool);
|
|
||||||
RUN_TEST(test_json_add_null);
|
|
||||||
}
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue