test.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var json_diff = require('./json-diff.js');
  2. var failures = 0;
  3. function expect(a, b, c) {
  4. var d = json_diff.diff(a, b);
  5. var ok = JSON.stringify(c) === JSON.stringify(d);
  6. console.log(JSON.stringify(a) + ' ' + JSON.stringify(b) + ': ' +
  7. (ok ? 'ok' : 'FAILURE: ') );
  8. if (!ok) {
  9. console.log('Expected: ' + JSON.stringify(c));
  10. console.log('Got: ' + JSON.stringify(d));
  11. failures++;
  12. }
  13. }
  14. expect({}, {}, [])
  15. expect([], [], [])
  16. expect([1], [1], [])
  17. expect(['a'], ['a'], [])
  18. expect([null], [null], [])
  19. expect([true], [true], [])
  20. expect([false], [false], [])
  21. expect({a: 'a', b: 'b'}, {b: 'b', a: 'a'}, [])
  22. expect([1, 2, 3], [1, 2, 3], [])
  23. expect({}, {'//~~': ''}, [{op: 'add', path: '/~1~1~0~0', value: ''}])
  24. expect(null, 1, [{op: 'replace', path: '/', value: 1}])
  25. expect(1, null, [{op: 'replace', path: '/', value: null}])
  26. expect([1], 1, [{op: 'replace', path: '/', value: 1}])
  27. expect(1, [1], [{op: 'replace', path: '/', value: [1]}])
  28. expect({a: 1}, 1, [{op: 'replace', path: '/', value: 1}])
  29. expect(1, {a: 1}, [{op: 'replace', path: '/', value: {a: 1}}])
  30. expect({a: 'a'}, {},
  31. [{op: 'remove', path: '/a'}])
  32. expect({}, {a: 'a'},
  33. [{op: 'add', path: '/a', value: 'a'}])
  34. expect([[1]], [],
  35. [{op: 'remove', path: '/0'}])
  36. expect([], [1],
  37. [{op: 'add', path: '/0', value: 1}])
  38. expect([], [[1]],
  39. [{op: 'add', path: '/0', value: [1]}])
  40. expect([[]], [[1]],
  41. [{op: 'add', path: '/0/0', value: 1}])
  42. expect({}, [],
  43. [{op: 'replace', path: '/', value: []}])
  44. expect([2, 3], [1, 2, 3], [{op: 'add', path: '/0', value: 1}])
  45. expect([1, 3], [1, 2, 3], [{op: 'add', path: '/1', value: 2}])
  46. expect([1, 2], [1, 2, 3], [{op: 'add', path: '/2', value: 3}])
  47. expect([1, 2, 3], [1, 4, 3], [{op: 'replace', path: '/1', value: 4}])
  48. expect({a: [9, 8, 7], b: 2, c: 3}, {a: [9, 7], b: 2, c: 4, d: 5},
  49. [{op: 'remove', path: '/a/1'}, {op: 'replace', path: '/c', value: 4}, {op: 'add', path: '/d', value: 5}])
  50. expect([1, 0, 0], [1, 1, 0], [{op: 'replace', path: '/1', value: 1}]);
  51. expect([1, 1, 0], [1, 0, 0], [{op: 'replace', path: '/1', value: 0}]);
  52. // Reverse examples from RFC 6902, as far as applicable:
  53. expect({foo: 'bar'}, {baz: 'qux', foo: 'bar'}, [{op: 'add', path: '/baz', value: 'qux'}])
  54. expect({foo: ['bar', 'baz']}, {foo: ['bar', 'qux', 'baz']}, [{op: 'add', path: '/foo/1', value: 'qux'}])
  55. expect({baz: 'qux', foo: 'bar'}, {foo: 'bar'}, [{op: 'remove', path: '/baz'}])
  56. expect({foo: ['bar', 'qux', 'baz']}, {foo: ['bar', 'baz']}, [{op: 'remove', path: '/foo/1'}])
  57. expect({baz: 'qux', foo: 'bar'}, {baz: 'boo', foo: 'bar'}, [{op: 'replace', path: '/baz', value: 'boo'}])
  58. expect({foo: 'bar'}, {foo: 'bar', child: {grandchild: {}}}, [{op: 'add', path: '/child', value: {'grandchild': {}}}])
  59. expect({foo: ['bar']}, {foo: ['bar', ['abc', 'def']]}, [{op: 'add', path: '/foo/1', value: ['abc', 'def']}])
  60. // In this case, it would be better to replace the entire array:
  61. expect([0, 0], [1, 1],
  62. [{op: 'replace', path: '/0', value: 1}, {op: 'replace', path: '/1', value: 1}]);
  63. // [{op: 'replace', path: '/', value: [1, 1]}]
  64. // In this example, the array element at index 0 is actually undefined, but
  65. // JSON.stringify({op:"add",path:"/0",value:undefined}) would result in
  66. // '{"op":"add","path":"/0"}' which is invalid, so we must cast such undefined
  67. // values to null.
  68. expect([], [,1], [{"op":"add","path":"/0","value":null},{"op":"add","path":"/1","value":1}]);
  69. if (failures) {
  70. console.log(failures + ' FAILURE(S)');
  71. process.exit(1);
  72. }