diff --git a/test_utils.c b/test_utils.c index 4e0091c..032882e 100644 --- a/test_utils.c +++ b/test_utils.c @@ -114,21 +114,6 @@ int main(void) } cJSON_Delete(root); - - printf("JSON Apply Patch Tests\n"); - for (i = 0; i < 15; i++) - { - cJSON *object_to_be_patched = cJSON_Parse(patches[i][0]); - cJSON *patch = cJSON_Parse(patches[i][1]); - int err = cJSONUtils_ApplyPatches(object_to_be_patched, patch); - char *output = cJSON_Print(object_to_be_patched); - printf("Test %d (err %d):\n%s\n\n", i + 1, err, output); - - free(output); - cJSON_Delete(object_to_be_patched); - cJSON_Delete(patch); - } - /* JSON Generate Patch tests: */ printf("JSON Generate Patch Tests\n"); for (i = 0; i < 15; i++) diff --git a/tests/json-patch-tests/cjson-utils-tests.json b/tests/json-patch-tests/cjson-utils-tests.json new file mode 100644 index 0000000..8037773 --- /dev/null +++ b/tests/json-patch-tests/cjson-utils-tests.json @@ -0,0 +1,84 @@ +[ + { + "comment": "1", + "doc": { "foo": "bar"}, + "patch": [{ "op": "add", "path": "/baz", "value": "qux" }], + "expected": {"baz": "qux", "foo": "bar"} + }, + { + "comment": "2", + "doc": { "foo": [ "bar", "baz" ] }, + "patch": [{ "op": "add", "path": "/foo/1", "value": "qux" }], + "expected": {"foo": [ "bar", "qux", "baz" ] } + }, + { + "comment": "3", + "doc": {"baz": "qux","foo": "bar"}, + "patch": [{ "op": "remove", "path": "/baz" }], + "expected": {"foo": "bar" } + }, + { + "comment": "4", + "doc": { "foo": [ "bar", "qux", "baz" ] }, + "patch": [{ "op": "remove", "path": "/foo/1" }], + "expected": {"foo": [ "bar", "baz" ] } + }, + { + "comment": "5", + "doc": { "baz": "qux","foo": "bar"}, + "patch": [{ "op": "replace", "path": "/baz", "value": "boo" }], + "expected": {"baz": "boo","foo": "bar"} + }, + { + "comment": "6", + "doc": {"foo": {"bar": "baz","waldo": "fred"},"qux": {"corge": "grault"}}, + "patch": [{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }], + "expected": {"foo": {"bar": "baz"},"qux": {"corge": "grault","thud": "fred"}} + }, + { + "comment": "7", + "doc": { "foo": [ "all", "grass", "cows", "eat" ] }, + "patch": [ { "op": "move", "from": "/foo/1", "path": "/foo/3" }], + "expected": { "foo": [ "all", "cows", "eat", "grass" ] } + }, + { + "comment": "8", + "doc": {"baz": "qux","foo": [ "a", 2, "c" ]}, + "patch": [{ "op": "test", "path": "/baz", "value": "qux" },{ "op": "test", "path": "/foo/1", "value": 2 }] + }, + { + "comment": "9", + "doc": { "baz": "qux" }, + "patch": [ { "op": "test", "path": "/baz", "value": "bar" }], + "error": "\"bar\" doesn't exist" + }, + { + "comment": "10", + "doc": { "foo": "bar" }, + "patch": [{ "op": "add", "path": "/child", "value": { "grandchild": { } } }], + "expected": {"foo": "bar","child": {"grandchild": {}}} + }, + { + "comment": "11", + "doc": { "foo": "bar" }, + "patch": [{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }], + "expected": {"foo": "bar","baz": "qux"} + }, + { + "comment": "12", + "doc": { "foo": "bar" }, + "patch": [{ "op": "add", "path": "/baz/bat", "value": "qux" }], + "error": "Can't add to nonexistent object" + }, + { + "comment": "13", + "doc": {"/": 9,"~1": 10}, + "patch": [{"op": "test", "path": "/~01", "value": 10}] + }, + { + "comment": "14", + "doc": { "foo": ["bar"] }, + "patch": [ { "op": "add", "path": "/foo/-", "value": ["abc", "def"] }], + "expected": {"foo": ["bar", ["abc", "def"]] } + } +] diff --git a/tests/json_patch_tests.c b/tests/json_patch_tests.c index a1c03eb..dc13cce 100644 --- a/tests/json_patch_tests.c +++ b/tests/json_patch_tests.c @@ -151,12 +151,29 @@ static void cjson_utils_should_pass_json_patch_test_spec_tests(void) TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed."); } +static void cjson_utils_should_pass_json_patch_test_cjson_utils_tests(void) +{ + cJSON *tests = parse_test_file("json-patch-tests/cjson-utils-tests.json"); + cJSON *test = NULL; + + cJSON_bool failed = false; + cJSON_ArrayForEach(test, tests) + { + failed |= !test_apply_patch(test); + } + + cJSON_Delete(tests); + + TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed."); +} + int main(void) { UNITY_BEGIN(); RUN_TEST(cjson_utils_should_pass_json_patch_test_tests); RUN_TEST(cjson_utils_should_pass_json_patch_test_spec_tests); + RUN_TEST(cjson_utils_should_pass_json_patch_test_cjson_utils_tests); return UNITY_END(); }