Add more tests
This commit is contained in:
parent
932b593a58
commit
905251e586
4 changed files with 290 additions and 74 deletions
158
tests/json_tests_array.c
Normal file
158
tests/json_tests_array.c
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
#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();
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue