Skip to content

Commit 29a06be

Browse files
committed
Fix test op for whole document comparison. Handle null in deepEquals
1 parent b61fe77 commit 29a06be

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

lib/deepEquals.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function compareArrays(a, b) {
3737
}
3838

3939
function compareObjects(a, b) {
40+
if((a === null && b !== null) || (a !== null && b === null)) {
41+
return false;
42+
}
43+
4044
var akeys = Object.keys(a);
4145
var bkeys = Object.keys(b);
4246

lib/patches.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ exports.copy = {
5555

5656
function applyTest(x, test) {
5757
var pointer = find(x, test.path);
58+
var target = pointer.key === void 0 ? pointer.target : pointer.target[pointer.key];
5859

59-
if(!deepEquals(pointer.target[pointer.key], test.value)) {
60+
if(!deepEquals(target, test.value)) {
6061
throw new TestFailedError('test failed ' + JSON.stringify(test));
6162
}
6263

test/jsonPatch-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var refute = buster.referee.refute;
55
var patches = require('../lib/patches');
66
var jsonPatch = require('../lib/jsonPatch');
77
var InvalidPatchOperationError = require('../lib/InvalidPatchOperationError');
8+
var TestFailedError = require('../lib/TestFailedError');
89

910
buster.testCase('jsonPatch', {
1011
'add': {
@@ -161,6 +162,40 @@ buster.testCase('jsonPatch', {
161162
assert.exception(function() {
162163
jsonPatch.apply([{ op: 'test', path: '/x', value: y }], a);
163164
}, 'TestFailedError');
165+
},
166+
167+
'should test whole document': {
168+
'when document and value are not null': function() {
169+
var doc = { a: { b: 123 } };
170+
refute.exception(function() {
171+
jsonPatch.apply([{ op: 'test', path: '', value: doc }], doc);
172+
});
173+
},
174+
175+
'when document and value are null': function() {
176+
refute.exception(function() {
177+
jsonPatch.apply([{ op: 'test', path: '', value: null }], null);
178+
});
179+
},
180+
181+
'when value is null': function() {
182+
var doc = { a: { b: 123 } };
183+
assert.exception(function() {
184+
jsonPatch.apply([{ op: 'test', path: '', value: doc }], null);
185+
}, function(e) {
186+
return e instanceof TestFailedError;
187+
});
188+
},
189+
190+
'when document is null': function() {
191+
var doc = { a: { b: 123 } };
192+
assert.exception(function() {
193+
jsonPatch.apply([{ op: 'test', path: '', value: null }], doc);
194+
}, function(e) {
195+
return e instanceof TestFailedError;
196+
});
197+
}
164198
}
199+
165200
}
166201
});

0 commit comments

Comments
 (0)