parse_with_opts.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "unity/examples/unity_config.h"
  20. #include "unity/src/unity.h"
  21. #include "common.h"
  22. static void parse_with_opts_should_handle_null(void)
  23. {
  24. const char *error_pointer = NULL;
  25. cJSON *item = NULL;
  26. TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, &error_pointer, false), "Failed to handle NULL input.");
  27. item = cJSON_ParseWithOpts("{}", NULL, false);
  28. TEST_ASSERT_NOT_NULL_MESSAGE(item, "Failed to handle NULL error pointer.");
  29. cJSON_Delete(item);
  30. TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts(NULL, NULL, false), "Failed to handle both NULL.");
  31. TEST_ASSERT_NULL_MESSAGE(cJSON_ParseWithOpts("{", NULL, false), "Failed to handle NULL error pointer with parse error.");
  32. }
  33. static void parse_with_opts_should_handle_empty_strings(void)
  34. {
  35. const char empty_string[] = "";
  36. const char *error_pointer = NULL;
  37. TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, NULL, false));
  38. TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr());
  39. TEST_ASSERT_NULL(cJSON_ParseWithOpts(empty_string, &error_pointer, false));
  40. TEST_ASSERT_EQUAL_PTR(empty_string, error_pointer);
  41. TEST_ASSERT_EQUAL_PTR(empty_string, cJSON_GetErrorPtr());
  42. }
  43. static void parse_with_opts_should_handle_incomplete_json(void)
  44. {
  45. const char json[] = "{ \"name\": ";
  46. const char *parse_end = NULL;
  47. TEST_ASSERT_NULL(cJSON_ParseWithOpts(json, &parse_end, false));
  48. TEST_ASSERT_EQUAL_PTR(json + strlen(json), parse_end);
  49. TEST_ASSERT_EQUAL_PTR(json + strlen(json), cJSON_GetErrorPtr());
  50. }
  51. static void parse_with_opts_should_require_null_if_requested(void)
  52. {
  53. cJSON *item = cJSON_ParseWithOpts("{}", NULL, true);
  54. TEST_ASSERT_NOT_NULL(item);
  55. cJSON_Delete(item);
  56. item = cJSON_ParseWithOpts("{} \n", NULL, true);
  57. TEST_ASSERT_NOT_NULL(item);
  58. cJSON_Delete(item);
  59. TEST_ASSERT_NULL(cJSON_ParseWithOpts("{}x", NULL, true));
  60. }
  61. static void parse_with_opts_should_return_parse_end(void)
  62. {
  63. const char json[] = "[] empty array XD";
  64. const char *parse_end = NULL;
  65. cJSON *item = cJSON_ParseWithOpts(json, &parse_end, false);
  66. TEST_ASSERT_NOT_NULL(item);
  67. TEST_ASSERT_EQUAL_PTR(json + 2, parse_end);
  68. cJSON_Delete(item);
  69. }
  70. static void parse_with_opts_should_parse_utf8_bom(void)
  71. {
  72. cJSON *with_bom = NULL;
  73. cJSON *without_bom = NULL;
  74. with_bom = cJSON_ParseWithOpts("\xEF\xBB\xBF{}", NULL, true);
  75. TEST_ASSERT_NOT_NULL(with_bom);
  76. without_bom = cJSON_ParseWithOpts("{}", NULL, true);
  77. TEST_ASSERT_NOT_NULL(with_bom);
  78. TEST_ASSERT_TRUE(cJSON_Compare(with_bom, without_bom, true));
  79. cJSON_Delete(with_bom);
  80. cJSON_Delete(without_bom);
  81. }
  82. int CJSON_CDECL main(void)
  83. {
  84. UNITY_BEGIN();
  85. RUN_TEST(parse_with_opts_should_handle_null);
  86. RUN_TEST(parse_with_opts_should_handle_empty_strings);
  87. RUN_TEST(parse_with_opts_should_handle_incomplete_json);
  88. RUN_TEST(parse_with_opts_should_require_null_if_requested);
  89. RUN_TEST(parse_with_opts_should_return_parse_end);
  90. RUN_TEST(parse_with_opts_should_parse_utf8_bom);
  91. return UNITY_END();
  92. }