1 /*global require:true, setTimeout:true */
2 var forEach = require('../lib/foreach').forEach;
5 setUp: function(done) {
7 this.track = function() {
8 [].push.apply(this.order, arguments);
12 'Synchronous': function(test) {
16 var arr = ["a", "b", "c"];
17 forEach(arr, function(item, index, arr) {
18 that.track("each", item, index, arr);
21 test.deepEqual(that.order, [
25 ], "should call eachFn for each array item, in order.");
28 'Synchronous, done': function(test) {
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);
39 test.deepEqual(that.order, [
44 ], "should call eachFn for each array item, in order, followed by doneFn.");
47 'Synchronous, early abort': function(test) {
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);
59 test.deepEqual(that.order, [
63 ], "should call eachFn for each array item, in order, followed by doneFn.");
66 'Asynchronous': function(test) {
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();
77 setTimeout(function() {
78 test.deepEqual(that.order, [
82 ], "should call eachFn for each array item, in order.");
86 'Asynchronous, done': function(test) {
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();
95 }, function(notAborted, arr) {
96 that.track("done", notAborted, arr);
97 test.deepEqual(that.order, [
102 ], "should call eachFn for each array item, in order, followed by doneFn.");
106 'Asynchronous, early abort': function(test) {
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() {
117 }, function(notAborted, arr) {
118 that.track("done", notAborted, arr);
119 test.deepEqual(that.order, [
123 ], "should call eachFn for each array item, in order, followed by doneFn.");
127 'Not actually asynchronous': function(test) {
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();
136 }, function(notAborted, arr) {
137 that.track("done", notAborted, arr);
138 test.deepEqual(that.order, [
143 ], "should call eachFn for each array item, in order, followed by doneFn.");
147 'Not actually asynchronous, early abort': function(test) {
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();
156 }, function(notAborted, arr) {
157 that.track("done", notAborted, arr);
158 test.deepEqual(that.order, [
162 ], "should call eachFn for each array item, in order, followed by doneFn.");
166 'Sparse array support': function(test) {
174 forEach(arr, function(item, index, arr) {
175 that.track("each", item, index, arr);
178 test.deepEqual(that.order, [
181 ], "should skip nonexistent array items.");
184 'Invalid length sanitization': function(test) {
188 var obj = {length: 4294967299, 0: "a", 2: "b", 3: "c" };
190 forEach(obj, function(item, index, arr) {
191 that.track("each", item, index, arr);
194 test.deepEqual(that.order, [
197 ], "should sanitize length property (ToUint32).");