foreach_test.js 5.4 KB

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