Add more tests
This commit is contained in:
parent
932b593a58
commit
905251e586
4 changed files with 290 additions and 74 deletions
103
tests/json_tests_simple.c
Normal file
103
tests/json_tests_simple.c
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#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