tests.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. module.exports = function (includes, t) {
  3. var sparseish = { length: 5, 0: 'a', 1: 'b' };
  4. var overfullarrayish = { length: 2, 0: 'a', 1: 'b', 2: 'c' };
  5. var thrower = { valueOf: function () { throw new RangeError('whoa'); } };
  6. var numberish = { valueOf: function () { return 2; } };
  7. t.test('simple examples', function (st) {
  8. st.equal(true, includes([1, 2, 3], 1), '[1, 2, 3] includes 1');
  9. st.equal(false, includes([1, 2, 3], 4), '[1, 2, 3] does not include 4');
  10. st.equal(true, includes([NaN], NaN), '[NaN] includes NaN');
  11. st.end();
  12. });
  13. t.test('does not skip holes', function (st) {
  14. st.equal(true, includes(Array(1)), 'Array(1) includes undefined');
  15. st.end();
  16. });
  17. t.test('exceptions', function (et) {
  18. et.test('fromIndex conversion', function (st) {
  19. st['throws'](function () { includes([0], 0, thrower); }, RangeError, 'fromIndex conversion throws');
  20. st.end();
  21. });
  22. et.test('ToLength', function (st) {
  23. st['throws'](function () { includes({ length: thrower, 0: true }, true); }, RangeError, 'ToLength conversion throws');
  24. st.end();
  25. });
  26. et.end();
  27. });
  28. t.test('arraylike', function (st) {
  29. st.equal(true, includes(sparseish, 'a'), 'sparse array-like object includes "a"');
  30. st.equal(false, includes(sparseish, 'c'), 'sparse array-like object does not include "c"');
  31. st.equal(true, includes(overfullarrayish, 'b'), 'sparse array-like object includes "b"');
  32. st.equal(false, includes(overfullarrayish, 'c'), 'sparse array-like object does not include "c"');
  33. st.end();
  34. });
  35. t.test('fromIndex', function (ft) {
  36. ft.equal(true, includes([1], 1, NaN), 'NaN fromIndex -> 0 fromIndex');
  37. ft.equal(true, includes([0, 1, 2], 1, 0), 'starting from 0 finds index 1');
  38. ft.equal(true, includes([0, 1, 2], 1, 1), 'starting from 1 finds index 1');
  39. ft.equal(false, includes([0, 1, 2], 1, 2), 'starting from 2 does not find index 1');
  40. ft.test('number coercion', function (st) {
  41. st.equal(false, includes(['a', 'b', 'c'], 'a', numberish), 'does not find "a" with object fromIndex coercing to 2');
  42. st.equal(false, includes(['a', 'b', 'c'], 'a', '2'), 'does not find "a" with string fromIndex coercing to 2');
  43. st.equal(true, includes(['a', 'b', 'c'], 'c', numberish), 'finds "c" with object fromIndex coercing to 2');
  44. st.equal(true, includes(['a', 'b', 'c'], 'c', '2'), 'finds "c" with string fromIndex coercing to 2');
  45. st.end();
  46. });
  47. ft.test('fromIndex greater than length', function (st) {
  48. st.equal(false, includes([1], 1, 2), 'array of length 1 is not searched if fromIndex is > 1');
  49. st.equal(false, includes([1], 1, 1), 'array of length 1 is not searched if fromIndex is >= 1');
  50. st.equal(false, includes([1], 1, 1.1), 'array of length 1 is not searched if fromIndex is 1.1');
  51. st.equal(false, includes([1], 1, Infinity), 'array of length 1 is not searched if fromIndex is Infinity');
  52. st.end();
  53. });
  54. ft.test('negative fromIndex', function (st) {
  55. st.equal(true, includes([1, 3], 1, -4), 'computed length would be negative; fromIndex is thus 0');
  56. st.equal(true, includes([1, 3], 3, -4), 'computed length would be negative; fromIndex is thus 0');
  57. st.equal(true, includes([1, 3], 1, -Infinity), 'computed length would be negative; fromIndex is thus 0');
  58. st.equal(true, includes([12, 13], 13, -1), 'finds -1st item with -1 fromIndex');
  59. st.equal(false, includes([12, 13], 12, -1), 'does not find -2nd item with -1 fromIndex');
  60. st.equal(true, includes([12, 13], 13, -2), 'finds -2nd item with -2 fromIndex');
  61. st.equal(true, includes(sparseish, 'b', -4), 'finds -4th item with -4 fromIndex');
  62. st.equal(false, includes(sparseish, 'a', -4), 'does not find -5th item with -4 fromIndex');
  63. st.equal(true, includes(sparseish, 'a', -5), 'finds -5th item with -5 fromIndex');
  64. st.end();
  65. });
  66. ft.end();
  67. });
  68. t.test('strings', function (st) {
  69. st.equal(true, includes('abc', 'c'), 'string includes one of its chars');
  70. st.equal(false, includes('abc', 'd'), 'string does not include a char it should not');
  71. st.equal(true, includes(Object('abc'), 'c'), 'boxed string includes one of its chars');
  72. st.equal(false, includes(Object('abc'), 'd'), 'boxed string does not include a char it should not');
  73. st.end();
  74. });
  75. };