tests.json 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. [
  2. { "comment": "empty list, empty docs",
  3. "doc": {},
  4. "patch": [],
  5. "expected": {} },
  6. { "comment": "empty patch list",
  7. "doc": {"foo": 1},
  8. "patch": [],
  9. "expected": {"foo": 1} },
  10. { "comment": "rearrangements OK?",
  11. "doc": {"foo": 1, "bar": 2},
  12. "patch": [],
  13. "expected": {"bar":2, "foo": 1} },
  14. { "comment": "rearrangements OK? How about one level down ... array",
  15. "doc": [{"foo": 1, "bar": 2}],
  16. "patch": [],
  17. "expected": [{"bar":2, "foo": 1}] },
  18. { "comment": "rearrangements OK? How about one level down...",
  19. "doc": {"foo":{"foo": 1, "bar": 2}},
  20. "patch": [],
  21. "expected": {"foo":{"bar":2, "foo": 1}} },
  22. { "comment": "add replaces any existing field",
  23. "doc": {"foo": null},
  24. "patch": [{"op": "add", "path": "/foo", "value":1}],
  25. "expected": {"foo": 1} },
  26. { "comment": "toplevel array",
  27. "doc": [],
  28. "patch": [{"op": "add", "path": "/0", "value": "foo"}],
  29. "expected": ["foo"] },
  30. { "comment": "toplevel array, no change",
  31. "doc": ["foo"],
  32. "patch": [],
  33. "expected": ["foo"] },
  34. { "comment": "toplevel object, numeric string",
  35. "doc": {},
  36. "patch": [{"op": "add", "path": "/foo", "value": "1"}],
  37. "expected": {"foo":"1"} },
  38. { "comment": "toplevel object, integer",
  39. "doc": {},
  40. "patch": [{"op": "add", "path": "/foo", "value": 1}],
  41. "expected": {"foo":1} },
  42. { "comment": "Toplevel scalar values OK?",
  43. "doc": "foo",
  44. "patch": [{"op": "replace", "path": "", "value": "bar"}],
  45. "expected": "bar",
  46. "disabled": true },
  47. { "comment": "replace object document with array document?",
  48. "doc": {},
  49. "patch": [{"op": "add", "path": "", "value": []}],
  50. "expected": [] },
  51. { "comment": "replace array document with object document?",
  52. "doc": [],
  53. "patch": [{"op": "add", "path": "", "value": {}}],
  54. "expected": {} },
  55. { "comment": "append to root array document?",
  56. "doc": [],
  57. "patch": [{"op": "add", "path": "/-", "value": "hi"}],
  58. "expected": ["hi"] },
  59. { "comment": "Add, / target",
  60. "doc": {},
  61. "patch": [ {"op": "add", "path": "/", "value":1 } ],
  62. "expected": {"":1} },
  63. { "comment": "Add, /foo/ deep target (trailing slash)",
  64. "doc": {"foo": {}},
  65. "patch": [ {"op": "add", "path": "/foo/", "value":1 } ],
  66. "expected": {"foo":{"": 1}} },
  67. { "comment": "Add composite value at top level",
  68. "doc": {"foo": 1},
  69. "patch": [{"op": "add", "path": "/bar", "value": [1, 2]}],
  70. "expected": {"foo": 1, "bar": [1, 2]} },
  71. { "comment": "Add into composite value",
  72. "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
  73. "patch": [{"op": "add", "path": "/baz/0/foo", "value": "world"}],
  74. "expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} },
  75. { "doc": {"bar": [1, 2]},
  76. "patch": [{"op": "add", "path": "/bar/8", "value": "5"}],
  77. "error": "Out of bounds (upper)" },
  78. { "doc": {"bar": [1, 2]},
  79. "patch": [{"op": "add", "path": "/bar/-1", "value": "5"}],
  80. "error": "Out of bounds (lower)" },
  81. { "doc": {"foo": 1},
  82. "patch": [{"op": "add", "path": "/bar", "value": true}],
  83. "expected": {"foo": 1, "bar": true} },
  84. { "doc": {"foo": 1},
  85. "patch": [{"op": "add", "path": "/bar", "value": false}],
  86. "expected": {"foo": 1, "bar": false} },
  87. { "doc": {"foo": 1},
  88. "patch": [{"op": "add", "path": "/bar", "value": null}],
  89. "expected": {"foo": 1, "bar": null} },
  90. { "comment": "0 can be an array index or object element name",
  91. "doc": {"foo": 1},
  92. "patch": [{"op": "add", "path": "/0", "value": "bar"}],
  93. "expected": {"foo": 1, "0": "bar" } },
  94. { "doc": ["foo"],
  95. "patch": [{"op": "add", "path": "/1", "value": "bar"}],
  96. "expected": ["foo", "bar"] },
  97. { "doc": ["foo", "sil"],
  98. "patch": [{"op": "add", "path": "/1", "value": "bar"}],
  99. "expected": ["foo", "bar", "sil"] },
  100. { "doc": ["foo", "sil"],
  101. "patch": [{"op": "add", "path": "/0", "value": "bar"}],
  102. "expected": ["bar", "foo", "sil"] },
  103. { "comment": "push item to array via last index + 1",
  104. "doc": ["foo", "sil"],
  105. "patch": [{"op":"add", "path": "/2", "value": "bar"}],
  106. "expected": ["foo", "sil", "bar"] },
  107. { "comment": "add item to array at index > length should fail",
  108. "doc": ["foo", "sil"],
  109. "patch": [{"op":"add", "path": "/3", "value": "bar"}],
  110. "error": "index is greater than number of items in array" },
  111. { "comment": "test against implementation-specific numeric parsing",
  112. "doc": {"1e0": "foo"},
  113. "patch": [{"op": "test", "path": "/1e0", "value": "foo"}],
  114. "expected": {"1e0": "foo"} },
  115. { "comment": "test with bad number should fail",
  116. "doc": ["foo", "bar"],
  117. "patch": [{"op": "test", "path": "/1e0", "value": "bar"}],
  118. "error": "test op shouldn't get array element 1" },
  119. { "doc": ["foo", "sil"],
  120. "patch": [{"op": "add", "path": "/bar", "value": 42}],
  121. "error": "Object operation on array target" },
  122. { "doc": ["foo", "sil"],
  123. "patch": [{"op": "add", "path": "/1", "value": ["bar", "baz"]}],
  124. "expected": ["foo", ["bar", "baz"], "sil"],
  125. "comment": "value in array add not flattened" },
  126. { "doc": {"foo": 1, "bar": [1, 2, 3, 4]},
  127. "patch": [{"op": "remove", "path": "/bar"}],
  128. "expected": {"foo": 1} },
  129. { "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
  130. "patch": [{"op": "remove", "path": "/baz/0/qux"}],
  131. "expected": {"foo": 1, "baz": [{}]} },
  132. { "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
  133. "patch": [{"op": "replace", "path": "/foo", "value": [1, 2, 3, 4]}],
  134. "expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} },
  135. { "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]},
  136. "patch": [{"op": "replace", "path": "/baz/0/qux", "value": "world"}],
  137. "expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} },
  138. { "doc": ["foo"],
  139. "patch": [{"op": "replace", "path": "/0", "value": "bar"}],
  140. "expected": ["bar"] },
  141. { "doc": [""],
  142. "patch": [{"op": "replace", "path": "/0", "value": 0}],
  143. "expected": [0] },
  144. { "doc": [""],
  145. "patch": [{"op": "replace", "path": "/0", "value": true}],
  146. "expected": [true] },
  147. { "doc": [""],
  148. "patch": [{"op": "replace", "path": "/0", "value": false}],
  149. "expected": [false] },
  150. { "doc": [""],
  151. "patch": [{"op": "replace", "path": "/0", "value": null}],
  152. "expected": [null] },
  153. { "doc": ["foo", "sil"],
  154. "patch": [{"op": "replace", "path": "/1", "value": ["bar", "baz"]}],
  155. "expected": ["foo", ["bar", "baz"]],
  156. "comment": "value in array replace not flattened" },
  157. { "comment": "replace whole document",
  158. "doc": {"foo": "bar"},
  159. "patch": [{"op": "replace", "path": "", "value": {"baz": "qux"}}],
  160. "expected": {"baz": "qux"} },
  161. { "comment": "test replace with missing parent key should fail",
  162. "doc": {"bar": "baz"},
  163. "patch": [{"op": "replace", "path": "/foo/bar", "value": false}],
  164. "error": "replace op should fail with missing parent key" },
  165. { "comment": "spurious patch properties",
  166. "doc": {"foo": 1},
  167. "patch": [{"op": "test", "path": "/foo", "value": 1, "spurious": 1}],
  168. "expected": {"foo": 1} },
  169. { "doc": {"foo": null},
  170. "patch": [{"op": "test", "path": "/foo", "value": null}],
  171. "expected": {"foo": null},
  172. "comment": "null value should be valid obj property" },
  173. { "doc": {"foo": null},
  174. "patch": [{"op": "replace", "path": "/foo", "value": "truthy"}],
  175. "expected": {"foo": "truthy"},
  176. "comment": "null value should be valid obj property to be replaced with something truthy" },
  177. { "doc": {"foo": null},
  178. "patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
  179. "expected": {"bar": null},
  180. "comment": "null value should be valid obj property to be moved" },
  181. { "doc": {"foo": null},
  182. "patch": [{"op": "copy", "from": "/foo", "path": "/bar"}],
  183. "expected": {"foo": null, "bar": null},
  184. "comment": "null value should be valid obj property to be copied" },
  185. { "doc": {"foo": null},
  186. "patch": [{"op": "remove", "path": "/foo"}],
  187. "expected": {},
  188. "comment": "null value should be valid obj property to be removed" },
  189. { "doc": {"foo": "bar"},
  190. "patch": [{"op": "replace", "path": "/foo", "value": null}],
  191. "expected": {"foo": null},
  192. "comment": "null value should still be valid obj property replace other value" },
  193. { "doc": {"foo": {"foo": 1, "bar": 2}},
  194. "patch": [{"op": "test", "path": "/foo", "value": {"bar": 2, "foo": 1}}],
  195. "expected": {"foo": {"foo": 1, "bar": 2}},
  196. "comment": "test should pass despite rearrangement" },
  197. { "doc": {"foo": [{"foo": 1, "bar": 2}]},
  198. "patch": [{"op": "test", "path": "/foo", "value": [{"bar": 2, "foo": 1}]}],
  199. "expected": {"foo": [{"foo": 1, "bar": 2}]},
  200. "comment": "test should pass despite (nested) rearrangement" },
  201. { "doc": {"foo": {"bar": [1, 2, 5, 4]}},
  202. "patch": [{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 5, 4]}}],
  203. "expected": {"foo": {"bar": [1, 2, 5, 4]}},
  204. "comment": "test should pass - no error" },
  205. { "doc": {"foo": {"bar": [1, 2, 5, 4]}},
  206. "patch": [{"op": "test", "path": "/foo", "value": [1, 2]}],
  207. "error": "test op should fail" },
  208. { "comment": "Whole document",
  209. "doc": { "foo": 1 },
  210. "patch": [{"op": "test", "path": "", "value": {"foo": 1}}],
  211. "disabled": true },
  212. { "comment": "Empty-string element",
  213. "doc": { "": 1 },
  214. "patch": [{"op": "test", "path": "/", "value": 1}],
  215. "expected": { "": 1 } },
  216. { "doc": {
  217. "foo": ["bar", "baz"],
  218. "": 0,
  219. "a/b": 1,
  220. "c%d": 2,
  221. "e^f": 3,
  222. "g|h": 4,
  223. "i\\j": 5,
  224. "k\"l": 6,
  225. " ": 7,
  226. "m~n": 8
  227. },
  228. "patch": [{"op": "test", "path": "/foo", "value": ["bar", "baz"]},
  229. {"op": "test", "path": "/foo/0", "value": "bar"},
  230. {"op": "test", "path": "/", "value": 0},
  231. {"op": "test", "path": "/a~1b", "value": 1},
  232. {"op": "test", "path": "/c%d", "value": 2},
  233. {"op": "test", "path": "/e^f", "value": 3},
  234. {"op": "test", "path": "/g|h", "value": 4},
  235. {"op": "test", "path": "/i\\j", "value": 5},
  236. {"op": "test", "path": "/k\"l", "value": 6},
  237. {"op": "test", "path": "/ ", "value": 7},
  238. {"op": "test", "path": "/m~0n", "value": 8}],
  239. "expected": {
  240. "": 0,
  241. " ": 7,
  242. "a/b": 1,
  243. "c%d": 2,
  244. "e^f": 3,
  245. "foo": [
  246. "bar",
  247. "baz"
  248. ],
  249. "g|h": 4,
  250. "i\\j": 5,
  251. "k\"l": 6,
  252. "m~n": 8
  253. }
  254. },
  255. { "comment": "Move to same location has no effect",
  256. "doc": {"foo": 1},
  257. "patch": [{"op": "move", "from": "/foo", "path": "/foo"}],
  258. "expected": {"foo": 1} },
  259. { "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
  260. "patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
  261. "expected": {"baz": [{"qux": "hello"}], "bar": 1} },
  262. { "doc": {"baz": [{"qux": "hello"}], "bar": 1},
  263. "patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}],
  264. "expected": {"baz": [{}, "hello"], "bar": 1} },
  265. { "doc": {"baz": [{"qux": "hello"}], "bar": 1},
  266. "patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}],
  267. "expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} },
  268. { "comment": "replacing the root of the document is possible with add",
  269. "doc": {"foo": "bar"},
  270. "patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}],
  271. "expected": {"baz":"qux"}},
  272. { "comment": "Adding to \"/-\" adds to the end of the array",
  273. "doc": [ 1, 2 ],
  274. "patch": [ { "op": "add", "path": "/-", "value": { "foo": [ "bar", "baz" ] } } ],
  275. "expected": [ 1, 2, { "foo": [ "bar", "baz" ] } ]},
  276. { "comment": "Adding to \"/-\" adds to the end of the array, even n levels down",
  277. "doc": [ 1, 2, [ 3, [ 4, 5 ] ] ],
  278. "patch": [ { "op": "add", "path": "/2/1/-", "value": { "foo": [ "bar", "baz" ] } } ],
  279. "expected": [ 1, 2, [ 3, [ 4, 5, { "foo": [ "bar", "baz" ] } ] ] ]},
  280. { "comment": "test remove with bad number should fail",
  281. "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
  282. "patch": [{"op": "remove", "path": "/baz/1e0/qux"}],
  283. "error": "remove op shouldn't remove from array with bad number" },
  284. { "comment": "test remove on array",
  285. "doc": [1, 2, 3, 4],
  286. "patch": [{"op": "remove", "path": "/0"}],
  287. "expected": [2, 3, 4] },
  288. { "comment": "test repeated removes",
  289. "doc": [1, 2, 3, 4],
  290. "patch": [{ "op": "remove", "path": "/1" },
  291. { "op": "remove", "path": "/2" }],
  292. "expected": [1, 3] },
  293. { "comment": "test remove with bad index should fail",
  294. "doc": [1, 2, 3, 4],
  295. "patch": [{"op": "remove", "path": "/1e0"}],
  296. "error": "remove op shouldn't remove from array with bad number" },
  297. { "comment": "test replace with bad number should fail",
  298. "doc": [""],
  299. "patch": [{"op": "replace", "path": "/1e0", "value": false}],
  300. "error": "replace op shouldn't replace in array with bad number" },
  301. { "comment": "test copy with bad number should fail",
  302. "doc": {"baz": [1,2,3], "bar": 1},
  303. "patch": [{"op": "copy", "from": "/baz/1e0", "path": "/boo"}],
  304. "error": "copy op shouldn't work with bad number" },
  305. { "comment": "test move with bad number should fail",
  306. "doc": {"foo": 1, "baz": [1,2,3,4]},
  307. "patch": [{"op": "move", "from": "/baz/1e0", "path": "/foo"}],
  308. "error": "move op shouldn't work with bad number" },
  309. { "comment": "test add with bad number should fail",
  310. "doc": ["foo", "sil"],
  311. "patch": [{"op": "add", "path": "/1e0", "value": "bar"}],
  312. "error": "add op shouldn't add to array with bad number" },
  313. { "comment": "missing 'value' parameter to add",
  314. "doc": [ 1 ],
  315. "patch": [ { "op": "add", "path": "/-" } ],
  316. "error": "missing 'value' parameter" },
  317. { "comment": "missing 'value' parameter to replace",
  318. "doc": [ 1 ],
  319. "patch": [ { "op": "replace", "path": "/0" } ],
  320. "error": "missing 'value' parameter" },
  321. { "comment": "missing 'value' parameter to test",
  322. "doc": [ null ],
  323. "patch": [ { "op": "test", "path": "/0" } ],
  324. "error": "missing 'value' parameter" },
  325. { "comment": "missing value parameter to test - where undef is falsy",
  326. "doc": [ false ],
  327. "patch": [ { "op": "test", "path": "/0" } ],
  328. "error": "missing 'value' parameter" },
  329. { "comment": "missing from parameter to copy",
  330. "doc": [ 1 ],
  331. "patch": [ { "op": "copy", "path": "/-" } ],
  332. "error": "missing 'from' parameter" },
  333. { "comment": "missing from location to copy",
  334. "doc": { "foo": 1 },
  335. "patch": [ { "op": "copy", "from": "/bar", "path": "/foo" } ],
  336. "error": "missing 'from' location" },
  337. { "comment": "missing from parameter to move",
  338. "doc": { "foo": 1 },
  339. "patch": [ { "op": "move", "path": "" } ],
  340. "error": "missing 'from' parameter" },
  341. { "comment": "missing from location to move",
  342. "doc": { "foo": 1 },
  343. "patch": [ { "op": "move", "from": "/bar", "path": "/foo" } ],
  344. "error": "missing 'from' location" },
  345. { "comment": "duplicate ops",
  346. "doc": { "foo": "bar" },
  347. "patch": [ { "op": "add", "path": "/baz", "value": "qux",
  348. "op": "move", "from":"/foo" } ],
  349. "error": "patch has two 'op' members",
  350. "disabled": true },
  351. { "comment": "unrecognized op should fail",
  352. "doc": {"foo": 1},
  353. "patch": [{"op": "spam", "path": "/foo", "value": 1}],
  354. "error": "Unrecognized op 'spam'" },
  355. { "comment": "test with bad array number that has leading zeros",
  356. "doc": ["foo", "bar"],
  357. "patch": [{"op": "test", "path": "/00", "value": "foo"}],
  358. "error": "test op should reject the array value, it has leading zeros" },
  359. { "comment": "test with bad array number that has leading zeros",
  360. "doc": ["foo", "bar"],
  361. "patch": [{"op": "test", "path": "/01", "value": "bar"}],
  362. "error": "test op should reject the array value, it has leading zeros" },
  363. { "comment": "Removing nonexistent field",
  364. "doc": {"foo" : "bar"},
  365. "patch": [{"op": "remove", "path": "/baz"}],
  366. "error": "removing a nonexistent field should fail" },
  367. { "comment": "Removing nonexistent index",
  368. "doc": ["foo", "bar"],
  369. "patch": [{"op": "remove", "path": "/2"}],
  370. "error": "removing a nonexistent index should fail" },
  371. { "comment": "Patch with different capitalisation than doc",
  372. "doc": {"foo":"bar"},
  373. "patch": [{"op": "add", "path": "/FOO", "value": "BAR"}],
  374. "expected": {"foo": "bar", "FOO": "BAR"}
  375. }
  376. ]