You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_json_patch.c 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <assert.h>
  5. #include <errno.h>
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "json.h"
  10. #ifndef PATH_MAX
  11. #define PATH_MAX 256
  12. #endif
  13. void test_json_patch_op(struct json_object *jo)
  14. {
  15. const char *comment = json_object_get_string(json_object_object_get(jo, "comment"));
  16. struct json_object *doc = json_object_object_get(jo, "doc");
  17. struct json_object *patch = json_object_object_get(jo, "patch");
  18. struct json_object *expected = json_object_object_get(jo, "expected");
  19. struct json_object *error = json_object_object_get(jo, "error");
  20. int disabled_test = json_object_get_boolean(json_object_object_get(jo, "disabled_in_json_c"));
  21. const char *error_s = json_object_get_string(error);
  22. struct json_object *res = NULL;
  23. int ret;
  24. printf("Testing '%s', doc '%s' patch '%s' : ",
  25. comment ? comment : error_s,
  26. json_object_get_string(doc),
  27. json_object_get_string(patch));
  28. if (disabled_test) {
  29. printf("SKIPPING - disabled in the test spec\n");
  30. return;
  31. }
  32. if (!error && !expected) {
  33. printf("SKIPPING - no expected or error conditions in test\n");
  34. return;
  35. }
  36. fflush(stdout);
  37. if (error) {
  38. assert(-1 == json_patch_apply(doc, patch, &res));
  39. assert(res == NULL);
  40. } else {
  41. ret = json_patch_apply(doc, patch, &res);
  42. if (ret) {
  43. fprintf(stderr, "json_patch_apply() returned '%d'\n", ret);
  44. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  45. fprintf(stderr, "Got: %s\n", json_object_get_string(res));
  46. fflush(stderr);
  47. assert(0);
  48. }
  49. assert(res != NULL);
  50. ret = json_object_equal(expected, res);
  51. if (ret == 0) {
  52. fprintf(stderr, "json_object_equal() returned '%d'\n", ret);
  53. fprintf(stderr, "Expected: %s\n", json_object_get_string(expected));
  54. fprintf(stderr, "Got: %s\n", json_object_get_string(res));
  55. fflush(stderr);
  56. assert(0);
  57. }
  58. json_object_put(res);
  59. res = NULL;
  60. }
  61. printf("OK\n");
  62. }
  63. void test_json_patch_using_file(const char *testdir, const char *filename)
  64. {
  65. char full_filename[PATH_MAX];
  66. (void)snprintf(full_filename, sizeof(full_filename), "%s/%s", testdir, filename);
  67. int i;
  68. json_object *jo = json_object_from_file(full_filename);
  69. if (!jo) {
  70. fprintf(stderr, "FAIL: unable to open %s: %s\n", full_filename, strerror(errno));
  71. exit(EXIT_FAILURE);
  72. }
  73. for (i = 0; i < json_object_array_length(jo); i++) {
  74. struct json_object *jo1 = json_object_array_get_idx(jo, i);
  75. test_json_patch_op(jo1);
  76. }
  77. json_object_put(jo);
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. const char *testdir;
  82. if (argc < 2)
  83. {
  84. fprintf(stderr,
  85. "Usage: %s <testdir>\n"
  86. " <testdir> is the location of input files\n",
  87. argv[0]);
  88. return EXIT_FAILURE;
  89. }
  90. testdir = argv[1];
  91. // Test json_c_version.c
  92. if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
  93. {
  94. printf("FAIL: Output from json_c_version(): %s does not match %s",
  95. json_c_version(), JSON_C_VERSION);
  96. return EXIT_FAILURE;
  97. }
  98. if (json_c_version_num() != JSON_C_VERSION_NUM)
  99. {
  100. printf("FAIL: Output from json_c_version_num(): %d does not match %d",
  101. json_c_version_num(), JSON_C_VERSION_NUM);
  102. return EXIT_FAILURE;
  103. }
  104. test_json_patch_using_file(testdir, "json_patch_spec_tests.json");
  105. test_json_patch_using_file(testdir, "json_patch_tests.json");
  106. return 0;
  107. }