Skip to content

Commit def5864

Browse files
currankbadk
andauthored
Add iterative tests (#22)
Co-authored-by: Kristian Antonsen <[email protected]>
1 parent 39baafd commit def5864

File tree

2 files changed

+125
-73
lines changed

2 files changed

+125
-73
lines changed

test/index.test.js

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ describe("Jsondiff", function() {
8989
]
9090
}
9191
];
92-
tests.forEach(test => {
93-
it(test.name, function() {
94-
let output = jsondiff(test.start, test.end);
95-
expect(output).to.deep.have.same.members(test.expectedCommand);
96-
});
97-
});
92+
runTests(tests);
9893
});
9994
describe("List Replace (oi + od)", function() {
10095
let tests = [
@@ -103,15 +98,9 @@ describe("Jsondiff", function() {
10398
start: ["one", "two"],
10499
end: ["one", "three", "two"],
105100
expectedCommand: [
106-
{
107-
p: [1],
108-
ld: "two",
109-
li: "three"
110-
},
111-
{
112-
p: [2],
113-
li: "two"
114-
}
101+
{ sd: 'wo', p: [ 1, 1 ] },
102+
{ si: 'hree', p: [ 1, 1 ] },
103+
{ p: [ 2 ], li: 'two' }
115104
]
116105
},
117106
{
@@ -147,12 +136,7 @@ describe("Jsondiff", function() {
147136
]
148137
}
149138
];
150-
tests.forEach(test => {
151-
it(test.name, function() {
152-
let output = jsondiff(test.start, test.end);
153-
expect(output).to.deep.have.same.members(test.expectedCommand);
154-
});
155-
});
139+
runTests(tests);
156140
});
157141
describe("Object Insert (oi)", function() {
158142
let tests = [
@@ -190,12 +174,7 @@ describe("Jsondiff", function() {
190174
]
191175
}
192176
];
193-
tests.forEach(test => {
194-
it(test.name, function() {
195-
let output = jsondiff(test.start, test.end);
196-
expect(output).to.deep.equal(test.expectedCommand);
197-
});
198-
});
177+
runTests(tests);
199178
});
200179
describe("Object Replace (oi + od)", function() {
201180
let tests = [
@@ -204,11 +183,8 @@ describe("Jsondiff", function() {
204183
start: { one: "one" },
205184
end: { one: "two" },
206185
expectedCommand: [
207-
{
208-
p: ["one"],
209-
oi: "two",
210-
od: "one"
211-
}
186+
{ sd: 'one', p: [ 'one', 0 ] },
187+
{ si: 'two', p: [ 'one', 0 ] }
212188
]
213189
},
214190
{
@@ -236,12 +212,7 @@ describe("Jsondiff", function() {
236212
]
237213
}
238214
];
239-
tests.forEach(test => {
240-
it(test.name, function() {
241-
let output = jsondiff(test.start, test.end);
242-
expect(output).to.deep.equal(test.expectedCommand);
243-
});
244-
});
215+
runTests(tests);
245216
});
246217
describe("String Mutation (si + sd)", function() {
247218
// These test cases come from diff-match-patch tests.
@@ -298,6 +269,15 @@ describe("Jsondiff", function() {
298269
{ si: 'xyz', p: [ 'one', 0 ] }
299270
]
300271
},
272+
{
273+
name: "Strings suffix/prefix overlap, overlap case",
274+
start: { one: "123456xxx" },
275+
end: { one: "xxxabcd" },
276+
expectedCommand: [
277+
{ "p": [ "one", 0 ], "sd": "123456xxx" },
278+
{ "p": [ "one", 0 ], "si": "xxxabcd" }
279+
]
280+
},
301281
{
302282
name: "Example from README",
303283
start: ["foo", "The only change here is at the end.", 1, 2, 3],
@@ -308,39 +288,49 @@ describe("Jsondiff", function() {
308288
]
309289
}
310290
];
311-
tests.forEach(test => {
312-
it(test.name, function() {
291+
runTests(tests);
292+
});
293+
});
294+
});
295+
296+
function runTests(tests) {
297+
tests.forEach(test => {
298+
it(test.name, function() {
299+
300+
//////////////////
301+
// Verify JSON0 //
302+
//////////////////
303+
let json0Op = jsondiff(test.start, test.end, diffMatchPatch);
304+
expect(json0Op).to.deep.equal(test.expectedCommand);
313305

314-
//////////////////
315-
// Verify JSON0 //
316-
//////////////////
317-
let json0Op = jsondiff(test.start, test.end, diffMatchPatch);
318-
expect(json0Op).to.deep.equal(test.expectedCommand);
306+
// Test actual application of the expected ops.
307+
// Clone the input, because json0 mutates the input to `apply`.
308+
let json0Start = clone(test.start);
309+
let json0End = json0.type.apply(json0Start, json0Op);
310+
expect(json0End).to.deep.equal(test.end);
319311

320-
// Test actual application of the expected ops.
321-
// Clone the input, because json0 mutates the input to `apply`.
322-
let json0Start = clone(test.start);
323-
let json0End = json0.type.apply(json0Start, json0Op);
324-
expect(json0End).to.deep.equal(test.end);
312+
// Test application of ops generated _without_ diffMatchPatch.
313+
let json0SimpleOp = jsondiff(test.start, test.end);
314+
let json0SimpleStart = clone(test.start);
315+
let json0SimpleEnd = json0.type.apply(json0SimpleStart, json0SimpleOp);
316+
expect(json0SimpleEnd).to.deep.equal(test.end);
325317

326318

327-
//////////////////
328-
// Verify JSON1 //
329-
//////////////////
330-
let json1Op = jsondiff(
331-
test.start,
332-
test.end,
333-
diffMatchPatch,
334-
json1,
335-
textUnicode
336-
);
319+
//////////////////
320+
// Verify JSON1 //
321+
//////////////////
322+
let json1Op = jsondiff(
323+
test.start,
324+
test.end,
325+
diffMatchPatch,
326+
json1,
327+
textUnicode
328+
);
337329

338-
// Test actual application of the expected ops.
339-
// No need to clone the input, json1 does _not_ mutate the input to `apply`.
340-
let json1End = json1.type.apply(test.start, json1Op);
341-
expect(json1End).to.deep.equal(test.end);
342-
});
343-
});
330+
// Test actual application of the expected ops.
331+
// No need to clone the input, json1 does _not_ mutate the input to `apply`.
332+
let json1End = json1.type.apply(test.start, json1Op);
333+
expect(json1End).to.deep.equal(test.end);
344334
});
345335
});
346-
});
336+
}

tests.js

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@ let textUnicode = require("ot-text-unicode");
1010
var jsondiff = require("./index.js");
1111

1212
var tests = [
13-
//tests of li/ld
13+
// Tests of equality
14+
[
15+
5,
16+
5,
17+
],
18+
[
19+
"foo",
20+
"foo",
21+
],
22+
[
23+
["foo"],
24+
["foo"],
25+
],
26+
[
27+
[],
28+
[],
29+
],
30+
[
31+
{},
32+
{},
33+
],
34+
// Tests of li/ld
1435
[
1536
[],
1637
["foo"]
@@ -43,7 +64,7 @@ var tests = [
4364
["foo", "bar", "quux"],
4465
["bar", "quux"]
4566
],
46-
// tests for object/array
67+
// Tests for object/array
4768
[
4869
{},
4970
[]
@@ -52,7 +73,7 @@ var tests = [
5273
[],
5374
{}
5475
],
55-
// tests for oi/od
76+
// Tests for oi/od
5677
[
5778
{},
5879
{"foo":"bar"}
@@ -65,7 +86,44 @@ var tests = [
6586
[ { foo: 'bar' } ],
6687
[ {} ]
6788
],
68-
// big tests
89+
// String tests
90+
// Inspired by https://github.com/google/diff-match-patch/blob/master/javascript/tests/diff_match_patch_test.js
91+
["abc", "xyz"],
92+
["1234abcdef", "1234xyz"],
93+
["1234", "1234xyz"],
94+
["abc", "xyz"],
95+
["abcdef1234", "xyz1234"],
96+
["1234", "xyz1234"],
97+
["", "abcd"],
98+
["abc", "abcd"],
99+
["123456", "abcd"],
100+
["123456xxx", "xxxabcd"],
101+
["fi", "\ufb01i"],
102+
["1234567890", "abcdef"],
103+
["12345", "23"],
104+
["1234567890", "a345678z"],
105+
["a345678z", "1234567890"],
106+
["abc56789z", "1234567890"],
107+
["a23456xyz", "1234567890"],
108+
["121231234123451234123121", "a1234123451234z"],
109+
["x-=-=-=-=-=-=-=-=-=-=-=-=", "xx-=-=-=-=-=-=-="],
110+
["-=-=-=-=-=-=-=-=-=-=-=-=y", "-=-=-=-=-=-=-=yy"],
111+
["qHilloHelloHew", "xHelloHeHulloy"],
112+
["abcdefghijk", "fgh"],
113+
["abcdefghijk", "efxhi"],
114+
["abcdefghijk", "cdefxyhijk"],
115+
["abcdefghijk", "bxy"],
116+
["123456789xx0", "3456789x0"],
117+
["abcdefghijk", "efxyhi"],
118+
["abcdefghijk", "bcdef"],
119+
["abcdexyzabcde", "abccde"],
120+
["abcdefghijklmnopqrstuvwxyz01234567890", "XabXcdXefXghXijXklXmnXopXqrXstXuvXwxXyzX01X23X45X67X89X0"],
121+
["abcdef1234567890123456789012345678901234567890123456789012345678901234567890uvwxyz", "abcdefuvwxyz"],
122+
["1234567890123456789012345678901234567890123456789012345678901234567890", "abc"],
123+
["XY", "XtestY"],
124+
["XXXXYYYY", "XXXXtestYYYY"],
125+
["The quick brown fox jumps over the lazy dog.", "Woof"],
126+
// Big tests
69127
[
70128
[],
71129
["the", {"quick":"brown", "fox":"jumped"}, "over", {"the": ["lazy", "dog"]}]
@@ -78,7 +136,7 @@ var tests = [
78136
[["the", {"quick":"black", "fox":"jumped"}, "over", {"the": ["lazy", "dog"]}]],
79137
["the", {"quick":"brown", "fox":"leapt"}, "over", {"the": ["stupid", "dog"]}]
80138
],
81-
// real-life jsonml tests
139+
// Real-life jsonml tests
82140
[
83141
[ 'html', {}, [ 'body', {}, '\n\n', '\n\n', [ 'p', {}, 'Quux!' ] ], '\n' ],
84142
[ 'html', {}, [ 'body', {}, '\n\n', '\n\n\n\n', [ 'p', {}, 'Quux!' ] ], '\n' ]
@@ -140,6 +198,10 @@ tests.forEach(function([input, output]) {
140198
// Actual tests for json0
141199
tests.forEach(function([input, output]) {
142200
var ops = jsondiff(input, output);
201+
202+
// Don't let json0 mutate the input,
203+
// so we can use it in later tests.
204+
input = clone(input);
143205
ops.forEach(function(op) {
144206
assert.doesNotThrow(function() {
145207
input = json0.type.apply(input, [op]);
@@ -157,7 +219,7 @@ tests.forEach(function([input, output]) {
157219
json1,
158220
textUnicode
159221
);
160-
assert(equal(input, json1.type.apply(input, ops)));
222+
assert(equal(json1.type.apply(input, ops), output));
161223
});
162224

163225
console.log("No errors!");

0 commit comments

Comments
 (0)