diff.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var diff = require("../diff")
  3. exports["test diff from null"] = function(assert) {
  4. var to = { a: 1, b: 2 }
  5. assert.equal(diff(null, to), to, "diff null to x returns x")
  6. assert.equal(diff(void(0), to), to, "diff undefined to x returns x")
  7. }
  8. exports["test diff to null"] = function(assert) {
  9. var from = { a: 1, b: 2 }
  10. assert.deepEqual(diff({ a: 1, b: 2 }, null),
  11. { a: null, b: null },
  12. "diff x null returns x with all properties nullified")
  13. }
  14. exports["test diff identical"] = function(assert) {
  15. assert.deepEqual(diff({}, {}), {}, "diff on empty objects is {}")
  16. assert.deepEqual(diff({ a: 1, b: 2 }, { a: 1, b: 2 }), {},
  17. "if properties match diff is {}")
  18. assert.deepEqual(diff({ a: 1, b: { c: { d: 3, e: 4 } } },
  19. { a: 1, b: { c: { d: 3, e: 4 } } }), {},
  20. "diff between identical nested hashes is {}")
  21. }
  22. exports["test diff delete"] = function(assert) {
  23. assert.deepEqual(diff({ a: 1, b: 2 }, { b: 2 }), { a: null },
  24. "missing property is deleted")
  25. assert.deepEqual(diff({ a: 1, b: 2 }, { a: 2 }), { a: 2, b: null },
  26. "missing property is deleted another updated")
  27. assert.deepEqual(diff({ a: 1, b: 2 }, {}), { a: null, b: null },
  28. "missing propertes are deleted")
  29. assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, {}),
  30. { a: null, b: null },
  31. "missing deep propertes are deleted")
  32. assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, { b: { c: {} } }),
  33. { a: null, b: { c: { d: null } } },
  34. "missing nested propertes are deleted")
  35. }
  36. exports["test add update"] = function(assert) {
  37. assert.deepEqual(diff({ a: 1, b: 2 }, { b: 2, c: 3 }), { a: null, c: 3 },
  38. "delete and add")
  39. assert.deepEqual(diff({ a: 1, b: 2 }, { a: 2, c: 3 }), { a: 2, b: null, c: 3 },
  40. "delete and adds")
  41. assert.deepEqual(diff({}, { a: 1, b: 2 }), { a: 1, b: 2 },
  42. "diff on empty objcet returns equivalen of to")
  43. assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, { d: 3 }),
  44. { a: null, b: null, d: 3 },
  45. "missing deep propertes are deleted")
  46. assert.deepEqual(diff({ b: { c: {} }, d: null }, { a: 1, b: { c: { d: 2 } } }),
  47. { a: 1, b: { c: { d: 2 } } },
  48. "missing nested propertes are deleted")
  49. }