parse_object.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unity/examples/unity_config.h"
  23. #include "unity/src/unity.h"
  24. #include "common.h"
  25. static cJSON item[1];
  26. static void assert_is_object(cJSON *object_item)
  27. {
  28. TEST_ASSERT_NOT_NULL_MESSAGE(object_item, "Item is NULL.");
  29. assert_not_in_list(object_item);
  30. assert_has_type(object_item, cJSON_Object);
  31. assert_has_no_reference(object_item);
  32. assert_has_no_const_string(object_item);
  33. assert_has_no_valuestring(object_item);
  34. assert_has_no_string(object_item);
  35. }
  36. static void assert_is_child(cJSON *child_item, const char *name, int type)
  37. {
  38. TEST_ASSERT_NOT_NULL_MESSAGE(child_item, "Child item is NULL.");
  39. TEST_ASSERT_NOT_NULL_MESSAGE(child_item->string, "Child item doesn't have a name.");
  40. TEST_ASSERT_EQUAL_STRING_MESSAGE(name, child_item->string, "Child item has the wrong name.");
  41. TEST_ASSERT_BITS(0xFF, type, child_item->type);
  42. }
  43. static void assert_not_object(const char *json)
  44. {
  45. parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  46. parsebuffer.content = (const unsigned char*)json;
  47. parsebuffer.length = strlen(json) + sizeof("");
  48. parsebuffer.hooks = global_hooks;
  49. TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
  50. assert_is_invalid(item);
  51. reset(item);
  52. }
  53. static void assert_parse_object(const char *json)
  54. {
  55. parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  56. parsebuffer.content = (const unsigned char*)json;
  57. parsebuffer.length = strlen(json) + sizeof("");
  58. parsebuffer.hooks = global_hooks;
  59. TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
  60. assert_is_object(item);
  61. }
  62. static void parse_object_should_parse_empty_objects(void)
  63. {
  64. assert_parse_object("{}");
  65. assert_has_no_child(item);
  66. reset(item);
  67. assert_parse_object("{\n\t}");
  68. assert_has_no_child(item);
  69. reset(item);
  70. }
  71. static void parse_object_should_parse_objects_with_one_element(void)
  72. {
  73. assert_parse_object("{\"one\":1}");
  74. assert_is_child(item->child, "one", cJSON_Number);
  75. reset(item);
  76. assert_parse_object("{\"hello\":\"world!\"}");
  77. assert_is_child(item->child, "hello", cJSON_String);
  78. reset(item);
  79. assert_parse_object("{\"array\":[]}");
  80. assert_is_child(item->child, "array", cJSON_Array);
  81. reset(item);
  82. assert_parse_object("{\"null\":null}");
  83. assert_is_child(item->child, "null", cJSON_NULL);
  84. reset(item);
  85. }
  86. static void parse_object_should_parse_objects_with_multiple_elements(void)
  87. {
  88. assert_parse_object("{\"one\":1\t,\t\"two\"\n:2, \"three\":3}");
  89. assert_is_child(item->child, "one", cJSON_Number);
  90. assert_is_child(item->child->next, "two", cJSON_Number);
  91. assert_is_child(item->child->next->next, "three", cJSON_Number);
  92. reset(item);
  93. {
  94. size_t i = 0;
  95. cJSON *node = NULL;
  96. int expected_types[7] =
  97. {
  98. cJSON_Number,
  99. cJSON_NULL,
  100. cJSON_True,
  101. cJSON_False,
  102. cJSON_Array,
  103. cJSON_String,
  104. cJSON_Object
  105. };
  106. const char *expected_names[7] =
  107. {
  108. "one",
  109. "NULL",
  110. "TRUE",
  111. "FALSE",
  112. "array",
  113. "world",
  114. "object"
  115. };
  116. assert_parse_object("{\"one\":1, \"NULL\":null, \"TRUE\":true, \"FALSE\":false, \"array\":[], \"world\":\"hello\", \"object\":{}}");
  117. node = item->child;
  118. for (
  119. i = 0;
  120. (i < (sizeof(expected_types)/sizeof(int)))
  121. && (node != NULL);
  122. (void)i++, node = node->next)
  123. {
  124. assert_is_child(node, expected_names[i], expected_types[i]);
  125. }
  126. TEST_ASSERT_EQUAL_INT(i, 7);
  127. reset(item);
  128. }
  129. }
  130. static void parse_object_should_not_parse_non_objects(void)
  131. {
  132. assert_not_object("");
  133. assert_not_object("{");
  134. assert_not_object("}");
  135. assert_not_object("[\"hello\",{}]");
  136. assert_not_object("42");
  137. assert_not_object("3.14");
  138. assert_not_object("\"{}hello world!\n\"");
  139. }
  140. int CJSON_CDECL main(void)
  141. {
  142. /* initialize cJSON item */
  143. memset(item, 0, sizeof(cJSON));
  144. UNITY_BEGIN();
  145. RUN_TEST(parse_object_should_parse_empty_objects);
  146. RUN_TEST(parse_object_should_not_parse_non_objects);
  147. RUN_TEST(parse_object_should_parse_objects_with_multiple_elements);
  148. RUN_TEST(parse_object_should_parse_objects_with_one_element);
  149. return UNITY_END();
  150. }