123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- __ _____ _____ _____
- __| | __| | | | JSON for Modern C++ (test suite)
- | | |__ | | | | | | version 3.7.3
- |_____|_____|_____|_|___| https://github.com/nlohmann/json
- Licensed under the MIT License <http://opensource.org/licenses/MIT>.
- SPDX-License-Identifier: MIT
- Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #include "doctest_compatibility.h"
- #include <nlohmann/json.hpp>
- using nlohmann::json;
- TEST_CASE("JSON Merge Patch")
- {
- SECTION("examples from RFC 7396")
- {
- SECTION("Section 1")
- {
- json document = R"({
- "a": "b",
- "c": {
- "d": "e",
- "f": "g"
- }
- })"_json;
- json patch = R"({
- "a": "z",
- "c": {
- "f": null
- }
- })"_json;
- json expected = R"({
- "a": "z",
- "c": {
- "d": "e"
- }
- })"_json;
- document.merge_patch(patch);
- CHECK(document == expected);
- }
- SECTION("Section 3")
- {
- json document = R"({
- "title": "Goodbye!",
- "author": {
- "givenName": "John",
- "familyName": "Doe"
- },
- "tags": [
- "example",
- "sample"
- ],
- "content": "This will be unchanged"
- })"_json;
- json patch = R"({
- "title": "Hello!",
- "phoneNumber": "+01-123-456-7890",
- "author": {
- "familyName": null
- },
- "tags": [
- "example"
- ]
- })"_json;
- json expected = R"({
- "title": "Hello!",
- "author": {
- "givenName": "John"
- },
- "tags": [
- "example"
- ],
- "content": "This will be unchanged",
- "phoneNumber": "+01-123-456-7890"
- })"_json;
- document.merge_patch(patch);
- CHECK(document == expected);
- }
- SECTION("Appendix A")
- {
- SECTION("Example 1")
- {
- json original = R"({"a":"b"})"_json;
- json patch = R"({"a":"c"})"_json;
- json result = R"({"a":"c"})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 2")
- {
- json original = R"({"a":"b"})"_json;
- json patch = R"({"b":"c"})"_json;
- json result = R"({"a":"b", "b":"c"})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 3")
- {
- json original = R"({"a":"b"})"_json;
- json patch = R"({"a":null})"_json;
- json result = R"({})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 4")
- {
- json original = R"({"a":"b","b":"c"})"_json;
- json patch = R"({"a":null})"_json;
- json result = R"({"b":"c"})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 5")
- {
- json original = R"({"a":["b"]})"_json;
- json patch = R"({"a":"c"})"_json;
- json result = R"({"a":"c"})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 6")
- {
- json original = R"({"a":"c"})"_json;
- json patch = R"({"a":["b"]})"_json;
- json result = R"({"a":["b"]})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 7")
- {
- json original = R"({"a":{"b": "c"}})"_json;
- json patch = R"({"a":{"b":"d","c":null}})"_json;
- json result = R"({"a": {"b": "d"}})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 8")
- {
- json original = R"({"a":[{"b":"c"}]})"_json;
- json patch = R"({"a":[1]})"_json;
- json result = R"({"a":[1]})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 9")
- {
- json original = R"(["a","b"])"_json;
- json patch = R"(["c","d"])"_json;
- json result = R"(["c","d"])"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 10")
- {
- json original = R"({"a":"b"})"_json;
- json patch = R"(["c"])"_json;
- json result = R"(["c"])"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 11")
- {
- json original = R"({"a":"foo"})"_json;
- json patch = R"(null)"_json;
- json result = R"(null)"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 12")
- {
- json original = R"({"a":"foo"})"_json;
- json patch = R"("bar")"_json;
- json result = R"("bar")"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 13")
- {
- json original = R"({"e":null})"_json;
- json patch = R"({"a":1})"_json;
- json result = R"({"e":null,"a":1})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 14")
- {
- json original = R"([1,2])"_json;
- json patch = R"({"a":"b","c":null})"_json;
- json result = R"({"a":"b"})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- SECTION("Example 15")
- {
- json original = R"({})"_json;
- json patch = R"({"a":{"bb":{"ccc":null}}})"_json;
- json result = R"({"a":{"bb":{}}})"_json;
- original.merge_patch(patch);
- CHECK(original == result);
- }
- }
- }
- }
|