Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.js
More file actions
Latest commit
95 lines (82 loc) · 2.45 KB
/
Copy pathtest.js
File metadata and controls
95 lines (82 loc) · 2.45 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
varexpect=require('expect.js');
vararrayDiff=require('./index');
varInsertDiff=arrayDiff.InsertDiff;
varRemoveDiff=arrayDiff.RemoveDiff;
varMoveDiff=arrayDiff.MoveDiff;
functioninsert(array,index,values){
array.splice.apply(array,[index,0].concat(values));
}
functionremove(array,index,howMany){
returnarray.splice(index,howMany);
}
functionmove(array,from,to,howMany){
varvalues=remove(array,from,howMany);
insert(array,to,values);
}
functionapplyDiff(before,diff){
varout=before.slice();
for(vari=0;i<diff.length;i++){
varitem=diff[i];
// console.log 'applying:', out, item
if(iteminstanceofInsertDiff){
insert(out,item.index,item.values);
}elseif(iteminstanceofRemoveDiff){
remove(out,item.index,item.howMany);
}elseif(iteminstanceofMoveDiff){
move(out,item.from,item.to,item.howMany);
}
}
returnout;
}
functionrandomWhole(max){
returnMath.floor(Math.random()*(max+1));
}
functionrandomArray(maxLength,maxValues){
if(maxLength==null)maxLength=20;
if(maxValues==null)maxValues=maxLength;
varresults=[];
for(vari=randomWhole(maxLength);i--;){
results.push(randomWhole(maxValues));
}
returnresults;
}
functiontestDiff(before,after,equalFn){
// console.log()
// console.log 'before =', before
// console.log 'after =', after
vardiff=arrayDiff(before,after,equalFn);
varexpected=applyDiff(before,diff);
expect(expected).to.eql(after);
}
describe('arrayDiff',function(){
it('diffs empty arrays',function(){
testDiff([],[]);
testDiff([],[0,1,2]);
testDiff([0,1,2],[]);
});
it('supports custom equality comparisons',function(){
varbefore=[{id: 1},{id: 2}];
varafter=[{id: 1}];
testDiff(before,after,function(a,b){
returna.id===b.id;
});
});
it('diffs randomly rearranged arrays of numbers',function(){
functionrandomSort(){
returnMath.random()-0.5;
}
for(vari=1000;i--;){
// before = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
varbefore=randomArray(50);
varafter=before.slice().sort(randomSort);
testDiff(before,after);
}
});
it('diffs random arrays of numbers',function(){
for(vari=1000;i--;){
varbefore=randomArray(50,20);
varafter=randomArray(50,20);
testDiff(before,after);
}
});
});