foreach.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var test = require('tape');
  2. var forEach = require('../foreach.js');
  3. test('second argument: iterator', function (t) {
  4. var arr = [];
  5. t.throws(function () { forEach(arr); }, TypeError, 'undefined is not a function');
  6. t.throws(function () { forEach(arr, null); }, TypeError, 'null is not a function');
  7. t.throws(function () { forEach(arr, ''); }, TypeError, 'string is not a function');
  8. t.throws(function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
  9. t.throws(function () { forEach(arr, true); }, TypeError, 'true is not a function');
  10. t.throws(function () { forEach(arr, false); }, TypeError, 'false is not a function');
  11. t.throws(function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
  12. t.throws(function () { forEach(arr, 42); }, TypeError, '42 is not a function');
  13. t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
  14. t.doesNotThrow(function () { forEach(arr, setTimeout); }, 'setTimeout is a function');
  15. if (typeof window !== 'undefined') {
  16. t.doesNotThrow(function () { forEach(arr, window.alert); }, 'alert is a function');
  17. }
  18. t.end();
  19. });
  20. test('array', function (t) {
  21. var arr = [1, 2, 3];
  22. t.test('iterates over every item', function (st) {
  23. var index = 0;
  24. forEach(arr, function () { index += 1; });
  25. st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
  26. st.end();
  27. });
  28. t.test('first iterator argument', function (st) {
  29. var index = 0;
  30. st.plan(arr.length);
  31. forEach(arr, function (item) {
  32. st.equal(arr[index], item, 'item ' + index + ' is passed as first argument');
  33. index += 1;
  34. });
  35. st.end();
  36. });
  37. t.test('second iterator argument', function (st) {
  38. var counter = 0;
  39. st.plan(arr.length);
  40. forEach(arr, function (item, index) {
  41. st.equal(counter, index, 'index ' + index + ' is passed as second argument');
  42. counter += 1;
  43. });
  44. st.end();
  45. });
  46. t.test('third iterator argument', function (st) {
  47. st.plan(arr.length);
  48. forEach(arr, function (item, index, array) {
  49. st.deepEqual(arr, array, 'array is passed as third argument');
  50. });
  51. st.end();
  52. });
  53. t.test('context argument', function (st) {
  54. var context = {};
  55. st.plan(arr.length);
  56. forEach(arr, function () {
  57. st.equal(this, context, '"this" is the passed context');
  58. }, context);
  59. st.end();
  60. });
  61. t.end();
  62. });
  63. test('object', function (t) {
  64. var obj = {
  65. a: 1,
  66. b: 2,
  67. c: 3
  68. };
  69. var keys = ['a', 'b', 'c'];
  70. var F = function () {
  71. this.a = 1;
  72. this.b = 2;
  73. };
  74. F.prototype.c = 3;
  75. var fKeys = ['a', 'b'];
  76. t.test('iterates over every object literal key', function (st) {
  77. var counter = 0;
  78. forEach(obj, function () { counter += 1; });
  79. st.equal(counter, keys.length, 'iterated ' + counter + ' times');
  80. st.end();
  81. });
  82. t.test('iterates only over own keys', function (st) {
  83. var counter = 0;
  84. forEach(new F(), function () { counter += 1; });
  85. st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
  86. st.end();
  87. });
  88. t.test('first iterator argument', function (st) {
  89. var index = 0;
  90. st.plan(keys.length);
  91. forEach(obj, function (item) {
  92. st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
  93. index += 1;
  94. });
  95. st.end();
  96. });
  97. t.test('second iterator argument', function (st) {
  98. var counter = 0;
  99. st.plan(keys.length);
  100. forEach(obj, function (item, key) {
  101. st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
  102. counter += 1;
  103. });
  104. st.end();
  105. });
  106. t.test('third iterator argument', function (st) {
  107. st.plan(keys.length);
  108. forEach(obj, function (item, key, object) {
  109. st.deepEqual(obj, object, 'object is passed as third argument');
  110. });
  111. st.end();
  112. });
  113. t.test('context argument', function (st) {
  114. var context = {};
  115. st.plan(1);
  116. forEach({foo: 'bar'}, function () {
  117. st.equal(this, context, '"this" is the passed context');
  118. }, context);
  119. st.end();
  120. });
  121. t.end();
  122. });
  123. test('string', function (t) {
  124. var str = 'str';
  125. t.test('second iterator argument', function (st) {
  126. var counter = 0;
  127. st.plan(str.length * 2 + 1);
  128. forEach(str, function (item, index) {
  129. st.equal(counter, index, 'index ' + index + ' is passed as second argument');
  130. st.equal(str.charAt(index), item);
  131. counter += 1;
  132. });
  133. st.equal(counter, str.length, 'iterates ' + str.length + ' times');
  134. });
  135. t.end();
  136. });