Skip to content

Commit

Permalink
make some tests work in pre-es5 engines
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 11, 2023
1 parent 57dd86a commit 867540b
Show file tree
Hide file tree
Showing 53 changed files with 308 additions and 140 deletions.
6 changes: 2 additions & 4 deletions tests/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ try {
REDEFINABLE_PROTO = true;
} catch (error) { /* empty */ }

export const STRICT = !function () {
return this;
}();

export const STRICT_THIS = (function () {
return this;
})();

export const STRICT = !STRICT_THIS;

export const FREEZING = !function () {
try {
return Object.isExtensible(Object.preventExtensions({}));
Expand Down
7 changes: 5 additions & 2 deletions tests/unit-global/esnext.async-iterator.as-indexed-pairs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('AsyncIterator#asIndexedPairs', assert => {
Expand All @@ -9,8 +10,10 @@ QUnit.test('AsyncIterator#asIndexedPairs', assert => {
assert.looksNative(asIndexedPairs);
assert.nonEnumerable(AsyncIterator.prototype, 'asIndexedPairs');

assert.throws(() => asIndexedPairs.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => asIndexedPairs.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => asIndexedPairs.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => asIndexedPairs.call(null, () => { /* empty */ }), TypeError);
}

return asIndexedPairs.call(createIterator(['a', 'b', 'c'])).toArray().then(it => {
assert.same(it.toString(), '0,a,1,b,2,c', 'basic functionality');
Expand Down
8 changes: 6 additions & 2 deletions tests/unit-global/esnext.async-iterator.drop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('AsyncIterator#drop', assert => {
Expand All @@ -9,8 +10,11 @@ QUnit.test('AsyncIterator#drop', assert => {
assert.looksNative(drop);
assert.nonEnumerable(AsyncIterator.prototype, 'drop');

assert.throws(() => drop.call(undefined, 1), TypeError);
assert.throws(() => drop.call(null, 1), TypeError);
if (STRICT) {
assert.throws(() => drop.call(undefined, 1), TypeError);
assert.throws(() => drop.call(null, 1), TypeError);
}

assert.throws(() => drop.call(createIterator([1, 2, 3]), -1), RangeError, 'negative');
assert.throws(() => drop.call(createIterator([1, 2, 3]), NaN), RangeError, 'NaN');

Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.every.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#every', assert => {
const { every } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#every', assert => {
assert.looksNative(every);
assert.nonEnumerable(AsyncIterator.prototype, 'every');

assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => every.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => every.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => every.call(createIterator([1]), undefined), TypeError);
assert.throws(() => every.call(createIterator([1]), null), TypeError);
assert.throws(() => every.call(createIterator([1]), {}), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#filter', assert => {
const { filter } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#filter', assert => {
assert.looksNative(filter);
assert.nonEnumerable(AsyncIterator.prototype, 'filter');

assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => filter.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => filter.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => filter.call(createIterator([1]), undefined), TypeError);
assert.throws(() => filter.call(createIterator([1]), null), TypeError);
assert.throws(() => filter.call(createIterator([1]), {}), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.find.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#find', assert => {
const { find } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#find', assert => {
assert.looksNative(find);
assert.nonEnumerable(AsyncIterator.prototype, 'find');

assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => find.call(createIterator([1]), undefined), TypeError);
assert.throws(() => find.call(createIterator([1]), null), TypeError);
assert.throws(() => find.call(createIterator([1]), {}), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator, createIterable } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#flatMap', assert => {
const { flatMap } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#flatMap', assert => {
assert.looksNative(flatMap);
assert.nonEnumerable(AsyncIterator.prototype, 'flatMap');

assert.throws(() => flatMap.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => flatMap.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => flatMap.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => flatMap.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => flatMap.call(createIterator([1]), undefined), TypeError);
assert.throws(() => flatMap.call(createIterator([1]), null), TypeError);
assert.throws(() => flatMap.call(createIterator([1]), {}), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.for-each.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#forEach', assert => {
const { forEach } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#forEach', assert => {
assert.looksNative(forEach);
assert.nonEnumerable(AsyncIterator.prototype, 'forEach');

assert.throws(() => forEach.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => forEach.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => forEach.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => forEach.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => forEach.call(createIterator([1]), undefined), TypeError);
assert.throws(() => forEach.call(createIterator([1]), null), TypeError);
assert.throws(() => forEach.call(createIterator([1]), {}), TypeError);
Expand Down
7 changes: 5 additions & 2 deletions tests/unit-global/esnext.async-iterator.indexed.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('AsyncIterator#indexed', assert => {
Expand All @@ -9,8 +10,10 @@ QUnit.test('AsyncIterator#indexed', assert => {
assert.looksNative(indexed);
assert.nonEnumerable(AsyncIterator.prototype, 'indexed');

assert.throws(() => indexed.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => indexed.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => indexed.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => indexed.call(null, () => { /* empty */ }), TypeError);
}

return indexed.call(createIterator(['a', 'b', 'c'])).toArray().then(it => {
assert.same(it.toString(), '0,a,1,b,2,c', 'basic functionality');
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#map', assert => {
const { map } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#map', assert => {
assert.looksNative(map);
assert.nonEnumerable(AsyncIterator.prototype, 'map');

assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => map.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => map.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => map.call(createIterator([1]), undefined), TypeError);
assert.throws(() => map.call(createIterator([1]), null), TypeError);
assert.throws(() => map.call(createIterator([1]), {}), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#reduce', assert => {
const { reduce } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#reduce', assert => {
assert.looksNative(reduce);
assert.nonEnumerable(AsyncIterator.prototype, 'reduce');

assert.throws(() => reduce.call(undefined, () => { /* empty */ }, 1), TypeError);
assert.throws(() => reduce.call(null, () => { /* empty */ }, 1), TypeError);
if (STRICT) {
assert.throws(() => reduce.call(undefined, () => { /* empty */ }, 1), TypeError);
assert.throws(() => reduce.call(null, () => { /* empty */ }, 1), TypeError);
}

assert.throws(() => reduce.call(createIterator([1]), undefined, 1), TypeError);
assert.throws(() => reduce.call(createIterator([1]), null, 1), TypeError);
assert.throws(() => reduce.call(createIterator([1]), {}, 1), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.async-iterator.some.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#some', assert => {
const { some } = AsyncIterator.prototype;
Expand All @@ -10,8 +10,11 @@ QUnit.test('AsyncIterator#some', assert => {
assert.looksNative(some);
assert.nonEnumerable(AsyncIterator.prototype, 'some');

assert.throws(() => some.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => some.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => some.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => some.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => some.call(createIterator([1]), undefined), TypeError);
assert.throws(() => some.call(createIterator([1]), null), TypeError);
assert.throws(() => some.call(createIterator([1]), {}), TypeError);
Expand Down
8 changes: 6 additions & 2 deletions tests/unit-global/esnext.async-iterator.take.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('AsyncIterator#take', assert => {
Expand All @@ -9,8 +10,11 @@ QUnit.test('AsyncIterator#take', assert => {
assert.looksNative(take);
assert.nonEnumerable(AsyncIterator.prototype, 'take');

assert.throws(() => take.call(undefined, 1), TypeError);
assert.throws(() => take.call(null, 1), TypeError);
if (STRICT) {
assert.throws(() => take.call(undefined, 1), TypeError);
assert.throws(() => take.call(null, 1), TypeError);
}

assert.throws(() => take.call(createIterator([1, 2, 3]), -1), RangeError, 'negative');
assert.throws(() => take.call(createIterator([1, 2, 3]), NaN), RangeError, 'NaN');

Expand Down
7 changes: 5 additions & 2 deletions tests/unit-global/esnext.async-iterator.to-array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('AsyncIterator#toArray', assert => {
Expand All @@ -9,8 +10,10 @@ QUnit.test('AsyncIterator#toArray', assert => {
assert.looksNative(toArray);
assert.nonEnumerable(AsyncIterator.prototype, 'toArray');

assert.throws(() => toArray.call(undefined), TypeError);
assert.throws(() => toArray.call(null), TypeError);
if (STRICT) {
assert.throws(() => toArray.call(undefined), TypeError);
assert.throws(() => toArray.call(null), TypeError);
}

return toArray.call(createIterator([1, 2, 3])).then(it => {
assert.arrayEqual(it, [1, 2, 3]);
Expand Down
8 changes: 6 additions & 2 deletions tests/unit-global/esnext.iterator.as-indexed-pairs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('Iterator#asIndexedPairs', assert => {
Expand All @@ -11,8 +12,11 @@ QUnit.test('Iterator#asIndexedPairs', assert => {

assert.arrayEqual(asIndexedPairs.call(createIterator(['a', 'b', 'c'])).toArray().toString(), '0,a,1,b,2,c', 'basic functionality');

assert.throws(() => asIndexedPairs.call(undefined), TypeError);
assert.throws(() => asIndexedPairs.call(null), TypeError);
if (STRICT) {
assert.throws(() => asIndexedPairs.call(undefined), TypeError);
assert.throws(() => asIndexedPairs.call(null), TypeError);
}

assert.throws(() => asIndexedPairs.call({}).next(), TypeError);
assert.throws(() => asIndexedPairs.call([]).next(), TypeError);
});
8 changes: 6 additions & 2 deletions tests/unit-global/esnext.iterator.drop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STRICT } from '../helpers/constants';
import { createIterator } from '../helpers/helpers';

QUnit.test('Iterator#drop', assert => {
Expand All @@ -14,8 +15,11 @@ QUnit.test('Iterator#drop', assert => {
assert.arrayEqual(drop.call(createIterator([1, 2, 3]), 4).toArray(), [], 'big');
assert.arrayEqual(drop.call(createIterator([1, 2, 3]), 0).toArray(), [1, 2, 3], 'zero');

assert.throws(() => drop.call(undefined, 1), TypeError);
assert.throws(() => drop.call(null, 1), TypeError);
if (STRICT) {
assert.throws(() => drop.call(undefined, 1), TypeError);
assert.throws(() => drop.call(null, 1), TypeError);
}

assert.throws(() => drop.call({}, 1).next(), TypeError);
assert.throws(() => drop.call([], 1).next(), TypeError);
assert.throws(() => drop.call(createIterator([1, 2, 3]), -1), RangeError, 'negative');
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.iterator.every.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('Iterator#every', assert => {
const { every } = Iterator.prototype;
Expand All @@ -19,8 +19,11 @@ QUnit.test('Iterator#every', assert => {
assert.same(counter, 0, 'counter');
});

assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => every.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => every.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => every.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => every.call({}, () => { /* empty */ }), TypeError);
assert.throws(() => every.call([], () => { /* empty */ }), TypeError);
assert.throws(() => every.call(createIterator([1]), undefined), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.iterator.filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('Iterator#filter', assert => {
const { filter } = Iterator.prototype;
Expand All @@ -18,8 +18,11 @@ QUnit.test('Iterator#filter', assert => {
assert.same(counter, 0, 'counter');
});

assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => filter.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => filter.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => filter.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => filter.call({}, () => { /* empty */ }).next(), TypeError);
assert.throws(() => filter.call([], () => { /* empty */ }).next(), TypeError);
assert.throws(() => filter.call(createIterator([1]), undefined), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.iterator.find.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('Iterator#find', assert => {
const { find } = Iterator.prototype;
Expand All @@ -18,8 +18,11 @@ QUnit.test('Iterator#find', assert => {
assert.same(counter, 0, 'counter');
});

assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => find.call({}, () => { /* empty */ }), TypeError);
assert.throws(() => find.call([], () => { /* empty */ }), TypeError);
assert.throws(() => find.call(createIterator([1]), undefined), TypeError);
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-global/esnext.iterator.flat-map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createIterator, createIterable } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';
import { STRICT, STRICT_THIS } from '../helpers/constants';

QUnit.test('Iterator#flatMap', assert => {
const { flatMap } = Iterator.prototype;
Expand All @@ -23,8 +23,11 @@ QUnit.test('Iterator#flatMap', assert => {
return [arg];
}).toArray();

assert.throws(() => flatMap.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => flatMap.call(null, () => { /* empty */ }), TypeError);
if (STRICT) {
assert.throws(() => flatMap.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => flatMap.call(null, () => { /* empty */ }), TypeError);
}

assert.throws(() => flatMap.call({}, () => { /* empty */ }).next(), TypeError);
assert.throws(() => flatMap.call([], () => { /* empty */ }).next(), TypeError);
assert.throws(() => flatMap.call(createIterator([1]), it => it).next(), TypeError);
Expand Down
Loading

0 comments on commit 867540b

Please sign in to comment.