2017-03-21 23:40:50 +01: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 "unity/examples/unity_config.h"
|
|
|
|
#include "unity/src/unity.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
static void parse_with_opts_should_handle_null(void)
|
|
|
|
{
|
|
|
|
const char *error_pointer = NULL;
|
|
|
|
cJSON *item = NULL;
|
|
|
|
TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, &error_pointer, false), "Failed to handle NULL input.");
|
|
|
|
item = cJSON_ParseWithOpts("{}", NULL, false);
|
|
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(item, "Failed to handle NULL error pointer.");
|
|
|
|
cJSON_Delete(item);
|
|
|
|
TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, NULL, false), "Failed to handle both NULL.");
|
|
|
|
TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts("{", NULL, false), "Failed to handle NULL error pointer with parse error.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_with_opts_should_handle_empty_strings(void)
|
|
|
|
{
|
|
|
|
const char empty_string[] = "";
|
|
|
|
const char *error_pointer = NULL;
|
2017-09-08 01:20:52 +01:00
|
|
|
|
2017-03-21 23:40:50 +01:00
|
|
|
TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, NULL, false));
|
2017-09-08 01:20:52 +01:00
|
|
|
TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr());
|
|
|
|
|
2017-03-21 23:40:50 +01:00
|
|
|
TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, &error_pointer, false));
|
2017-09-08 01:20:52 +01:00
|
|
|
TEST_ASSERT_EQUAL_PTR(empty_string, error_pointer);
|
2017-09-06 23:50:38 +01:00
|
|
|
TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr());
|
2017-09-08 01:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_with_opts_should_handle_incomplete_json(void)
|
|
|
|
{
|
|
|
|
const char json[] = "{ \"name\": ";
|
|
|
|
const char *parse_end = NULL;
|
|
|
|
|
|
|
|
TEST_ASSERT_NULL(cJSON_ParseWithOpts(json, &parse_end, false));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(json + strlen(json), parse_end);
|
2017-09-06 23:50:38 +01:00
|
|
|
TEST_ASSERT_EQUAL_PTR(json + strlen(json), cJSON_GetErrorPtr());
|
2017-03-21 23:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_with_opts_should_require_null_if_requested(void)
|
|
|
|
{
|
|
|
|
cJSON *item = cJSON_ParseWithOpts("{}", NULL, true);
|
|
|
|
TEST_ASSERT_NOT_NULL(item);
|
|
|
|
cJSON_Delete(item);
|
|
|
|
item = cJSON_ParseWithOpts("{} \n", NULL, true);
|
|
|
|
TEST_ASSERT_NOT_NULL(item);
|
|
|
|
cJSON_Delete(item);
|
|
|
|
TEST_ASSERT_NULL(cJSON_ParseWithOpts("{}x", NULL, true));
|
|
|
|
}
|
|
|
|
|
2017-03-22 01:05:03 +01:00
|
|
|
static void parse_with_opts_should_return_parse_end(void)
|
|
|
|
{
|
|
|
|
const char json[] = "[] empty array XD";
|
|
|
|
const char *parse_end = NULL;
|
|
|
|
|
|
|
|
cJSON *item = cJSON_ParseWithOpts(json, &parse_end, false);
|
|
|
|
TEST_ASSERT_NOT_NULL(item);
|
2017-09-08 01:20:52 +01:00
|
|
|
TEST_ASSERT_EQUAL_PTR(json + 2, parse_end);
|
2017-03-22 01:05:03 +01:00
|
|
|
cJSON_Delete(item);
|
|
|
|
}
|
|
|
|
|
2017-07-03 21:43:14 +02:00
|
|
|
static void parse_with_opts_should_parse_utf8_bom(void)
|
|
|
|
{
|
|
|
|
cJSON *with_bom = NULL;
|
|
|
|
cJSON *without_bom = NULL;
|
|
|
|
|
|
|
|
with_bom = cJSON_ParseWithOpts("\xEF\xBB\xBF{}", NULL, true);
|
|
|
|
TEST_ASSERT_NOT_NULL(with_bom);
|
|
|
|
without_bom = cJSON_ParseWithOpts("{}", NULL, true);
|
|
|
|
TEST_ASSERT_NOT_NULL(with_bom);
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(cJSON_Compare(with_bom, without_bom, true));
|
|
|
|
|
|
|
|
cJSON_Delete(with_bom);
|
|
|
|
cJSON_Delete(without_bom);
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:32:30 -05:00
|
|
|
int CJSON_CDECL main(void)
|
2017-03-21 23:40:50 +01:00
|
|
|
{
|
|
|
|
UNITY_BEGIN();
|
|
|
|
|
|
|
|
RUN_TEST(parse_with_opts_should_handle_null);
|
|
|
|
RUN_TEST(parse_with_opts_should_handle_empty_strings);
|
2017-09-08 01:20:52 +01:00
|
|
|
RUN_TEST(parse_with_opts_should_handle_incomplete_json);
|
2017-03-21 23:40:50 +01:00
|
|
|
RUN_TEST(parse_with_opts_should_require_null_if_requested);
|
2017-03-22 01:05:03 +01:00
|
|
|
RUN_TEST(parse_with_opts_should_return_parse_end);
|
2017-07-03 21:43:14 +02:00
|
|
|
RUN_TEST(parse_with_opts_should_parse_utf8_bom);
|
2017-03-21 23:40:50 +01:00
|
|
|
|
|
|
|
return UNITY_END();
|
|
|
|
}
|