foreach_test.js
5.38 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*global require:true, setTimeout:true */
var forEach = require('../lib/foreach').forEach;
exports['foreach'] = {
setUp: function(done) {
this.order = [];
this.track = function() {
[].push.apply(this.order, arguments);
};
done();
},
'Synchronous': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
});
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"each", "c", 2, arr
], "should call eachFn for each array item, in order.");
test.done();
},
'Synchronous, done': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
});
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"each", "c", 2, arr,
"done", true, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
},
'Synchronous, early abort': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
if (item === "b") { return false; }
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
});
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"done", false, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
},
'Asynchronous': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
var done = this.async();
setTimeout(done, 10);
});
setTimeout(function() {
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"each", "c", 2, arr
], "should call eachFn for each array item, in order.");
test.done();
}, 100);
},
'Asynchronous, done': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
var done = this.async();
setTimeout(done, 10);
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"each", "c", 2, arr,
"done", true, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
});
},
'Asynchronous, early abort': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
var done = this.async();
setTimeout(function() {
done(item !== "b");
}, 10);
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"done", false, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
});
},
'Not actually asynchronous': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
var done = this.async();
done();
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"each", "c", 2, arr,
"done", true, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
});
},
'Not actually asynchronous, early abort': function(test) {
test.expect(1);
var that = this;
var arr = ["a", "b", "c"];
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
var done = this.async();
done(item !== "b");
}, function(notAborted, arr) {
that.track("done", notAborted, arr);
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "b", 1, arr,
"done", false, arr
], "should call eachFn for each array item, in order, followed by doneFn.");
test.done();
});
},
'Sparse array support': function(test) {
test.expect(1);
var that = this;
var arr = [];
arr[0] = "a";
arr[9] = "z";
forEach(arr, function(item, index, arr) {
that.track("each", item, index, arr);
});
test.deepEqual(that.order, [
"each", "a", 0, arr,
"each", "z", 9, arr
], "should skip nonexistent array items.");
test.done();
},
'Invalid length sanitization': function(test) {
test.expect(1);
var that = this;
var obj = {length: 4294967299, 0: "a", 2: "b", 3: "c" };
forEach(obj, function(item, index, arr) {
that.track("each", item, index, arr);
});
test.deepEqual(that.order, [
"each", "a", 0, obj,
"each", "b", 2, obj
], "should sanitize length property (ToUint32).");
test.done();
}
};