Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 17, 2021
1 parent a41d7ec commit 7fee2c2
Show file tree
Hide file tree
Showing 12 changed files with 664 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/pure/esnext.array.with-at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { STRICT } from '../helpers/constants';

import Symbol from 'core-js-pure/es/symbol';
import withAt from 'core-js-pure/features/array/with-at';

QUnit.test('Array#withAt', assert => {
assert.isFunction(withAt);

let array = [1, 2, 3, 4, 5];
assert.ok(withAt(array, 2, 1) !== array);
assert.deepEqual(withAt([1, 2, 3, 4, 5], 2, 6), [1, 2, 6, 4, 5]);
assert.deepEqual(withAt([1, 2, 3, 4, 5], -2, 6), [1, 2, 3, 6, 5]);

assert.throws(() => withAt([1, 2, 3, 4, 5], 5, 6), RangeError);
assert.throws(() => withAt([1, 2, 3, 4, 5], -6, 6), RangeError);
assert.throws(() => withAt([1, 2, 3, 4, 5], '1', 6), RangeError);

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

array = [1, 2];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(withAt(array, 1, 2) instanceof Array, 'non-generic');
});
56 changes: 56 additions & 0 deletions tests/pure/esnext.array.with-reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { STRICT } from '../helpers/constants';

import Symbol from 'core-js-pure/es/symbol';
import withReversed from 'core-js-pure/features/array/with-reversed';

QUnit.test('Array#withReversed', assert => {
assert.isFunction(withReversed);

let array = [1, 2];
assert.ok(withReversed(array) !== array, 'immutable');
assert.deepEqual(withReversed([1, 2.2, 3.3]), [3.3, 2.2, 1], 'basic');

const object = {};

array = {
0: undefined,
1: 2,
2: 1,
3: 'X',
4: -1,
5: 'a',
6: true,
7: object,
8: NaN,
10: Infinity,
length: 11,
};

const expected = [
Infinity,
undefined,
NaN,
object,
true,
'a',
-1,
'X',
1,
2,
undefined,
];

assert.deepEqual(withReversed(array), expected, 'non-array target');

array = [1];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(withReversed(array) instanceof Array, 'non-generic');

if (STRICT) {
assert.throws(() => withReversed(null, () => { /* empty */ }, 1), TypeError);
assert.throws(() => withReversed(undefined, () => { /* empty */ }, 1), TypeError);
}
});
132 changes: 132 additions & 0 deletions tests/pure/esnext.array.with-sorted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { STRICT } from '../helpers/constants';

import Symbol from 'core-js-pure/es/symbol';
import withSorted from 'core-js-pure/features/array/with-sorted';

QUnit.test('Array#withSorted', assert => {
assert.isFunction(withSorted);

let array = [1];
assert.ok(withSorted(array) !== array, 'immutable');
assert.deepEqual(withSorted([1, 3, 2]), [1, 2, 3], '#1');
assert.deepEqual(withSorted([1, 3, 2, 11]), [1, 11, 2, 3], '#2');
assert.deepEqual(withSorted([1, -1, 3, NaN, 2, 0, 11, -0]), [-1, 0, -0, 1, 11, 2, 3, NaN], '#1');

array = Array(5);
array[0] = 1;
array[2] = 3;
array[4] = 2;
let expected = Array(5);
expected[0] = 1;
expected[1] = 2;
expected[2] = 3;
assert.deepEqual(withSorted(array), expected, 'holes');

array = 'zyxwvutsrqponMLKJIHGFEDCBA'.split('');
expected = 'ABCDEFGHIJKLMnopqrstuvwxyz'.split('');
assert.deepEqual(withSorted(array), expected, 'alpha #1');

array = 'ёяюэьыъщшчцхфутсрПОНМЛКЙИЗЖЕДГВБА'.split('');
expected = 'АБВГДЕЖЗИЙКЛМНОПрстуфхцчшщъыьэюяё'.split('');
assert.deepEqual(withSorted(array), expected, 'alpha #2');

array = [undefined, 1];
assert.notThrows(() => array = withSorted(array, () => { throw 1; }), 'undefined #1');
assert.deepEqual(array, [1, undefined], 'undefined #2');

const object = {
valueOf: () => 1,
toString: () => -1,
};

array = {
0: undefined,
1: 2,
2: 1,
3: 'X',
4: -1,
5: 'a',
6: true,
7: object,
8: NaN,
10: Infinity,
length: 11,
};

expected = [
-1,
object,
1,
2,
Infinity,
NaN,
'X',
'a',
true,
undefined,
undefined,
];

assert.deepEqual(withSorted(array), expected, 'non-array target');

let index, mod, code, chr, value;
expected = Array(516);
array = Array(516);

for (index = 0; index < 516; index++) {
mod = index % 4;
array[index] = 515 - index;
expected[index] = index - 2 * mod + 3;
}

assert.same(String(withSorted(array, (a, b) => (a / 4 | 0) - (b / 4 | 0))), String(expected), 'stable #1');

assert.ok(1 / withSorted([0, -0])[0] > 0, '-0');

let result = '';
array = [];

// generate an array with more 512 elements (Chakra and old V8 fails only in this case)
for (code = 65; code < 76; code++) {
chr = String.fromCharCode(code);

switch (code) {
case 66: case 69: case 70: case 72: value = 3; break;
case 68: case 71: value = 4; break;
default: value = 2;
}

for (index = 0; index < 47; index++) {
array.push({ k: chr + index, v: value });
}
}

array = withSorted(array, (a, b) => b.v - a.v);

for (index = 0; index < array.length; index++) {
chr = array[index].k.charAt(0);
if (result.charAt(result.length - 1) !== chr) result += chr;
}

assert.same(result, 'DGBEFHACIJK', 'stable #2');

assert.notThrows(() => withSorted([1, 2, 3], undefined).length === 3, 'works with undefined');
assert.throws(() => withSorted([1, 2, 3], null), 'throws on null');
assert.throws(() => withSorted([1, 2, 3], {}), 'throws on {}');

if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => withSorted([Symbol(1), Symbol(2)]), 'w/o cmp throws on symbols');
}

array = [1];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(withSorted(array) instanceof Array, 'non-generic');

if (STRICT) {
assert.throws(() => withSorted(null), TypeError, 'ToObject(this)');
assert.throws(() => withSorted(undefined), TypeError, 'ToObject(this)');
}
});
28 changes: 28 additions & 0 deletions tests/pure/esnext.array.with-spliced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { STRICT } from '../helpers/constants';

import Symbol from 'core-js-pure/es/symbol';
import withSpliced from 'core-js-pure/features/array/with-spliced';

QUnit.test('Array#withSpliced', assert => {
assert.isFunction(withSpliced);

let array = [1, 2, 3, 4, 5];
assert.ok(withSpliced(array, 2) !== array);
assert.deepEqual(withSpliced([1, 2, 3, 4, 5], 2), [1, 2]);
assert.deepEqual(withSpliced([1, 2, 3, 4, 5], -2), [1, 2, 3]);
assert.deepEqual(withSpliced([1, 2, 3, 4, 5], 2, 2), [1, 2, 5]);
assert.deepEqual(withSpliced([1, 2, 3, 4, 5], 2, -2), [1, 2, 3, 4, 5]);
assert.deepEqual(withSpliced([1, 2, 3, 4, 5], 2, 2, 6, 7), [1, 2, 6, 7, 5]);

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

array = [];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(withSpliced(array) instanceof Array, 'non-generic');
});
35 changes: 35 additions & 0 deletions tests/tests/esnext.array.with-at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { STRICT } from '../helpers/constants';

QUnit.test('Array#withAt', assert => {
const { withAt } = Array.prototype;

assert.isFunction(withAt);
assert.arity(withAt, 2);
assert.name(withAt, 'withAt');
assert.looksNative(withAt);
assert.nonEnumerable(Array.prototype, 'withAt');

let array = [1, 2, 3, 4, 5];
assert.ok(array.withAt(2, 1) !== array, 'immutable');

assert.deepEqual([1, 2, 3, 4, 5].withAt(2, 6), [1, 2, 6, 4, 5]);
assert.deepEqual([1, 2, 3, 4, 5].withAt(-2, 6), [1, 2, 3, 6, 5]);

assert.throws(() => [1, 2, 3, 4, 5].withAt(5, 6), RangeError);
assert.throws(() => [1, 2, 3, 4, 5].withAt(-6, 6), RangeError);
assert.throws(() => [1, 2, 3, 4, 5].withAt('1', 6), RangeError);

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

array = [1, 2];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(array.withAt(1, 2) instanceof Array, 'non-generic');

assert.ok('withAt' in Array.prototype[Symbol.unscopables], 'In Array#@@unscopables');
});
62 changes: 62 additions & 0 deletions tests/tests/esnext.array.with-reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { STRICT } from '../helpers/constants';

QUnit.test('Array#withReversed', assert => {
const { withReversed } = Array.prototype;

assert.isFunction(withReversed);
assert.arity(withReversed, 0);
assert.name(withReversed, 'withReversed');
assert.looksNative(withReversed);
assert.nonEnumerable(Array.prototype, 'withReversed');

let array = [1, 2];
assert.ok(array.withReversed() !== array, 'immutable');

assert.deepEqual([1, 2.2, 3.3].withReversed(), [3.3, 2.2, 1], 'basic');

const object = {};

array = {
0: undefined,
1: 2,
2: 1,
3: 'X',
4: -1,
5: 'a',
6: true,
7: object,
8: NaN,
10: Infinity,
length: 11,
};

const expected = [
Infinity,
undefined,
NaN,
object,
true,
'a',
-1,
'X',
1,
2,
undefined,
];

assert.deepEqual(withReversed.call(array), expected, 'non-array target');

array = [1];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.ok(array.withReversed() instanceof Array, 'non-generic');

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

assert.ok('withReversed' in Array.prototype[Symbol.unscopables], 'In Array#@@unscopables');
});
Loading

0 comments on commit 7fee2c2

Please sign in to comment.