test.js 4.6 KB

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