1 |
|
2 | var forEach = require('../lib/foreach').forEach;
|
3 |
|
4 | exports['foreach'] = {
|
5 | setUp: function(done) {
|
6 | this.order = [];
|
7 | this.track = function() {
|
8 | [].push.apply(this.order, arguments);
|
9 | };
|
10 | done();
|
11 | },
|
12 | 'Synchronous': function(test) {
|
13 | test.expect(1);
|
14 | var that = this;
|
15 |
|
16 | var arr = ["a", "b", "c"];
|
17 | forEach(arr, function(item, index, arr) {
|
18 | that.track("each", item, index, arr);
|
19 | });
|
20 |
|
21 | test.deepEqual(that.order, [
|
22 | "each", "a", 0, arr,
|
23 | "each", "b", 1, arr,
|
24 | "each", "c", 2, arr
|
25 | ], "should call eachFn for each array item, in order.");
|
26 | test.done();
|
27 | },
|
28 | 'Synchronous, done': function(test) {
|
29 | test.expect(1);
|
30 | var that = this;
|
31 |
|
32 | var arr = ["a", "b", "c"];
|
33 | forEach(arr, function(item, index, arr) {
|
34 | that.track("each", item, index, arr);
|
35 | }, function(notAborted, arr) {
|
36 | that.track("done", notAborted, arr);
|
37 | });
|
38 |
|
39 | test.deepEqual(that.order, [
|
40 | "each", "a", 0, arr,
|
41 | "each", "b", 1, arr,
|
42 | "each", "c", 2, arr,
|
43 | "done", true, arr
|
44 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
45 | test.done();
|
46 | },
|
47 | 'Synchronous, early abort': function(test) {
|
48 | test.expect(1);
|
49 | var that = this;
|
50 |
|
51 | var arr = ["a", "b", "c"];
|
52 | forEach(arr, function(item, index, arr) {
|
53 | that.track("each", item, index, arr);
|
54 | if (item === "b") { return false; }
|
55 | }, function(notAborted, arr) {
|
56 | that.track("done", notAborted, arr);
|
57 | });
|
58 |
|
59 | test.deepEqual(that.order, [
|
60 | "each", "a", 0, arr,
|
61 | "each", "b", 1, arr,
|
62 | "done", false, arr
|
63 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
64 | test.done();
|
65 | },
|
66 | 'Asynchronous': function(test) {
|
67 | test.expect(1);
|
68 | var that = this;
|
69 |
|
70 | var arr = ["a", "b", "c"];
|
71 | forEach(arr, function(item, index, arr) {
|
72 | that.track("each", item, index, arr);
|
73 | var done = this.async();
|
74 | setTimeout(done, 10);
|
75 | });
|
76 |
|
77 | setTimeout(function() {
|
78 | test.deepEqual(that.order, [
|
79 | "each", "a", 0, arr,
|
80 | "each", "b", 1, arr,
|
81 | "each", "c", 2, arr
|
82 | ], "should call eachFn for each array item, in order.");
|
83 | test.done();
|
84 | }, 100);
|
85 | },
|
86 | 'Asynchronous, done': function(test) {
|
87 | test.expect(1);
|
88 | var that = this;
|
89 |
|
90 | var arr = ["a", "b", "c"];
|
91 | forEach(arr, function(item, index, arr) {
|
92 | that.track("each", item, index, arr);
|
93 | var done = this.async();
|
94 | setTimeout(done, 10);
|
95 | }, function(notAborted, arr) {
|
96 | that.track("done", notAborted, arr);
|
97 | test.deepEqual(that.order, [
|
98 | "each", "a", 0, arr,
|
99 | "each", "b", 1, arr,
|
100 | "each", "c", 2, arr,
|
101 | "done", true, arr
|
102 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
103 | test.done();
|
104 | });
|
105 | },
|
106 | 'Asynchronous, early abort': function(test) {
|
107 | test.expect(1);
|
108 | var that = this;
|
109 |
|
110 | var arr = ["a", "b", "c"];
|
111 | forEach(arr, function(item, index, arr) {
|
112 | that.track("each", item, index, arr);
|
113 | var done = this.async();
|
114 | setTimeout(function() {
|
115 | done(item !== "b");
|
116 | }, 10);
|
117 | }, function(notAborted, arr) {
|
118 | that.track("done", notAborted, arr);
|
119 | test.deepEqual(that.order, [
|
120 | "each", "a", 0, arr,
|
121 | "each", "b", 1, arr,
|
122 | "done", false, arr
|
123 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
124 | test.done();
|
125 | });
|
126 | },
|
127 | 'Not actually asynchronous': function(test) {
|
128 | test.expect(1);
|
129 | var that = this;
|
130 |
|
131 | var arr = ["a", "b", "c"];
|
132 | forEach(arr, function(item, index, arr) {
|
133 | that.track("each", item, index, arr);
|
134 | var done = this.async();
|
135 | done();
|
136 | }, function(notAborted, arr) {
|
137 | that.track("done", notAborted, arr);
|
138 | test.deepEqual(that.order, [
|
139 | "each", "a", 0, arr,
|
140 | "each", "b", 1, arr,
|
141 | "each", "c", 2, arr,
|
142 | "done", true, arr
|
143 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
144 | test.done();
|
145 | });
|
146 | },
|
147 | 'Not actually asynchronous, early abort': function(test) {
|
148 | test.expect(1);
|
149 | var that = this;
|
150 |
|
151 | var arr = ["a", "b", "c"];
|
152 | forEach(arr, function(item, index, arr) {
|
153 | that.track("each", item, index, arr);
|
154 | var done = this.async();
|
155 | done(item !== "b");
|
156 | }, function(notAborted, arr) {
|
157 | that.track("done", notAborted, arr);
|
158 | test.deepEqual(that.order, [
|
159 | "each", "a", 0, arr,
|
160 | "each", "b", 1, arr,
|
161 | "done", false, arr
|
162 | ], "should call eachFn for each array item, in order, followed by doneFn.");
|
163 | test.done();
|
164 | });
|
165 | },
|
166 | 'Sparse array support': function(test) {
|
167 | test.expect(1);
|
168 | var that = this;
|
169 |
|
170 | var arr = [];
|
171 | arr[0] = "a";
|
172 | arr[9] = "z";
|
173 |
|
174 | forEach(arr, function(item, index, arr) {
|
175 | that.track("each", item, index, arr);
|
176 | });
|
177 |
|
178 | test.deepEqual(that.order, [
|
179 | "each", "a", 0, arr,
|
180 | "each", "z", 9, arr
|
181 | ], "should skip nonexistent array items.");
|
182 | test.done();
|
183 | },
|
184 | 'Invalid length sanitization': function(test) {
|
185 | test.expect(1);
|
186 | var that = this;
|
187 |
|
188 | var obj = {length: 4294967299, 0: "a", 2: "b", 3: "c" };
|
189 |
|
190 | forEach(obj, function(item, index, arr) {
|
191 | that.track("each", item, index, arr);
|
192 | });
|
193 |
|
194 | test.deepEqual(that.order, [
|
195 | "each", "a", 0, obj,
|
196 | "each", "b", 2, obj
|
197 | ], "should sanitize length property (ToUint32).");
|
198 | test.done();
|
199 | }
|
200 | };
|