• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.9.1
5 |_____|_____|_____|_|___|  https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby  granted, free of charge, to any  person obtaining a copy
12 of this software and associated  documentation files (the "Software"), to deal
13 in the Software  without restriction, including without  limitation the rights
14 to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
15 copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
22 IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
23 FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
24 AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
25 LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #include "doctest_compatibility.h"
31 
32 #include <nlohmann/json.hpp>
33 using nlohmann::json;
34 
35 TEST_CASE("JSON Merge Patch")
36 {
37     SECTION("examples from RFC 7396")
38     {
39         SECTION("Section 1")
40         {
41             json document = R"({
42                 "a": "b",
43                 "c": {
44                     "d": "e",
45                     "f": "g"
46                 }
47             })"_json;
48 
49             json patch = R"({
50                 "a": "z",
51                 "c": {
52                     "f": null
53                 }
54             })"_json;
55 
56             json expected = R"({
57                 "a": "z",
58                 "c": {
59                     "d": "e"
60                 }
61             })"_json;
62 
63             document.merge_patch(patch);
64             CHECK(document == expected);
65         }
66 
67         SECTION("Section 3")
68         {
69             json document = R"({
70                 "title": "Goodbye!",
71                 "author": {
72                     "givenName": "John",
73                     "familyName": "Doe"
74                 },
75                 "tags": [
76                     "example",
77                     "sample"
78                 ],
79                 "content": "This will be unchanged"
80             })"_json;
81 
82             json patch = R"({
83                 "title": "Hello!",
84                 "phoneNumber": "+01-123-456-7890",
85                 "author": {
86                     "familyName": null
87                 },
88                 "tags": [
89                     "example"
90                 ]
91             })"_json;
92 
93             json expected = R"({
94                 "title": "Hello!",
95                 "author": {
96                     "givenName": "John"
97                 },
98                 "tags": [
99                     "example"
100                 ],
101                 "content": "This will be unchanged",
102                 "phoneNumber": "+01-123-456-7890"
103             })"_json;
104 
105             document.merge_patch(patch);
106             CHECK(document == expected);
107         }
108 
109         SECTION("Appendix A")
110         {
111             SECTION("Example 1")
112             {
113                 json original = R"({"a":"b"})"_json;
114                 json patch = R"({"a":"c"})"_json;
115                 json result = R"({"a":"c"})"_json;
116 
117                 original.merge_patch(patch);
118                 CHECK(original == result);
119             }
120 
121             SECTION("Example 2")
122             {
123                 json original = R"({"a":"b"})"_json;
124                 json patch = R"({"b":"c"})"_json;
125                 json result = R"({"a":"b", "b":"c"})"_json;
126 
127                 original.merge_patch(patch);
128                 CHECK(original == result);
129             }
130 
131             SECTION("Example 3")
132             {
133                 json original = R"({"a":"b"})"_json;
134                 json patch = R"({"a":null})"_json;
135                 json result = R"({})"_json;
136 
137                 original.merge_patch(patch);
138                 CHECK(original == result);
139             }
140 
141             SECTION("Example 4")
142             {
143                 json original = R"({"a":"b","b":"c"})"_json;
144                 json patch = R"({"a":null})"_json;
145                 json result = R"({"b":"c"})"_json;
146 
147                 original.merge_patch(patch);
148                 CHECK(original == result);
149             }
150 
151             SECTION("Example 5")
152             {
153                 json original = R"({"a":["b"]})"_json;
154                 json patch = R"({"a":"c"})"_json;
155                 json result = R"({"a":"c"})"_json;
156 
157                 original.merge_patch(patch);
158                 CHECK(original == result);
159             }
160 
161             SECTION("Example 6")
162             {
163                 json original = R"({"a":"c"})"_json;
164                 json patch = R"({"a":["b"]})"_json;
165                 json result = R"({"a":["b"]})"_json;
166 
167                 original.merge_patch(patch);
168                 CHECK(original == result);
169             }
170 
171             SECTION("Example 7")
172             {
173                 json original = R"({"a":{"b": "c"}})"_json;
174                 json patch = R"({"a":{"b":"d","c":null}})"_json;
175                 json result = R"({"a": {"b": "d"}})"_json;
176 
177                 original.merge_patch(patch);
178                 CHECK(original == result);
179             }
180 
181             SECTION("Example 8")
182             {
183                 json original = R"({"a":[{"b":"c"}]})"_json;
184                 json patch = R"({"a":[1]})"_json;
185                 json result = R"({"a":[1]})"_json;
186 
187                 original.merge_patch(patch);
188                 CHECK(original == result);
189             }
190 
191             SECTION("Example 9")
192             {
193                 json original = R"(["a","b"])"_json;
194                 json patch = R"(["c","d"])"_json;
195                 json result = R"(["c","d"])"_json;
196 
197                 original.merge_patch(patch);
198                 CHECK(original == result);
199             }
200 
201             SECTION("Example 10")
202             {
203                 json original = R"({"a":"b"})"_json;
204                 json patch = R"(["c"])"_json;
205                 json result = R"(["c"])"_json;
206 
207                 original.merge_patch(patch);
208                 CHECK(original == result);
209             }
210 
211             SECTION("Example 11")
212             {
213                 json original = R"({"a":"foo"})"_json;
214                 json patch = R"(null)"_json;
215                 json result = R"(null)"_json;
216 
217                 original.merge_patch(patch);
218                 CHECK(original == result);
219             }
220 
221             SECTION("Example 12")
222             {
223                 json original = R"({"a":"foo"})"_json;
224                 json patch = R"("bar")"_json;
225                 json result = R"("bar")"_json;
226 
227                 original.merge_patch(patch);
228                 CHECK(original == result);
229             }
230 
231             SECTION("Example 13")
232             {
233                 json original = R"({"e":null})"_json;
234                 json patch = R"({"a":1})"_json;
235                 json result = R"({"e":null,"a":1})"_json;
236 
237                 original.merge_patch(patch);
238                 CHECK(original == result);
239             }
240 
241             SECTION("Example 14")
242             {
243                 json original = R"([1,2])"_json;
244                 json patch = R"({"a":"b","c":null})"_json;
245                 json result = R"({"a":"b"})"_json;
246 
247                 original.merge_patch(patch);
248                 CHECK(original == result);
249             }
250 
251             SECTION("Example 15")
252             {
253                 json original = R"({})"_json;
254                 json patch = R"({"a":{"bb":{"ccc":null}}})"_json;
255                 json result = R"({"a":{"bb":{}}})"_json;
256 
257                 original.merge_patch(patch);
258                 CHECK(original == result);
259             }
260         }
261     }
262 }
263