Skip to content

Commit bf01a2d

Browse files
author
Mike McCabe
committed
initial
1 parent 0f24370 commit bf01a2d

File tree

2 files changed

+217
-1
lines changed

2 files changed

+217
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
json-patch-tests
22
================
33

4-
Tests for implementations of json-patch
4+
Test cases for implementations of json-patch:
5+
6+
http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-02
7+
8+
Test format
9+
-----------
10+
11+
The test file contains a list of test records, each with a few
12+
fields that defines a test.
13+
14+
- doc: The JSON document to test against
15+
- patch: The patch(es) to apply.
16+
- expected: The expected resulting document, OR
17+
- error: A string describing an expected error
18+
- comment: A string describing the test.
19+
20+
'expected', 'error' and 'comment' are optional.
21+
22+
This test set is not complete - additions are welcome!

tests.json

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
[
2+
{ "comment": "empty list, empty docs",
3+
"doc": {},
4+
"patch": [],
5+
"expected": {} },
6+
7+
{ "comment": "empty patch list",
8+
"doc": {"foo": 1},
9+
"patch": [],
10+
"expected": {"foo": 1} },
11+
12+
{ "comment": "rearrangements OK?",
13+
"doc": {"foo": 1, "bar": 2},
14+
"patch": [],
15+
"expected": {"bar":2, "foo": 1} },
16+
17+
{ "comment": "rearrangements OK? How about one level down ... array",
18+
"doc": [{"foo": 1, "bar": 2}],
19+
"patch": [],
20+
"expected": [{"bar":2, "foo": 1}] },
21+
22+
{ "comment": "rearrangements OK? How about one level down...",
23+
"doc": {"foo":{"foo": 1, "bar": 2}},
24+
"patch": [],
25+
"expected": {"foo":{"bar":2, "foo": 1}} },
26+
27+
{ "comment": "add to existing null-valued field should err",
28+
"doc": {"foo": null},
29+
"patch": [{"add": "/foo", "value":1}],
30+
"error": "'add' target already set" },
31+
32+
{ "comment": "toplevel array",
33+
"doc": [],
34+
"patch": [{"add": "/0", "value": "foo"}],
35+
"expected": ["foo"] },
36+
37+
{ "comment": "toplevel array, no change",
38+
"doc": ["foo"],
39+
"patch": [],
40+
"expected": ["foo"] },
41+
42+
{ "comment": "toplevel object, numeric string",
43+
"doc": {},
44+
"patch": [{"add": "/foo", "value": "1"}],
45+
"expected": {"foo":"1"} },
46+
47+
{ "comment": "toplevel object, integer",
48+
"doc": {},
49+
"patch": [{"add": "/foo", "value": 1}],
50+
"expected": {"foo":1} },
51+
52+
{ "comment": "Toplevel scalar values OK?",
53+
"doc": "foo",
54+
"patch": [{"replace": "", "value": "bar"}],
55+
"expected": "bar",
56+
"disabled": true },
57+
58+
{ "comment": "Add, / target",
59+
"doc": {},
60+
"patch": [ {"add":"/", "value":1 } ],
61+
"expected": {"":1} },
62+
63+
{ "comment": "Add composite value at top level",
64+
"doc": {"foo": 1},
65+
"patch": [{"add": "/bar", "value": [1, 2]}],
66+
"expected": {"foo": 1, "bar": [1, 2]} },
67+
68+
{ "comment": "Add into composite value",
69+
"doc": {"foo": 1, "baz": [{"qux": "hello"}]},
70+
"patch": [{"add": "/baz/0/foo", "value": "world"}],
71+
"expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} },
72+
73+
{ "doc": {"bar": [1, 2]},
74+
"patch": [{"add": "/bar/8", "value": "5"}],
75+
"error": "Out of bounds (upper)" },
76+
77+
{ "doc": {"bar": [1, 2]},
78+
"patch": [{"add": "/bar/-1", "value": "5"}],
79+
"error": "Out of bounds (lower)" },
80+
81+
{ "doc": {"foo": 1},
82+
"patch": [{"add": "/bar", "value": true}],
83+
"expected": {"foo": 1, "bar": true} },
84+
85+
{ "doc": {"foo": 1},
86+
"patch": [{"add": "/bar", "value": false}],
87+
"expected": {"foo": 1, "bar": false} },
88+
89+
{ "doc": {"foo": 1},
90+
"patch": [{"add": "/bar", "value": null}],
91+
"expected": {"foo": 1, "bar": null} },
92+
93+
{ "doc": {"foo": 1},
94+
"patch": [{"add": "/0", "value": "bar"}],
95+
"error": "Array operation on object target" },
96+
97+
{ "doc": ["foo"],
98+
"patch": [{"add": "/1", "value": "bar"}],
99+
"expected": ["foo", "bar"] },
100+
101+
{ "doc": ["foo", "sil"],
102+
"patch": [{"add": "/1", "value": "bar"}],
103+
"expected": ["foo", "bar", "sil"] },
104+
105+
{ "doc": ["foo", "sil"],
106+
"patch": [{"add": "/0", "value": "bar"}],
107+
"expected": ["bar", "foo", "sil"] },
108+
109+
{ "doc": ["foo", "sil"],
110+
"patch": [{"add": "/2", "value": "bar"}],
111+
"expected": ["foo", "sil", "bar"] },
112+
113+
{ "doc": ["foo", "sil"],
114+
"patch": [{"add": "/bar", "value": 42}],
115+
"error": "Object operation on array target" },
116+
117+
{ "doc": ["foo", "sil"],
118+
"patch": [{"add": "/1", "value": ["bar", "baz"]}],
119+
"expected": ["foo", ["bar", "baz"], "sil"],
120+
"comment": "value in array add not flattened" },
121+
122+
{ "doc": {"foo": 1, "bar": [1, 2, 3, 4]},
123+
"patch": [{"remove": "/bar"}],
124+
"expected": {"foo": 1} },
125+
126+
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
127+
"patch": [{"remove": "/baz/0/qux"}],
128+
"expected": {"foo": 1, "baz": [{}]} },
129+
130+
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
131+
"patch": [{"replace": "/foo", "value": [1, 2, 3, 4]}],
132+
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} },
133+
134+
{ "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]},
135+
"patch": [{"replace": "/baz/0/qux", "value": "world"}],
136+
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} },
137+
138+
{ "doc": ["foo"],
139+
"patch": [{"replace": "/0", "value": "bar"}],
140+
"expected": ["bar"] },
141+
142+
{ "doc": [""],
143+
"patch": [{"replace": "/0", "value": 0}],
144+
"expected": [0] },
145+
146+
{ "doc": [""],
147+
"patch": [{"replace": "/0", "value": true}],
148+
"expected": [true] },
149+
150+
{ "doc": [""],
151+
"patch": [{"replace": "/0", "value": false}],
152+
"expected": [false] },
153+
154+
{ "doc": [""],
155+
"patch": [{"replace": "/0", "value": null}],
156+
"expected": [null] },
157+
158+
{ "doc": ["foo", "sil"],
159+
"patch": [{"replace": "/1", "value": ["bar", "baz"]}],
160+
"expected": ["foo", ["bar", "baz"]],
161+
"comment": "value in array replace not flattened" },
162+
163+
{ "doc": {"foo": null},
164+
"patch": [{"test": "/foo", "value": null}],
165+
"comment": "null value should still be valid obj property" },
166+
167+
{ "doc": {"foo": {"foo": 1, "bar": 2}},
168+
"patch": [{"test": "/foo", "value": {"bar": 2, "foo": 1}}],
169+
"comment": "test should pass despite rearrangement" },
170+
171+
{ "doc": {"foo": [{"foo": 1, "bar": 2}]},
172+
"patch": [{"test": "/foo", "value": [{"bar": 2, "foo": 1}]}],
173+
"comment": "test should pass despite (nested) rearrangement" },
174+
175+
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
176+
"patch": [{"test": "/foo", "value": {"bar": [1, 2, 5, 4]}}],
177+
"comment": "test should pass - no error" },
178+
179+
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}},
180+
"patch": [{"test": "/foo", "value": [1, 2]}],
181+
"error": "test op should fail" },
182+
183+
{ "doc": {"foo": 1},
184+
"patch": [{"move": "/foo", "to": "/foo"}],
185+
"expected": {"foo": 1} },
186+
187+
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
188+
"patch": [{"move": "/foo", "to": "/bar"}],
189+
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },
190+
191+
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
192+
"patch": [{"move": "/baz/0/qux", "to": "/baz/1"}],
193+
"expected": {"baz": [{}, "hello"], "bar": 1} },
194+
195+
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
196+
"patch": [{"copy":"/baz/0", "to": "/boo"}],
197+
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} }
198+
]

0 commit comments

Comments
 (0)