forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
160 lines (148 loc) · 4.76 KB
/
tests.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Tinytest.add("diff-sequence - diff changes ordering", function (test) {
var makeDocs = function (ids) {
return _.map(ids, function (id) { return {_id: id};});
};
var testMutation = function (a, b) {
var aa = makeDocs(a);
var bb = makeDocs(b);
var aaCopy = EJSON.clone(aa);
DiffSequence.diffQueryOrderedChanges(aa, bb, {
addedBefore: function (id, doc, before) {
if (before === null) {
aaCopy.push( _.extend({_id: id}, doc));
return;
}
for (var i = 0; i < aaCopy.length; i++) {
if (aaCopy[i]._id === before) {
aaCopy.splice(i, 0, _.extend({_id: id}, doc));
return;
}
}
},
movedBefore: function (id, before) {
var found;
for (var i = 0; i < aaCopy.length; i++) {
if (aaCopy[i]._id === id) {
found = aaCopy[i];
aaCopy.splice(i, 1);
}
}
if (before === null) {
aaCopy.push( _.extend({_id: id}, found));
return;
}
for (i = 0; i < aaCopy.length; i++) {
if (aaCopy[i]._id === before) {
aaCopy.splice(i, 0, _.extend({_id: id}, found));
return;
}
}
},
removed: function (id) {
var found;
for (var i = 0; i < aaCopy.length; i++) {
if (aaCopy[i]._id === id) {
found = aaCopy[i];
aaCopy.splice(i, 1);
}
}
}
});
test.equal(aaCopy, bb);
};
var testBothWays = function (a, b) {
testMutation(a, b);
testMutation(b, a);
};
testBothWays(["a", "b", "c"], ["c", "b", "a"]);
testBothWays(["a", "b", "c"], []);
testBothWays(["a", "b", "c"], ["e","f"]);
testBothWays(["a", "b", "c", "d"], ["c", "b", "a"]);
testBothWays(['A','B','C','D','E','F','G','H','I'],
['A','B','F','G','C','D','I','L','M','N','H']);
testBothWays(['A','B','C','D','E','F','G','H','I'],['A','B','C','D','F','G','H','E','I']);
});
Tinytest.add("diff-sequence - diff", function (test) {
// test correctness
var diffTest = function(origLen, newOldIdx) {
var oldResults = new Array(origLen);
for (var i = 1; i <= origLen; i++)
oldResults[i-1] = {_id: i};
var newResults = _.map(newOldIdx, function(n) {
var doc = {_id: Math.abs(n)};
if (n < 0)
doc.changed = true;
return doc;
});
var find = function (arr, id) {
for (var i = 0; i < arr.length; i++) {
if (EJSON.equals(arr[i]._id, id))
return i;
}
return -1;
};
var results = _.clone(oldResults);
var observer = {
addedBefore: function(id, fields, before) {
var before_idx;
if (before === null)
before_idx = results.length;
else
before_idx = find (results, before);
var doc = _.extend({_id: id}, fields);
test.isFalse(before_idx < 0 || before_idx > results.length);
results.splice(before_idx, 0, doc);
},
removed: function(id) {
var at_idx = find (results, id);
test.isFalse(at_idx < 0 || at_idx >= results.length);
results.splice(at_idx, 1);
},
changed: function(id, fields) {
var at_idx = find (results, id);
var oldDoc = results[at_idx];
var doc = EJSON.clone(oldDoc);
DiffSequence.applyChanges(doc, fields);
test.isFalse(at_idx < 0 || at_idx >= results.length);
test.equal(doc._id, oldDoc._id);
results[at_idx] = doc;
},
movedBefore: function(id, before) {
var old_idx = find(results, id);
var new_idx;
if (before === null)
new_idx = results.length;
else
new_idx = find (results, before);
if (new_idx > old_idx)
new_idx--;
test.isFalse(old_idx < 0 || old_idx >= results.length);
test.isFalse(new_idx < 0 || new_idx >= results.length);
results.splice(new_idx, 0, results.splice(old_idx, 1)[0]);
}
};
DiffSequence.diffQueryOrderedChanges(oldResults, newResults, observer);
test.equal(results, newResults);
};
// edge cases and cases run into during debugging
diffTest(5, [5, 1, 2, 3, 4]);
diffTest(0, [1, 2, 3, 4]);
diffTest(4, []);
diffTest(7, [4, 5, 6, 7, 1, 2, 3]);
diffTest(7, [5, 6, 7, 1, 2, 3, 4]);
diffTest(10, [7, 4, 11, 6, 12, 1, 5]);
diffTest(3, [3, 2, 1]);
diffTest(10, [2, 7, 4, 6, 11, 3, 8, 9]);
diffTest(0, []);
diffTest(1, []);
diffTest(0, [1]);
diffTest(1, [1]);
diffTest(5, [1, 2, 3, 4, 5]);
// interaction between "changed" and other ops
diffTest(5, [-5, -1, 2, -3, 4]);
diffTest(7, [-4, -5, 6, 7, -1, 2, 3]);
diffTest(7, [5, 6, -7, 1, 2, -3, 4]);
diffTest(10, [7, -4, 11, 6, 12, -1, 5]);
diffTest(3, [-3, -2, -1]);
diffTest(10, [-2, 7, 4, 6, 11, -3, -8, 9]);
});