tests.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(includes.bind(null, [0], 0, thrower), RangeError, 'fromIndex conversion throws');
  20. st.end();
  21. });
  22. et.test('ToLength', function (st) {
  23. st.throws(includes.bind(null, { 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.test('number coercion', function (st) {
  38. st.equal(false, includes(['a', 'b', 'c'], 'a', numberish), 'does not find "a" with object fromIndex coercing to 2');
  39. st.equal(false, includes(['a', 'b', 'c'], 'a', '2'), 'does not find "a" with string fromIndex coercing to 2');
  40. st.equal(true, includes(['a', 'b', 'c'], 'c', numberish), 'finds "c" with object fromIndex coercing to 2');
  41. st.equal(true, includes(['a', 'b', 'c'], 'c', '2'), 'finds "c" with string fromIndex coercing to 2');
  42. st.end();
  43. });
  44. ft.test('fromIndex greater than length', function (st) {
  45. st.equal(false, includes([1], 1, 2), 'array of length 1 is not searched if fromIndex is > 1');
  46. st.equal(false, includes([1], 1, 1), 'array of length 1 is not searched if fromIndex is >= 1');
  47. st.equal(false, includes([1], 1, 1.1), 'array of length 1 is not searched if fromIndex is 1.1');
  48. st.equal(false, includes([1], 1, Infinity), 'array of length 1 is not searched if fromIndex is Infinity');
  49. st.end();
  50. });
  51. ft.test('negative fromIndex', function (st) {
  52. st.equal(true, includes([1, 3], 1, -4), 'computed length would be negative; fromIndex is thus 0');
  53. st.equal(true, includes([1, 3], 3, -4), 'computed length would be negative; fromIndex is thus 0');
  54. st.equal(true, includes([1, 3], 1, -Infinity), 'computed length would be negative; fromIndex is thus 0');
  55. st.equal(true, includes([12, 13], 13, -1), 'finds -1st item with -1 fromIndex');
  56. st.equal(false, includes([12, 13], 12, -1), 'does not find -2nd item with -1 fromIndex');
  57. st.equal(true, includes([12, 13], 13, -2), 'finds -2nd item with -2 fromIndex');
  58. st.equal(true, includes(sparseish, 'b', -4), 'finds -4th item with -4 fromIndex');
  59. st.equal(false, includes(sparseish, 'a', -4), 'does not find -5th item with -4 fromIndex');
  60. st.equal(true, includes(sparseish, 'a', -5), 'finds -5th item with -5 fromIndex');
  61. st.end();
  62. });
  63. ft.end();
  64. });
  65. };