Skip to content

Commit 99111f0

Browse files
author
pkafel
committed
#12 Tests + small fixes
1 parent 9490413 commit 99111f0

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

json-diff.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ function getDiffRepresentation(left, right) {
175175
var leftParseResult = _parseJson(left);
176176
var rightParseResult = _parseJson(right);
177177

178-
if(leftParseResult["exception"] !== null || rightParseResult["exception"] !== null) throw new ValidationException(leftErrors, rightErrors);
178+
if(leftParseResult["exception"] !== null || rightParseResult["exception"] !== null)
179+
throw new ValidationException(leftParseResult["exception"], rightParseResult["exception"]);
179180

180181
var leftJson = leftParseResult["json"]; var rightJson = rightParseResult["json"];
181182
var leftJsonType = _getType(leftParseResult["json"]); var rightJsonType = _getType(rightParseResult["json"]);

test/spec/json-diff-spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,33 @@ describe("Get Json diff representation", function() {
193193
expect(result.diff[1].key).toEqual("a");
194194
expect(result.diff[1].op).toEqual(REMOVE);
195195
expect(result.diff[1].valueType).toEqual(SCALAR);
196-
})
196+
});
197+
198+
it("Diff should throw exception with correct error message when left json is invalid", function() {
199+
var call = function() {
200+
getDiffRepresentation("Oh, how I wish you were here now...", "{\"a\":\"1\"}");
201+
};
202+
expect(call).toThrow(new ValidationException("Input is not a valid JSON", null));
203+
});
204+
205+
it("Diff should throw exception with correct error message when right json is invalid", function() {
206+
var call = function() {
207+
getDiffRepresentation("{\"a\":\"1\"}", "... we were just two lost souls");
208+
};
209+
expect(call).toThrow(new ValidationException(null, "Input is not a valid JSON"));
210+
});
211+
212+
it("Diff should throw exception with correct error message when left and right json is invalid", function() {
213+
var call = function() {
214+
getDiffRepresentation("... swimming in a fish bowl...", "... year after year.");
215+
};
216+
expect(call).toThrow(new ValidationException("Input is not a valid JSON", "Input is not a valid JSON"));
217+
});
218+
219+
it("Diff should throw exception with correct error message when one of jsons is scalar", function() {
220+
var call = function() {
221+
getDiffRepresentation("11", "{}");
222+
};
223+
expect(call).toThrow(new ValidationException("Input is not a valid JSON", null));
224+
});
197225
});

0 commit comments

Comments
 (0)