147 lines
2.9 KiB
C
147 lines
2.9 KiB
C
#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, true);
|
|
}
|
|
|
|
void tearDown()
|
|
{
|
|
json_free(&json);
|
|
|
|
if (tmp) {
|
|
fclose(tmp);
|
|
tmp = nullptr;
|
|
}
|
|
}
|
|
|
|
void test_json_comma_separated_string()
|
|
{
|
|
json_add_string(&json, "one");
|
|
json_add_string(&json, "two");
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("\"one\",\"two\"", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_long()
|
|
{
|
|
json_add_long(&json, 42);
|
|
json_add_long(&json, 67);
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("42,67", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_double()
|
|
{
|
|
json_add_double(&json, 3.14);
|
|
json_add_double(&json, 6.28);
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("3.14,6.28", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_bool()
|
|
{
|
|
json_add_bool(&json, true);
|
|
json_add_bool(&json, false);
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("true,false", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_null()
|
|
{
|
|
json_add_null(&json);
|
|
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,null", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_object()
|
|
{
|
|
for (int i = 0; i < 2; ++i) {
|
|
json_begin_object(&json);
|
|
{
|
|
json_add_object_field(&json, "test");
|
|
json_add_number(&json, i);
|
|
}
|
|
json_end_object(&json);
|
|
}
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("{\"test\":0},{\"test\":1}", buffer);
|
|
}
|
|
|
|
void test_json_comma_separated_array()
|
|
{
|
|
for (int i = 0; i < 2; ++i) {
|
|
json_begin_array(&json);
|
|
{
|
|
json_add_number(&json, i);
|
|
}
|
|
json_end_array(&json);
|
|
}
|
|
fflush(tmp);
|
|
rewind(tmp);
|
|
|
|
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
|
buffer[n] = '\0';
|
|
|
|
TEST_ASSERT_EQUAL_STRING("[0],[1]", buffer);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
UNITY_BEGIN();
|
|
{
|
|
RUN_TEST(test_json_comma_separated_string);
|
|
RUN_TEST(test_json_comma_separated_long);
|
|
RUN_TEST(test_json_comma_separated_double);
|
|
RUN_TEST(test_json_comma_separated_bool);
|
|
RUN_TEST(test_json_comma_separated_null);
|
|
RUN_TEST(test_json_comma_separated_object);
|
|
RUN_TEST(test_json_comma_separated_array);
|
|
}
|
|
return UNITY_END();
|
|
}
|
|
|