Skip to content

Commit 41fa75f

Browse files
committed
port tests to js
1 parent 09a0464 commit 41fa75f

File tree

3 files changed

+98
-69
lines changed

3 files changed

+98
-69
lines changed

mocha.opts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--reporter spec
2+
--bail
3+
--check-leaks

test.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var expect = require('expect.js');
2+
var arrayDiff = require('./index');
3+
var InsertDiff = arrayDiff.InsertDiff;
4+
var RemoveDiff = arrayDiff.RemoveDiff;
5+
var MoveDiff = arrayDiff.MoveDiff;
6+
7+
function insert(array, index, values) {
8+
array.splice.apply(array, [index, 0].concat(values));
9+
}
10+
11+
function remove(array, index, howMany) {
12+
return array.splice(index, howMany);
13+
}
14+
15+
function move(array, from, to, howMany) {
16+
var values = remove(array, from, howMany);
17+
insert(array, to, values);
18+
}
19+
20+
function applyDiff(before, diff) {
21+
var out = before.slice();
22+
for (var i = 0; i < diff.length; i++) {
23+
var item = diff[i];
24+
// console.log 'applying:', out, item
25+
if (item instanceof InsertDiff) {
26+
insert(out, item.index, item.values);
27+
} else if (item instanceof RemoveDiff) {
28+
remove(out, item.index, item.howMany);
29+
} else if (item instanceof MoveDiff) {
30+
move(out, item.from, item.to, item.howMany);
31+
}
32+
}
33+
return out;
34+
}
35+
36+
function randomWhole(max) {
37+
return Math.floor(Math.random() * (max + 1));
38+
}
39+
40+
function randomArray(maxLength, maxValues) {
41+
if (maxLength == null) maxLength = 20;
42+
if (maxValues == null) maxValues = maxLength;
43+
var results = [];
44+
for (var i = randomWhole(maxLength); i--;) {
45+
results.push(randomWhole(maxValues));
46+
}
47+
return results;
48+
}
49+
50+
function testDiff(before, after, equalFn) {
51+
// console.log()
52+
// console.log 'before =', before
53+
// console.log 'after =', after
54+
var diff = arrayDiff(before, after, equalFn);
55+
var expected = applyDiff(before, diff);
56+
expect(expected).to.eql(after);
57+
}
58+
59+
describe('arrayDiff', function() {
60+
61+
it('diffs empty arrays', function() {
62+
testDiff([], []);
63+
testDiff([], [0, 1, 2]);
64+
testDiff([0, 1, 2], []);
65+
});
66+
67+
it('supports custom equality comparisons', function() {
68+
var before = [{id: 1}, {id: 2}];
69+
var after = [{id: 1}];
70+
testDiff(before, after, function(a, b) {
71+
return a.id === b.id;
72+
});
73+
});
74+
75+
it('diffs randomly rearranged arrays of numbers', function() {
76+
function randomSort() {
77+
return Math.random() - 0.5;
78+
}
79+
for (var i = 1000; i--;) {
80+
// before = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
81+
var before = randomArray(50);
82+
var after = before.slice().sort(randomSort);
83+
testDiff(before, after);
84+
}
85+
});
86+
87+
it('diffs random arrays of numbers', function() {
88+
for (var i = 1000; i--;) {
89+
var before = randomArray(50, 20);
90+
var after = randomArray(50, 20);
91+
testDiff(before, after);
92+
}
93+
});
94+
95+
});

test/test.mocha.coffee

-69
This file was deleted.

0 commit comments

Comments
 (0)