-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharraydiff.spec.js
More file actions
58 lines (50 loc) · 1.69 KB
/
arraydiff.spec.js
File metadata and controls
58 lines (50 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var expect = require('unexpected').clone()
.use(require('unexpected-check'));
var arrayDiff = require('arraydiff-papandreou');
var generators = require('chance-generators');
describe('arraydiff', function () {
var g;
beforeEach(function () {
g = generators(42);
});
function insert(array, index, values) {
array.splice.apply(array, [index, 0].concat(values));
}
function remove(array, index, howMany) {
return array.splice(index, howMany);
}
function move(array, from, to, howMany) {
var values = remove(array, from, howMany);
insert(array, to, values);
}
function executePlan(before, diff) {
var out = before.slice();
for (var i = 0; i < diff.length; i++) {
var item = diff[i];
if (item.type === 'insert') {
insert(out, item.index, item.values);
} else if (item.type === 'remove') {
remove(out, item.index, item.howMany);
} else if (item.type === 'move') {
move(out, item.from, item.to, item.howMany);
}
}
return out;
}
it('produces a valid plan', function () {
this.timeout(20000);
var arrays = g.array(g.natural({ max: 10 }), g.natural({ max: 10 }));
expect(function (actual, expected) {
var diff = arrayDiff(actual, expected, function (a, b) {
return a === b;
});
expect(
[actual, diff],
'when passed as parameters to',
executePlan,
'to equal',
expected
);
}, 'to be valid for all', arrays, arrays);
});
});