json_patch_tests.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include "../cJSON_Utils.h"
  26. static cJSON *parse_test_file(const char * const filename)
  27. {
  28. char *file = NULL;
  29. cJSON *json = NULL;
  30. file = read_file(filename);
  31. TEST_ASSERT_NOT_NULL_MESSAGE(file, "Failed to read file.");
  32. json = cJSON_Parse(file);
  33. TEST_ASSERT_NOT_NULL_MESSAGE(json, "Failed to parse test json.");
  34. TEST_ASSERT_TRUE_MESSAGE(cJSON_IsArray(json), "Json is not an array.");
  35. free(file);
  36. return json;
  37. }
  38. static cJSON_bool test_apply_patch(const cJSON * const test)
  39. {
  40. cJSON *doc = NULL;
  41. cJSON *patch = NULL;
  42. cJSON *expected = NULL;
  43. cJSON *error_element = NULL;
  44. cJSON *comment = NULL;
  45. cJSON *disabled = NULL;
  46. cJSON *object = NULL;
  47. cJSON_bool successful = false;
  48. /* extract all the data out of the test */
  49. comment = cJSON_GetObjectItemCaseSensitive(test, "comment");
  50. if (cJSON_IsString(comment))
  51. {
  52. printf("Testing \"%s\"\n", comment->valuestring);
  53. }
  54. else
  55. {
  56. printf("Testing unknown\n");
  57. }
  58. disabled = cJSON_GetObjectItemCaseSensitive(test, "disabled");
  59. if (cJSON_IsTrue(disabled))
  60. {
  61. printf("SKIPPED\n");
  62. return true;
  63. }
  64. doc = cJSON_GetObjectItemCaseSensitive(test, "doc");
  65. TEST_ASSERT_NOT_NULL_MESSAGE(doc, "No \"doc\" in the test.");
  66. patch = cJSON_GetObjectItemCaseSensitive(test, "patch");
  67. TEST_ASSERT_NOT_NULL_MESSAGE(patch, "No \"patch\"in the test.");
  68. /* Make a working copy of 'doc' */
  69. object = cJSON_Duplicate(doc, true);
  70. TEST_ASSERT_NOT_NULL(object);
  71. expected = cJSON_GetObjectItemCaseSensitive(test, "expected");
  72. error_element = cJSON_GetObjectItemCaseSensitive(test, "error");
  73. if (error_element != NULL)
  74. {
  75. /* excepting an error */
  76. TEST_ASSERT_TRUE_MESSAGE(0 != cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Test didn't fail as it's supposed to.");
  77. successful = true;
  78. }
  79. else
  80. {
  81. /* apply the patch */
  82. TEST_ASSERT_EQUAL_INT_MESSAGE(0, cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Failed to apply patches.");
  83. successful = true;
  84. if (expected != NULL)
  85. {
  86. successful = cJSON_Compare(object, expected, true);
  87. }
  88. }
  89. cJSON_Delete(object);
  90. if (successful)
  91. {
  92. printf("OK\n");
  93. }
  94. else
  95. {
  96. printf("FAILED\n");
  97. }
  98. return successful;
  99. }
  100. static cJSON_bool test_generate_test(cJSON *test)
  101. {
  102. cJSON *doc = NULL;
  103. cJSON *patch = NULL;
  104. cJSON *expected = NULL;
  105. cJSON *disabled = NULL;
  106. cJSON *object = NULL;
  107. cJSON_bool successful = false;
  108. char *printed_patch = NULL;
  109. disabled = cJSON_GetObjectItemCaseSensitive(test, "disabled");
  110. if (cJSON_IsTrue(disabled))
  111. {
  112. printf("SKIPPED\n");
  113. return true;
  114. }
  115. doc = cJSON_GetObjectItemCaseSensitive(test, "doc");
  116. TEST_ASSERT_NOT_NULL_MESSAGE(doc, "No \"doc\" in the test.");
  117. /* Make a working copy of 'doc' */
  118. object = cJSON_Duplicate(doc, true);
  119. TEST_ASSERT_NOT_NULL(object);
  120. expected = cJSON_GetObjectItemCaseSensitive(test, "expected");
  121. if (expected == NULL)
  122. {
  123. cJSON_Delete(object);
  124. /* if there is no expected output, this test doesn't make sense */
  125. return true;
  126. }
  127. patch = cJSONUtils_GeneratePatchesCaseSensitive(doc, expected);
  128. TEST_ASSERT_NOT_NULL_MESSAGE(patch, "Failed to generate patches.");
  129. printed_patch = cJSON_Print(patch);
  130. printf("%s\n", printed_patch);
  131. free(printed_patch);
  132. /* apply the generated patch */
  133. TEST_ASSERT_EQUAL_INT_MESSAGE(0, cJSONUtils_ApplyPatchesCaseSensitive(object, patch), "Failed to apply generated patch.");
  134. successful = cJSON_Compare(object, expected, true);
  135. cJSON_Delete(patch);
  136. cJSON_Delete(object);
  137. if (successful)
  138. {
  139. printf("generated patch: OK\n");
  140. }
  141. else
  142. {
  143. printf("generated patch: FAILED\n");
  144. }
  145. return successful;
  146. }
  147. static void cjson_utils_should_pass_json_patch_test_tests(void)
  148. {
  149. cJSON *tests = parse_test_file("json-patch-tests/tests.json");
  150. cJSON *test = NULL;
  151. cJSON_bool failed = false;
  152. cJSON_ArrayForEach(test, tests)
  153. {
  154. failed |= !test_apply_patch(test);
  155. failed |= !test_generate_test(test);
  156. }
  157. cJSON_Delete(tests);
  158. TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
  159. }
  160. static void cjson_utils_should_pass_json_patch_test_spec_tests(void)
  161. {
  162. cJSON *tests = parse_test_file("json-patch-tests/spec_tests.json");
  163. cJSON *test = NULL;
  164. cJSON_bool failed = false;
  165. cJSON_ArrayForEach(test, tests)
  166. {
  167. failed |= !test_apply_patch(test);
  168. failed |= !test_generate_test(test);
  169. }
  170. cJSON_Delete(tests);
  171. TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
  172. }
  173. static void cjson_utils_should_pass_json_patch_test_cjson_utils_tests(void)
  174. {
  175. cJSON *tests = parse_test_file("json-patch-tests/cjson-utils-tests.json");
  176. cJSON *test = NULL;
  177. cJSON_bool failed = false;
  178. cJSON_ArrayForEach(test, tests)
  179. {
  180. failed |= !test_apply_patch(test);
  181. failed |= !test_generate_test(test);
  182. }
  183. cJSON_Delete(tests);
  184. TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed.");
  185. }
  186. int main(void)
  187. {
  188. UNITY_BEGIN();
  189. RUN_TEST(cjson_utils_should_pass_json_patch_test_tests);
  190. RUN_TEST(cjson_utils_should_pass_json_patch_test_spec_tests);
  191. RUN_TEST(cjson_utils_should_pass_json_patch_test_cjson_utils_tests);
  192. return UNITY_END();
  193. }