提交 949c0833 编写于 作者: M Max Bruckner

Move 'hooks' parameter into buffers (parse/print)

上级 c4c217f2
此差异已折叠。
......@@ -44,21 +44,23 @@ static void assert_is_array(cJSON *array_item)
static void assert_not_array(const char *json)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof("");
buffer.hooks = global_hooks;
TEST_ASSERT_FALSE(parse_array(item, &buffer, &global_hooks));
TEST_ASSERT_FALSE(parse_array(item, &buffer));
assert_is_invalid(item);
}
static void assert_parse_array(const char *json)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof("");
buffer.hooks = global_hooks;
TEST_ASSERT_TRUE(parse_array(item, &buffer, &global_hooks));
TEST_ASSERT_TRUE(parse_array(item, &buffer));
assert_is_array(item);
}
......
......@@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item)
static void assert_parse_number(const char *string, int integer, double real)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
......
......@@ -52,22 +52,24 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
static void assert_not_object(const char *json)
{
parse_buffer parsebuffer = { 0, 0, 0, 0 };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.hooks = global_hooks;
TEST_ASSERT_FALSE(parse_object(item, &parsebuffer, &global_hooks));
TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
assert_is_invalid(item);
reset(item);
}
static void assert_parse_object(const char *json)
{
parse_buffer parsebuffer = { 0, 0, 0, 0 };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.hooks = global_hooks;
TEST_ASSERT_TRUE(parse_object(item, &parsebuffer, &global_hooks));
TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
assert_is_object(item);
}
......
......@@ -45,11 +45,12 @@ static void assert_is_string(cJSON *string_item)
static void assert_parse_string(const char *string, const char *expected)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.hooks = global_hooks;
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Couldn't parse string.");
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
assert_is_string(item);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
global_hooks.deallocate(item->valuestring);
......@@ -58,11 +59,12 @@ static void assert_parse_string(const char *string, const char *expected)
static void assert_not_parse_string(const char * const string)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.hooks = global_hooks;
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Malformed string should not be accepted.");
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
assert_is_invalid(item);
}
......
......@@ -43,10 +43,12 @@ static void assert_is_value(cJSON *value_item, int type)
static void assert_parse_value(const char *string, int type)
{
parse_buffer buffer = { 0, 0, 0, 0 };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.content = (const unsigned char*) string;
buffer.length = strlen(string) + sizeof("");
TEST_ASSERT_TRUE(parse_value(item, &buffer, &global_hooks));
buffer.hooks = global_hooks;
TEST_ASSERT_TRUE(parse_value(item, &buffer));
assert_is_value(item, type);
}
......
......@@ -31,34 +31,37 @@ static void assert_print_array(const char * const expected, const char * const i
cJSON item[1];
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0 };
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0 };
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0 };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
formatted_buffer.hooks = global_hooks;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
unformatted_buffer.hooks = global_hooks;
memset(item, 0, sizeof(item));
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer, &global_hooks), "Failed to parse array.");
TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array.");
unformatted_buffer.format = false;
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer, &global_hooks), "Failed to print unformatted string.");
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct.");
formatted_buffer.format = true;
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer, &global_hooks), "Failed to print formatted string.");
TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct.");
reset(item);
......
......@@ -28,16 +28,17 @@ static void assert_print_number(const char *expected, double input)
{
unsigned char printed[1024];
cJSON item[1];
printbuffer buffer = { 0, 0, 0, 0, 0, 0 };
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
memset(item, 0, sizeof(item));
cJSON_SetNumberValue(item, input);
TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number.");
TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer), "Failed to print number.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected.");
}
......
......@@ -31,35 +31,38 @@ static void assert_print_object(const char * const expected, const char * const
cJSON item[1];
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0 };
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0 };
parse_buffer parsebuffer = { 0, 0, 0, 0 };
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
/* buffer for parsing */
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
/* buffer for formatted printing */
formatted_buffer.buffer = printed_formatted;
formatted_buffer.length = sizeof(printed_formatted);
formatted_buffer.offset = 0;
formatted_buffer.noalloc = true;
formatted_buffer.hooks = global_hooks;
/* buffer for unformatted printing */
unformatted_buffer.buffer = printed_unformatted;
unformatted_buffer.length = sizeof(printed_unformatted);
unformatted_buffer.offset = 0;
unformatted_buffer.noalloc = true;
unformatted_buffer.hooks = global_hooks;
memset(item, 0, sizeof(item));
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer, &global_hooks), "Failed to parse object.");
TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object.");
unformatted_buffer.format = false;
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer, &global_hooks), "Failed to print unformatted string.");
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct.");
formatted_buffer.format = true;
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer, &global_hooks), "Failed to print formatted string.");
TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct.");
reset(item);
......
......@@ -27,13 +27,14 @@
static void assert_print_string(const char *expected, const char *input)
{
unsigned char printed[1024];
printbuffer buffer = { 0, 0, 0, 0, 0, 0 };
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string.");
TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");
}
......
......@@ -32,21 +32,23 @@ static void assert_print_value(const char *input)
{
unsigned char printed[1024];
cJSON item[1];
printbuffer buffer = { 0, 0, 0, 0, 0, 0 };
parse_buffer parsebuffer = { 0, 0, 0, 0 };
printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
buffer.buffer = printed;
buffer.length = sizeof(printed);
buffer.offset = 0;
buffer.noalloc = true;
buffer.hooks = global_hooks;
parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.hooks = global_hooks;
memset(item, 0, sizeof(item));
TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer, &global_hooks), "Failed to parse value.");
TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer), "Failed to parse value.");
TEST_ASSERT_TRUE_MESSAGE(print_value(item, &buffer, &global_hooks), "Failed to print value.");
TEST_ASSERT_TRUE_MESSAGE(print_value(item, &buffer), "Failed to print value.");
TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected.");
reset(item);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册