2017-02-07 19:39:45 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "unity/examples/unity_config.h"
|
|
|
|
#include "unity/src/unity.h"
|
2017-02-15 17:41:55 +00:00
|
|
|
#include "common.h"
|
2017-02-07 19:39:45 +00:00
|
|
|
|
|
|
|
static cJSON item[1];
|
|
|
|
|
|
|
|
static void assert_is_value(cJSON *value_item, int type)
|
|
|
|
{
|
|
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(value_item, "Item is NULL.");
|
|
|
|
|
2017-02-15 18:57:54 +00:00
|
|
|
assert_not_in_list(value_item);
|
|
|
|
assert_has_type(value_item, type);
|
|
|
|
assert_has_no_reference(value_item);
|
|
|
|
assert_has_no_const_string(value_item);
|
|
|
|
assert_has_no_string(value_item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void assert_parse_value(const char *string, int type)
|
|
|
|
{
|
2017-04-27 00:48:28 +00:00
|
|
|
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
|
2017-03-14 09:15:57 +00:00
|
|
|
buffer.content = (const unsigned char*) string;
|
|
|
|
buffer.length = strlen(string) + sizeof("");
|
2017-04-27 00:48:28 +00:00
|
|
|
buffer.hooks = global_hooks;
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(parse_value(item, &buffer));
|
2017-02-07 19:39:45 +00:00
|
|
|
assert_is_value(item, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_null(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("null", cJSON_NULL);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_true(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("true", cJSON_True);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_false(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("false", cJSON_False);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_number(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("1.5", cJSON_Number);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_string(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("\"\"", cJSON_String);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
assert_parse_value("\"hello\"", cJSON_String);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_array(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("[]", cJSON_Array);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_value_should_parse_object(void)
|
|
|
|
{
|
|
|
|
assert_parse_value("{}", cJSON_Object);
|
2017-02-15 17:41:55 +00:00
|
|
|
reset(item);
|
2017-02-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
/* initialize cJSON item */
|
|
|
|
memset(item, 0, sizeof(cJSON));
|
|
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(parse_value_should_parse_null);
|
|
|
|
RUN_TEST(parse_value_should_parse_true);
|
|
|
|
RUN_TEST(parse_value_should_parse_false);
|
|
|
|
RUN_TEST(parse_value_should_parse_number);
|
|
|
|
RUN_TEST(parse_value_should_parse_string);
|
|
|
|
RUN_TEST(parse_value_should_parse_array);
|
|
|
|
RUN_TEST(parse_value_should_parse_object);
|
|
|
|
return UNITY_END();
|
|
|
|
}
|