Add even more tests
This commit is contained in:
parent
905251e586
commit
af8fde1222
4 changed files with 338 additions and 0 deletions
195
tests/json_tests_object.c
Normal file
195
tests/json_tests_object.c
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
#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_object_empty()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
}
|
||||
json_end_object(&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_object_string_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_add_string(&json, "string");
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":\"string\"}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_long_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_add_long(&json, 42);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":42}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_double_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_add_double(&json, 3.14);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":3.14}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_bool_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_add_bool(&json, true);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":true}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_null_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_add_null(&json);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":null}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_multiple_fields()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "string field");
|
||||
json_add_string(&json, "string");
|
||||
|
||||
json_add_object_field(&json, "long field");
|
||||
json_add_long(&json, 42);
|
||||
|
||||
json_add_object_field(&json, "double field");
|
||||
json_add_double(&json, 3.14);
|
||||
|
||||
json_add_object_field(&json, "bool field");
|
||||
json_add_bool(&json, true);
|
||||
|
||||
json_add_object_field(&json, "null field");
|
||||
json_add_null(&json);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"string field\":\"string\",\"long field\":42,\"double field\":3.14,\"bool field\":true,\"null field\":null}", buffer);
|
||||
}
|
||||
|
||||
void test_json_object_nested_field()
|
||||
{
|
||||
json_begin_object(&json);
|
||||
{
|
||||
json_add_object_field(&json, "field");
|
||||
json_begin_object(&json);
|
||||
{
|
||||
}
|
||||
json_end_object(&json);
|
||||
}
|
||||
json_end_object(&json);
|
||||
fflush(tmp);
|
||||
rewind(tmp);
|
||||
|
||||
auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp);
|
||||
buffer[n] = '\0';
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING("{\"field\":{}}", buffer);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
{
|
||||
RUN_TEST(test_json_object_empty);
|
||||
RUN_TEST(test_json_object_string_field);
|
||||
RUN_TEST(test_json_object_long_field);
|
||||
RUN_TEST(test_json_object_double_field);
|
||||
RUN_TEST(test_json_object_bool_field);
|
||||
RUN_TEST(test_json_object_null_field);
|
||||
RUN_TEST(test_json_object_multiple_fields);
|
||||
RUN_TEST(test_json_object_nested_field);
|
||||
}
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue