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