addon-sdk/source/lib/diffpatcher/test/patch.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 "use strict";
michael@0 2
michael@0 3 var patch = require("../patch")
michael@0 4
michael@0 5 exports["test patch delete"] = function(assert) {
michael@0 6 var hash = { a: 1, b: 2 }
michael@0 7
michael@0 8 assert.deepEqual(patch(hash, { a: null }), { b: 2 }, "null removes property")
michael@0 9 }
michael@0 10
michael@0 11 exports["test patch delete with void"] = function(assert) {
michael@0 12 var hash = { a: 1, b: 2 }
michael@0 13
michael@0 14 assert.deepEqual(patch(hash, { a: void(0) }), { b: 2 },
michael@0 15 "void(0) removes property")
michael@0 16 }
michael@0 17
michael@0 18 exports["test patch delete missing"] = function(assert) {
michael@0 19 assert.deepEqual(patch({ a: 1, b: 2 }, { c: null }),
michael@0 20 { a: 1, b: 2 },
michael@0 21 "null removes property if exists");
michael@0 22
michael@0 23 assert.deepEqual(patch({ a: 1, b: 2 }, { c: void(0) }),
michael@0 24 { a: 1, b: 2 },
michael@0 25 "void removes property if exists");
michael@0 26 }
michael@0 27
michael@0 28 exports["test delete deleted"] = function(assert) {
michael@0 29 assert.deepEqual(patch({ a: null, b: 2, c: 3, d: void(0)},
michael@0 30 { a: void(0), b: null, d: null }),
michael@0 31 {c: 3},
michael@0 32 "removed all existing and non existing");
michael@0 33 }
michael@0 34
michael@0 35 exports["test update deleted"] = function(assert) {
michael@0 36 assert.deepEqual(patch({ a: null, b: void(0), c: 3},
michael@0 37 { a: { b: 2 } }),
michael@0 38 { a: { b: 2 }, c: 3 },
michael@0 39 "replace deleted");
michael@0 40 }
michael@0 41
michael@0 42 exports["test patch delete with void"] = function(assert) {
michael@0 43 var hash = { a: 1, b: 2 }
michael@0 44
michael@0 45 assert.deepEqual(patch(hash, { a: void(0) }), { b: 2 },
michael@0 46 "void(0) removes property")
michael@0 47 }
michael@0 48
michael@0 49
michael@0 50 exports["test patch addition"] = function(assert) {
michael@0 51 var hash = { a: 1, b: 2 }
michael@0 52
michael@0 53 assert.deepEqual(patch(hash, { c: 3 }), { a: 1, b: 2, c: 3 },
michael@0 54 "new properties are added")
michael@0 55 }
michael@0 56
michael@0 57 exports["test patch addition"] = function(assert) {
michael@0 58 var hash = { a: 1, b: 2 }
michael@0 59
michael@0 60 assert.deepEqual(patch(hash, { c: 3 }), { a: 1, b: 2, c: 3 },
michael@0 61 "new properties are added")
michael@0 62 }
michael@0 63
michael@0 64 exports["test hash on itself"] = function(assert) {
michael@0 65 var hash = { a: 1, b: 2 }
michael@0 66
michael@0 67 assert.deepEqual(patch(hash, hash), hash,
michael@0 68 "applying hash to itself returns hash itself")
michael@0 69 }
michael@0 70
michael@0 71 exports["test patch with empty delta"] = function(assert) {
michael@0 72 var hash = { a: 1, b: 2 }
michael@0 73
michael@0 74 assert.deepEqual(patch(hash, {}), hash,
michael@0 75 "applying empty delta results in no changes")
michael@0 76 }
michael@0 77
michael@0 78 exports["test patch nested data"] = function(assert) {
michael@0 79 assert.deepEqual(patch({ a: { b: 1 }, c: { d: 2 } },
michael@0 80 { a: { b: null, e: 3 }, c: { d: 4 } }),
michael@0 81 { a: { e: 3 }, c: { d: 4 } },
michael@0 82 "nested structures can also be patched")
michael@0 83 }

mercurial