summaryrefslogtreecommitdiff
path: root/toolkit/components/jetpack/diffpatcher/test/diff.js
blob: d1d67400556cf2ceeace8439bac97511d56d7390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";

var diff = require("../diff")

exports["test diff from null"] = function(assert) {
  var to = { a: 1, b: 2 }
  assert.equal(diff(null, to), to, "diff null to x returns x")
  assert.equal(diff(void(0), to), to, "diff undefined to x returns x")

}

exports["test diff to null"] = function(assert) {
  var from = { a: 1, b: 2 }
  assert.deepEqual(diff({ a: 1, b: 2 }, null),
                   { a: null, b: null },
                   "diff x null returns x with all properties nullified")
}

exports["test diff identical"] = function(assert) {
  assert.deepEqual(diff({}, {}), {}, "diff on empty objects is {}")

  assert.deepEqual(diff({ a: 1, b: 2 }, { a: 1, b: 2 }), {},
                   "if properties match diff is {}")

  assert.deepEqual(diff({ a: 1, b: { c: { d: 3, e: 4 } } },
                        { a: 1, b: { c: { d: 3, e: 4 } } }), {},
                   "diff between identical nested hashes is {}")

}

exports["test diff delete"] = function(assert) {
  assert.deepEqual(diff({ a: 1, b: 2 }, { b: 2 }), { a: null },
                   "missing property is deleted")
  assert.deepEqual(diff({ a: 1, b: 2 }, { a: 2 }), { a: 2, b: null },
                   "missing property is deleted another updated")
  assert.deepEqual(diff({ a: 1, b: 2 }, {}), { a: null, b: null },
                   "missing propertes are deleted")
  assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, {}),
                   { a: null, b: null },
                   "missing deep propertes are deleted")
  assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, { b: { c: {} } }),
                   { a: null, b: { c: { d: null } } },
                   "missing nested propertes are deleted")
}

exports["test add update"] = function(assert) {
  assert.deepEqual(diff({ a: 1, b: 2 }, { b: 2, c: 3 }), { a: null, c: 3 },
                   "delete and add")
  assert.deepEqual(diff({ a: 1, b: 2 }, { a: 2, c: 3 }), { a: 2, b: null, c: 3 },
                   "delete and adds")
  assert.deepEqual(diff({}, { a: 1, b: 2 }), { a: 1, b: 2 },
                   "diff on empty objcet returns equivalen of to")
  assert.deepEqual(diff({ a: 1, b: { c: { d: 2 } } }, { d: 3 }),
                   { a: null, b: null, d: 3 },
                   "missing deep propertes are deleted")
  assert.deepEqual(diff({ b: { c: {} }, d: null }, { a: 1, b: { c: { d: 2 } } }),
                   { a: 1, b: { c: { d: 2 } } },
                   "missing nested propertes are deleted")
}