Move ApplyPatch tests from test_utils.c to json.

This commit is contained in:
Max Bruckner 2017-04-13 20:15:25 +02:00
parent 8fd46d51b1
commit 49ebc85e7a
3 changed files with 101 additions and 15 deletions

View File

@ -114,21 +114,6 @@ int main(void)
} }
cJSON_Delete(root); 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: */ /* JSON Generate Patch tests: */
printf("JSON Generate Patch Tests\n"); printf("JSON Generate Patch Tests\n");
for (i = 0; i < 15; i++) for (i = 0; i < 15; i++)

View File

@ -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"]] }
}
]

View File

@ -151,12 +151,29 @@ static void cjson_utils_should_pass_json_patch_test_spec_tests(void)
TEST_ASSERT_FALSE_MESSAGE(failed, "Some tests failed."); 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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(cjson_utils_should_pass_json_patch_test_tests); 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_spec_tests);
RUN_TEST(cjson_utils_should_pass_json_patch_test_cjson_utils_tests);
return UNITY_END(); return UNITY_END();
} }