file_path
stringlengths 3
280
| file_language
stringclasses 66
values | content
stringlengths 1
1.04M
| repo_name
stringlengths 5
92
| repo_stars
int64 0
154k
| repo_description
stringlengths 0
402
| repo_primary_language
stringclasses 108
values | developer_username
stringlengths 1
25
| developer_name
stringlengths 0
30
| developer_company
stringlengths 0
82
|
|---|---|---|---|---|---|---|---|---|---|
tests/unit-global/es.object.lookup-setter.js
|
JavaScript
|
import { DESCRIPTORS, STRICT } from '../helpers/constants.js';
if (DESCRIPTORS) {
QUnit.test('Object#__lookupSetter__', assert => {
const { __lookupSetter__ } = Object.prototype;
const { create } = Object;
assert.isFunction(__lookupSetter__);
assert.arity(__lookupSetter__, 1);
assert.name(__lookupSetter__, '__lookupSetter__');
assert.looksNative(__lookupSetter__);
assert.nonEnumerable(Object.prototype, '__lookupSetter__');
assert.same({}.__lookupSetter__('key'), undefined, 'empty object');
assert.same({ key: 42 }.__lookupSetter__('key'), undefined, 'data descriptor');
const object = {};
function setter() { /* empty */ }
object.__defineSetter__('key', setter);
assert.same(object.__lookupSetter__('key'), setter, 'own getter');
assert.same(create(object).__lookupSetter__('key'), setter, 'proto getter');
assert.same(create(object).__lookupSetter__('foo'), undefined, 'empty proto');
if (STRICT) {
assert.throws(() => __lookupSetter__.call(null, 1, () => { /* empty */ }), TypeError, 'Throws on null as `this`');
assert.throws(() => __lookupSetter__.call(undefined, 1, () => { /* empty */ }), TypeError, 'Throws on undefined as `this`');
}
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.prevent-extensions.js
|
JavaScript
|
import { GLOBAL, NATIVE } from '../helpers/constants.js';
QUnit.test('Object.preventExtensions', assert => {
const { preventExtensions, keys, isExtensible, getOwnPropertyNames, getOwnPropertySymbols } = Object;
const { ownKeys } = GLOBAL.Reflect || {};
assert.isFunction(preventExtensions);
assert.arity(preventExtensions, 1);
assert.name(preventExtensions, 'preventExtensions');
assert.looksNative(preventExtensions);
assert.nonEnumerable(Object, 'preventExtensions');
const data = [42, 'foo', false, null, undefined, {}];
for (const value of data) {
assert.notThrows(() => preventExtensions(value) || true, `accept ${ {}.toString.call(value).slice(8, -1) }`);
assert.same(preventExtensions(value), value, `returns target on ${ {}.toString.call(value).slice(8, -1) }`);
}
if (NATIVE) assert.false(isExtensible(preventExtensions({})));
const results = [];
for (const key in preventExtensions({})) results.push(key);
assert.arrayEqual(results, []);
assert.arrayEqual(keys(preventExtensions({})), []);
assert.arrayEqual(getOwnPropertyNames(preventExtensions({})), []);
if (getOwnPropertySymbols) assert.arrayEqual(getOwnPropertySymbols(preventExtensions({})), []);
if (ownKeys) assert.arrayEqual(ownKeys(preventExtensions({})), []);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.proto.js
|
JavaScript
|
/* eslint-disable no-proto -- required for testing */
import { DESCRIPTORS, PROTO } from '../helpers/constants.js';
if (PROTO && DESCRIPTORS) QUnit.test('Object.prototype.__proto__', assert => {
assert.true('__proto__' in Object.prototype, 'in Object.prototype');
const O = {};
assert.same(O.__proto__, Object.prototype);
O.__proto__ = Array.prototype;
assert.same(O.__proto__, Array.prototype);
assert.same(Object.getPrototypeOf(O), Array.prototype);
assert.true(O instanceof Array);
O.__proto__ = null;
assert.same(Object.getPrototypeOf(O), null);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.seal.js
|
JavaScript
|
import { GLOBAL, NATIVE } from '../helpers/constants.js';
QUnit.test('Object.seal', assert => {
const { seal, isSealed, keys, getOwnPropertyNames, getOwnPropertySymbols } = Object;
const { ownKeys } = GLOBAL.Reflect || {};
assert.isFunction(seal);
assert.arity(seal, 1);
assert.name(seal, 'seal');
assert.looksNative(seal);
assert.nonEnumerable(Object, 'seal');
const data = [42, 'foo', false, null, undefined, {}];
for (const value of data) {
assert.notThrows(() => seal(value) || true, `accept ${ {}.toString.call(value).slice(8, -1) }`);
assert.same(seal(value), value, `returns target on ${ {}.toString.call(value).slice(8, -1) }`);
}
if (NATIVE) assert.true(isSealed(seal({})));
const results = [];
for (const key in seal({})) results.push(key);
assert.arrayEqual(results, []);
assert.arrayEqual(keys(seal({})), []);
assert.arrayEqual(getOwnPropertyNames(seal({})), []);
if (getOwnPropertySymbols) assert.arrayEqual(getOwnPropertySymbols(seal({})), []);
if (ownKeys) assert.arrayEqual(ownKeys(seal({})), []);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.set-prototype-of.js
|
JavaScript
|
import { PROTO } from '../helpers/constants.js';
if (PROTO) QUnit.test('Object.setPrototypeOf', assert => {
const { setPrototypeOf } = Object;
assert.isFunction(setPrototypeOf);
assert.arity(setPrototypeOf, 2);
assert.name(setPrototypeOf, 'setPrototypeOf');
assert.looksNative(setPrototypeOf);
assert.nonEnumerable(Object, 'setPrototypeOf');
assert.true('apply' in setPrototypeOf({}, Function.prototype), 'Parent properties in target');
assert.same(setPrototypeOf({ a: 2 }, {
b() {
return this.a ** 2;
},
}).b(), 4, 'Child and parent properties in target');
const object = {};
assert.same(setPrototypeOf(object, { a: 1 }), object, 'setPrototypeOf return target');
assert.false('toString' in setPrototypeOf({}, null), 'Can set null as prototype');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.to-string.js
|
JavaScript
|
import { GLOBAL, STRICT } from '../helpers/constants.js';
QUnit.test('Object#toString', assert => {
const { toString } = Object.prototype;
const Symbol = GLOBAL.Symbol || {};
assert.arity(toString, 0);
assert.name(toString, 'toString');
assert.looksNative(toString);
assert.nonEnumerable(Object.prototype, 'toString');
if (STRICT) {
assert.same(toString.call(null), '[object Null]', 'null -> `Null`');
assert.same(toString.call(undefined), '[object Undefined]', 'undefined -> `Undefined`');
}
assert.same(toString.call(true), '[object Boolean]', 'bool -> `Boolean`');
assert.same(toString.call('string'), '[object String]', 'string -> `String`');
assert.same(toString.call(7), '[object Number]', 'number -> `Number`');
assert.same(`${ {} }`, '[object Object]', '{} -> `Object`');
assert.same(toString.call([]), '[object Array]', ' [] -> `Array`');
assert.same(toString.call(() => { /* empty */ }), '[object Function]', 'function -> `Function`');
assert.same(toString.call(/./), '[object RegExp]', 'regexp -> `RegExp`');
assert.same(toString.call(new TypeError()), '[object Error]', 'new TypeError -> `Error`');
assert.same(toString.call(function () {
// eslint-disable-next-line prefer-rest-params -- required for testing
return arguments;
}()), '[object Arguments]', 'arguments -> `Arguments`');
const constructors = [
'Array',
'RegExp',
'Boolean',
'String',
'Number',
'Error',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'ArrayBuffer',
];
for (const name of constructors) {
const Constructor = GLOBAL[name];
if (Constructor) {
assert.same(toString.call(new Constructor(1)), `[object ${ name }]`, `new ${ name }(1) -> \`${ name }\``);
}
}
if (GLOBAL.DataView) {
assert.same(`${ new DataView(new ArrayBuffer(1)) }`, '[object DataView]', 'dataview -> `DataView`');
}
if (GLOBAL.Set) {
assert.same(`${ new Set() }`, '[object Set]', 'set -> `Set`');
}
if (GLOBAL.Map) {
assert.same(`${ new Map() }`, '[object Map]', 'map -> `Map`');
}
if (GLOBAL.WeakSet) {
assert.same(`${ new WeakSet() }`, '[object WeakSet]', 'weakset -> `WeakSet`');
}
if (GLOBAL.WeakMap) {
assert.same(`${ new WeakMap() }`, '[object WeakMap]', 'weakmap -> `WeakMap`');
}
if (GLOBAL.Promise) {
assert.same(`${ new Promise(() => { /* empty */ }) }`, '[object Promise]', 'promise -> `Promise`');
}
if (''[Symbol.iterator]) {
assert.same(`${ ''[Symbol.iterator]() }`, '[object String Iterator]', 'String Iterator -> `String Iterator`');
}
if ([].entries) {
assert.same(`${ [].entries() }`, '[object Array Iterator]', 'Array Iterator -> `Array Iterator`');
}
if (GLOBAL.Set && Set.prototype.entries) {
assert.same(`${ new Set().entries() }`, '[object Set Iterator]', 'Set Iterator -> `Set Iterator`');
}
if (GLOBAL.Map && Map.prototype.entries) {
assert.same(`${ new Map().entries() }`, '[object Map Iterator]', 'Map Iterator -> `Map Iterator`');
}
assert.same(`${ Math }`, '[object Math]', 'Math -> `Math`');
if (GLOBAL.JSON) {
assert.same(`${ JSON }`, '[object JSON]', 'JSON -> `JSON`');
}
function Class() { /* empty */ }
Class.prototype[Symbol.toStringTag] = 'Class';
assert.same(`${ new Class() }`, '[object Class]', 'user class instance -> [Symbol.toStringTag]');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.object.values.js
|
JavaScript
|
QUnit.test('Object.values', assert => {
const { values, create, assign } = Object;
assert.isFunction(values);
assert.arity(values, 1);
assert.name(values, 'values');
assert.looksNative(values);
assert.nonEnumerable(Object, 'values');
assert.deepEqual(values({ q: 1, w: 2, e: 3 }), [1, 2, 3]);
assert.deepEqual(values(new String('qwe')), ['q', 'w', 'e']);
assert.deepEqual(values(assign(create({ q: 1, w: 2, e: 3 }), { a: 4, s: 5, d: 6 })), [4, 5, 6]);
assert.deepEqual(values({ valueOf: 42 }), [42], 'IE enum keys bug');
try {
assert.deepEqual(Function('values', `
return values({ a: 1, get b() {
delete this.c;
return 2;
}, c: 3 });
`)(values), [1, 2]);
} catch { /* empty */ }
try {
assert.deepEqual(Function('values', `
return values({ a: 1, get b() {
Object.defineProperty(this, "c", {
value: 4,
enumerable: false
});
return 2;
}, c: 3 });
`)(values), [1, 2]);
} catch { /* empty */ }
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.parse-float.js
|
JavaScript
|
import { WHITESPACES } from '../helpers/constants.js';
QUnit.test('parseFloat', assert => {
assert.isFunction(parseFloat);
assert.name(parseFloat, 'parseFloat');
assert.arity(parseFloat, 1);
assert.looksNative(parseFloat);
assert.same(parseFloat('0'), 0);
assert.same(parseFloat(' 0'), 0);
assert.same(parseFloat('+0'), 0);
assert.same(parseFloat(' +0'), 0);
assert.same(parseFloat('-0'), -0);
assert.same(parseFloat(' -0'), -0);
assert.same(parseFloat(`${ WHITESPACES }+0`), 0);
assert.same(parseFloat(`${ WHITESPACES }-0`), -0);
// eslint-disable-next-line math/no-static-nan-calculations -- feature detection
assert.same(parseFloat(null), NaN);
// eslint-disable-next-line math/no-static-nan-calculations -- feature detection
assert.same(parseFloat(undefined), NaN);
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('parseFloat test');
assert.throws(() => parseFloat(symbol), 'throws on symbol argument');
assert.throws(() => parseFloat(Object(symbol)), 'throws on boxed symbol argument');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.parse-int.js
|
JavaScript
|
/* eslint-disable prefer-numeric-literals -- required for testing */
import { WHITESPACES } from '../helpers/constants.js';
/* eslint-disable radix -- required for testing */
QUnit.test('parseInt', assert => {
assert.isFunction(parseInt);
assert.name(parseInt, 'parseInt');
assert.arity(parseInt, 2);
assert.looksNative(parseInt);
for (let radix = 2; radix <= 36; ++radix) {
assert.same(parseInt('10', radix), radix, `radix ${ radix }`);
}
const strings = ['01', '08', '10', '42'];
for (const string of strings) {
assert.same(parseInt(string), parseInt(string, 10), `default radix is 10: ${ string }`);
}
assert.same(parseInt('0x16'), parseInt('0x16', 16), 'default radix is 16: 0x16');
assert.same(parseInt(' 0x16'), parseInt('0x16', 16), 'ignores leading whitespace #1');
assert.same(parseInt(' 42'), parseInt('42', 10), 'ignores leading whitespace #2');
assert.same(parseInt(' 08'), parseInt('08', 10), 'ignores leading whitespace #3');
assert.same(parseInt(`${ WHITESPACES }08`), parseInt('08', 10), 'ignores leading whitespace #4');
assert.same(parseInt(`${ WHITESPACES }0x16`), parseInt('0x16', 16), 'ignores leading whitespace #5');
const fakeZero = {
valueOf() {
return 0;
},
};
assert.same(parseInt('08', fakeZero), parseInt('08', 10), 'valueOf #1');
assert.same(parseInt('0x16', fakeZero), parseInt('0x16', 16), 'valueOf #2');
assert.same(parseInt('-0xF'), -15, 'signed hex #1');
assert.same(parseInt('-0xF', 16), -15, 'signed hex #2');
assert.same(parseInt('+0xF'), 15, 'signed hex #3');
assert.same(parseInt('+0xF', 16), 15, 'signed hex #4');
assert.same(parseInt('10', -4294967294), 2, 'radix uses ToUint32');
// eslint-disable-next-line math/no-static-nan-calculations -- feature detection
assert.same(parseInt(null), NaN);
// eslint-disable-next-line math/no-static-nan-calculations -- feature detection
assert.same(parseInt(undefined), NaN);
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('parseInt test');
assert.throws(() => parseInt(symbol), 'throws on symbol argument');
assert.throws(() => parseInt(Object(symbol)), 'throws on boxed symbol argument');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.all-settled.js
|
JavaScript
|
import { createIterable } from '../helpers/helpers.js';
QUnit.test('Promise.allSettled', assert => {
assert.isFunction(Promise.allSettled);
assert.arity(Promise.allSettled, 1);
assert.looksNative(Promise.allSettled);
assert.nonEnumerable(Promise, 'allSettled');
assert.true(Promise.allSettled([1, 2, 3]) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.allSettled, resolved', assert => {
return Promise.allSettled([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(it => {
assert.deepEqual(it, [
{ value: 1, status: 'fulfilled' },
{ value: 2, status: 'fulfilled' },
{ value: 3, status: 'fulfilled' },
], 'resolved with a correct value');
});
});
QUnit.test('Promise.allSettled, resolved with rejection', assert => {
return Promise.allSettled([
Promise.resolve(1),
Promise.reject(2),
Promise.resolve(3),
]).then(it => {
assert.deepEqual(it, [
{ value: 1, status: 'fulfilled' },
{ reason: 2, status: 'rejected' },
{ value: 3, status: 'fulfilled' },
], 'resolved with a correct value');
});
});
QUnit.test('Promise.allSettled, rejected', assert => {
// eslint-disable-next-line promise/valid-params -- required for testing
return Promise.allSettled().then(() => {
assert.avoid();
}, () => {
assert.required('rejected as expected');
});
});
QUnit.test('Promise.allSettled, resolved with timeouts', assert => {
return Promise.allSettled([
Promise.resolve(1),
new Promise(resolve => setTimeout(() => resolve(2), 10)),
Promise.resolve(3),
]).then(it => {
assert.deepEqual(it, [
{ value: 1, status: 'fulfilled' },
{ value: 2, status: 'fulfilled' },
{ value: 3, status: 'fulfilled' },
], 'keeps correct mapping, even with delays');
});
});
QUnit.test('Promise.allSettled, subclassing', assert => {
const { allSettled, resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise.resolve = resolve.bind(Promise);
assert.true(allSettled.call(SubPromise, [1, 2, 3]) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise);
assert.throws(() => {
allSettled.call(FakePromise1, [1, 2, 3]);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
allSettled.call(FakePromise2, [1, 2, 3]);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
allSettled.call(FakePromise3, [1, 2, 3]);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.allSettled, iterables', assert => {
const iterable = createIterable([1, 2, 3]);
Promise.allSettled(iterable).catch(() => { /* empty */ });
assert.true(iterable.received, 'works with iterables: iterator received');
assert.true(iterable.called, 'works with iterables: next called');
});
QUnit.test('Promise.allSettled, iterables 2', assert => {
const array = [];
let done = false;
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
array['@@iterator'] = undefined;
array[Symbol.iterator] = function () {
done = true;
return [][Symbol.iterator].call(this);
};
Promise.allSettled(array);
assert.true(done);
});
QUnit.test('Promise.allSettled, iterator closing', assert => {
const { resolve } = Promise;
let done = false;
try {
Promise.resolve = function () {
throw new Error();
};
Promise.allSettled(createIterable([1, 2, 3], {
return() {
done = true;
},
})).catch(() => { /* empty */ });
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
});
QUnit.test('Promise.allSettled, without constructor context', assert => {
const { allSettled } = Promise;
assert.throws(() => allSettled([]), TypeError, 'Throws if called without a constructor context');
assert.throws(() => allSettled.call(null, []), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.all.js
|
JavaScript
|
import { createIterable } from '../helpers/helpers.js';
QUnit.test('Promise.all', assert => {
const { all } = Promise;
assert.isFunction(all);
assert.arity(all, 1);
assert.name(all, 'all');
assert.looksNative(all);
assert.nonEnumerable(Promise, 'all');
assert.true(Promise.all([]) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.all, resolved', assert => {
return Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(it => {
assert.deepEqual(it, [1, 2, 3], 'resolved with a correct value');
});
});
QUnit.test('Promise.all, resolved with rejection', assert => {
return Promise.all([
Promise.resolve(1),
Promise.reject(2),
Promise.resolve(3),
]).then(() => {
assert.avoid();
}, error => {
assert.same(error, 2, 'rejected with a correct value');
});
});
QUnit.test('Promise.all, resolved with empty array', assert => {
return Promise.all([]).then(it => {
assert.deepEqual(it, [], 'resolved with a correct value');
});
});
QUnit.test('Promise.all, resolved with timeouts', assert => {
return Promise.all([
Promise.resolve(1),
new Promise(resolve => setTimeout(() => resolve(2), 10)),
Promise.resolve(3),
]).then(it => {
assert.deepEqual(it, [1, 2, 3], 'keeps correct mapping, even with delays');
});
});
QUnit.test('Promise.all, subclassing', assert => {
const { all, resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise.resolve = resolve.bind(Promise);
assert.true(all.call(SubPromise, [1, 2, 3]) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise);
assert.throws(() => {
all.call(FakePromise1, [1, 2, 3]);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
all.call(FakePromise2, [1, 2, 3]);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
all.call(FakePromise3, [1, 2, 3]);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.all, iterables', assert => {
const iterable = createIterable([1, 2, 3]);
Promise.all(iterable).catch(() => { /* empty */ });
assert.true(iterable.received, 'works with iterables: iterator received');
assert.true(iterable.called, 'works with iterables: next called');
});
QUnit.test('Promise.all, iterables 2', assert => {
const array = [];
let done = false;
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
array['@@iterator'] = undefined;
array[Symbol.iterator] = function () {
done = true;
return [][Symbol.iterator].call(this);
};
Promise.all(array);
assert.true(done);
});
QUnit.test('Promise.all, iterator closing', assert => {
const { resolve } = Promise;
let done = false;
try {
Promise.resolve = function () {
throw new Error();
};
Promise.all(createIterable([1, 2, 3], {
return() {
done = true;
},
})).catch(() => { /* empty */ });
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
});
QUnit.test('Promise.all, without constructor context', assert => {
const { all } = Promise;
assert.throws(() => all([]), TypeError, 'Throws if called without a constructor context');
assert.throws(() => all.call(null, []), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.any.js
|
JavaScript
|
import { createIterable } from '../helpers/helpers.js';
QUnit.test('Promise.any', assert => {
assert.isFunction(Promise.any);
assert.arity(Promise.any, 1);
assert.looksNative(Promise.any);
assert.nonEnumerable(Promise, 'any');
assert.true(Promise.any([1, 2, 3]) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.any, resolved', assert => {
return Promise.any([
Promise.resolve(1),
Promise.reject(2),
Promise.resolve(3),
]).then(it => {
assert.same(it, 1, 'resolved with a correct value');
});
});
QUnit.test('Promise.any, rejected #1', assert => {
return Promise.any([
Promise.reject(1),
Promise.reject(2),
Promise.reject(3),
]).then(() => {
assert.avoid();
}, error => {
assert.true(error instanceof AggregateError, 'instanceof AggregateError');
assert.deepEqual(error.errors, [1, 2, 3], 'rejected with a correct value');
});
});
QUnit.test('Promise.any, rejected #2', assert => {
// eslint-disable-next-line promise/valid-params -- required for testing
return Promise.any().then(() => {
assert.avoid();
}, () => {
assert.required('rejected as expected');
});
});
QUnit.test('Promise.any, rejected #3', assert => {
return Promise.any([]).then(() => {
assert.avoid();
}, error => {
assert.true(error instanceof AggregateError, 'instanceof AggregateError');
assert.deepEqual(error.errors, [], 'rejected with a correct value');
});
});
QUnit.test('Promise.any, resolved with timeout', assert => {
return Promise.any([
new Promise(resolve => setTimeout(() => resolve(1), 50)),
Promise.resolve(2),
]).then(it => {
assert.same(it, 2, 'resolved with a correct value');
});
});
QUnit.test('Promise.any, subclassing', assert => {
const { any, resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise.resolve = resolve.bind(Promise);
assert.true(any.call(SubPromise, [1, 2, 3]) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise);
assert.throws(() => {
any.call(FakePromise1, [1, 2, 3]);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
any.call(FakePromise2, [1, 2, 3]);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
any.call(FakePromise3, [1, 2, 3]);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.any, iterables', assert => {
const iterable = createIterable([1, 2, 3]);
Promise.any(iterable).catch(() => { /* empty */ });
assert.true(iterable.received, 'works with iterables: iterator received');
assert.true(iterable.called, 'works with iterables: next called');
});
QUnit.test('Promise.any, empty iterables', assert => {
const array = [];
let done = false;
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
array['@@iterator'] = undefined;
array[Symbol.iterator] = function () {
done = true;
return [][Symbol.iterator].call(this);
};
return Promise.any(array).then(() => {
assert.avoid();
}, error => {
assert.true(error instanceof AggregateError, 'instanceof AggregateError');
assert.true(done, 'iterator called');
});
});
QUnit.test('Promise.any, iterator closing', assert => {
const { resolve } = Promise;
let done = false;
try {
Promise.resolve = function () {
throw new Error();
};
Promise.any(createIterable([1, 2, 3], {
return() {
done = true;
},
})).catch(() => { /* empty */ });
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
});
QUnit.test('Promise.any, without constructor context', assert => {
const { any } = Promise;
assert.throws(() => any([]), TypeError, 'Throws if called without a constructor context');
assert.throws(() => any.call(null, []), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.catch.js
|
JavaScript
|
import { NATIVE } from '../helpers/constants.js';
QUnit.test('Promise#catch', assert => {
assert.isFunction(Promise.prototype.catch);
if (NATIVE) assert.arity(Promise.prototype.catch, 1);
if (NATIVE) assert.name(Promise.prototype.catch, 'catch');
assert.looksNative(Promise.prototype.catch);
assert.nonEnumerable(Promise.prototype, 'catch');
let promise = new Promise(resolve => {
resolve(42);
});
const FakePromise1 = promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
const FakePromise2 = FakePromise1[Symbol.species] = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
assert.true(promise.catch(() => { /* empty */ }) instanceof FakePromise2, 'subclassing, @@species pattern');
promise = new Promise(resolve => {
resolve(42);
});
promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
assert.true(promise.catch(() => { /* empty */ }) instanceof Promise, 'subclassing, incorrect `this` pattern');
promise = new Promise(resolve => {
resolve(42);
});
const FakePromise3 = promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
FakePromise3[Symbol.species] = function () { /* empty */ };
assert.throws(() => {
promise.catch(() => { /* empty */ });
}, 'NewPromiseCapability validations, #1');
FakePromise3[Symbol.species] = function (executor) {
executor(null, () => { /* empty */ });
};
assert.throws(() => {
promise.catch(() => { /* empty */ });
}, 'NewPromiseCapability validations, #2');
FakePromise3[Symbol.species] = function (executor) {
executor(() => { /* empty */ }, null);
};
assert.throws(() => {
promise.catch(() => { /* empty */ });
}, 'NewPromiseCapability validations, #3');
assert.same(Promise.prototype.catch.call({
// eslint-disable-next-line unicorn/no-thenable -- required for testing
then(x, y) {
return y;
},
}, 42), 42, 'calling `.then`');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.constructor.js
|
JavaScript
|
import { DESCRIPTORS, GLOBAL, NATIVE, PROTO, STRICT } from '../helpers/constants.js';
const Symbol = GLOBAL.Symbol || {};
const { setPrototypeOf, create } = Object;
QUnit.test('Promise', assert => {
assert.isFunction(Promise);
assert.arity(Promise, 1);
assert.name(Promise, 'Promise');
assert.looksNative(Promise);
assert.throws(() => {
Promise();
}, 'throws w/o `new`');
new Promise(function (resolve, reject) {
assert.isFunction(resolve, 'resolver is function');
assert.isFunction(reject, 'rejector is function');
if (STRICT) assert.same(this, undefined, 'correct executor context');
});
});
if (DESCRIPTORS) QUnit.test('Promise operations order', assert => {
let $resolve, $resolve2;
assert.expect(1);
const EXPECTED_ORDER = 'DEHAFGBC';
const async = assert.async();
let result = '';
const promise1 = new Promise(resolve => {
$resolve = resolve;
});
$resolve({
// eslint-disable-next-line unicorn/no-thenable -- required for testing
then() {
result += 'A';
throw new Error();
},
});
promise1.catch(() => {
result += 'B';
});
promise1.catch(() => {
result += 'C';
assert.same(result, EXPECTED_ORDER);
async();
});
const promise2 = new Promise(resolve => {
$resolve2 = resolve;
});
// eslint-disable-next-line unicorn/no-thenable -- required for testing
$resolve2(Object.defineProperty({}, 'then', {
get() {
result += 'D';
throw new Error();
},
}));
result += 'E';
promise2.catch(() => {
result += 'F';
});
promise2.catch(() => {
result += 'G';
});
result += 'H';
setTimeout(() => {
if (!~result.indexOf('C')) {
assert.same(result, EXPECTED_ORDER);
async();
}
}, 1e3);
});
QUnit.test('Promise#then', assert => {
assert.isFunction(Promise.prototype.then);
if (NATIVE) assert.arity(Promise.prototype.then, 2);
assert.name(Promise.prototype.then, 'then');
assert.looksNative(Promise.prototype.then);
assert.nonEnumerable(Promise.prototype, 'then');
let promise = new Promise(resolve => {
resolve(42);
});
const FakePromise1 = promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
const FakePromise2 = FakePromise1[Symbol.species] = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
assert.true(promise.then(() => { /* empty */ }) instanceof FakePromise2, 'subclassing, @@species pattern');
promise = new Promise(resolve => {
resolve(42);
});
promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
assert.true(promise.then(() => { /* empty */ }) instanceof Promise, 'subclassing, incorrect `this` pattern');
promise = new Promise(resolve => {
resolve(42);
});
const FakePromise3 = promise.constructor = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
FakePromise3[Symbol.species] = function () { /* empty */ };
assert.throws(() => {
promise.then(() => { /* empty */ });
}, 'NewPromiseCapability validations, #1');
FakePromise3[Symbol.species] = function (executor) {
executor(null, () => { /* empty */ });
};
assert.throws(() => {
promise.then(() => { /* empty */ });
}, 'NewPromiseCapability validations, #2');
FakePromise3[Symbol.species] = function (executor) {
executor(() => { /* empty */ }, null);
};
assert.throws(() => {
promise.then(() => { /* empty */ });
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise#@@toStringTag', assert => {
assert.same(Promise.prototype[Symbol.toStringTag], 'Promise', 'Promise::@@toStringTag is `Promise`');
assert.same(String(new Promise(() => { /* empty */ })), '[object Promise]', 'correct stringification');
});
if (PROTO) QUnit.test('Promise subclassing', assert => {
function SubPromise(executor) {
const self = new Promise(executor);
setPrototypeOf(self, SubPromise.prototype);
// eslint-disable-next-line es/no-nonstandard-promise-prototype-properties -- safe
self.mine = 'subclass';
return self;
}
setPrototypeOf(SubPromise, Promise);
SubPromise.prototype = create(Promise.prototype);
SubPromise.prototype.constructor = SubPromise;
let promise1 = SubPromise.resolve(5);
assert.same(promise1.mine, 'subclass');
promise1 = promise1.then(it => {
assert.same(it, 5);
});
assert.same(promise1.mine, 'subclass');
let promise2 = new SubPromise(resolve => {
resolve(6);
});
assert.same(promise2.mine, 'subclass');
promise2 = promise2.then(it => {
assert.same(it, 6);
});
assert.same(promise2.mine, 'subclass');
const promise3 = SubPromise.all([promise1, promise2]);
assert.same(promise3.mine, 'subclass');
assert.true(promise3 instanceof Promise);
assert.true(promise3 instanceof SubPromise);
promise3.then(assert.async(), error => {
assert.avoid(error);
});
});
// qunit@2.5 strange bug
QUnit.skip('Unhandled rejection tracking', assert => {
let done = false;
const resume = assert.async();
if (GLOBAL.process) {
assert.expect(3);
function onunhandledrejection(reason, promise) {
process.removeListener('unhandledRejection', onunhandledrejection);
assert.same(promise, $promise, 'unhandledRejection, promise');
assert.same(reason, 42, 'unhandledRejection, reason');
$promise.catch(() => {
// empty
});
}
function onrejectionhandled(promise) {
process.removeListener('rejectionHandled', onrejectionhandled);
assert.same(promise, $promise, 'rejectionHandled, promise');
done || resume();
done = true;
}
process.on('unhandledRejection', onunhandledrejection);
process.on('rejectionHandled', onrejectionhandled);
} else {
if (GLOBAL.addEventListener) {
assert.expect(8);
function onunhandledrejection(it) {
assert.same(it.promise, $promise, 'addEventListener(unhandledrejection), promise');
assert.same(it.reason, 42, 'addEventListener(unhandledrejection), reason');
GLOBAL.removeEventListener('unhandledrejection', onunhandledrejection);
}
GLOBAL.addEventListener('rejectionhandled', onunhandledrejection);
function onrejectionhandled(it) {
assert.same(it.promise, $promise, 'addEventListener(rejectionhandled), promise');
assert.same(it.reason, 42, 'addEventListener(rejectionhandled), reason');
GLOBAL.removeEventListener('rejectionhandled', onrejectionhandled);
}
GLOBAL.addEventListener('rejectionhandled', onrejectionhandled);
} else assert.expect(4);
GLOBAL.onunhandledrejection = function (it) {
assert.same(it.promise, $promise, 'onunhandledrejection, promise');
assert.same(it.reason, 42, 'onunhandledrejection, reason');
setTimeout(() => {
$promise.catch(() => {
// empty
});
}, 1);
GLOBAL.onunhandledrejection = null;
};
GLOBAL.onrejectionhandled = function (it) {
assert.same(it.promise, $promise, 'onrejectionhandled, promise');
assert.same(it.reason, 42, 'onrejectionhandled, reason');
GLOBAL.onrejectionhandled = null;
done || resume();
done = true;
};
}
Promise.reject(43).catch(() => {
// empty
});
const $promise = Promise.reject(42);
setTimeout(() => {
done || resume();
done = true;
}, 3e3);
});
const promise = (() => {
try {
return Function('return (async function () { /* empty */ })()')();
} catch { /* empty */ }
})();
if (promise) QUnit.test('Native Promise, maybe patched', assert => {
assert.isFunction(promise.then);
assert.arity(promise.then, 2);
assert.looksNative(promise.then);
assert.nonEnumerable(promise.constructor.prototype, 'then');
function empty() { /* empty */ }
assert.true(promise.then(empty) instanceof Promise, '`.then` returns `Promise` instance #1');
assert.true(new promise.constructor(empty).then(empty) instanceof Promise, '`.then` returns `Promise` instance #2');
assert.true(promise.catch(empty) instanceof Promise, '`.catch` returns `Promise` instance #1');
assert.true(new promise.constructor(empty).catch(empty) instanceof Promise, '`.catch` returns `Promise` instance #2');
assert.true(promise.finally(empty) instanceof Promise, '`.finally` returns `Promise` instance #1');
assert.true(new promise.constructor(empty).finally(empty) instanceof Promise, '`.finally` returns `Promise` instance #2');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.finally.js
|
JavaScript
|
QUnit.test('Promise#finally', assert => {
assert.isFunction(Promise.prototype.finally);
assert.arity(Promise.prototype.finally, 1);
assert.looksNative(Promise.prototype.finally);
assert.nonEnumerable(Promise.prototype, 'finally');
assert.true(Promise.resolve(42).finally(() => { /* empty */ }) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise#finally, resolved', assert => {
let called = 0;
let argument = null;
return Promise.resolve(42).finally(it => {
called++;
argument = it;
}).then(it => {
assert.same(it, 42, 'resolved with a correct value');
assert.same(called, 1, 'onFinally function called one time');
assert.same(argument, undefined, 'onFinally function called with a correct argument');
});
});
QUnit.test('Promise#finally, rejected', assert => {
let called = 0;
let argument = null;
return Promise.reject(42).finally(it => {
called++;
argument = it;
}).then(() => {
assert.avoid();
}, () => {
assert.same(called, 1, 'onFinally function called one time');
assert.same(argument, undefined, 'onFinally function called with a correct argument');
});
});
const promise = (() => {
try {
return Function('return (async function () { /* empty */ })()')();
} catch { /* empty */ }
})();
if (promise && promise.constructor !== Promise) QUnit.test('Native Promise, patched', assert => {
assert.isFunction(promise.finally);
assert.arity(promise.finally, 1);
assert.looksNative(promise.finally);
assert.nonEnumerable(promise.constructor.prototype, 'finally');
function empty() { /* empty */ }
assert.true(promise.finally(empty) instanceof Promise, '`.finally` returns `Promise` instance #1');
assert.true(new promise.constructor(empty).finally(empty) instanceof Promise, '`.finally` returns `Promise` instance #2');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.race.js
|
JavaScript
|
import { createIterable } from '../helpers/helpers.js';
QUnit.test('Promise.race', assert => {
const { race } = Promise;
assert.isFunction(race);
assert.arity(race, 1);
assert.name(race, 'race');
assert.looksNative(race);
assert.nonEnumerable(Promise, 'race');
assert.true(Promise.race([]) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.race, resolved', assert => {
return Promise.race([
Promise.resolve(1),
Promise.resolve(2),
]).then(it => {
assert.same(it, 1, 'resolved with a correct value');
});
});
QUnit.test('Promise.race, resolved with rejection', assert => {
return Promise.race([
Promise.reject(1),
Promise.resolve(2),
]).then(() => {
assert.avoid();
}, error => {
assert.same(error, 1, 'rejected with a correct value');
});
});
QUnit.test('Promise.race, resolved with timeouts', assert => {
return Promise.race([
new Promise(resolve => setTimeout(() => resolve(1), 50)),
Promise.resolve(2),
]).then(it => {
assert.same(it, 2, 'keeps correct mapping, even with delays');
});
});
QUnit.test('Promise.race, subclassing', assert => {
const { race, resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise.resolve = resolve.bind(Promise);
assert.true(race.call(SubPromise, [1, 2, 3]) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise);
assert.throws(() => {
race.call(FakePromise1, [1, 2, 3]);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
race.call(FakePromise2, [1, 2, 3]);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
race.call(FakePromise3, [1, 2, 3]);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.race, iterables', assert => {
const iterable = createIterable([1, 2, 3]);
Promise.race(iterable).catch(() => { /* empty */ });
assert.true(iterable.received, 'works with iterables: iterator received');
assert.true(iterable.called, 'works with iterables: next called');
});
QUnit.test('Promise.race, iterables 2', assert => {
const array = [];
let done = false;
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
array['@@iterator'] = undefined;
array[Symbol.iterator] = function () {
done = true;
return [][Symbol.iterator].call(this);
};
Promise.race(array);
assert.true(done);
});
QUnit.test('Promise.race, iterator closing', assert => {
const { resolve } = Promise;
let done = false;
try {
Promise.resolve = function () {
throw new Error();
};
Promise.race(createIterable([1, 2, 3], {
return() {
done = true;
},
})).catch(() => { /* empty */ });
} catch { /* empty */ }
Promise.resolve = resolve;
assert.true(done, 'iteration closing');
});
QUnit.test('Promise.race, without constructor context', assert => {
const { race } = Promise;
assert.throws(() => race([]), TypeError, 'Throws if called without a constructor context');
assert.throws(() => race.call(null, []), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.reject.js
|
JavaScript
|
import { NATIVE } from '../helpers/constants.js';
QUnit.test('Promise.reject', assert => {
const { reject } = Promise;
assert.isFunction(reject);
if (NATIVE) assert.arity(reject, 1);
assert.name(reject, 'reject');
assert.looksNative(reject);
assert.nonEnumerable(Promise, 'reject');
});
QUnit.test('Promise.reject, rejects with value', assert => {
return Promise.reject(42)
.then(() => {
assert.avoid('Should not resolve');
}, error => {
assert.same(error, 42, 'rejected with correct reason');
});
});
QUnit.test('Promise.reject, rejects with undefined', assert => {
return Promise.reject()
.then(() => {
assert.avoid('Should not resolve');
}, error => {
assert.same(error, undefined, 'rejected with correct reason');
});
});
QUnit.test('Promise.reject, subclassing', assert => {
const { reject } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
assert.true(reject.call(SubPromise, 42) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
assert.throws(() => {
reject.call(FakePromise1, 42);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
reject.call(FakePromise2, 42);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
reject.call(FakePromise3, 42);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.reject, without constructor context', assert => {
const { reject } = Promise;
assert.throws(() => reject(''), TypeError, 'Throws if called without a constructor context');
assert.throws(() => reject.call(null, ''), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.resolve.js
|
JavaScript
|
QUnit.test('Promise.resolve', assert => {
const { resolve } = Promise;
assert.isFunction(resolve);
assert.arity(resolve, 1);
assert.name(resolve, 'resolve');
assert.looksNative(resolve);
assert.nonEnumerable(Promise, 'resolve');
assert.true(Promise.resolve(42) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.resolve, resolves with value', assert => {
return Promise.resolve(42).then(result => {
assert.same(result, 42, 'resolved with a correct value');
});
});
QUnit.test('Promise.resolve, resolves with thenable', assert => {
const thenable = {
// eslint-disable-next-line unicorn/no-thenable -- safe
then(resolve) { resolve('foo'); },
};
return Promise.resolve(thenable).then(result => {
assert.same(result, 'foo', 'resolved with a correct value');
});
});
QUnit.test('Promise.resolve, returns input if input is already promise', assert => {
const p = Promise.resolve('ok');
assert.same(Promise.resolve(p), p, 'resolved with a correct value');
});
QUnit.test('Promise.resolve, resolves with undefined', assert => {
return Promise.resolve().then(result => {
assert.same(result, undefined, 'resolved with a correct value');
});
});
QUnit.test('Promise.resolve, subclassing', assert => {
const { resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise[Symbol.species] = function (executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
};
assert.true(resolve.call(SubPromise, 42) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
assert.throws(() => {
resolve.call(FakePromise1, 42);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
resolve.call(FakePromise2, 42);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
resolve.call(FakePromise3, 42);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.resolve, without constructor context', assert => {
const { resolve } = Promise;
assert.throws(() => resolve(''), TypeError, 'Throws if called without a constructor context');
assert.throws(() => resolve.call(null, ''), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.try.js
|
JavaScript
|
import Promise from 'core-js-pure/es/promise';
QUnit.test('Promise.try', assert => {
assert.isFunction(Promise.try);
assert.arity(Promise.try, 1);
assert.looksNative(Promise.try);
assert.nonEnumerable(Promise, 'try');
assert.true(Promise.try(() => 42) instanceof Promise, 'returns a promise');
});
QUnit.test('Promise.try, resolved', assert => {
return Promise.try(() => 42).then(it => {
assert.same(it, 42, 'resolved with a correct value');
});
});
QUnit.test('Promise.try, resolved, with args', assert => {
return Promise.try((a, b) => Promise.resolve(a + b), 1, 2).then(it => {
assert.same(it, 3, 'resolved with a correct value');
});
});
QUnit.test('Promise.try, rejected', assert => {
return Promise.try(() => {
throw new Error();
}).then(() => {
assert.avoid();
}, () => {
assert.required('rejected as expected');
});
});
QUnit.test('Promise.try, subclassing', assert => {
const { try: promiseTry, resolve } = Promise;
function SubPromise(executor) {
executor(() => { /* empty */ }, () => { /* empty */ });
}
SubPromise.resolve = resolve.bind(Promise);
assert.true(promiseTry.call(SubPromise, () => 42) instanceof SubPromise, 'subclassing, `this` pattern');
function FakePromise1() { /* empty */ }
function FakePromise2(executor) {
executor(null, () => { /* empty */ });
}
function FakePromise3(executor) {
executor(() => { /* empty */ }, null);
}
FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = resolve.bind(Promise);
assert.throws(() => {
promiseTry.call(FakePromise1, () => 42);
}, 'NewPromiseCapability validations, #1');
assert.throws(() => {
promiseTry.call(FakePromise2, () => 42);
}, 'NewPromiseCapability validations, #2');
assert.throws(() => {
promiseTry.call(FakePromise3, () => 42);
}, 'NewPromiseCapability validations, #3');
});
QUnit.test('Promise.try, without constructor context', assert => {
const { try: promiseTry } = Promise;
assert.throws(() => promiseTry(() => 42), TypeError, 'Throws if called without a constructor context');
assert.throws(() => promiseTry.call(null, () => 42), TypeError, 'Throws if called with null as this');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.promise.with-resolvers.js
|
JavaScript
|
const { getPrototypeOf } = Object;
QUnit.test('Promise.withResolvers', assert => {
const { withResolvers } = Promise;
assert.isFunction(withResolvers);
assert.arity(withResolvers, 0);
assert.name(withResolvers, 'withResolvers');
assert.nonEnumerable(Promise, 'withResolvers');
assert.looksNative(withResolvers);
const d1 = Promise.withResolvers();
assert.same(getPrototypeOf(d1), Object.prototype, 'proto is Object.prototype');
assert.true(d1.promise instanceof Promise, 'promise is promise');
assert.isFunction(d1.resolve, 'resolve is function');
assert.isFunction(d1.reject, 'reject is function');
const promise = {};
const resolve = () => { /* empty */ };
let reject = () => { /* empty */ };
function P(exec) {
exec(resolve, reject);
return promise;
}
const d2 = withResolvers.call(P);
assert.same(d2.promise, promise, 'promise is promise #2');
assert.same(d2.resolve, resolve, 'resolve is resolve #2');
assert.same(d2.reject, reject, 'reject is reject #2');
reject = {};
assert.throws(() => withResolvers.call(P), TypeError, 'broken resolver');
assert.throws(() => withResolvers.call({}), TypeError, 'broken constructor #1');
assert.throws(() => withResolvers.call(null), TypeError, 'broken constructor #2');
});
QUnit.test('Promise.withResolvers, resolve', assert => {
const d = Promise.withResolvers();
d.resolve(42);
return d.promise.then(it => {
assert.same(it, 42, 'resolved as expected');
}, () => {
assert.avoid();
});
});
QUnit.test('Promise.withResolvers, reject', assert => {
const d = Promise.withResolvers();
d.reject(42);
return d.promise.then(() => {
assert.avoid();
}, error => {
assert.same(error, 42, 'rejected as expected');
});
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.apply.js
|
JavaScript
|
QUnit.test('Reflect.apply', assert => {
const { apply } = Reflect;
assert.isFunction(apply);
assert.arity(apply, 3);
assert.name(apply, 'apply');
assert.looksNative(apply);
assert.nonEnumerable(Reflect, 'apply');
assert.same(apply(Array.prototype.push, [1, 2], [3, 4, 5]), 5);
function f(a, b, c) {
return a + b + c;
}
f.apply = 42;
assert.same(apply(f, null, ['foo', 'bar', 'baz']), 'foobarbaz', 'works with redefined apply');
assert.throws(() => apply(42, null, []), TypeError, 'throws on primitive');
assert.throws(() => apply(() => { /* empty */ }, null), TypeError, 'throws without third argument');
assert.throws(() => apply(() => { /* empty */ }, null, '123'), TypeError, 'throws on primitive as third argument');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.construct.js
|
JavaScript
|
QUnit.test('Reflect.construct', assert => {
const { construct } = Reflect;
const { getPrototypeOf } = Object;
assert.isFunction(construct);
assert.arity(construct, 2);
assert.name(construct, 'construct');
assert.looksNative(construct);
assert.nonEnumerable(Reflect, 'construct');
function A(a, b, c) {
this.qux = a + b + c;
}
assert.same(construct(A, ['foo', 'bar', 'baz']).qux, 'foobarbaz', 'basic');
A.apply = 42;
assert.same(construct(A, ['foo', 'bar', 'baz']).qux, 'foobarbaz', 'works with redefined apply');
const instance = construct(function () {
this.x = 42;
}, [], Array);
assert.same(instance.x, 42, 'constructor with newTarget');
assert.true(instance instanceof Array, 'prototype with newTarget');
assert.throws(() => construct(42, []), TypeError, 'throws on primitive');
function B() { /* empty */ }
B.prototype = 42;
assert.notThrows(() => getPrototypeOf(construct(B, [])) === Object.prototype);
assert.notThrows(() => typeof construct(Date, []).getTime() == 'number', 'works with native constructors with 2 arguments');
assert.throws(() => construct(() => { /* empty */ }), 'throws when the second argument is not an object');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.define-property.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Reflect.defineProperty', assert => {
const { defineProperty } = Reflect;
const { getOwnPropertyDescriptor, create } = Object;
assert.isFunction(defineProperty);
assert.arity(defineProperty, 3);
assert.name(defineProperty, 'defineProperty');
assert.looksNative(defineProperty);
assert.nonEnumerable(Reflect, 'defineProperty');
let object = {};
assert.true(defineProperty(object, 'foo', { value: 123 }));
assert.same(object.foo, 123);
if (DESCRIPTORS) {
object = {};
defineProperty(object, 'foo', {
value: 123,
enumerable: true,
});
assert.deepEqual(getOwnPropertyDescriptor(object, 'foo'), {
value: 123,
enumerable: true,
configurable: false,
writable: false,
});
assert.false(defineProperty(object, 'foo', {
value: 42,
}));
}
assert.throws(() => defineProperty(42, 'foo', {
value: 42,
}), TypeError, 'throws on primitive');
assert.throws(() => defineProperty(42, 1, {}));
assert.throws(() => defineProperty({}, create(null), {}));
assert.throws(() => defineProperty({}, 1, 1));
// ToPropertyDescriptor errors should throw, not return false
assert.throws(() => defineProperty({}, 'a', { get: 42 }), TypeError, 'throws on non-callable getter');
assert.throws(() => defineProperty({}, 'a', { set: 'str' }), TypeError, 'throws on non-callable setter');
assert.throws(() => defineProperty({}, 'a', { get: null }), TypeError, 'throws on null getter');
assert.throws(() => defineProperty({}, 'a', { get: false }), TypeError, 'throws on false getter');
if (DESCRIPTORS) {
assert.throws(() => defineProperty({}, 'a', { get() { /* empty */ }, value: 1 }), TypeError, 'throws on mixed accessor/data descriptor');
assert.true(defineProperty({}, 'a', { get: undefined }), 'undefined getter is valid');
}
});
QUnit.test('Reflect.defineProperty.sham flag', assert => {
assert.same(Reflect.defineProperty.sham, DESCRIPTORS ? undefined : true);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.delete-property.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
import { createConversionChecker } from '../helpers/helpers.js';
QUnit.test('Reflect.deleteProperty', assert => {
const { deleteProperty } = Reflect;
const { defineProperty, keys } = Object;
assert.isFunction(deleteProperty);
assert.arity(deleteProperty, 2);
assert.name(deleteProperty, 'deleteProperty');
assert.looksNative(deleteProperty);
assert.nonEnumerable(Reflect, 'deleteProperty');
const object = { bar: 456 };
assert.true(deleteProperty(object, 'bar'));
assert.same(keys(object).length, 0);
if (DESCRIPTORS) {
assert.false(deleteProperty(defineProperty({}, 'foo', {
value: 42,
}), 'foo'));
}
assert.throws(() => deleteProperty(42, 'foo'), TypeError, 'throws on primitive');
// ToPropertyKey should be called exactly once
const keyObj = createConversionChecker(1, 'bar');
deleteProperty({ bar: 1 }, keyObj);
assert.same(keyObj.$valueOf, 0, 'ToPropertyKey called once in Reflect.deleteProperty, #1');
assert.same(keyObj.$toString, 1, 'ToPropertyKey called once in Reflect.deleteProperty, #2');
// argument order: target should be validated before ToPropertyKey
const orderChecker = createConversionChecker(1, 'qux');
assert.throws(() => deleteProperty(42, orderChecker), TypeError, 'throws on primitive before ToPropertyKey');
assert.same(orderChecker.$toString, 0, 'ToPropertyKey not called before target validation in Reflect.deleteProperty');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.get-own-property-descriptor.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Reflect.getOwnPropertyDescriptor', assert => {
const { getOwnPropertyDescriptor } = Reflect;
assert.isFunction(getOwnPropertyDescriptor);
assert.arity(getOwnPropertyDescriptor, 2);
assert.name(getOwnPropertyDescriptor, 'getOwnPropertyDescriptor');
assert.looksNative(getOwnPropertyDescriptor);
assert.nonEnumerable(Reflect, 'getOwnPropertyDescriptor');
const object = { baz: 789 };
const descriptor = getOwnPropertyDescriptor(object, 'baz');
assert.same(descriptor.value, 789);
assert.throws(() => getOwnPropertyDescriptor(42, 'constructor'), TypeError, 'throws on primitive');
});
QUnit.test('Reflect.getOwnPropertyDescriptor.sham flag', assert => {
assert.same(Reflect.getOwnPropertyDescriptor.sham, DESCRIPTORS ? undefined : true);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.get-prototype-of.js
|
JavaScript
|
import { CORRECT_PROTOTYPE_GETTER } from '../helpers/constants.js';
QUnit.test('Reflect.getPrototypeOf', assert => {
const { getPrototypeOf } = Reflect;
assert.isFunction(getPrototypeOf);
assert.arity(getPrototypeOf, 1);
assert.name(getPrototypeOf, 'getPrototypeOf');
assert.looksNative(getPrototypeOf);
assert.nonEnumerable(Reflect, 'getPrototypeOf');
assert.same(getPrototypeOf([]), Array.prototype);
assert.throws(() => getPrototypeOf(42), TypeError, 'throws on primitive');
});
QUnit.test('Reflect.getPrototypeOf.sham flag', assert => {
assert.same(Reflect.getPrototypeOf.sham, CORRECT_PROTOTYPE_GETTER ? undefined : true);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.get.js
|
JavaScript
|
import { DESCRIPTORS, NATIVE } from '../helpers/constants.js';
import { createConversionChecker } from '../helpers/helpers.js';
QUnit.test('Reflect.get', assert => {
const { defineProperty, create } = Object;
const { get } = Reflect;
assert.isFunction(get);
if (NATIVE) assert.arity(get, 2);
assert.name(get, 'get');
assert.looksNative(get);
assert.nonEnumerable(Reflect, 'get');
assert.same(get({ qux: 987 }, 'qux'), 987);
if (DESCRIPTORS) {
const target = create(defineProperty({ z: 3 }, 'w', {
get() {
return this;
},
}), {
x: {
value: 1,
},
y: {
get() {
return this;
},
},
});
const receiver = {};
assert.same(get(target, 'x', receiver), 1, 'get x');
assert.same(get(target, 'y', receiver), receiver, 'get y');
assert.same(get(target, 'z', receiver), 3, 'get z');
assert.same(get(target, 'w', receiver), receiver, 'get w');
assert.same(get(target, 'u', receiver), undefined, 'get u');
// ToPropertyKey should be called exactly once even with prototype chain traversal
const keyObj = createConversionChecker(1, 'x');
get(create({ x: 42 }), keyObj, {});
assert.same(keyObj.$valueOf, 0, 'ToPropertyKey called once in Reflect.get, #1');
assert.same(keyObj.$toString, 1, 'ToPropertyKey called once in Reflect.get, #2');
}
assert.throws(() => get(42, 'constructor'), TypeError, 'throws on primitive');
// argument order: target should be validated before ToPropertyKey
const orderChecker = createConversionChecker(1, 'qux');
assert.throws(() => get(42, orderChecker), TypeError, 'throws on primitive before ToPropertyKey');
assert.same(orderChecker.$toString, 0, 'ToPropertyKey not called before target validation in Reflect.get');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.has.js
|
JavaScript
|
QUnit.test('Reflect.has', assert => {
const { has } = Reflect;
assert.isFunction(has);
assert.arity(has, 2);
assert.name(has, 'has');
assert.looksNative(has);
assert.nonEnumerable(Reflect, 'has');
const object = { qux: 987 };
assert.true(has(object, 'qux'));
assert.false(has(object, 'qwe'));
assert.true(has(object, 'toString'));
assert.throws(() => has(42, 'constructor'), TypeError, 'throws on primitive');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.is-extensible.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Reflect.isExtensible', assert => {
const { isExtensible } = Reflect;
const { preventExtensions } = Object;
assert.isFunction(isExtensible);
assert.arity(isExtensible, 1);
assert.name(isExtensible, 'isExtensible');
assert.looksNative(isExtensible);
assert.nonEnumerable(Reflect, 'isExtensible');
assert.true(isExtensible({}));
if (DESCRIPTORS) {
assert.false(isExtensible(preventExtensions({})));
}
assert.throws(() => isExtensible(42), TypeError, 'throws on primitive');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.own-keys.js
|
JavaScript
|
import { includes } from '../helpers/helpers.js';
QUnit.test('Reflect.ownKeys', assert => {
const { ownKeys } = Reflect;
const { defineProperty, create } = Object;
const symbol = Symbol('c');
assert.isFunction(ownKeys);
assert.arity(ownKeys, 1);
assert.name(ownKeys, 'ownKeys');
assert.looksNative(ownKeys);
assert.nonEnumerable(Reflect, 'ownKeys');
const object = { a: 1 };
defineProperty(object, 'b', {
value: 2,
});
object[symbol] = 3;
let keys = ownKeys(object);
assert.same(keys.length, 3, 'ownKeys return all own keys');
assert.true(includes(keys, 'a'), 'ownKeys return all own keys: simple');
assert.true(includes(keys, 'b'), 'ownKeys return all own keys: hidden');
assert.same(object[keys[2]], 3, 'ownKeys return all own keys: symbol');
keys = ownKeys(create(object));
assert.same(keys.length, 0, 'ownKeys return only own keys');
assert.throws(() => ownKeys(42), TypeError, 'throws on primitive');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.prevent-extensions.js
|
JavaScript
|
import { DESCRIPTORS, FREEZING } from '../helpers/constants.js';
QUnit.test('Reflect.preventExtensions', assert => {
const { preventExtensions } = Reflect;
const { isExtensible } = Object;
assert.isFunction(preventExtensions);
assert.arity(preventExtensions, 1);
assert.name(preventExtensions, 'preventExtensions');
assert.looksNative(preventExtensions);
assert.nonEnumerable(Reflect, 'preventExtensions');
const object = {};
assert.true(preventExtensions(object));
if (DESCRIPTORS) {
assert.false(isExtensible(object));
}
assert.throws(() => preventExtensions(42), TypeError, 'throws on primitive');
});
QUnit.test('Reflect.preventExtensions.sham flag', assert => {
assert.same(Reflect.preventExtensions.sham, FREEZING ? undefined : true);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.set-prototype-of.js
|
JavaScript
|
import { NATIVE, PROTO } from '../helpers/constants.js';
if (PROTO) QUnit.test('Reflect.setPrototypeOf', assert => {
const { setPrototypeOf } = Reflect;
assert.isFunction(setPrototypeOf);
if (NATIVE) assert.arity(setPrototypeOf, 2);
assert.name(setPrototypeOf, 'setPrototypeOf');
assert.looksNative(setPrototypeOf);
assert.nonEnumerable(Reflect, 'setPrototypeOf');
let object = {};
assert.true(setPrototypeOf(object, Array.prototype));
assert.true(object instanceof Array);
assert.throws(() => setPrototypeOf({}, 42), TypeError);
assert.throws(() => setPrototypeOf(42, {}), TypeError, 'throws on primitive');
object = {};
assert.false(setPrototypeOf(object, object), 'false on recursive __proto__');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.set.js
|
JavaScript
|
import { DESCRIPTORS, NATIVE } from '../helpers/constants.js';
import { createConversionChecker } from '../helpers/helpers.js';
QUnit.test('Reflect.set', assert => {
const { set } = Reflect;
const { defineProperty, getOwnPropertyDescriptor, create, getPrototypeOf } = Object;
assert.isFunction(set);
if (NATIVE) assert.arity(set, 3);
assert.name(set, 'set');
assert.looksNative(set);
assert.nonEnumerable(Reflect, 'set');
const object = {};
assert.true(set(object, 'quux', 654));
assert.same(object.quux, 654);
let target = {};
const receiver = {};
set(target, 'foo', 1, receiver);
assert.same(target.foo, undefined, 'target.foo === undefined');
assert.same(receiver.foo, 1, 'receiver.foo === 1');
if (DESCRIPTORS) {
defineProperty(receiver, 'bar', {
value: 0,
writable: true,
enumerable: false,
configurable: true,
});
set(target, 'bar', 1, receiver);
assert.same(receiver.bar, 1, 'receiver.bar === 1');
assert.false(getOwnPropertyDescriptor(receiver, 'bar').enumerable, 'enumerability not overridden');
let out;
target = create(defineProperty({ z: 3 }, 'w', {
set() {
out = this;
},
}), {
x: {
value: 1,
writable: true,
configurable: true,
},
y: {
set() {
out = this;
},
},
c: {
value: 1,
writable: false,
configurable: false,
},
});
assert.true(set(target, 'x', 2, target), 'set x');
assert.same(target.x, 2, 'set x');
out = null;
assert.true(set(target, 'y', 2, target), 'set y');
assert.same(out, target, 'set y');
assert.true(set(target, 'z', 4, target));
assert.same(target.z, 4, 'set z');
out = null;
assert.true(set(target, 'w', 1, target), 'set w');
assert.same(out, target, 'set w');
assert.true(set(target, 'u', 0, target), 'set u');
assert.same(target.u, 0, 'set u');
assert.false(set(target, 'c', 2, target), 'set c');
assert.same(target.c, 1, 'set c');
// https://github.com/zloirock/core-js/issues/392
let o = defineProperty({}, 'test', {
writable: false,
configurable: true,
});
assert.false(set(getPrototypeOf(o), 'test', 1, o));
// https://github.com/zloirock/core-js/issues/393
o = defineProperty({}, 'test', {
get() { /* empty */ },
});
assert.notThrows(() => !set(getPrototypeOf(o), 'test', 1, o));
o = defineProperty({}, 'test', {
// eslint-disable-next-line no-unused-vars -- required for testing
set(v) { /* empty */ },
});
assert.notThrows(() => !set(getPrototypeOf(o), 'test', 1, o));
// accessor descriptor with get: undefined, set: undefined on receiver should return false
const accessorReceiver = {};
defineProperty(accessorReceiver, 'prop', { get: undefined, set: undefined, configurable: true });
const accessorTarget = defineProperty({}, 'prop', { value: 1, writable: true, configurable: true });
assert.false(set(accessorTarget, 'prop', 2, accessorReceiver), 'accessor descriptor on receiver with undefined get/set');
// ToPropertyKey should be called exactly once
const keyObj = createConversionChecker(1, 'x');
set(create({ x: 42 }), keyObj, 1);
assert.same(keyObj.$valueOf, 0, 'ToPropertyKey called once in Reflect.set, #1');
assert.same(keyObj.$toString, 1, 'ToPropertyKey called once in Reflect.set, #2');
}
assert.throws(() => set(42, 'q', 42), TypeError, 'throws on primitive');
// Reflect.set should pass only { value: V } to [[DefineOwnProperty]] when updating existing data property
if (DESCRIPTORS) {
const obj = defineProperty({}, 'x', { value: 1, writable: true, enumerable: true, configurable: true });
assert.true(set(obj, 'x', 42), 'set existing writable property');
const desc = getOwnPropertyDescriptor(obj, 'x');
assert.same(desc.value, 42, 'value updated');
assert.true(desc.writable, 'writable preserved');
assert.true(desc.enumerable, 'enumerable preserved');
assert.true(desc.configurable, 'configurable preserved');
}
// argument order: target should be validated before ToPropertyKey
const orderChecker = createConversionChecker(1, 'qux');
assert.throws(() => set(42, orderChecker, 1), TypeError, 'throws on primitive before ToPropertyKey');
assert.same(orderChecker.$toString, 0, 'ToPropertyKey not called before target validation in Reflect.set');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.reflect.to-string-tag.js
|
JavaScript
|
QUnit.test('Reflect[@@toStringTag]', assert => {
assert.same(Reflect[Symbol.toStringTag], 'Reflect', 'Reflect[@@toStringTag] is `Reflect`');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.constructor.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals, regexp/no-invalid-regexp, regexp/sort-flags -- required for testing */
/* eslint-disable regexp/no-useless-assertions, regexp/no-useless-character-class, regexp/no-useless-flag -- required for testing */
import { DESCRIPTORS, GLOBAL } from '../helpers/constants.js';
import { nativeSubclass } from '../helpers/helpers.js';
const { getPrototypeOf } = Object;
if (DESCRIPTORS) {
QUnit.test('RegExp constructor', assert => {
const Symbol = GLOBAL.Symbol || {};
assert.isFunction(RegExp);
assert.arity(RegExp, 2);
assert.name(RegExp, 'RegExp');
assert.looksNative(RegExp);
assert.same({}.toString.call(RegExp()).slice(8, -1), 'RegExp');
assert.same({}.toString.call(new RegExp()).slice(8, -1), 'RegExp');
let regexp = /a/g;
assert.notSame(regexp, new RegExp(regexp), 'new RegExp(regexp) is not regexp');
assert.same(regexp, RegExp(regexp), 'RegExp(regexp) is regexp');
regexp[Symbol.match] = false;
assert.notSame(regexp, RegExp(regexp), 'RegExp(regexp) is not regexp, changed Symbol.match');
const object = {};
assert.notSame(object, RegExp(object), 'RegExp(O) is not O');
object[Symbol.match] = true;
object.constructor = RegExp;
assert.same(object, RegExp(object), 'RegExp(O) is O, changed Symbol.match');
assert.same(String(regexp), '/a/g', 'b is /a/g');
assert.same(String(new RegExp(/a/g, 'mi')), '/a/im', 'Allows a regex with flags');
assert.true(new RegExp(/a/g, 'im') instanceof RegExp, 'Works with instanceof');
assert.same(new RegExp(/a/g, 'im').constructor, RegExp, 'Has the right constructor');
const orig = /^https?:\/\//i;
regexp = new RegExp(orig);
assert.notSame(regexp, orig, 'new + re + no flags #1');
assert.same(String(regexp), '/^https?:\\/\\//i', 'new + re + no flags #2');
let result = regexp.exec('http://github.com');
assert.deepEqual(result, ['http://'], 'new + re + no flags #3');
/(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)/.exec('abcdefghijklmnopq');
result = true;
const characters = 'bcdefghij';
for (let i = 0, { length } = characters; i < length; ++i) {
const chr = characters[i];
if (RegExp[`$${ i + 1 }`] !== chr) {
result = false;
}
}
assert.true(result, 'Updates RegExp globals');
if (nativeSubclass) {
const Subclass = nativeSubclass(RegExp);
assert.true(new Subclass() instanceof Subclass, 'correct subclassing with native classes #1');
assert.true(new Subclass() instanceof RegExp, 'correct subclassing with native classes #2');
assert.true(new Subclass('^abc$').test('abc'), 'correct subclassing with native classes #3');
}
assert.throws(() => RegExp(Symbol(1)), 'throws on symbol argument');
});
QUnit.test('RegExp dotAll', assert => {
assert.false(RegExp('.', '').test('\n'), 'dotAll missed');
assert.true(RegExp('.', 's').test('\n'), 'dotAll basic');
assert.false(RegExp('[.]', 's').test('\n'), 'dotAll brackets #1');
assert.false(RegExp('[.].', '').test('.\n'), 'dotAll brackets #2');
assert.true(RegExp('[.].', 's').test('.\n'), 'dotAll brackets #3');
assert.true(RegExp('[[].', 's').test('[\n'), 'dotAll brackets #4');
assert.same(RegExp('.[.[].\\..', 's').source, '.[.[].\\..', 'dotAll correct source');
const string = '123\n456789\n012';
const re = RegExp('(\\d{3}).\\d{3}', 'sy');
let match = re.exec(string);
assert.same(match[1], '123', 's with y #1');
assert.same(re.lastIndex, 7, 's with y #2');
match = re.exec(string);
assert.same(match[1], '789', 's with y #3');
assert.same(re.lastIndex, 14, 's with y #4');
// dotAll combined with NCG - groups should be populated
const dotAllNCG = RegExp('(?<a>.).(?<b>.)', 's').exec('a\nb');
assert.same(dotAllNCG?.groups?.a, 'a', 'dotAll + NCG groups #1');
assert.same(dotAllNCG?.groups?.b, 'b', 'dotAll + NCG groups #2');
});
QUnit.test('RegExp NCG', assert => {
assert.same(RegExp('(?<a>b)').exec('b').groups?.a, 'b', 'NCG #1');
// eslint-disable-next-line regexp/no-unused-capturing-group -- required for testing
assert.same(RegExp('(b)').exec('b').groups, undefined, 'NCG #2');
const { groups } = RegExp('foo:(?<foo>\\w+),bar:(?<bar>\\w+)').exec('foo:abc,bar:def');
assert.same(getPrototypeOf(groups), null, 'null prototype');
assert.deepEqual(groups, { foo: 'abc', bar: 'def' }, 'NCG #3');
// eslint-disable-next-line regexp/no-useless-non-capturing-group -- required for testing
const { groups: nonCaptured, length } = RegExp('foo:(?:value=(?<foo>\\w+)),bar:(?:value=(?<bar>\\w+))').exec('foo:value=abc,bar:value=def');
assert.deepEqual(nonCaptured, { foo: 'abc', bar: 'def' }, 'NCG #4');
assert.same(length, 3, 'incorrect number of matched entries #1');
// eslint-disable-next-line regexp/no-unused-capturing-group -- required for testing
const { groups: skipBar } = RegExp('foo:(?<foo>\\w+),bar:(\\w+),buz:(?<buz>\\w+)').exec('foo:abc,bar:def,buz:ghi');
assert.deepEqual(skipBar, { foo: 'abc', buz: 'ghi' }, 'NCG #5');
// fails in Safari
// assert.same(Object.getPrototypeOf(groups), null, 'NCG #4');
assert.same('foo:abc,bar:def'.replace(RegExp('foo:(?<foo>\\w+),bar:(?<bar>\\w+)'), '$<bar>,$<foo>'), 'def,abc', 'replace #1');
assert.same('foo:abc,bar:def'.replace(RegExp('foo:(?<foo>\\w+),bar:(?<bar>\\w+)'), (...args) => {
const { foo, bar } = args.pop();
return `${ bar },${ foo }`;
}), 'def,abc', 'replace #2');
assert.same('12345'.replaceAll(RegExp('(?<d>[2-4])', 'g'), '$<d>$<d>'), '12233445', 'replaceAll');
assert.throws(() => RegExp('(?<1a>b)'), SyntaxError, 'incorrect group name #1');
assert.throws(() => RegExp('(?<a#>b)'), SyntaxError, 'incorrect group name #2');
assert.throws(() => RegExp('(?< a >b)'), SyntaxError, 'incorrect group name #3');
// regression — lookahead / lookbehind assertions should not increment group counter
assert.same(RegExp('(?=b)(?<a>b)').exec('b').groups?.a, 'b', 'NCG with positive lookahead');
assert.same(RegExp('(?!c)(?<a>b)').exec('b').groups?.a, 'b', 'NCG with negative lookahead');
// prevent crash in ancient engines without lookbehind support
try {
assert.same(RegExp('(?<=a)(?<b>b)').exec('ab').groups?.b, 'b', 'NCG with positive lookbehind');
assert.same(RegExp('(?<!c)(?<a>b)').exec('ab').groups?.a, 'b', 'NCG with negative lookbehind');
} catch { /* empty */ }
// eslint-disable-next-line regexp/no-unused-capturing-group -- required for testing
assert.same(RegExp('(?=b)(b)(?<a>c)').exec('bc').groups?.a, 'c', 'NCG with lookahead and capturing group');
// named backreferences
assert.same(RegExp('(?<year>\\d{4})-\\k<year>').exec('2024-2024')?.[0], '2024-2024', 'NCG \\k backreference #1');
assert.same(RegExp('(?<year>\\d{4})-\\k<year>').exec('2024-2025'), null, 'NCG \\k backreference #2');
assert.same(RegExp('(?<a>.)(?<b>.)\\k<b>\\k<a>').exec('abba')?.[0], 'abba', 'NCG \\k multiple backreferences');
// escaped backslash before `k<name>` should not be treated as backreference
// eslint-disable-next-line regexp/no-unused-capturing-group -- required for testing
assert.same(RegExp('(?<a>x)\\\\k<a>').exec('x\\k<a>')?.[0], 'x\\k<a>', 'NCG \\\\k not confused with \\k backreference');
assert.same(RegExp('(?<a>x)\\\\\\k<a>').exec('x\\x')?.[0], 'x\\x', 'NCG escaped backslash before backreference');
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.dot-all.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals -- required for testing */
import { DESCRIPTORS } from '../helpers/constants.js';
if (DESCRIPTORS) {
QUnit.test('RegExp#dotAll', assert => {
const re = RegExp('.', 's');
assert.true(re.dotAll, '.dotAll is true');
assert.same(re.flags, 's', '.flags contains s');
assert.false(RegExp('.').dotAll, 'no');
assert.false(/a/.dotAll, 'no in literal');
const dotAllGetter = Object.getOwnPropertyDescriptor(RegExp.prototype, 'dotAll').get;
if (typeof dotAllGetter == 'function') {
assert.throws(() => {
dotAllGetter.call({});
}, undefined, '.dotAll getter can only be called on RegExp instances');
try {
dotAllGetter.call(/a/);
assert.required('.dotAll getter works on literals');
} catch {
assert.avoid('.dotAll getter works on literals');
}
try {
dotAllGetter.call(new RegExp('a'));
assert.required('.dotAll getter works on instances');
} catch {
assert.avoid('.dotAll getter works on instances');
}
assert.true(Object.hasOwn(RegExp.prototype, 'dotAll'), 'prototype has .dotAll property');
}
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.escape.js
|
JavaScript
|
/* eslint-disable @stylistic/max-len -- ok*/
QUnit.test('RegExp.escape', assert => {
const { escape } = RegExp;
assert.isFunction(escape);
assert.arity(escape, 1);
assert.name(escape, 'escape');
assert.looksNative(escape);
assert.nonEnumerable(RegExp, 'escape');
assert.same(escape('10$'), '\\x310\\$', '10$');
assert.same(escape('abcdefg_123456'), '\\x61bcdefg_123456', 'abcdefg_123456');
assert.same(escape('Привет'), 'Привет', 'Привет');
assert.same(
escape('(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`'),
'\\(\\)\\{\\}\\[\\]\\|\\x2c\\.\\?\\*\\+\\x2d\\^\\$\\x3d\\x3c\\x3e\\\\\\/\\x23\\x26\\x21\\x25\\x3a\\x3b\\x40\\x7e\\x27\\x22\\x60',
'(){}[]|,.?*+-^$=<>\\/#&!%:;@~\'"`',
);
assert.same(
escape('\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF'),
'\\t\\n\\v\\f\\r\\x20\\xa0\\u1680\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000\\u2028\\u2029\\ufeff',
'whitespaces and control',
);
assert.throws(() => escape(42), TypeError, 'throws on non-string #1');
assert.throws(() => escape({}), TypeError, 'throws on non-string #2');
// Test262
// Copyright 2024 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the https://github.com/tc39/test262/blob/main/LICENSE file.
assert.same(escape('\u2028'), '\\u2028', 'line terminator \\u2028 is escaped correctly to \\\\u2028');
assert.same(escape('\u2029'), '\\u2029', 'line terminator \\u2029 is escaped correctly to \\\\u2029');
assert.same(escape('\u2028\u2029'), '\\u2028\\u2029', 'line terminators are escaped correctly');
assert.same(escape('\u2028a\u2029a'), '\\u2028a\\u2029a', 'mixed line terminators are escaped correctly');
assert.same(escape('.a/b'), '\\.a\\/b', 'mixed string with solidus character is escaped correctly');
assert.same(escape('/./'), '\\/\\.\\/', 'solidus character is escaped correctly - regexp similar');
assert.same(escape('./a\\/*b+c?d^e$f|g{2}h[i]j\\k'), '\\.\\/a\\\\\\/\\*b\\+c\\?d\\^e\\$f\\|g\\{2\\}h\\[i\\]j\\\\k', 'complex string with multiple special characters is escaped correctly');
assert.same(escape('/'), '\\/', 'solidus character is escaped correctly');
assert.same(escape('//'), '\\/\\/', 'solidus character is escaped correctly - multiple occurrences 1');
assert.same(escape('///'), '\\/\\/\\/', 'solidus character is escaped correctly - multiple occurrences 2');
assert.same(escape('////'), '\\/\\/\\/\\/', 'solidus character is escaped correctly - multiple occurrences 3');
assert.same(escape('.'), '\\.', 'dot character is escaped correctly');
assert.same(escape('*'), '\\*', 'asterisk character is escaped correctly');
assert.same(escape('+'), '\\+', 'plus character is escaped correctly');
assert.same(escape('?'), '\\?', 'question mark character is escaped correctly');
assert.same(escape('^'), '\\^', 'caret character is escaped correctly');
assert.same(escape('$'), '\\$', 'dollar character is escaped correctly');
assert.same(escape('|'), '\\|', 'pipe character is escaped correctly');
assert.same(escape('('), '\\(', 'open parenthesis character is escaped correctly');
assert.same(escape(')'), '\\)', 'close parenthesis character is escaped correctly');
assert.same(escape('['), '\\[', 'open bracket character is escaped correctly');
assert.same(escape(']'), '\\]', 'close bracket character is escaped correctly');
assert.same(escape('{'), '\\{', 'open brace character is escaped correctly');
assert.same(escape('}'), '\\}', 'close brace character is escaped correctly');
assert.same(escape('\\'), '\\\\', 'backslash character is escaped correctly');
const codePoints = String.fromCharCode(0x100, 0x200, 0x300);
assert.same(escape(codePoints), codePoints, 'characters are correctly not escaped');
assert.same(escape('你好'), '你好', 'Chinese characters are correctly not escaped');
assert.same(escape('こんにちは'), 'こんにちは', 'Japanese characters are correctly not escaped');
assert.same(escape('안녕하세요'), '안녕하세요', 'Korean characters are correctly not escaped');
assert.same(escape('Привет'), 'Привет', 'Cyrillic characters are correctly not escaped');
assert.same(escape('مرحبا'), 'مرحبا', 'Arabic characters are correctly not escaped');
assert.same(escape('हेलो'), 'हेलो', 'Devanagari characters are correctly not escaped');
assert.same(escape('Γειά σου'), 'Γειά\\x20σου', 'Greek characters are correctly not escaped');
assert.same(escape('שלום'), 'שלום', 'Hebrew characters are correctly not escaped');
assert.same(escape('สวัสดี'), 'สวัสดี', 'Thai characters are correctly not escaped');
assert.same(escape('नमस्ते'), 'नमस्ते', 'Hindi characters are correctly not escaped');
assert.same(escape('ሰላም'), 'ሰላም', 'Amharic characters are correctly not escaped');
assert.same(escape('हैलो'), 'हैलो', 'Hindi characters with diacritics are correctly not escaped');
assert.same(escape('안녕!'), '안녕\\x21', 'Korean character with special character is correctly escaped');
assert.same(escape('.hello\uD7FFworld'), '\\.hello\uD7FFworld', 'Mixed ASCII and Unicode characters are correctly escaped');
assert.same(escape('\uFEFF'), '\\ufeff', 'whitespace \\uFEFF is escaped correctly to \\uFEFF');
assert.same(escape('\u0020'), '\\x20', 'whitespace \\u0020 is escaped correctly to \\x20');
assert.same(escape('\u00A0'), '\\xa0', 'whitespace \\u00A0 is escaped correctly to \\xA0');
assert.same(escape('\u202F'), '\\u202f', 'whitespace \\u202F is escaped correctly to \\u202F');
assert.same(escape('\u0009'), '\\t', 'whitespace \\u0009 is escaped correctly to \\t');
assert.same(escape('\u000B'), '\\v', 'whitespace \\u000B is escaped correctly to \\v');
assert.same(escape('\u000C'), '\\f', 'whitespace \\u000C is escaped correctly to \\f');
assert.same(escape('\uFEFF\u0020\u00A0\u202F\u0009\u000B\u000C'), '\\ufeff\\x20\\xa0\\u202f\\t\\v\\f', 'whitespaces are escaped correctly');
// Escaping initial digits
assert.same(escape('1111'), '\\x31111', 'Initial decimal digit 1 is escaped');
assert.same(escape('2222'), '\\x32222', 'Initial decimal digit 2 is escaped');
assert.same(escape('3333'), '\\x33333', 'Initial decimal digit 3 is escaped');
assert.same(escape('4444'), '\\x34444', 'Initial decimal digit 4 is escaped');
assert.same(escape('5555'), '\\x35555', 'Initial decimal digit 5 is escaped');
assert.same(escape('6666'), '\\x36666', 'Initial decimal digit 6 is escaped');
assert.same(escape('7777'), '\\x37777', 'Initial decimal digit 7 is escaped');
assert.same(escape('8888'), '\\x38888', 'Initial decimal digit 8 is escaped');
assert.same(escape('9999'), '\\x39999', 'Initial decimal digit 9 is escaped');
assert.same(escape('0000'), '\\x30000', 'Initial decimal digit 0 is escaped');
// Escaping initial ASCII letters
assert.same(escape('aaa'), '\\x61aa', 'Initial ASCII letter a is escaped');
assert.same(escape('bbb'), '\\x62bb', 'Initial ASCII letter b is escaped');
assert.same(escape('ccc'), '\\x63cc', 'Initial ASCII letter c is escaped');
assert.same(escape('ddd'), '\\x64dd', 'Initial ASCII letter d is escaped');
assert.same(escape('eee'), '\\x65ee', 'Initial ASCII letter e is escaped');
assert.same(escape('fff'), '\\x66ff', 'Initial ASCII letter f is escaped');
assert.same(escape('ggg'), '\\x67gg', 'Initial ASCII letter g is escaped');
assert.same(escape('hhh'), '\\x68hh', 'Initial ASCII letter h is escaped');
assert.same(escape('iii'), '\\x69ii', 'Initial ASCII letter i is escaped');
assert.same(escape('jjj'), '\\x6ajj', 'Initial ASCII letter j is escaped');
assert.same(escape('kkk'), '\\x6bkk', 'Initial ASCII letter k is escaped');
assert.same(escape('lll'), '\\x6cll', 'Initial ASCII letter l is escaped');
assert.same(escape('mmm'), '\\x6dmm', 'Initial ASCII letter m is escaped');
assert.same(escape('nnn'), '\\x6enn', 'Initial ASCII letter n is escaped');
assert.same(escape('ooo'), '\\x6foo', 'Initial ASCII letter o is escaped');
assert.same(escape('ppp'), '\\x70pp', 'Initial ASCII letter p is escaped');
assert.same(escape('qqq'), '\\x71qq', 'Initial ASCII letter q is escaped');
assert.same(escape('rrr'), '\\x72rr', 'Initial ASCII letter r is escaped');
assert.same(escape('sss'), '\\x73ss', 'Initial ASCII letter s is escaped');
assert.same(escape('ttt'), '\\x74tt', 'Initial ASCII letter t is escaped');
assert.same(escape('uuu'), '\\x75uu', 'Initial ASCII letter u is escaped');
assert.same(escape('vvv'), '\\x76vv', 'Initial ASCII letter v is escaped');
assert.same(escape('www'), '\\x77ww', 'Initial ASCII letter w is escaped');
assert.same(escape('xxx'), '\\x78xx', 'Initial ASCII letter x is escaped');
assert.same(escape('yyy'), '\\x79yy', 'Initial ASCII letter y is escaped');
assert.same(escape('zzz'), '\\x7azz', 'Initial ASCII letter z is escaped');
assert.same(escape('AAA'), '\\x41AA', 'Initial ASCII letter A is escaped');
assert.same(escape('BBB'), '\\x42BB', 'Initial ASCII letter B is escaped');
assert.same(escape('CCC'), '\\x43CC', 'Initial ASCII letter C is escaped');
assert.same(escape('DDD'), '\\x44DD', 'Initial ASCII letter D is escaped');
assert.same(escape('EEE'), '\\x45EE', 'Initial ASCII letter E is escaped');
assert.same(escape('FFF'), '\\x46FF', 'Initial ASCII letter F is escaped');
assert.same(escape('GGG'), '\\x47GG', 'Initial ASCII letter G is escaped');
assert.same(escape('HHH'), '\\x48HH', 'Initial ASCII letter H is escaped');
assert.same(escape('III'), '\\x49II', 'Initial ASCII letter I is escaped');
assert.same(escape('JJJ'), '\\x4aJJ', 'Initial ASCII letter J is escaped');
assert.same(escape('KKK'), '\\x4bKK', 'Initial ASCII letter K is escaped');
assert.same(escape('LLL'), '\\x4cLL', 'Initial ASCII letter L is escaped');
assert.same(escape('MMM'), '\\x4dMM', 'Initial ASCII letter M is escaped');
assert.same(escape('NNN'), '\\x4eNN', 'Initial ASCII letter N is escaped');
assert.same(escape('OOO'), '\\x4fOO', 'Initial ASCII letter O is escaped');
assert.same(escape('PPP'), '\\x50PP', 'Initial ASCII letter P is escaped');
assert.same(escape('QQQ'), '\\x51QQ', 'Initial ASCII letter Q is escaped');
assert.same(escape('RRR'), '\\x52RR', 'Initial ASCII letter R is escaped');
assert.same(escape('SSS'), '\\x53SS', 'Initial ASCII letter S is escaped');
assert.same(escape('TTT'), '\\x54TT', 'Initial ASCII letter T is escaped');
assert.same(escape('UUU'), '\\x55UU', 'Initial ASCII letter U is escaped');
assert.same(escape('VVV'), '\\x56VV', 'Initial ASCII letter V is escaped');
assert.same(escape('WWW'), '\\x57WW', 'Initial ASCII letter W is escaped');
assert.same(escape('XXX'), '\\x58XX', 'Initial ASCII letter X is escaped');
assert.same(escape('YYY'), '\\x59YY', 'Initial ASCII letter Y is escaped');
assert.same(escape('ZZZ'), '\\x5aZZ', 'Initial ASCII letter Z is escaped');
// Mixed case with special characters
assert.same(escape('1+1'), '\\x31\\+1', 'Initial decimal digit 1 with special character is escaped');
assert.same(escape('2+2'), '\\x32\\+2', 'Initial decimal digit 2 with special character is escaped');
assert.same(escape('3+3'), '\\x33\\+3', 'Initial decimal digit 3 with special character is escaped');
assert.same(escape('4+4'), '\\x34\\+4', 'Initial decimal digit 4 with special character is escaped');
assert.same(escape('5+5'), '\\x35\\+5', 'Initial decimal digit 5 with special character is escaped');
assert.same(escape('6+6'), '\\x36\\+6', 'Initial decimal digit 6 with special character is escaped');
assert.same(escape('7+7'), '\\x37\\+7', 'Initial decimal digit 7 with special character is escaped');
assert.same(escape('8+8'), '\\x38\\+8', 'Initial decimal digit 8 with special character is escaped');
assert.same(escape('9+9'), '\\x39\\+9', 'Initial decimal digit 9 with special character is escaped');
assert.same(escape('0+0'), '\\x30\\+0', 'Initial decimal digit 0 with special character is escaped');
assert.same(escape('a*a'), '\\x61\\*a', 'Initial ASCII letter a with special character is escaped');
assert.same(escape('b*b'), '\\x62\\*b', 'Initial ASCII letter b with special character is escaped');
assert.same(escape('c*c'), '\\x63\\*c', 'Initial ASCII letter c with special character is escaped');
assert.same(escape('d*d'), '\\x64\\*d', 'Initial ASCII letter d with special character is escaped');
assert.same(escape('e*e'), '\\x65\\*e', 'Initial ASCII letter e with special character is escaped');
assert.same(escape('f*f'), '\\x66\\*f', 'Initial ASCII letter f with special character is escaped');
assert.same(escape('g*g'), '\\x67\\*g', 'Initial ASCII letter g with special character is escaped');
assert.same(escape('h*h'), '\\x68\\*h', 'Initial ASCII letter h with special character is escaped');
assert.same(escape('i*i'), '\\x69\\*i', 'Initial ASCII letter i with special character is escaped');
assert.same(escape('j*j'), '\\x6a\\*j', 'Initial ASCII letter j with special character is escaped');
assert.same(escape('k*k'), '\\x6b\\*k', 'Initial ASCII letter k with special character is escaped');
assert.same(escape('l*l'), '\\x6c\\*l', 'Initial ASCII letter l with special character is escaped');
assert.same(escape('m*m'), '\\x6d\\*m', 'Initial ASCII letter m with special character is escaped');
assert.same(escape('n*n'), '\\x6e\\*n', 'Initial ASCII letter n with special character is escaped');
assert.same(escape('o*o'), '\\x6f\\*o', 'Initial ASCII letter o with special character is escaped');
assert.same(escape('p*p'), '\\x70\\*p', 'Initial ASCII letter p with special character is escaped');
assert.same(escape('q*q'), '\\x71\\*q', 'Initial ASCII letter q with special character is escaped');
assert.same(escape('r*r'), '\\x72\\*r', 'Initial ASCII letter r with special character is escaped');
assert.same(escape('s*s'), '\\x73\\*s', 'Initial ASCII letter s with special character is escaped');
assert.same(escape('t*t'), '\\x74\\*t', 'Initial ASCII letter t with special character is escaped');
assert.same(escape('u*u'), '\\x75\\*u', 'Initial ASCII letter u with special character is escaped');
assert.same(escape('v*v'), '\\x76\\*v', 'Initial ASCII letter v with special character is escaped');
assert.same(escape('w*w'), '\\x77\\*w', 'Initial ASCII letter w with special character is escaped');
assert.same(escape('x*x'), '\\x78\\*x', 'Initial ASCII letter x with special character is escaped');
assert.same(escape('y*y'), '\\x79\\*y', 'Initial ASCII letter y with special character is escaped');
assert.same(escape('z*z'), '\\x7a\\*z', 'Initial ASCII letter z with special character is escaped');
assert.same(escape('A*A'), '\\x41\\*A', 'Initial ASCII letter A with special character is escaped');
assert.same(escape('B*B'), '\\x42\\*B', 'Initial ASCII letter B with special character is escaped');
assert.same(escape('C*C'), '\\x43\\*C', 'Initial ASCII letter C with special character is escaped');
assert.same(escape('D*D'), '\\x44\\*D', 'Initial ASCII letter D with special character is escaped');
assert.same(escape('E*E'), '\\x45\\*E', 'Initial ASCII letter E with special character is escaped');
assert.same(escape('F*F'), '\\x46\\*F', 'Initial ASCII letter F with special character is escaped');
assert.same(escape('G*G'), '\\x47\\*G', 'Initial ASCII letter G with special character is escaped');
assert.same(escape('H*H'), '\\x48\\*H', 'Initial ASCII letter H with special character is escaped');
assert.same(escape('I*I'), '\\x49\\*I', 'Initial ASCII letter I with special character is escaped');
assert.same(escape('J*J'), '\\x4a\\*J', 'Initial ASCII letter J with special character is escaped');
assert.same(escape('K*K'), '\\x4b\\*K', 'Initial ASCII letter K with special character is escaped');
assert.same(escape('L*L'), '\\x4c\\*L', 'Initial ASCII letter L with special character is escaped');
assert.same(escape('M*M'), '\\x4d\\*M', 'Initial ASCII letter M with special character is escaped');
assert.same(escape('N*N'), '\\x4e\\*N', 'Initial ASCII letter N with special character is escaped');
assert.same(escape('O*O'), '\\x4f\\*O', 'Initial ASCII letter O with special character is escaped');
assert.same(escape('P*P'), '\\x50\\*P', 'Initial ASCII letter P with special character is escaped');
assert.same(escape('Q*Q'), '\\x51\\*Q', 'Initial ASCII letter Q with special character is escaped');
assert.same(escape('R*R'), '\\x52\\*R', 'Initial ASCII letter R with special character is escaped');
assert.same(escape('S*S'), '\\x53\\*S', 'Initial ASCII letter S with special character is escaped');
assert.same(escape('T*T'), '\\x54\\*T', 'Initial ASCII letter T with special character is escaped');
assert.same(escape('U*U'), '\\x55\\*U', 'Initial ASCII letter U with special character is escaped');
assert.same(escape('V*V'), '\\x56\\*V', 'Initial ASCII letter V with special character is escaped');
assert.same(escape('W*W'), '\\x57\\*W', 'Initial ASCII letter W with special character is escaped');
assert.same(escape('X*X'), '\\x58\\*X', 'Initial ASCII letter X with special character is escaped');
assert.same(escape('Y*Y'), '\\x59\\*Y', 'Initial ASCII letter Y with special character is escaped');
assert.same(escape('Z*Z'), '\\x5a\\*Z', 'Initial ASCII letter Z with special character is escaped');
assert.same(escape('_'), '_', 'Single underscore character is not escaped');
assert.same(escape('__'), '__', 'Thunderscore character is not escaped');
assert.same(escape('hello_world'), '\\x68ello_world', 'String starting with ASCII letter and containing underscore is not escaped');
assert.same(escape('1_hello_world'), '\\x31_hello_world', 'String starting with digit and containing underscore is correctly escaped');
assert.same(escape('a_b_c'), '\\x61_b_c', 'String starting with ASCII letter and containing multiple underscores is correctly escaped');
assert.same(escape('3_b_4'), '\\x33_b_4', 'String starting with digit and containing multiple underscores is correctly escaped');
assert.same(escape('_hello'), '_hello', 'String starting with underscore and containing other characters is not escaped');
assert.same(escape('_1hello'), '_1hello', 'String starting with underscore and digit is not escaped');
assert.same(escape('_a_1_2'), '_a_1_2', 'String starting with underscore and mixed characters is not escaped');
// Specific surrogate points
assert.same(escape('\uD800'), '\\ud800', 'High surrogate \\uD800 is correctly escaped');
assert.same(escape('\uDBFF'), '\\udbff', 'High surrogate \\uDBFF is correctly escaped');
assert.same(escape('\uDC00'), '\\udc00', 'Low surrogate \\uDC00 is correctly escaped');
assert.same(escape('\uDFFF'), '\\udfff', 'Low surrogate \\uDFFF is correctly escaped');
// Leading Surrogates
const highSurrogatesGroup1 = '\uD800\uD801\uD802\uD803\uD804\uD805\uD806\uD807\uD808\uD809\uD80A\uD80B\uD80C\uD80D\uD80E\uD80F';
const highSurrogatesGroup2 = '\uD810\uD811\uD812\uD813\uD814\uD815\uD816\uD817\uD818\uD819\uD81A\uD81B\uD81C\uD81D\uD81E\uD81F';
const highSurrogatesGroup3 = '\uD820\uD821\uD822\uD823\uD824\uD825\uD826\uD827\uD828\uD829\uD82A\uD82B\uD82C\uD82D\uD82E\uD82F';
const highSurrogatesGroup4 = '\uD830\uD831\uD832\uD833\uD834\uD835\uD836\uD837\uD838\uD839\uD83A\uD83B\uD83C\uD83D\uD83E\uD83F';
const highSurrogatesGroup5 = '\uD840\uD841\uD842\uD843\uD844\uD845\uD846\uD847\uD848\uD849\uD84A\uD84B\uD84C\uD84D\uD84E\uD84F';
const highSurrogatesGroup6 = '\uD850\uD851\uD852\uD853\uD854\uD855\uD856\uD857\uD858\uD859\uD85A\uD85B\uD85C\uD85D\uD85E\uD85F';
const highSurrogatesGroup7 = '\uD860\uD861\uD862\uD863\uD864\uD865\uD866\uD867\uD868\uD869\uD86A\uD86B\uD86C\uD86D\uD86E\uD86F';
const highSurrogatesGroup8 = '\uD870\uD871\uD872\uD873\uD874\uD875\uD876\uD877\uD878\uD879\uD87A\uD87B\uD87C\uD87D\uD87E\uD87F';
const highSurrogatesGroup9 = '\uD880\uD881\uD882\uD883\uD884\uD885\uD886\uD887\uD888\uD889\uD88A\uD88B\uD88C\uD88D\uD88E\uD88F';
const highSurrogatesGroup10 = '\uD890\uD891\uD892\uD893\uD894\uD895\uD896\uD897\uD898\uD899\uD89A\uD89B\uD89C\uD89D\uD89E\uD89F';
const highSurrogatesGroup11 = '\uD8A0\uD8A1\uD8A2\uD8A3\uD8A4\uD8A5\uD8A6\uD8A7\uD8A8\uD8A9\uD8AA\uD8AB\uD8AC\uD8AD\uD8AE\uD8AF';
const highSurrogatesGroup12 = '\uD8B0\uD8B1\uD8B2\uD8B3\uD8B4\uD8B5\uD8B6\uD8B7\uD8B8\uD8B9\uD8BA\uD8BB\uD8BC\uD8BD\uD8BE\uD8BF';
const highSurrogatesGroup13 = '\uD8C0\uD8C1\uD8C2\uD8C3\uD8C4\uD8C5\uD8C6\uD8C7\uD8C8\uD8C9\uD8CA\uD8CB\uD8CC\uD8CD\uD8CE\uD8CF';
const highSurrogatesGroup14 = '\uD8D0\uD8D1\uD8D2\uD8D3\uD8D4\uD8D5\uD8D6\uD8D7\uD8D8\uD8D9\uD8DA\uD8DB\uD8DC\uD8DD\uD8DE\uD8DF';
const highSurrogatesGroup15 = '\uD8E0\uD8E1\uD8E2\uD8E3\uD8E4\uD8E5\uD8E6\uD8E7\uD8E8\uD8E9\uD8EA\uD8EB\uD8EC\uD8ED\uD8EE\uD8EF';
const highSurrogatesGroup16 = '\uD8F0\uD8F1\uD8F2\uD8F3\uD8F4\uD8F5\uD8F6\uD8F7\uD8F8\uD8F9\uD8FA\uD8FB\uD8FC\uD8FD\uD8FE\uD8FF';
assert.same(escape(highSurrogatesGroup1), '\\ud800\\ud801\\ud802\\ud803\\ud804\\ud805\\ud806\\ud807\\ud808\\ud809\\ud80a\\ud80b\\ud80c\\ud80d\\ud80e\\ud80f', 'High surrogates group 1 are correctly escaped');
assert.same(escape(highSurrogatesGroup2), '\\ud810\\ud811\\ud812\\ud813\\ud814\\ud815\\ud816\\ud817\\ud818\\ud819\\ud81a\\ud81b\\ud81c\\ud81d\\ud81e\\ud81f', 'High surrogates group 2 are correctly escaped');
assert.same(escape(highSurrogatesGroup3), '\\ud820\\ud821\\ud822\\ud823\\ud824\\ud825\\ud826\\ud827\\ud828\\ud829\\ud82a\\ud82b\\ud82c\\ud82d\\ud82e\\ud82f', 'High surrogates group 3 are correctly escaped');
assert.same(escape(highSurrogatesGroup4), '\\ud830\\ud831\\ud832\\ud833\\ud834\\ud835\\ud836\\ud837\\ud838\\ud839\\ud83a\\ud83b\\ud83c\\ud83d\\ud83e\\ud83f', 'High surrogates group 4 are correctly escaped');
assert.same(escape(highSurrogatesGroup5), '\\ud840\\ud841\\ud842\\ud843\\ud844\\ud845\\ud846\\ud847\\ud848\\ud849\\ud84a\\ud84b\\ud84c\\ud84d\\ud84e\\ud84f', 'High surrogates group 5 are correctly escaped');
assert.same(escape(highSurrogatesGroup6), '\\ud850\\ud851\\ud852\\ud853\\ud854\\ud855\\ud856\\ud857\\ud858\\ud859\\ud85a\\ud85b\\ud85c\\ud85d\\ud85e\\ud85f', 'High surrogates group 6 are correctly escaped');
assert.same(escape(highSurrogatesGroup7), '\\ud860\\ud861\\ud862\\ud863\\ud864\\ud865\\ud866\\ud867\\ud868\\ud869\\ud86a\\ud86b\\ud86c\\ud86d\\ud86e\\ud86f', 'High surrogates group 7 are correctly escaped');
assert.same(escape(highSurrogatesGroup8), '\\ud870\\ud871\\ud872\\ud873\\ud874\\ud875\\ud876\\ud877\\ud878\\ud879\\ud87a\\ud87b\\ud87c\\ud87d\\ud87e\\ud87f', 'High surrogates group 8 are correctly escaped');
assert.same(escape(highSurrogatesGroup9), '\\ud880\\ud881\\ud882\\ud883\\ud884\\ud885\\ud886\\ud887\\ud888\\ud889\\ud88a\\ud88b\\ud88c\\ud88d\\ud88e\\ud88f', 'High surrogates group 9 are correctly escaped');
assert.same(escape(highSurrogatesGroup10), '\\ud890\\ud891\\ud892\\ud893\\ud894\\ud895\\ud896\\ud897\\ud898\\ud899\\ud89a\\ud89b\\ud89c\\ud89d\\ud89e\\ud89f', 'High surrogates group 10 are correctly escaped');
assert.same(escape(highSurrogatesGroup11), '\\ud8a0\\ud8a1\\ud8a2\\ud8a3\\ud8a4\\ud8a5\\ud8a6\\ud8a7\\ud8a8\\ud8a9\\ud8aa\\ud8ab\\ud8ac\\ud8ad\\ud8ae\\ud8af', 'High surrogates group 11 are correctly escaped');
assert.same(escape(highSurrogatesGroup12), '\\ud8b0\\ud8b1\\ud8b2\\ud8b3\\ud8b4\\ud8b5\\ud8b6\\ud8b7\\ud8b8\\ud8b9\\ud8ba\\ud8bb\\ud8bc\\ud8bd\\ud8be\\ud8bf', 'High surrogates group 12 are correctly escaped');
assert.same(escape(highSurrogatesGroup13), '\\ud8c0\\ud8c1\\ud8c2\\ud8c3\\ud8c4\\ud8c5\\ud8c6\\ud8c7\\ud8c8\\ud8c9\\ud8ca\\ud8cb\\ud8cc\\ud8cd\\ud8ce\\ud8cf', 'High surrogates group 13 are correctly escaped');
assert.same(escape(highSurrogatesGroup14), '\\ud8d0\\ud8d1\\ud8d2\\ud8d3\\ud8d4\\ud8d5\\ud8d6\\ud8d7\\ud8d8\\ud8d9\\ud8da\\ud8db\\ud8dc\\ud8dd\\ud8de\\ud8df', 'High surrogates group 14 are correctly escaped');
assert.same(escape(highSurrogatesGroup15), '\\ud8e0\\ud8e1\\ud8e2\\ud8e3\\ud8e4\\ud8e5\\ud8e6\\ud8e7\\ud8e8\\ud8e9\\ud8ea\\ud8eb\\ud8ec\\ud8ed\\ud8ee\\ud8ef', 'High surrogates group 15 are correctly escaped');
assert.same(escape(highSurrogatesGroup16), '\\ud8f0\\ud8f1\\ud8f2\\ud8f3\\ud8f4\\ud8f5\\ud8f6\\ud8f7\\ud8f8\\ud8f9\\ud8fa\\ud8fb\\ud8fc\\ud8fd\\ud8fe\\ud8ff', 'High surrogates group 16 are correctly escaped');
// Trailing Surrogates
const lowSurrogatesGroup1 = '\uDC00\uDC01\uDC02\uDC03\uDC04\uDC05\uDC06\uDC07\uDC08\uDC09\uDC0A\uDC0B\uDC0C\uDC0D\uDC0E\uDC0F';
const lowSurrogatesGroup2 = '\uDC10\uDC11\uDC12\uDC13\uDC14\uDC15\uDC16\uDC17\uDC18\uDC19\uDC1A\uDC1B\uDC1C\uDC1D\uDC1E\uDC1F';
const lowSurrogatesGroup3 = '\uDC20\uDC21\uDC22\uDC23\uDC24\uDC25\uDC26\uDC27\uDC28\uDC29\uDC2A\uDC2B\uDC2C\uDC2D\uDC2E\uDC2F';
const lowSurrogatesGroup4 = '\uDC30\uDC31\uDC32\uDC33\uDC34\uDC35\uDC36\uDC37\uDC38\uDC39\uDC3A\uDC3B\uDC3C\uDC3D\uDC3E\uDC3F';
const lowSurrogatesGroup5 = '\uDC40\uDC41\uDC42\uDC43\uDC44\uDC45\uDC46\uDC47\uDC48\uDC49\uDC4A\uDC4B\uDC4C\uDC4D\uDC4E\uDC4F';
const lowSurrogatesGroup6 = '\uDC50\uDC51\uDC52\uDC53\uDC54\uDC55\uDC56\uDC57\uDC58\uDC59\uDC5A\uDC5B\uDC5C\uDC5D\uDC5E\uDC5F';
const lowSurrogatesGroup7 = '\uDC60\uDC61\uDC62\uDC63\uDC64\uDC65\uDC66\uDC67\uDC68\uDC69\uDC6A\uDC6B\uDC6C\uDC6D\uDC6E\uDC6F';
const lowSurrogatesGroup8 = '\uDC70\uDC71\uDC72\uDC73\uDC74\uDC75\uDC76\uDC77\uDC78\uDC79\uDC7A\uDC7B\uDC7C\uDC7D\uDC7E\uDC7F';
const lowSurrogatesGroup9 = '\uDC80\uDC81\uDC82\uDC83\uDC84\uDC85\uDC86\uDC87\uDC88\uDC89\uDC8A\uDC8B\uDC8C\uDC8D\uDC8E\uDC8F';
const lowSurrogatesGroup10 = '\uDC90\uDC91\uDC92\uDC93\uDC94\uDC95\uDC96\uDC97\uDC98\uDC99\uDC9A\uDC9B\uDC9C\uDC9D\uDC9E\uDC9F';
const lowSurrogatesGroup11 = '\uDCA0\uDCA1\uDCA2\uDCA3\uDCA4\uDCA5\uDCA6\uDCA7\uDCA8\uDCA9\uDCAA\uDCAB\uDCAC\uDCAD\uDCAE\uDCAF';
const lowSurrogatesGroup12 = '\uDCB0\uDCB1\uDCB2\uDCB3\uDCB4\uDCB5\uDCB6\uDCB7\uDCB8\uDCB9\uDCBA\uDCBB\uDCBC\uDCBD\uDCBE\uDCBF';
const lowSurrogatesGroup13 = '\uDCC0\uDCC1\uDCC2\uDCC3\uDCC4\uDCC5\uDCC6\uDCC7\uDCC8\uDCC9\uDCCA\uDCCB\uDCCC\uDCCD\uDCCE\uDCCF';
const lowSurrogatesGroup14 = '\uDCD0\uDCD1\uDCD2\uDCD3\uDCD4\uDCD5\uDCD6\uDCD7\uDCD8\uDCD9\uDCDA\uDCDB\uDCDC\uDCDD\uDCDE\uDCDF';
const lowSurrogatesGroup15 = '\uDCE0\uDCE1\uDCE2\uDCE3\uDCE4\uDCE5\uDCE6\uDCE7\uDCE8\uDCE9\uDCEA\uDCEB\uDCEC\uDCED\uDCEE\uDCEF';
const lowSurrogatesGroup16 = '\uDCF0\uDCF1\uDCF2\uDCF3\uDCF4\uDCF5\uDCF6\uDCF7\uDCF8\uDCF9\uDCFA\uDCFB\uDCFC\uDCFD\uDCFE\uDCFF';
assert.same(escape(lowSurrogatesGroup1), '\\udc00\\udc01\\udc02\\udc03\\udc04\\udc05\\udc06\\udc07\\udc08\\udc09\\udc0a\\udc0b\\udc0c\\udc0d\\udc0e\\udc0f', 'Low surrogates group 1 are correctly escaped');
assert.same(escape(lowSurrogatesGroup2), '\\udc10\\udc11\\udc12\\udc13\\udc14\\udc15\\udc16\\udc17\\udc18\\udc19\\udc1a\\udc1b\\udc1c\\udc1d\\udc1e\\udc1f', 'Low surrogates group 2 are correctly escaped');
assert.same(escape(lowSurrogatesGroup3), '\\udc20\\udc21\\udc22\\udc23\\udc24\\udc25\\udc26\\udc27\\udc28\\udc29\\udc2a\\udc2b\\udc2c\\udc2d\\udc2e\\udc2f', 'Low surrogates group 3 are correctly escaped');
assert.same(escape(lowSurrogatesGroup4), '\\udc30\\udc31\\udc32\\udc33\\udc34\\udc35\\udc36\\udc37\\udc38\\udc39\\udc3a\\udc3b\\udc3c\\udc3d\\udc3e\\udc3f', 'Low surrogates group 4 are correctly escaped');
assert.same(escape(lowSurrogatesGroup5), '\\udc40\\udc41\\udc42\\udc43\\udc44\\udc45\\udc46\\udc47\\udc48\\udc49\\udc4a\\udc4b\\udc4c\\udc4d\\udc4e\\udc4f', 'Low surrogates group 5 are correctly escaped');
assert.same(escape(lowSurrogatesGroup6), '\\udc50\\udc51\\udc52\\udc53\\udc54\\udc55\\udc56\\udc57\\udc58\\udc59\\udc5a\\udc5b\\udc5c\\udc5d\\udc5e\\udc5f', 'Low surrogates group 6 are correctly escaped');
assert.same(escape(lowSurrogatesGroup7), '\\udc60\\udc61\\udc62\\udc63\\udc64\\udc65\\udc66\\udc67\\udc68\\udc69\\udc6a\\udc6b\\udc6c\\udc6d\\udc6e\\udc6f', 'Low surrogates group 7 are correctly escaped');
assert.same(escape(lowSurrogatesGroup8), '\\udc70\\udc71\\udc72\\udc73\\udc74\\udc75\\udc76\\udc77\\udc78\\udc79\\udc7a\\udc7b\\udc7c\\udc7d\\udc7e\\udc7f', 'Low surrogates group 8 are correctly escaped');
assert.same(escape(lowSurrogatesGroup9), '\\udc80\\udc81\\udc82\\udc83\\udc84\\udc85\\udc86\\udc87\\udc88\\udc89\\udc8a\\udc8b\\udc8c\\udc8d\\udc8e\\udc8f', 'Low surrogates group 9 are correctly escaped');
assert.same(escape(lowSurrogatesGroup10), '\\udc90\\udc91\\udc92\\udc93\\udc94\\udc95\\udc96\\udc97\\udc98\\udc99\\udc9a\\udc9b\\udc9c\\udc9d\\udc9e\\udc9f', 'Low surrogates group 10 are correctly escaped');
assert.same(escape(lowSurrogatesGroup11), '\\udca0\\udca1\\udca2\\udca3\\udca4\\udca5\\udca6\\udca7\\udca8\\udca9\\udcaa\\udcab\\udcac\\udcad\\udcae\\udcaf', 'Low surrogates group 11 are correctly escaped');
assert.same(escape(lowSurrogatesGroup12), '\\udcb0\\udcb1\\udcb2\\udcb3\\udcb4\\udcb5\\udcb6\\udcb7\\udcb8\\udcb9\\udcba\\udcbb\\udcbc\\udcbd\\udcbe\\udcbf', 'Low surrogates group 12 are correctly escaped');
assert.same(escape(lowSurrogatesGroup13), '\\udcc0\\udcc1\\udcc2\\udcc3\\udcc4\\udcc5\\udcc6\\udcc7\\udcc8\\udcc9\\udcca\\udccb\\udccc\\udccd\\udcce\\udccf', 'Low surrogates group 13 are correctly escaped');
assert.same(escape(lowSurrogatesGroup14), '\\udcd0\\udcd1\\udcd2\\udcd3\\udcd4\\udcd5\\udcd6\\udcd7\\udcd8\\udcd9\\udcda\\udcdb\\udcdc\\udcdd\\udcde\\udcdf', 'Low surrogates group 14 are correctly escaped');
assert.same(escape(lowSurrogatesGroup15), '\\udce0\\udce1\\udce2\\udce3\\udce4\\udce5\\udce6\\udce7\\udce8\\udce9\\udcea\\udceb\\udcec\\udced\\udcee\\udcef', 'Low surrogates group 15 are correctly escaped');
assert.same(escape(lowSurrogatesGroup16), '\\udcf0\\udcf1\\udcf2\\udcf3\\udcf4\\udcf5\\udcf6\\udcf7\\udcf8\\udcf9\\udcfa\\udcfb\\udcfc\\udcfd\\udcfe\\udcff', 'Low surrogates group 16 are correctly escaped');
assert.same(escape('.a.b'), '\\.a\\.b', 'mixed string with dot character is escaped correctly');
assert.same(escape('.1+2'), '\\.1\\+2', 'mixed string with plus character is escaped correctly');
assert.same(escape('.a(b)c'), '\\.a\\(b\\)c', 'mixed string with parentheses is escaped correctly');
assert.same(escape('.a*b+c'), '\\.a\\*b\\+c', 'mixed string with asterisk and plus characters is escaped correctly');
assert.same(escape('.a?b^c'), '\\.a\\?b\\^c', 'mixed string with question mark and caret characters is escaped correctly');
assert.same(escape('.a{2}'), '\\.a\\{2\\}', 'mixed string with curly braces is escaped correctly');
assert.same(escape('.a|b'), '\\.a\\|b', 'mixed string with pipe character is escaped correctly');
assert.same(escape('.a\\b'), '\\.a\\\\b', 'mixed string with backslash is escaped correctly');
assert.same(escape('.a\\\\b'), '\\.a\\\\\\\\b', 'mixed string with backslash is escaped correctly');
assert.same(escape('.a^b'), '\\.a\\^b', 'mixed string with caret character is escaped correctly');
assert.same(escape('.a$b'), '\\.a\\$b', 'mixed string with dollar sign is escaped correctly');
assert.same(escape('.a[b]'), '\\.a\\[b\\]', 'mixed string with square brackets is escaped correctly');
assert.same(escape('.a.b(c)'), '\\.a\\.b\\(c\\)', 'mixed string with dot and parentheses is escaped correctly');
assert.same(escape('.a*b+c?d^e$f|g{2}h[i]j\\k'), '\\.a\\*b\\+c\\?d\\^e\\$f\\|g\\{2\\}h\\[i\\]j\\\\k', 'complex string with multiple special characters is escaped correctly');
assert.same(escape('^$\\.*+?()[]{}|'), '\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|', 'Syntax characters are correctly escaped');
assert.throws(() => escape(123), TypeError, 'non-string input (number) throws TypeError');
assert.throws(() => escape({}), TypeError, 'non-string input (object) throws TypeError');
assert.throws(() => escape([]), TypeError, 'non-string input (array) throws TypeError');
assert.throws(() => escape(null), TypeError, 'non-string input (null) throws TypeError');
assert.throws(() => escape(undefined), TypeError, 'non-string input (undefined) throws TypeError');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.exec.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals -- required for testing */
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('RegExp#exec lastIndex updating', assert => {
let re = /b/;
assert.same(re.lastIndex, 0, '.lastIndex starts at 0 for non-global regexps');
re.exec('abc');
assert.same(re.lastIndex, 0, '.lastIndex is not updated for non-global regexps');
re = /b/g;
assert.same(re.lastIndex, 0, '.lastIndex starts at 0 for global regexps');
re.exec('abc');
assert.same(re.lastIndex, 2, '.lastIndex is updated for global regexps');
re = /b*/;
re.exec('a');
assert.same(re.lastIndex, 0, '.lastIndex is not updated for non-global regexps if the match is empty');
re = /b*/g;
re.exec('a');
assert.same(re.lastIndex, 0, '.lastIndex is not updated for global regexps if the match is empty');
});
QUnit.test('RegExp#exec capturing groups', assert => {
assert.deepEqual(/(a?)/.exec('x'), ['', ''], '/(a?)/.exec("x") returns ["", ""]');
assert.deepEqual(/(a)?/.exec('x'), ['', undefined], '/(a)?/.exec("x") returns ["", undefined]');
// @nicolo-ribaudo: I don't know how to fix this in IE8. For the `/(a)?/` case it is fixed using
// #replace, but here also #replace is buggy :(
// assert.deepEqual(/(a?)?/.exec('x'), ['', undefined], '/(a?)?/.exec("x") returns ["", undefined]');
});
if (DESCRIPTORS) {
QUnit.test('RegExp#exec regression', assert => {
assert.throws(() => /l/.exec(Symbol('RegExp#exec test')), 'throws on symbol argument');
});
QUnit.test('RegExp#exec sticky', assert => {
const re = new RegExp('a', 'y');
const str = 'bbabaab';
assert.same(re.lastIndex, 0, '#1');
assert.same(re.exec(str), null, '#2');
assert.same(re.lastIndex, 0, '#3');
re.lastIndex = 1;
assert.same(re.exec(str), null, '#4');
assert.same(re.lastIndex, 0, '#5');
re.lastIndex = 2;
const result = re.exec(str);
assert.deepEqual(result, ['a'], '#6');
assert.same(result.index, 2, '#7');
assert.same(re.lastIndex, 3, '#8');
assert.same(re.exec(str), null, '#9');
assert.same(re.lastIndex, 0, '#10');
re.lastIndex = 4;
assert.deepEqual(re.exec(str), ['a'], '#11');
assert.same(re.lastIndex, 5, '#12');
assert.deepEqual(re.exec(str), ['a'], '#13');
assert.same(re.lastIndex, 6, '#14');
assert.same(re.exec(str), null, '#15');
assert.same(re.lastIndex, 0, '#16');
});
QUnit.test('RegExp#exec sticky anchored', assert => {
const regex = new RegExp('^foo', 'y');
assert.deepEqual(regex.exec('foo'), ['foo'], '#1');
regex.lastIndex = 2;
assert.same(regex.exec('..foo'), null, '#2');
regex.lastIndex = 2;
assert.same(regex.exec('.\nfoo'), null, '#3');
const regex2 = new RegExp('^foo', 'my');
regex2.lastIndex = 2;
assert.same(regex2.exec('..foo'), null, '#4');
regex2.lastIndex = 2;
assert.deepEqual(regex2.exec('.\nfoo'), ['foo'], '#5');
assert.same(regex2.lastIndex, 5, '#6');
// all line terminators should allow ^ to match in multiline+sticky mode
const regex3 = new RegExp('^bar', 'my');
regex3.lastIndex = 2;
assert.deepEqual(regex3.exec('.\rbar'), ['bar'], 'multiline sticky after \\r');
const regex4 = new RegExp('^bar', 'my');
regex4.lastIndex = 2;
assert.deepEqual(regex4.exec('.\u2028bar'), ['bar'], 'multiline sticky after \\u2028');
const regex5 = new RegExp('^bar', 'my');
regex5.lastIndex = 2;
assert.deepEqual(regex5.exec('.\u2029bar'), ['bar'], 'multiline sticky after \\u2029');
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.flags.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals, regexp/sort-flags, regexp/no-useless-flag -- required for testing */
import { DESCRIPTORS } from '../helpers/constants.js';
if (DESCRIPTORS) {
QUnit.test('RegExp#flags', assert => {
assert.nonEnumerable(RegExp.prototype, 'flags');
assert.same(/./g.flags, 'g', '/./g.flags is "g"');
assert.same(/./.flags, '', '/./.flags is ""');
assert.same(RegExp('.', 'gim').flags, 'gim', 'RegExp(".", "gim").flags is "gim"');
assert.same(RegExp('.').flags, '', 'RegExp(".").flags is ""');
assert.same(/./gim.flags, 'gim', '/./gim.flags is "gim"');
assert.same(/./gmi.flags, 'gim', '/./gmi.flags is "gim"');
assert.same(/./mig.flags, 'gim', '/./mig.flags is "gim"');
assert.same(/./mgi.flags, 'gim', '/./mgi.flags is "gim"');
let INDICES_SUPPORT = true;
try {
RegExp('.', 'd');
} catch {
INDICES_SUPPORT = false;
}
const O = {};
// modern V8 bug
let calls = '';
const expected = INDICES_SUPPORT ? 'dgimsy' : 'gimsy';
function addGetter(key, chr) {
Object.defineProperty(O, key, { get() {
calls += chr;
return true;
} });
}
const pairs = {
dotAll: 's',
global: 'g',
ignoreCase: 'i',
multiline: 'm',
sticky: 'y',
};
if (INDICES_SUPPORT) pairs.hasIndices = 'd';
for (const key in pairs) addGetter(key, pairs[key]);
const result = Object.getOwnPropertyDescriptor(RegExp.prototype, 'flags').get.call(O);
assert.same(result, expected, 'proper order, result');
assert.same(calls, expected, 'proper order, calls');
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.sticky.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals -- required for testing */
import { DESCRIPTORS } from '../helpers/constants.js';
if (DESCRIPTORS) {
QUnit.test('RegExp#sticky', assert => {
const re = new RegExp('a', 'y');
assert.true(re.sticky, '.sticky is true');
assert.same(re.flags, 'y', '.flags contains y');
assert.false(/a/.sticky);
const stickyGetter = Object.getOwnPropertyDescriptor(RegExp.prototype, 'sticky').get;
if (typeof stickyGetter == 'function') {
// Old firefox versions set a non-configurable non-writable .sticky property
// It works correctly, but it isn't a getter and it can't be polyfilled.
// We need to skip these tests.
assert.throws(() => {
stickyGetter.call({});
}, undefined, '.sticky getter can only be called on RegExp instances');
try {
stickyGetter.call(/a/);
assert.required('.sticky getter works on literals');
} catch {
assert.avoid('.sticky getter works on literals');
}
try {
stickyGetter.call(new RegExp('a'));
assert.required('.sticky getter works on instances');
} catch {
assert.avoid('.sticky getter works on instances');
}
assert.true(Object.hasOwn(RegExp.prototype, 'sticky'), 'prototype has .sticky property');
// relaxed for early implementations
// assert.same(RegExp.prototype.sticky, undefined, '.sticky is undefined on prototype');
}
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.test.js
|
JavaScript
|
QUnit.test('RegExp#test delegates to exec', assert => {
const exec = function (...args) {
execCalled = true;
return /./.exec.apply(this, args);
};
let execCalled = false;
let re = /[ac]/;
re.exec = exec;
assert.true(re.test('abc'), '#1');
assert.true(execCalled, '#2');
re = /a/;
// Not a function, should be ignored
re.exec = 3;
assert.true(re.test('abc'), '#3');
re = /a/;
// Does not return an object, should throw
re.exec = () => 3;
assert.throws(() => re.test('abc', '#4'));
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.regexp.to-string.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals, regexp/sort-flags, regexp/no-useless-flag -- required for testing */
import { STRICT } from '../helpers/constants.js';
QUnit.test('RegExp#toString', assert => {
const { toString } = RegExp.prototype;
assert.isFunction(toString);
assert.arity(toString, 0);
assert.name(toString, 'toString');
assert.looksNative(toString);
assert.nonEnumerable(RegExp.prototype, 'toString');
assert.same(String(/pattern/), '/pattern/');
assert.same(String(/pattern/i), '/pattern/i');
assert.same(String(/pattern/mi), '/pattern/im');
assert.same(String(/pattern/im), '/pattern/im');
assert.same(String(/pattern/mgi), '/pattern/gim');
assert.same(String(new RegExp('pattern')), '/pattern/');
assert.same(String(new RegExp('pattern', 'i')), '/pattern/i');
assert.same(String(new RegExp('pattern', 'mi')), '/pattern/im');
assert.same(String(new RegExp('pattern', 'im')), '/pattern/im');
assert.same(String(new RegExp('pattern', 'mgi')), '/pattern/gim');
assert.same(toString.call({
source: 'foo',
flags: 'bar',
}), '/foo/bar');
assert.same(toString.call({}), '/undefined/undefined');
if (STRICT) {
assert.throws(() => toString.call(7));
assert.throws(() => toString.call('a'));
assert.throws(() => toString.call(false));
assert.throws(() => toString.call(null));
assert.throws(() => toString.call(undefined));
}
assert.throws(() => toString.call({
source: Symbol('RegExp#toString test'),
flags: 'g',
}), 'throws on symbol');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.difference.js
|
JavaScript
|
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#difference', assert => {
const { difference } = Set.prototype;
const { from } = Array;
assert.isFunction(difference);
assert.arity(difference, 1);
assert.name(difference, 'difference');
assert.looksNative(difference);
assert.nonEnumerable(Set.prototype, 'difference');
const set = new Set([1]);
assert.notSame(set.difference(new Set()), set);
assert.deepEqual(from(new Set([1, 2, 3]).difference(new Set([4, 5]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).difference(new Set([3, 4]))), [1, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).difference(createSetLike([4, 5]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).difference(createSetLike([3, 4]))), [1, 2]);
// TODO: drop from core-js@4
assert.deepEqual(from(new Set([1, 2, 3]).difference([4, 5])), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).difference([3, 4])), [1, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).difference(createIterable([3, 4]))), [1, 2]);
assert.same(new Set([42, 43]).difference({
size: Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}).size, 0);
assert.throws(() => new Set().difference({
size: -Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set([1, 2, 3]).difference(), TypeError);
assert.throws(() => difference.call({}, [1, 2, 3]), TypeError);
assert.throws(() => difference.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => difference.call(null, [1, 2, 3]), TypeError);
// A WebKit bug occurs when `this` is updated while Set.prototype.difference is being executed
// https://bugs.webkit.org/show_bug.cgi?id=288595
const values = [2];
const setLike = {
size: values.length,
has() { return true; },
keys() {
let index = 0;
return {
next() {
const done = index >= values.length;
if (baseSet.has(1)) baseSet.clear();
return { done, value: values[index++] };
},
};
},
};
const baseSet = new Set([1, 2, 3, 4]);
const result = baseSet.difference(setLike);
assert.deepEqual(from(result), [1, 3, 4], 'incorrect behavior when this updated while Set#difference is being executed');
// Mutation via has() in the size(O) <= otherRec.size branch should not skip elements
const mutatingSet = new Set([1, 2, 3]);
const mutatingResult = mutatingSet.difference({
size: 10,
has(v) {
if (v === 1) {
mutatingSet.delete(2);
return true;
}
return false;
},
keys() { return { next() { return { done: true }; } }; },
});
assert.deepEqual(from(mutatingResult), [2, 3], 'iterates copy, not live set in has() branch');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.intersection.js
|
JavaScript
|
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#intersection', assert => {
const { intersection } = Set.prototype;
const { from } = Array;
assert.isFunction(intersection);
assert.arity(intersection, 1);
assert.name(intersection, 'intersection');
assert.looksNative(intersection);
assert.nonEnumerable(Set.prototype, 'intersection');
const set = new Set([1]);
assert.notSame(set.intersection(new Set()), set);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([4, 5]))), []);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([2, 3, 4]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([4, 5]))), []);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([2, 3, 4]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1, 0]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1, 0]))), [1, 2, 3]);
// TODO: drop from core-js@4
assert.deepEqual(from(new Set([1, 2, 3]).intersection([4, 5])), []);
assert.deepEqual(from(new Set([1, 2, 3]).intersection([2, 3, 4])), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createIterable([2, 3, 4]))), [2, 3]);
assert.deepEqual(from(new Set([42, 43]).intersection({
size: Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
})), [42, 43]);
assert.throws(() => new Set().intersection({
size: -Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set([1, 2, 3]).intersection(), TypeError);
assert.throws(() => intersection.call({}, [1, 2, 3]), TypeError);
assert.throws(() => intersection.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => intersection.call(null, [1, 2, 3]), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.is-disjoint-from.js
|
JavaScript
|
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#isDisjointFrom', assert => {
const { isDisjointFrom } = Set.prototype;
assert.isFunction(isDisjointFrom);
assert.arity(isDisjointFrom, 1);
assert.name(isDisjointFrom, 'isDisjointFrom');
assert.looksNative(isDisjointFrom);
assert.nonEnumerable(Set.prototype, 'isDisjointFrom');
assert.true(new Set([1]).isDisjointFrom(new Set([2])));
assert.false(new Set([1]).isDisjointFrom(new Set([1])));
assert.true(new Set([1, 2, 3]).isDisjointFrom(new Set([4, 5, 6])));
assert.false(new Set([1, 2, 3]).isDisjointFrom(new Set([5, 4, 3])));
assert.true(new Set([1]).isDisjointFrom(createSetLike([2])));
assert.false(new Set([1]).isDisjointFrom(createSetLike([1])));
assert.true(new Set([1, 2, 3]).isDisjointFrom(createSetLike([4, 5, 6])));
assert.false(new Set([1, 2, 3]).isDisjointFrom(createSetLike([5, 4, 3])));
// TODO: drop from core-js@4
assert.true(new Set([1]).isDisjointFrom([2]));
assert.false(new Set([1]).isDisjointFrom([1]));
assert.true(new Set([1, 2, 3]).isDisjointFrom([4, 5, 6]));
assert.false(new Set([1, 2, 3]).isDisjointFrom([5, 4, 3]));
assert.true(new Set([1]).isDisjointFrom(createIterable([2])));
assert.false(new Set([1]).isDisjointFrom(createIterable([1])));
assert.false(new Set([42, 43]).isDisjointFrom({
size: Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set().isDisjointFrom({
size: -Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
let closed = false;
assert.false(new Set([1, 2, 3, 4]).isDisjointFrom({
size: 3,
has() { return true; },
keys() {
let index = 0;
return {
next() {
return { value: [5, 1, 6][index++], done: index > 3 };
},
return() {
closed = true;
return { done: true };
},
};
},
}));
assert.true(closed, 'iterator is closed on early exit');
assert.throws(() => new Set([1, 2, 3]).isDisjointFrom(), TypeError);
assert.throws(() => isDisjointFrom.call({}, [1, 2, 3]), TypeError);
assert.throws(() => isDisjointFrom.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => isDisjointFrom.call(null, [1, 2, 3]), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.is-subset-of.js
|
JavaScript
|
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#isSubsetOf', assert => {
const { isSubsetOf } = Set.prototype;
assert.isFunction(isSubsetOf);
assert.arity(isSubsetOf, 1);
assert.name(isSubsetOf, 'isSubsetOf');
assert.looksNative(isSubsetOf);
assert.nonEnumerable(Set.prototype, 'isSubsetOf');
assert.true(new Set([1]).isSubsetOf(new Set([1, 2, 3])));
assert.false(new Set([1]).isSubsetOf(new Set([2, 3, 4])));
assert.true(new Set([1, 2, 3]).isSubsetOf(new Set([5, 4, 3, 2, 1])));
assert.false(new Set([1, 2, 3]).isSubsetOf(new Set([5, 4, 3, 2])));
assert.true(new Set([1]).isSubsetOf(createSetLike([1, 2, 3])));
assert.false(new Set([1]).isSubsetOf(createSetLike([2, 3, 4])));
assert.true(new Set([1, 2, 3]).isSubsetOf(createSetLike([5, 4, 3, 2, 1])));
assert.false(new Set([1, 2, 3]).isSubsetOf(createSetLike([5, 4, 3, 2])));
// TODO: drop from core-js@4
assert.true(new Set([1]).isSubsetOf([1, 2, 3]));
assert.false(new Set([1]).isSubsetOf([2, 3, 4]));
assert.true(new Set([1, 2, 3]).isSubsetOf([5, 4, 3, 2, 1]));
assert.false(new Set([1, 2, 3]).isSubsetOf([5, 4, 3, 2]));
assert.true(new Set([1]).isSubsetOf(createIterable([1, 2, 3])));
assert.false(new Set([1]).isSubsetOf(createIterable([2, 3, 4])));
assert.true(new Set([42, 43]).isSubsetOf({
size: Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set().isSubsetOf({
size: -Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set([1, 2, 3]).isSubsetOf(), TypeError);
assert.throws(() => isSubsetOf.call({}, [1, 2, 3]), TypeError);
assert.throws(() => isSubsetOf.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => isSubsetOf.call(null, [1, 2, 3]), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.is-superset-of.js
|
JavaScript
|
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#isSupersetOf', assert => {
const { isSupersetOf } = Set.prototype;
assert.isFunction(isSupersetOf);
assert.arity(isSupersetOf, 1);
assert.name(isSupersetOf, 'isSupersetOf');
assert.looksNative(isSupersetOf);
assert.nonEnumerable(Set.prototype, 'isSupersetOf');
assert.true(new Set([1, 2, 3]).isSupersetOf(new Set([1])));
assert.false(new Set([2, 3, 4]).isSupersetOf(new Set([1])));
assert.true(new Set([5, 4, 3, 2, 1]).isSupersetOf(new Set([1, 2, 3])));
assert.false(new Set([5, 4, 3, 2]).isSupersetOf(new Set([1, 2, 3])));
assert.true(new Set([1, 2, 3]).isSupersetOf(createSetLike([1])));
assert.false(new Set([2, 3, 4]).isSupersetOf(createSetLike([1])));
assert.true(new Set([5, 4, 3, 2, 1]).isSupersetOf(createSetLike([1, 2, 3])));
assert.false(new Set([5, 4, 3, 2]).isSupersetOf(createSetLike([1, 2, 3])));
// TODO: drop from core-js@4
assert.true(new Set([1, 2, 3]).isSupersetOf([1]));
assert.false(new Set([2, 3, 4]).isSupersetOf([1]));
assert.true(new Set([5, 4, 3, 2, 1]).isSupersetOf([1, 2, 3]));
assert.false(new Set([5, 4, 3, 2]).isSupersetOf([1, 2, 3]));
assert.true(new Set([1, 2, 3]).isSupersetOf(createIterable([1])));
assert.false(new Set([2, 3, 4]).isSupersetOf(createIterable([1])));
assert.false(new Set([42, 43]).isSupersetOf({
size: Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
assert.throws(() => new Set().isSupersetOf({
size: -Infinity,
has() {
return true;
},
keys() {
throw new Error('Unexpected call to |keys| method');
},
}));
let closed = false;
assert.false(new Set([1, 2, 3, 4]).isSupersetOf({
size: 3,
has() { return true; },
keys() {
let index = 0;
return {
next() {
return { value: [1, 5, 3][index++], done: index > 3 };
},
return() {
closed = true;
return { done: true };
},
};
},
}));
assert.true(closed, 'iterator is closed on early exit');
assert.throws(() => new Set([1, 2, 3]).isSupersetOf(), TypeError);
assert.throws(() => isSupersetOf.call({}, [1, 2, 3]), TypeError);
assert.throws(() => isSupersetOf.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => isSupersetOf.call(null, [1, 2, 3]), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.js
|
JavaScript
|
/* eslint-disable sonarjs/no-element-overwrite -- required for testing */
import { DESCRIPTORS, GLOBAL, NATIVE } from '../helpers/constants.js';
import { createIterable, is, nativeSubclass } from '../helpers/helpers.js';
const Symbol = GLOBAL.Symbol || {};
const { getOwnPropertyDescriptor, keys, getOwnPropertyNames, getOwnPropertySymbols, freeze } = Object;
const { ownKeys } = GLOBAL.Reflect || {};
const { from } = Array;
QUnit.test('Set', assert => {
assert.isFunction(Set);
assert.name(Set, 'Set');
assert.arity(Set, 0);
assert.looksNative(Set);
assert.true('add' in Set.prototype, 'add in Set.prototype');
assert.true('clear' in Set.prototype, 'clear in Set.prototype');
assert.true('delete' in Set.prototype, 'delete in Set.prototype');
assert.true('forEach' in Set.prototype, 'forEach in Set.prototype');
assert.true('has' in Set.prototype, 'has in Set.prototype');
assert.true(new Set() instanceof Set, 'new Set instanceof Set');
let set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
assert.same(set.size, 3);
const result = [];
set.forEach(val => {
result.push(val);
});
assert.deepEqual(result, [1, 2, 3]);
assert.same(new Set(createIterable([1, 2, 3])).size, 3, 'Init from iterable');
assert.same(new Set([freeze({}), 1]).size, 2, 'Support frozen objects');
assert.same(new Set([NaN, NaN, NaN]).size, 1);
assert.deepEqual(from(new Set([3, 4]).add(2).add(1)), [3, 4, 2, 1]);
let done = false;
const { add } = Set.prototype;
// eslint-disable-next-line no-extend-native -- required for testing
Set.prototype.add = function () {
throw new Error();
};
try {
new Set(createIterable([null, 1, 2], {
return() {
return done = true;
},
}));
} catch { /* empty */ }
// eslint-disable-next-line no-extend-native -- required for testing
Set.prototype.add = add;
assert.true(done, '.return #throw');
const array = [];
done = false;
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
array['@@iterator'] = undefined;
array[Symbol.iterator] = function () {
done = true;
return [][Symbol.iterator].call(this);
};
new Set(array);
assert.true(done);
const object = {};
new Set().add(object);
if (DESCRIPTORS) {
const results = [];
for (const key in results) keys.push(key);
assert.arrayEqual(results, []);
assert.arrayEqual(keys(object), []);
}
assert.arrayEqual(getOwnPropertyNames(object), []);
if (getOwnPropertySymbols) assert.arrayEqual(getOwnPropertySymbols(object), []);
if (ownKeys) assert.arrayEqual(ownKeys(object), []);
if (nativeSubclass) {
const Subclass = nativeSubclass(Set);
assert.true(new Subclass() instanceof Subclass, 'correct subclassing with native classes #1');
assert.true(new Subclass() instanceof Set, 'correct subclassing with native classes #2');
assert.true(new Subclass().add(2).has(2), 'correct subclassing with native classes #3');
}
const buffer = new ArrayBuffer(8);
set = new Set([buffer]);
assert.true(set.has(buffer), 'works with ArrayBuffer keys');
});
QUnit.test('Set#add', assert => {
assert.isFunction(Set.prototype.add);
assert.name(Set.prototype.add, 'add');
assert.arity(Set.prototype.add, 1);
assert.looksNative(Set.prototype.add);
assert.nonEnumerable(Set.prototype, 'add');
const array = [];
let set = new Set();
set.add(NaN);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.add(array);
assert.same(set.size, 5);
const chain = set.add(NaN);
assert.same(chain, set);
assert.same(set.size, 5);
set.add(2);
assert.same(set.size, 5);
set.add(array);
assert.same(set.size, 5);
set.add([]);
assert.same(set.size, 6);
set.add(4);
assert.same(set.size, 7);
const frozen = freeze({});
set = new Set();
set.add(frozen);
assert.true(set.has(frozen));
});
QUnit.test('Set#clear', assert => {
assert.isFunction(Set.prototype.clear);
assert.name(Set.prototype.clear, 'clear');
assert.arity(Set.prototype.clear, 0);
assert.looksNative(Set.prototype.clear);
assert.nonEnumerable(Set.prototype, 'clear');
let set = new Set();
set.clear();
assert.same(set.size, 0);
set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.clear();
assert.same(set.size, 0);
assert.false(set.has(1));
assert.false(set.has(2));
assert.false(set.has(3));
const frozen = freeze({});
set = new Set();
set.add(1);
set.add(frozen);
set.clear();
assert.same(set.size, 0, 'Support frozen objects');
assert.false(set.has(1));
assert.false(set.has(frozen));
});
QUnit.test('Set#delete', assert => {
assert.isFunction(Set.prototype.delete);
if (NATIVE) assert.name(Set.prototype.delete, 'delete');
assert.arity(Set.prototype.delete, 1);
assert.looksNative(Set.prototype.delete);
assert.nonEnumerable(Set.prototype, 'delete');
const array = [];
const set = new Set();
set.add(NaN);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.add(array);
assert.same(set.size, 5);
assert.true(set.delete(NaN));
assert.same(set.size, 4);
assert.false(set.delete(4));
assert.same(set.size, 4);
set.delete([]);
assert.same(set.size, 4);
set.delete(array);
assert.same(set.size, 3);
const frozen = freeze({});
set.add(frozen);
assert.same(set.size, 4);
set.delete(frozen);
assert.same(set.size, 3);
});
QUnit.test('Set#forEach', assert => {
assert.isFunction(Set.prototype.forEach);
assert.name(Set.prototype.forEach, 'forEach');
assert.arity(Set.prototype.forEach, 1);
assert.looksNative(Set.prototype.forEach);
assert.nonEnumerable(Set.prototype, 'forEach');
let result = [];
let count = 0;
let set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.forEach(value => {
count++;
result.push(value);
});
assert.same(count, 3);
assert.deepEqual(result, [1, 2, 3]);
set = new Set();
set.add('0');
set.add('1');
set.add('2');
set.add('3');
result = '';
set.forEach(it => {
result += it;
if (it === '2') {
set.delete('2');
set.delete('3');
set.delete('1');
set.add('4');
}
});
assert.same(result, '0124');
set = new Set();
set.add('0');
result = '';
set.forEach(it => {
set.delete('0');
if (result !== '') throw new Error();
result += it;
});
assert.same(result, '0');
assert.throws(() => {
Set.prototype.forEach.call(new Map(), () => { /* empty */ });
}, 'non-generic');
});
QUnit.test('Set#has', assert => {
assert.isFunction(Set.prototype.has);
assert.name(Set.prototype.has, 'has');
assert.arity(Set.prototype.has, 1);
assert.looksNative(Set.prototype.has);
assert.nonEnumerable(Set.prototype, 'has');
const array = [];
const frozen = freeze({});
const set = new Set();
set.add(NaN);
set.add(2);
set.add(3);
set.add(2);
set.add(1);
set.add(frozen);
set.add(array);
assert.true(set.has(NaN));
assert.true(set.has(array));
assert.true(set.has(frozen));
assert.true(set.has(2));
assert.false(set.has(4));
assert.false(set.has([]));
});
QUnit.test('Set#size', assert => {
assert.nonEnumerable(Set.prototype, 'size');
const set = new Set();
set.add(1);
const { size } = set;
assert.same(typeof size, 'number', 'size is number');
assert.same(size, 1, 'size is correct');
if (DESCRIPTORS) {
const sizeDescriptor = getOwnPropertyDescriptor(Set.prototype, 'size');
const getter = sizeDescriptor && sizeDescriptor.get;
const setter = sizeDescriptor && sizeDescriptor.set;
assert.same(typeof getter, 'function', 'size is getter');
assert.same(typeof setter, 'undefined', 'size is not setter');
assert.throws(() => Set.prototype.size, TypeError);
}
});
QUnit.test('Set & -0', assert => {
let set = new Set();
set.add(-0);
assert.same(set.size, 1);
assert.true(set.has(0));
assert.true(set.has(-0));
set.forEach(it => {
assert.false(is(it, -0));
});
set.delete(-0);
assert.same(set.size, 0);
set = new Set([-0]);
set.forEach(key => {
assert.false(is(key, -0));
});
set = new Set();
set.add(4);
set.add(3);
set.add(2);
set.add(1);
set.add(0);
assert.true(set.has(-0));
});
QUnit.test('Set#@@toStringTag', assert => {
assert.same(Set.prototype[Symbol.toStringTag], 'Set', 'Set::@@toStringTag is `Set`');
assert.same(String(new Set()), '[object Set]', 'correct stringification');
});
QUnit.test('Set Iterator', assert => {
const set = new Set();
set.add('a');
set.add('b');
set.add('c');
set.add('d');
const results = [];
const iterator = set.keys();
results.push(iterator.next().value);
assert.true(set.delete('a'));
assert.true(set.delete('b'));
assert.true(set.delete('c'));
set.add('e');
results.push(iterator.next().value, iterator.next().value);
assert.true(iterator.next().done);
set.add('f');
assert.true(iterator.next().done);
assert.deepEqual(results, ['a', 'd', 'e']);
});
QUnit.test('Set#keys', assert => {
assert.isFunction(Set.prototype.keys);
assert.name(Set.prototype.keys, 'values');
assert.arity(Set.prototype.keys, 0);
assert.looksNative(Set.prototype.keys);
assert.same(Set.prototype.keys, Set.prototype.values);
assert.nonEnumerable(Set.prototype, 'keys');
const set = new Set();
set.add('q');
set.add('w');
set.add('e');
const iterator = set.keys();
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'Set Iterator');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'w',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'e',
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
});
QUnit.test('Set#values', assert => {
assert.isFunction(Set.prototype.values);
assert.name(Set.prototype.values, 'values');
assert.arity(Set.prototype.values, 0);
assert.looksNative(Set.prototype.values);
assert.nonEnumerable(Set.prototype, 'values');
const set = new Set();
set.add('q');
set.add('w');
set.add('e');
const iterator = set.values();
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'Set Iterator');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'w',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'e',
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
});
QUnit.test('Set#entries', assert => {
assert.isFunction(Set.prototype.entries);
assert.name(Set.prototype.entries, 'entries');
assert.arity(Set.prototype.entries, 0);
assert.looksNative(Set.prototype.entries);
assert.nonEnumerable(Set.prototype, 'entries');
const set = new Set();
set.add('q');
set.add('w');
set.add('e');
const iterator = set.entries();
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'Set Iterator');
assert.deepEqual(iterator.next(), {
value: ['q', 'q'],
done: false,
});
assert.deepEqual(iterator.next(), {
value: ['w', 'w'],
done: false,
});
assert.deepEqual(iterator.next(), {
value: ['e', 'e'],
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
});
QUnit.test('Set#@@iterator', assert => {
assert.isIterable(Set.prototype);
assert.name(Set.prototype[Symbol.iterator], 'values');
assert.arity(Set.prototype[Symbol.iterator], 0);
assert.looksNative(Set.prototype[Symbol.iterator]);
assert.same(Set.prototype[Symbol.iterator], Set.prototype.values);
assert.nonEnumerable(Set.prototype, 'values');
const set = new Set();
set.add('q');
set.add('w');
set.add('e');
const iterator = set[Symbol.iterator]();
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'Set Iterator');
assert.same(String(iterator), '[object Set Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'w',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'e',
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.symmetric-difference.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#symmetricDifference', assert => {
const { symmetricDifference } = Set.prototype;
const { from } = Array;
const { defineProperty } = Object;
assert.isFunction(symmetricDifference);
assert.arity(symmetricDifference, 1);
assert.name(symmetricDifference, 'symmetricDifference');
assert.looksNative(symmetricDifference);
assert.nonEnumerable(Set.prototype, 'symmetricDifference');
const set = new Set([1]);
assert.notSame(set.symmetricDifference(new Set()), set);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(new Set([4, 5]))), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(new Set([3, 4]))), [1, 2, 4]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(createSetLike([4, 5]))), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(createSetLike([3, 4]))), [1, 2, 4]);
// TODO: drop from core-js@4
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference([4, 5])), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference([3, 4])), [1, 2, 4]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(createIterable([4, 5]))), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).symmetricDifference(createIterable([3, 4]))), [1, 2, 4]);
assert.throws(() => new Set([1, 2, 3]).symmetricDifference(), TypeError);
assert.throws(() => symmetricDifference.call({}, [1, 2, 3]), TypeError);
assert.throws(() => symmetricDifference.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => symmetricDifference.call(null, [1, 2, 3]), TypeError);
// Duplicate keys in other's iterator: value present in O should be removed idempotently
{
const baseSet = new Set([1, 2, 3]);
const setLike = {
size: 2,
has() { return false; },
keys() {
const vals = [2, 2];
let i = 0;
return { next() { return i < vals.length ? { done: false, value: vals[i++] } : { done: true }; } };
},
};
// 2 is in O → both occurrences remove 2 from result (second is a no-op)
assert.deepEqual(from(baseSet.symmetricDifference(setLike)), [1, 3]);
}
{
// https://github.com/WebKit/WebKit/pull/27264/files#diff-7bdbbad7ceaa222787994f2db702dd45403fa98e14d6270aa65aaf09754dcfe0R8
const baseSet = new Set(['a', 'b', 'c', 'd', 'e']);
const values = ['f', 'g', 'h', 'i', 'j'];
const setLike = {
size: values.length,
has() { return true; },
keys() {
let index = 0;
return {
next() {
const done = index >= values.length;
if (!baseSet.has('f')) baseSet.add('f');
return { done, value: values[index++] };
},
};
},
};
assert.deepEqual(from(baseSet.symmetricDifference(setLike)), ['a', 'b', 'c', 'd', 'e', 'g', 'h', 'i', 'j']);
}
if (DESCRIPTORS) {
// Should get iterator record of a set-like object before cloning this
// https://bugs.webkit.org/show_bug.cgi?id=289430
const baseSet = new Set();
const setLike = {
size: 0,
has() { return true; },
keys() {
return defineProperty({}, 'next', { get() {
baseSet.clear();
baseSet.add(4);
return function () {
return { done: true };
};
} });
},
};
assert.deepEqual(from(baseSet.symmetricDifference(setLike)), [4]);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.set.union.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
import { createIterable, createSetLike } from '../helpers/helpers.js';
QUnit.test('Set#union', assert => {
const { union } = Set.prototype;
const { from } = Array;
const { defineProperty } = Object;
assert.isFunction(union);
assert.arity(union, 1);
assert.name(union, 'union');
assert.looksNative(union);
assert.nonEnumerable(Set.prototype, 'union');
const set = new Set([1]);
assert.notSame(set.union(new Set()), set);
assert.deepEqual(from(new Set([1, 2, 3]).union(new Set([4, 5]))), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).union(new Set([3, 4]))), [1, 2, 3, 4]);
assert.deepEqual(from(new Set([1, 2, 3]).union(createSetLike([4, 5]))), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).union(createSetLike([3, 4]))), [1, 2, 3, 4]);
// TODO: drop from core-js@4
assert.deepEqual(from(new Set([1, 2, 3]).union([4, 5])), [1, 2, 3, 4, 5]);
assert.deepEqual(from(new Set([1, 2, 3]).union([3, 4])), [1, 2, 3, 4]);
assert.deepEqual(from(new Set([1, 2, 3]).union(createIterable([3, 4]))), [1, 2, 3, 4]);
assert.throws(() => new Set([1, 2, 3]).union(), TypeError);
assert.throws(() => union.call({}, [1, 2, 3]), TypeError);
assert.throws(() => union.call(undefined, [1, 2, 3]), TypeError);
assert.throws(() => union.call(null, [1, 2, 3]), TypeError);
if (DESCRIPTORS) {
// Should get iterator record of a set-like object before cloning this
// https://bugs.webkit.org/show_bug.cgi?id=289430
const baseSet = new Set();
const setLike = {
size: 0,
has() { return true; },
keys() {
return defineProperty({}, 'next', { get() {
baseSet.clear();
baseSet.add(4);
return function () {
return { done: true };
};
} });
},
};
assert.deepEqual(from(baseSet.union(setLike)), [4]);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.anchor.js
|
JavaScript
|
QUnit.test('String#anchor', assert => {
const { anchor } = String.prototype;
assert.isFunction(anchor);
assert.arity(anchor, 1);
assert.name(anchor, 'anchor');
assert.looksNative(anchor);
assert.nonEnumerable(String.prototype, 'anchor');
assert.same('a'.anchor('b'), '<a name="b">a</a>', 'lower case');
assert.same('a'.anchor('"'), '<a name=""">a</a>', 'escape quotes');
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('anchor test');
assert.throws(() => anchor.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => anchor.call('a', symbol), 'throws on symbol argument');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.at-alternative.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#at', assert => {
const { at } = String.prototype;
assert.isFunction(at);
assert.arity(at, 1);
assert.name(at, 'at');
assert.looksNative(at);
assert.nonEnumerable(String.prototype, 'at');
assert.same('1', '123'.at(0));
assert.same('2', '123'.at(1));
assert.same('3', '123'.at(2));
assert.same(undefined, '123'.at(3));
assert.same('3', '123'.at(-1));
assert.same('2', '123'.at(-2));
assert.same('1', '123'.at(-3));
assert.same(undefined, '123'.at(-4));
assert.same('1', '123'.at(0.4));
assert.same('1', '123'.at(0.5));
assert.same('1', '123'.at(0.6));
assert.same('1', '1'.at(NaN));
assert.same('1', '1'.at());
assert.same('1', '123'.at(-0));
// TODO: disabled by default because of the conflict with old proposal
// assert.same('\uD842', '𠮷'.at());
assert.same('1', at.call({ toString() { return '123'; } }, 0));
assert.throws(() => at.call(Symbol('at-alternative test'), 0), 'throws on symbol context');
if (STRICT) {
assert.throws(() => at.call(null, 0), TypeError);
assert.throws(() => at.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.big.js
|
JavaScript
|
QUnit.test('String#big', assert => {
const { big } = String.prototype;
assert.isFunction(big);
assert.arity(big, 0);
assert.name(big, 'big');
assert.looksNative(big);
assert.nonEnumerable(String.prototype, 'big');
assert.same('a'.big(), '<big>a</big>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => big.call(Symbol('big test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.blink.js
|
JavaScript
|
QUnit.test('String#blink', assert => {
const { blink } = String.prototype;
assert.isFunction(blink);
assert.arity(blink, 0);
assert.name(blink, 'blink');
assert.looksNative(blink);
assert.nonEnumerable(String.prototype, 'blink');
assert.same('a'.blink(), '<blink>a</blink>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => blink.call(Symbol('blink test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.bold.js
|
JavaScript
|
QUnit.test('String#bold', assert => {
const { bold } = String.prototype;
assert.isFunction(bold);
assert.arity(bold, 0);
assert.name(bold, 'bold');
assert.looksNative(bold);
assert.nonEnumerable(String.prototype, 'bold');
assert.same('a'.bold(), '<b>a</b>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => bold.call(Symbol('bold test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.code-point-at.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#codePointAt', assert => {
const { codePointAt } = String.prototype;
assert.isFunction(codePointAt);
assert.arity(codePointAt, 1);
assert.name(codePointAt, 'codePointAt');
assert.looksNative(codePointAt);
assert.nonEnumerable(String.prototype, 'codePointAt');
assert.same('abc\uD834\uDF06def'.codePointAt(''), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt('_'), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(-Infinity), undefined);
assert.same('abc\uD834\uDF06def'.codePointAt(-1), undefined);
assert.same('abc\uD834\uDF06def'.codePointAt(-0), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(0), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(3), 0x1D306);
assert.same('abc\uD834\uDF06def'.codePointAt(4), 0xDF06);
assert.same('abc\uD834\uDF06def'.codePointAt(5), 0x64);
assert.same('abc\uD834\uDF06def'.codePointAt(42), undefined);
assert.same('abc\uD834\uDF06def'.codePointAt(Infinity), undefined);
assert.same('abc\uD834\uDF06def'.codePointAt(Infinity), undefined);
assert.same('abc\uD834\uDF06def'.codePointAt(NaN), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(false), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(null), 0x61);
assert.same('abc\uD834\uDF06def'.codePointAt(undefined), 0x61);
assert.same('\uD834\uDF06def'.codePointAt(''), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt('1'), 0xDF06);
assert.same('\uD834\uDF06def'.codePointAt('_'), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(-1), undefined);
assert.same('\uD834\uDF06def'.codePointAt(-0), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(0), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(1), 0xDF06);
assert.same('\uD834\uDF06def'.codePointAt(42), undefined);
assert.same('\uD834\uDF06def'.codePointAt(false), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(null), 0x1D306);
assert.same('\uD834\uDF06def'.codePointAt(undefined), 0x1D306);
assert.same('\uD834abc'.codePointAt(''), 0xD834);
assert.same('\uD834abc'.codePointAt('_'), 0xD834);
assert.same('\uD834abc'.codePointAt(), 0xD834);
assert.same('\uD834abc'.codePointAt(-1), undefined);
assert.same('\uD834abc'.codePointAt(-0), 0xD834);
assert.same('\uD834abc'.codePointAt(0), 0xD834);
assert.same('\uD834abc'.codePointAt(false), 0xD834);
assert.same('\uD834abc'.codePointAt(NaN), 0xD834);
assert.same('\uD834abc'.codePointAt(null), 0xD834);
assert.same('\uD834abc'.codePointAt(undefined), 0xD834);
assert.same('\uDF06abc'.codePointAt(''), 0xDF06);
assert.same('\uDF06abc'.codePointAt('_'), 0xDF06);
assert.same('\uDF06abc'.codePointAt(), 0xDF06);
assert.same('\uDF06abc'.codePointAt(-1), undefined);
assert.same('\uDF06abc'.codePointAt(-0), 0xDF06);
assert.same('\uDF06abc'.codePointAt(0), 0xDF06);
assert.same('\uDF06abc'.codePointAt(false), 0xDF06);
assert.same('\uDF06abc'.codePointAt(NaN), 0xDF06);
assert.same('\uDF06abc'.codePointAt(null), 0xDF06);
assert.same('\uDF06abc'.codePointAt(undefined), 0xDF06);
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => codePointAt.call(Symbol('codePointAt test'), 1), 'throws on symbol context');
}
if (STRICT) {
assert.throws(() => codePointAt.call(null, 0), TypeError);
assert.throws(() => codePointAt.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.ends-with.js
|
JavaScript
|
import { GLOBAL, STRICT } from '../helpers/constants.js';
const Symbol = GLOBAL.Symbol || {};
QUnit.test('String#endsWith', assert => {
const { endsWith } = String.prototype;
assert.isFunction(endsWith);
assert.arity(endsWith, 1);
assert.name(endsWith, 'endsWith');
assert.looksNative(endsWith);
assert.nonEnumerable(String.prototype, 'endsWith');
assert.true('undefined'.endsWith());
assert.false('undefined'.endsWith(null));
assert.true('abc'.endsWith(''));
assert.true('abc'.endsWith('c'));
assert.true('abc'.endsWith('bc'));
assert.false('abc'.endsWith('ab'));
assert.true('abc'.endsWith('', NaN));
assert.false('abc'.endsWith('c', -1));
assert.true('abc'.endsWith('a', 1));
assert.true('abc'.endsWith('c', Infinity));
assert.true('abc'.endsWith('a', true));
assert.false('abc'.endsWith('c', 'x'));
assert.false('abc'.endsWith('a', 'x'));
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('endsWith test');
assert.throws(() => endsWith.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => endsWith.call('a', symbol), 'throws on symbol argument');
}
if (STRICT) {
assert.throws(() => endsWith.call(null, '.'), TypeError);
assert.throws(() => endsWith.call(undefined, '.'), TypeError);
}
const regexp = /./;
assert.throws(() => '/./'.endsWith(regexp), TypeError);
regexp[Symbol.match] = false;
assert.notThrows(() => '/./'.endsWith(regexp));
const object = {};
assert.notThrows(() => '[object Object]'.endsWith(object));
object[Symbol.match] = true;
assert.throws(() => '[object Object]'.endsWith(object), TypeError);
// side-effect ordering: ToString(searchString) should happen before ToIntegerOrInfinity(endPosition)
const order = [];
'abc'.endsWith(
{ toString() { order.push('search'); return 'c'; } },
{ valueOf() { order.push('pos'); return 3; } },
);
assert.deepEqual(order, ['search', 'pos'], 'ToString(searchString) before ToIntegerOrInfinity(endPosition)');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.fixed.js
|
JavaScript
|
QUnit.test('String#fixed', assert => {
const { fixed } = String.prototype;
assert.isFunction(fixed);
assert.arity(fixed, 0);
assert.name(fixed, 'fixed');
assert.looksNative(fixed);
assert.nonEnumerable(String.prototype, 'fixed');
assert.same('a'.fixed(), '<tt>a</tt>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => fixed.call(Symbol('fixed test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.fontcolor.js
|
JavaScript
|
QUnit.test('String#fontcolor', assert => {
const { fontcolor } = String.prototype;
assert.isFunction(fontcolor);
assert.arity(fontcolor, 1);
assert.name(fontcolor, 'fontcolor');
assert.looksNative(fontcolor);
assert.nonEnumerable(String.prototype, 'fontcolor');
assert.same('a'.fontcolor('b'), '<font color="b">a</font>', 'lower case');
assert.same('a'.fontcolor('"'), '<font color=""">a</font>', 'escape quotes');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => fontcolor.call(Symbol('fontcolor test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.fontsize.js
|
JavaScript
|
QUnit.test('String#fontsize', assert => {
const { fontsize } = String.prototype;
assert.isFunction(fontsize);
assert.arity(fontsize, 1);
assert.name(fontsize, 'fontsize');
assert.looksNative(fontsize);
assert.nonEnumerable(String.prototype, 'fontsize');
assert.same('a'.fontsize('b'), '<font size="b">a</font>', 'lower case');
assert.same('a'.fontsize('"'), '<font size=""">a</font>', 'escape quotes');
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('fontsize test');
assert.throws(() => fontsize.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => fontsize.call('a', symbol), 'throws on symbol argument');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.from-code-point.js
|
JavaScript
|
/* eslint-disable prefer-spread -- required for testing */
QUnit.test('String.fromCodePoint', assert => {
const { fromCodePoint } = String;
assert.isFunction(fromCodePoint);
assert.arity(fromCodePoint, 1);
assert.name(fromCodePoint, 'fromCodePoint');
assert.looksNative(fromCodePoint);
assert.nonEnumerable(String, 'fromCodePoint');
assert.same(fromCodePoint(''), '\0');
assert.same(fromCodePoint(), '');
assert.same(fromCodePoint(-0), '\0');
assert.same(fromCodePoint(0), '\0');
assert.same(fromCodePoint(0x1D306), '\uD834\uDF06');
assert.same(fromCodePoint(0x1D306, 0x61, 0x1D307), '\uD834\uDF06a\uD834\uDF07');
assert.same(fromCodePoint(0x61, 0x62, 0x1D307), 'ab\uD834\uDF07');
assert.same(fromCodePoint(false), '\0');
assert.same(fromCodePoint(null), '\0');
assert.throws(() => fromCodePoint('_'), RangeError);
assert.throws(() => fromCodePoint('+Infinity'), RangeError);
assert.throws(() => fromCodePoint('-Infinity'), RangeError);
assert.throws(() => fromCodePoint(-1), RangeError);
assert.throws(() => fromCodePoint(0x10FFFF + 1), RangeError);
assert.throws(() => fromCodePoint(3.14), RangeError);
assert.throws(() => fromCodePoint(3e-2), RangeError);
assert.throws(() => fromCodePoint(-Infinity), RangeError);
assert.throws(() => fromCodePoint(Infinity), RangeError);
assert.throws(() => fromCodePoint(NaN), RangeError);
assert.throws(() => fromCodePoint(undefined), RangeError);
assert.throws(() => fromCodePoint({}), RangeError);
assert.throws(() => fromCodePoint(/./), RangeError);
let number = 0x60;
assert.same(fromCodePoint({
valueOf() {
return ++number;
},
}), 'a');
assert.same(number, 0x61);
// one code unit per symbol
let counter = 2 ** 15 * 3 / 2;
let result = [];
while (--counter >= 0) result.push(0);
// should not throw
fromCodePoint.apply(null, result);
counter = 2 ** 15 * 3 / 2;
result = [];
while (--counter >= 0) result.push(0xFFFF + 1);
// should not throw
fromCodePoint.apply(null, result);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.includes.js
|
JavaScript
|
import { GLOBAL, STRICT } from '../helpers/constants.js';
const Symbol = GLOBAL.Symbol || {};
QUnit.test('String#includes', assert => {
const { includes } = String.prototype;
assert.isFunction(includes);
assert.arity(includes, 1);
assert.name(includes, 'includes');
assert.looksNative(includes);
assert.nonEnumerable(String.prototype, 'includes');
assert.false('abc'.includes());
assert.true('aundefinedb'.includes());
assert.true('abcd'.includes('b', 1));
assert.false('abcd'.includes('b', 2));
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('includes test');
assert.throws(() => includes.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => includes.call('a', symbol), 'throws on symbol argument');
}
if (STRICT) {
assert.throws(() => includes.call(null, '.'), TypeError);
assert.throws(() => includes.call(undefined, '.'), TypeError);
}
const regexp = /./;
assert.throws(() => '/./'.includes(regexp), TypeError);
regexp[Symbol.match] = false;
assert.notThrows(() => '/./'.includes(regexp));
const object = {};
assert.notThrows(() => '[object Object]'.includes(object));
object[Symbol.match] = true;
assert.throws(() => '[object Object]'.includes(object), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.is-well-formed.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#isWellFormed', assert => {
const { isWellFormed } = String.prototype;
assert.isFunction(isWellFormed);
assert.arity(isWellFormed, 0);
assert.name(isWellFormed, 'isWellFormed');
assert.looksNative(isWellFormed);
assert.nonEnumerable(String.prototype, 'isWellFormed');
assert.true(isWellFormed.call('a'), 'a');
assert.true(isWellFormed.call('abc'), 'abc');
assert.true(isWellFormed.call('💩'), '💩');
assert.true(isWellFormed.call('💩b'), '💩b');
assert.true(isWellFormed.call('a💩'), '💩');
assert.true(isWellFormed.call('a💩b'), 'a💩b');
assert.true(isWellFormed.call('💩a💩'), '💩a💩');
assert.true(!isWellFormed.call('\uD83D'), '\uD83D');
assert.true(!isWellFormed.call('\uDCA9'), '\uDCA9');
assert.true(!isWellFormed.call('\uDCA9\uD83D'), '\uDCA9\uD83D');
assert.true(!isWellFormed.call('a\uD83D'), 'a\uD83D');
assert.true(!isWellFormed.call('\uDCA9a'), '\uDCA9a');
assert.true(!isWellFormed.call('a\uD83Da'), 'a\uD83Da');
assert.true(!isWellFormed.call('a\uDCA9a'), 'a\uDCA9a');
assert.true(isWellFormed.call({
toString() {
return 'abc';
},
}), 'conversion #1');
assert.true(!isWellFormed.call({
toString() {
return '\uD83D';
},
}), 'conversion #2');
if (STRICT) {
assert.throws(() => isWellFormed.call(null), TypeError, 'coercible #1');
assert.throws(() => isWellFormed.call(undefined), TypeError, 'coercible #2');
}
assert.throws(() => isWellFormed.call(Symbol('isWellFormed test')), 'throws on symbol context');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.italics.js
|
JavaScript
|
QUnit.test('String#italics', assert => {
const { italics } = String.prototype;
assert.isFunction(italics);
assert.arity(italics, 0);
assert.name(italics, 'italics');
assert.looksNative(italics);
assert.nonEnumerable(String.prototype, 'italics');
assert.same('a'.italics(), '<i>a</i>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => italics.call(Symbol('italics test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.iterator.js
|
JavaScript
|
import { GLOBAL } from '../helpers/constants.js';
const Symbol = GLOBAL.Symbol || {};
QUnit.test('String#@@iterator', assert => {
assert.isIterable(String.prototype);
let iterator = 'qwe'[Symbol.iterator]();
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'String Iterator');
assert.same(String(iterator), '[object String Iterator]');
assert.deepEqual(iterator.next(), {
value: 'q',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'w',
done: false,
});
assert.deepEqual(iterator.next(), {
value: 'e',
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
assert.same(Array.from('𠮷𠮷𠮷').length, 3);
iterator = '𠮷𠮷𠮷'[Symbol.iterator]();
assert.deepEqual(iterator.next(), {
value: '𠮷',
done: false,
});
assert.deepEqual(iterator.next(), {
value: '𠮷',
done: false,
});
assert.deepEqual(iterator.next(), {
value: '𠮷',
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
// early FF case with native method, but polyfilled `Symbol`
// assert.throws(() => ''[Symbol.iterator].call(Symbol()), 'throws on symbol context');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.link.js
|
JavaScript
|
QUnit.test('String#link', assert => {
const { link } = String.prototype;
assert.isFunction(link);
assert.arity(link, 1);
assert.name(link, 'link');
assert.looksNative(link);
assert.nonEnumerable(String.prototype, 'link');
assert.same('a'.link('b'), '<a href="b">a</a>', 'lower case');
assert.same('a'.link('"'), '<a href=""">a</a>', 'escape quotes');
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('link test');
assert.throws(() => link.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => link.call('a', symbol), 'throws on symbol argument');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.match-all.js
|
JavaScript
|
import { DESCRIPTORS, STRICT } from '../helpers/constants.js';
QUnit.test('String#matchAll', assert => {
const { matchAll } = String.prototype;
const { assign } = Object;
assert.isFunction(matchAll);
assert.arity(matchAll, 1);
assert.name(matchAll, 'matchAll');
assert.looksNative(matchAll);
assert.nonEnumerable(String.prototype, 'matchAll');
let data = ['aabc', { toString() { return 'aabc'; } }];
for (const target of data) {
const iterator = matchAll.call(target, /[ac]/g);
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.deepEqual(iterator.next(), {
value: assign(['a'], {
input: 'aabc',
index: 0,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['a'], {
input: 'aabc',
index: 1,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['c'], {
input: 'aabc',
index: 3,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
}
let iterator = '1111a2b3cccc'.matchAll(/(\d)(\D)/g);
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.same(iterator[Symbol.toStringTag], 'RegExp String Iterator');
assert.same(String(iterator), '[object RegExp String Iterator]');
assert.deepEqual(iterator.next(), {
value: assign(['1a', '1', 'a'], {
input: '1111a2b3cccc',
index: 3,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['2b', '2', 'b'], {
input: '1111a2b3cccc',
index: 5,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['3c', '3', 'c'], {
input: '1111a2b3cccc',
index: 7,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
// eslint-disable-next-line regexp/no-missing-g-flag -- required for testing
assert.throws(() => '1111a2b3cccc'.matchAll(/(\d)(\D)/), TypeError);
iterator = '1111a2b3cccc'.matchAll('(\\d)(\\D)');
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.deepEqual(iterator.next(), {
value: assign(['1a', '1', 'a'], {
input: '1111a2b3cccc',
index: 3,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['2b', '2', 'b'], {
input: '1111a2b3cccc',
index: 5,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign(['3c', '3', 'c'], {
input: '1111a2b3cccc',
index: 7,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
/* IE8- issue
iterator = 'abc'.matchAll(/\B/g);
assert.isIterator(iterator);
assert.isIterable(iterator);
assert.deepEqual(iterator.next(), {
value: assign([''], {
input: 'abc',
index: 1,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: assign([''], {
input: 'abc',
index: 2,
}),
done: false,
});
assert.deepEqual(iterator.next(), {
value: undefined,
done: true,
});
*/
data = [null, undefined, NaN, 42, {}, []];
for (const target of data) {
assert.notThrows(() => ''.matchAll(target), `Not throws on ${ target } as the first argument`);
}
if (DESCRIPTORS && typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('matchAll test');
assert.throws(() => matchAll.call(symbol, /./), 'throws on symbol context');
assert.throws(() => matchAll.call('a', symbol), 'throws on symbol argument');
}
if (STRICT) {
assert.throws(() => matchAll.call(null, /./g), TypeError, 'Throws on null as `this`');
assert.throws(() => matchAll.call(undefined, /./g), TypeError, 'Throws on undefined as `this`');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.match.js
|
JavaScript
|
// TODO: fix escaping in regexps
/* eslint-disable prefer-regex-literals, regexp/prefer-regexp-exec -- required for testing */
import { GLOBAL, NATIVE, STRICT } from '../helpers/constants.js';
import { patchRegExp$exec } from '../helpers/helpers.js';
const Symbol = GLOBAL.Symbol || {};
const run = assert => {
assert.isFunction(''.match);
assert.arity(''.match, 1);
assert.name(''.match, 'match');
assert.looksNative(''.match);
assert.nonEnumerable(String.prototype, 'match');
let instance = Object(true);
instance.match = String.prototype.match;
assert.same(instance.match(true)[0], 'true', 'S15.5.4.10_A1_T1');
instance = Object(false);
instance.match = String.prototype.match;
assert.same(instance.match(false)[0], 'false', 'S15.5.4.10_A1_T2');
let matched = ''.match();
let expected = RegExp().exec('');
assert.same(matched.length, expected.length, 'S15.5.4.10_A1_T4 #1');
assert.same(matched.index, expected.index, 'S15.5.4.10_A1_T4 #2');
assert.same(matched.input, expected.input, 'S15.5.4.10_A1_T4 #3');
assert.same('gnulluna'.match(null)[0], 'null', 'S15.5.4.10_A1_T5');
matched = Object('undefined').match(undefined);
expected = RegExp(undefined).exec('undefined');
assert.same(matched.length, expected.length, 'S15.5.4.10_A1_T6 #1');
assert.same(matched.index, expected.index, 'S15.5.4.10_A1_T6 #2');
assert.same(matched.input, expected.input, 'S15.5.4.10_A1_T6 #3');
let object = { toString() { /* empty */ } };
matched = String(object).match(undefined);
expected = RegExp(undefined).exec('undefined');
assert.same(matched.length, expected.length, 'S15.5.4.10_A1_T8 #1');
assert.same(matched.index, expected.index, 'S15.5.4.10_A1_T8 #2');
assert.same(matched.input, expected.input, 'S15.5.4.10_A1_T8 #3');
object = { toString() { return '\u0041B'; } };
let string = 'ABB\u0041BABAB';
assert.same(string.match(object)[0], 'AB', 'S15.5.4.10_A1_T10');
object = { toString() { throw new Error('intostr'); } };
try {
string.match(object);
assert.avoid('S15.5.4.10_A1_T11 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intostr', `S15.5.4.10_A1_T11 #1.1: Exception === "intostr". Actual: ${ error }`);
}
object = {
toString() {
return {};
},
valueOf() {
throw new Error('intostr');
},
};
try {
string.match(object);
assert.avoid('S15.5.4.10_A1_T12 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intostr', `S15.5.4.10_A1_T12 #1.1: Exception === "intostr". Actual: ${ error }`);
}
object = {
toString() {
return {};
},
valueOf() {
return 1;
},
};
assert.same('ABB\u0041B\u0031ABAB\u0031BBAA'.match(object)[0], '1', 'S15.5.4.10_A1_T13 #1');
assert.same('ABB\u0041B\u0031ABAB\u0031BBAA'.match(object).length, 1, 'S15.5.4.10_A1_T13 #2');
let regexp = RegExp('77');
assert.same('ABB\u0041BABAB\u0037\u0037BBAA'.match(regexp)[0], '77', 'S15.5.4.10_A1_T14');
string = '1234567890';
assert.same(string.match(3)[0], '3', 'S15.5.4.10_A2_T1 #1');
assert.same(string.match(3).length, 1, 'S15.5.4.10_A2_T1 #2');
assert.same(string.match(3).index, 2, 'S15.5.4.10_A2_T1 #3');
assert.same(string.match(3).input, string, 'S15.5.4.10_A2_T1 #4');
let matches = ['34', '34', '34'];
string = '343443444';
assert.same(string.match(/34/g).length, 3, 'S15.5.4.10_A2_T2 #1');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(/34/g)[i], matches[i], 'S15.5.4.10_A2_T2 #2');
}
matches = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
string = '123456abcde7890';
assert.same(string.match(/\d/g).length, 10, 'S15.5.4.10_A2_T3 #1');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(/\d/g)[i], matches[i], 'S15.5.4.10_A2_T3 #2');
}
matches = ['12', '34', '56', '78', '90'];
assert.same(string.match(/\d{2}/g).length, 5, 'S15.5.4.10_A2_T4 #1');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(/\d{2}/g)[i], matches[i], 'S15.5.4.10_A2_T4 #2');
}
matches = ['ab', 'cd'];
assert.same(string.match(/\D{2}/g).length, 2, 'S15.5.4.10_A2_T5 #1');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(/\D{2}/g)[i], matches[i], 'S15.5.4.10_A2_T5 #2');
}
string = 'Boston, Mass. 02134';
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/)[0], '02134', 'S15.5.4.10_A2_T6 #1');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/)[1], '02134', 'S15.5.4.10_A2_T6 #2');
if (NATIVE) assert.same(string.match(/(\d{5})([ -]?\d{4})?$/)[2], undefined, 'S15.5.4.10_A2_T6 #3');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/).length, 3, 'S15.5.4.10_A2_T6 #4');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/).index, 14, 'S15.5.4.10_A2_T6 #5');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/).input, string, 'S15.5.4.10_A2_T6 #6');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/g).length, 1, 'S15.5.4.10_A2_T7 #1');
assert.same(string.match(/(\d{5})([ -]?\d{4})?$/g)[0], '02134', 'S15.5.4.10_A2_T7 #2');
/* IE8- buggy here (empty string instead of `undefined`), but we don't polyfill base `.match` logic
matches = ['02134', '02134', undefined];
string = 'Boston, MA 02134';
regexp = /([\d]{5})([-\ ]?[\d]{4})?$/;
regexp.lastIndex = 0;
assert.same(string.match(regexp).length, 3, 'S15.5.4.10_A2_T8 #1');
assert.same(string.match(regexp).index, string.lastIndexOf('0'), 'S15.5.4.10_A2_T8 #2');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(regexp)[i], matches[i], 'S15.5.4.10_A2_T8 #3');
}
string = 'Boston, MA 02134';
matches = ['02134', '02134', undefined];
regexp = /([\d]{5})([-\ ]?[\d]{4})?$/;
regexp.lastIndex = string.length;
assert.same(string.match(regexp).length, 3, 'S15.5.4.10_A2_T9 #1');
assert.same(string.match(regexp).index, string.lastIndexOf('0'), 'S15.5.4.10_A2_T9 #2');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(regexp)[i], matches[i], 'S15.5.4.10_A2_T9 #3');
}
string = 'Boston, MA 02134';
matches = ['02134', '02134', undefined];
regexp = /([\d]{5})([-\ ]?[\d]{4})?$/;
regexp.lastIndex = string.lastIndexOf('0');
assert.same(string.match(regexp).length, 3, 'S15.5.4.10_A2_T10 #1');
assert.same(string.match(regexp).index, string.lastIndexOf('0'), 'S15.5.4.10_A2_T10 #2');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(regexp)[i], matches[i], 'S15.5.4.10_A2_T10 #3');
}
string = 'Boston, MA 02134';
matches = ['02134', '02134', undefined];
regexp = /([\d]{5})([-\ ]?[\d]{4})?$/;
regexp.lastIndex = string.lastIndexOf('0') + 1;
assert.same(string.match(regexp).length, 3, 'S15.5.4.10_A2_T11 #1');
assert.same(string.match(regexp).index, string.lastIndexOf('0'), 'S15.5.4.10_A2_T11 #2');
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(string.match(regexp)[i], matches[i], 'S15.5.4.10_A2_T11 #3');
}
*/
string = 'Boston, MA 02134';
regexp = /(\d{5})([ -]?\d{4})?$/g;
assert.same(string.match(regexp).length, 1, 'S15.5.4.10_A2_T12 #1');
assert.same(string.match(regexp)[0], '02134', 'S15.5.4.10_A2_T12 #2');
regexp.lastIndex = 0;
assert.same(string.match(regexp).length, 1, 'S15.5.4.10_A2_T13 #1');
assert.same(string.match(regexp)[0], '02134', 'S15.5.4.10_A2_T13 #2');
regexp.lastIndex = string.length;
assert.same(string.match(regexp).length, 1, 'S15.5.4.10_A2_T14 #1');
assert.same(string.match(regexp)[0], '02134', 'S15.5.4.10_A2_T14 #2');
regexp.lastIndex = string.lastIndexOf('0');
assert.same(string.match(regexp).length, 1, 'S15.5.4.10_A2_T15 #1');
assert.same(string.match(regexp)[0], '02134', 'S15.5.4.10_A2_T15 #2');
regexp.lastIndex = string.lastIndexOf('0') + 1;
assert.same(string.match(regexp).length, 1, 'S15.5.4.10_A2_T16 #1');
assert.same(string.match(regexp)[0], '02134', 'S15.5.4.10_A2_T16 #2');
regexp = /0./;
const number = 10203040506070809000;
assert.same(''.match.call(number, regexp)[0], '02', 'S15.5.4.10_A2_T17 #1');
assert.same(''.match.call(number, regexp).length, 1, 'S15.5.4.10_A2_T17 #2');
assert.same(''.match.call(number, regexp).index, 1, 'S15.5.4.10_A2_T17 #3');
assert.same(''.match.call(number, regexp).input, String(number), 'S15.5.4.10_A2_T17 #4');
regexp.lastIndex = 0;
assert.same(''.match.call(number, regexp)[0], '02', 'S15.5.4.10_A2_T18 #1');
assert.same(''.match.call(number, regexp).length, 1, 'S15.5.4.10_A2_T18 #2');
assert.same(''.match.call(number, regexp).index, 1, 'S15.5.4.10_A2_T18 #3');
assert.same(''.match.call(number, regexp).input, String(number), 'S15.5.4.10_A2_T18 #4');
assert.throws(() => ''.match.call(Symbol('match test'), /./), 'throws on symbol context');
};
QUnit.test('String#match regression', run);
QUnit.test('RegExp#@@match appearance', assert => {
const match = /./[Symbol.match];
assert.isFunction(match);
// assert.name(match, '[Symbol.match]');
assert.arity(match, 1);
assert.looksNative(match);
assert.nonEnumerable(RegExp.prototype, Symbol.match);
});
QUnit.test('RegExp#@@match basic behavior', assert => {
const string = '123456abcde7890';
const matches = ['12', '34', '56', '78', '90'];
assert.same(/\d{2}/g[Symbol.match](string).length, 5);
for (let i = 0, { length } = matches; i < length; ++i) {
assert.same(/\d{2}/g[Symbol.match](string)[i], matches[i]);
}
});
QUnit.test('String#match delegates to @@match', assert => {
const string = STRICT ? 'string' : Object('string');
const number = STRICT ? 42 : Object(42);
const object = {};
object[Symbol.match] = function (it) {
return { value: it };
};
assert.same(string.match(object).value, string);
assert.same(''.match.call(number, object).value, number);
const regexp = /./;
regexp[Symbol.match] = function (it) {
return { value: it };
};
assert.same(string.match(regexp).value, string);
assert.same(''.match.call(number, regexp).value, number);
});
QUnit.test('RegExp#@@match delegates to exec', assert => {
const exec = function (...args) {
execCalled = true;
return /./.exec.apply(this, args);
};
let execCalled = false;
let re = /[ac]/;
re.exec = exec;
assert.deepEqual(re[Symbol.match]('abc'), ['a']);
assert.true(execCalled);
re = /a/;
// Not a function, should be ignored
re.exec = 3;
assert.deepEqual(re[Symbol.match]('abc'), ['a']);
re = /a/;
// Does not return an object, should throw
re.exec = () => 3;
assert.throws(() => re[Symbol.match]('abc'));
});
QUnit.test('RegExp#@@match implementation', patchRegExp$exec(run));
QUnit.test('RegExp#@@match global+unicode empty match at string end', assert => {
// eslint-disable-next-line regexp/no-empty-group -- testing
const result = 'abc'.match(/(?:)/gu);
assert.arrayEqual(result, ['', '', '', ''], 'does not infinite loop on global+unicode empty match');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.pad-end.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#padEnd', assert => {
const { padEnd } = String.prototype;
assert.isFunction(padEnd);
assert.arity(padEnd, 1);
assert.name(padEnd, 'padEnd');
assert.looksNative(padEnd);
assert.nonEnumerable(String.prototype, 'padEnd');
assert.same('abc'.padEnd(5), 'abc ');
assert.same('abc'.padEnd(4, 'de'), 'abcd');
assert.same('abc'.padEnd(), 'abc');
assert.same('abc'.padEnd(5, '_'), 'abc__');
assert.same(''.padEnd(0), '');
assert.same('foo'.padEnd(1), 'foo');
assert.same('foo'.padEnd(5, ''), 'foo');
const symbol = Symbol('padEnd test');
assert.throws(() => padEnd.call(symbol, 10, 'a'), 'throws on symbol context');
assert.throws(() => padEnd.call('a', 10, symbol), 'throws on symbol argument');
if (STRICT) {
assert.throws(() => padEnd.call(null, 0), TypeError);
assert.throws(() => padEnd.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.pad-start.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#padStart', assert => {
const { padStart } = String.prototype;
assert.isFunction(padStart);
assert.arity(padStart, 1);
assert.name(padStart, 'padStart');
assert.looksNative(padStart);
assert.nonEnumerable(String.prototype, 'padStart');
assert.same('abc'.padStart(5), ' abc');
assert.same('abc'.padStart(4, 'de'), 'dabc');
assert.same('abc'.padStart(), 'abc');
assert.same('abc'.padStart(5, '_'), '__abc');
assert.same(''.padStart(0), '');
assert.same('foo'.padStart(1), 'foo');
assert.same('foo'.padStart(5, ''), 'foo');
const symbol = Symbol('padStart test');
assert.throws(() => padStart.call(symbol, 10, 'a'), 'throws on symbol context');
assert.throws(() => padStart.call('a', 10, symbol), 'throws on symbol argument');
if (STRICT) {
assert.throws(() => padStart.call(null, 0), TypeError);
assert.throws(() => padStart.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.raw.js
|
JavaScript
|
QUnit.test('String.raw', assert => {
const { raw } = String;
assert.isFunction(raw);
assert.arity(raw, 1);
assert.name(raw, 'raw');
assert.looksNative(raw);
assert.nonEnumerable(String, 'raw');
assert.same(raw({ raw: ['Hi\\n', '!'] }, 'Bob'), 'Hi\\nBob!', 'raw is array');
assert.same(raw({ raw: 'test' }, 0, 1, 2), 't0e1s2t', 'raw is string');
assert.same(raw({ raw: 'test' }, 0), 't0est', 'lacks substituting');
assert.same(raw({ raw: [] }), '', 'empty template');
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('raw test');
assert.throws(() => raw({ raw: [symbol] }, 0), TypeError, 'throws on symbol #1');
assert.throws(() => raw({ raw: 'test' }, symbol), TypeError, 'throws on symbol #2');
}
assert.throws(() => raw({}), TypeError);
assert.throws(() => raw({ raw: null }), TypeError);
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.repeat.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#repeat', assert => {
const { repeat } = String.prototype;
assert.isFunction(repeat);
assert.arity(repeat, 1);
assert.name(repeat, 'repeat');
assert.looksNative(repeat);
assert.nonEnumerable(String.prototype, 'repeat');
assert.same('qwe'.repeat(3), 'qweqweqwe');
assert.same('qwe'.repeat(2.5), 'qweqwe');
assert.throws(() => 'qwe'.repeat(-1), RangeError);
assert.throws(() => 'qwe'.repeat(Infinity), RangeError);
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => repeat.call(Symbol('repeat test')), 'throws on symbol context');
}
if (STRICT) {
assert.throws(() => repeat.call(null, 1), TypeError);
assert.throws(() => repeat.call(undefined, 1), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.replace-all.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#replaceAll', assert => {
const { replaceAll } = String.prototype;
assert.isFunction(replaceAll);
assert.arity(replaceAll, 2);
assert.name(replaceAll, 'replaceAll');
assert.looksNative(replaceAll);
assert.nonEnumerable(String.prototype, 'replaceAll');
assert.same('q=query+string+parameters'.replaceAll('+', ' '), 'q=query string parameters');
assert.same('foo'.replaceAll('o', {}), 'f[object Object][object Object]');
assert.same('[object Object]x[object Object]'.replaceAll({}, 'y'), 'yxy');
assert.same(replaceAll.call({}, 'bject', 'lolo'), '[ololo Ololo]');
assert.same('aba'.replaceAll('b', (search, i, string) => {
assert.same(search, 'b', '`search` is `b`');
assert.same(i, 1, '`i` is 1');
assert.same(string, 'aba', '`string` is `aba`');
return 'c';
}), 'aca');
const searcher = {
[Symbol.replace](O, replaceValue) {
assert.same(this, searcher, '`this` is `searcher`');
assert.same(String(O), 'aba', '`O` is `aba`');
assert.same(String(replaceValue), 'c', '`replaceValue` is `c`');
return 'foo';
},
};
assert.same('aba'.replaceAll(searcher, 'c'), 'foo');
assert.same('aba'.replaceAll('b'), 'aundefineda');
assert.same('xxx'.replaceAll('', '_'), '_x_x_x_');
assert.same('121314'.replaceAll('1', '$$'), '$2$3$4', '$$');
assert.same('121314'.replaceAll('1', '$&'), '121314', '$&');
assert.same('121314'.replaceAll('1', '$`'), '212312134', '$`');
assert.same('121314'.replaceAll('1', "$'"), '213142314344', "$'");
const symbol = Symbol('replaceAll test');
assert.throws(() => replaceAll.call(symbol, 'a', 'b'), 'throws on symbol context');
assert.throws(() => replaceAll.call('a', symbol, 'b'), 'throws on symbol argument 1');
assert.throws(() => replaceAll.call('a', 'b', symbol), 'throws on symbol argument 2');
if (STRICT) {
assert.throws(() => replaceAll.call(null, 'a', 'b'), TypeError);
assert.throws(() => replaceAll.call(undefined, 'a', 'b'), TypeError);
}
// eslint-disable-next-line regexp/no-missing-g-flag -- required for testing
assert.throws(() => 'b.b.b.b.b'.replaceAll(/\./, 'a'), TypeError);
// eslint-disable-next-line unicorn/prefer-string-replace-all -- required for testing
assert.same('b.b.b.b.b'.replaceAll(/\./g, 'a'), 'babababab');
const object = {};
assert.same('[object Object]'.replaceAll(object, 'a'), 'a');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.replace.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals, regexp/no-unused-capturing-group, sonarjs/slow-regex, unicorn/prefer-string-replace-all -- required for testing */
import { GLOBAL, NATIVE, STRICT } from '../helpers/constants.js';
import { patchRegExp$exec } from '../helpers/helpers.js';
const Symbol = GLOBAL.Symbol || {};
const run = assert => {
assert.isFunction(''.replace);
assert.arity(''.replace, 2);
assert.name(''.replace, 'replace');
assert.looksNative(''.replace);
assert.nonEnumerable(String.prototype, 'replace');
let instance = Object(true);
instance.replace = String.prototype.replace;
assert.same(instance.replace(true, 1), '1', 'S15.5.4.11_A1_T1');
instance = Object(false);
instance.replace = String.prototype.replace;
assert.same(instance.replace(false, undefined), 'undefined', 'S15.5.4.11_A1_T2');
assert.same('gnulluna'.replace(null, (a1, a2) => `${ a2 }`), 'g1una', 'S15.5.4.11_A1_T4');
assert.same('gnulluna'.replace(null, () => { /* empty */ }), 'gundefineduna', 'S15.5.4.11_A1_T5');
assert.same(Object('undefined').replace(undefined, (a1, a2) => a2 + 42), '42', 'S15.5.4.11_A1_T6');
assert.same('undefined'.replace('e', undefined), 'undundefinedfined', 'S15.5.4.11_A1_T7');
assert.same(String({
toString() { /* empty */ },
}).replace(/e/g, undefined), 'undundefinedfinundefinedd', 'S15.5.4.11_A1_T8');
assert.same(new String({
valueOf() { /* empty */ },
toString: undefined,
}).replace(function () { /* empty */ }(), (a1, a2, a3) => a1 + a2 + a3), 'undefined0undefined', 'S15.5.4.11_A1_T9');
assert.same('ABB\u0041BABAB'.replace({
toString() {
return '\u0041B';
},
}, () => { /* empty */ }), 'undefinedBABABAB', 'S15.5.4.11_A1_T10');
if (NATIVE) {
try {
'ABB\u0041BABAB'.replace({
toString() {
throw new Error('insearchValue');
},
}, {
toString() {
throw new Error('inreplaceValue');
},
});
assert.avoid('S15.5.4.11_A1_T11 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'insearchValue', 'S15.5.4.11_A1_T11 #2');
}
try {
Object('ABB\u0041BABAB').replace({
toString() {
return {};
},
valueOf() {
throw new Error('insearchValue');
},
}, {
toString() {
throw new Error('inreplaceValue');
},
});
assert.avoid('S15.5.4.11_A1_T12 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'insearchValue', 'S15.5.4.11_A1_T12 #2');
}
}
try {
'ABB\u0041BABAB\u0031BBAA'.replace({
toString() {
return {};
},
valueOf() {
throw new Error('insearchValue');
},
}, {
toString() {
return 1;
},
});
assert.avoid('S15.5.4.11_A1_T13 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'insearchValue', 'S15.5.4.11_A1_T13 #2');
}
assert.same('ABB\u0041BABAB\u0037\u0037BBAA'.replace(new RegExp('77'), 1), 'ABBABABAB\u0031BBAA', 'S15.5.4.11_A1_T14');
instance = Object(1100.00777001);
instance.replace = String.prototype.replace;
try {
instance.replace({
toString() {
return /77/;
},
}, 1);
assert.avoid('S15.5.4.11_A1_T15 #1 lead to throwing exception');
} catch (error) {
assert.true(error instanceof TypeError, 'S15.5.4.11_A1_T15 #2');
}
instance = Object(1100.00777001);
instance.replace = String.prototype.replace;
try {
instance.replace(/77/, {
toString() {
return (a1, a2) => `${ a2 }z`;
},
});
assert.avoid('S15.5.4.11_A1_T16 #1 lead to throwing exception');
} catch (error) {
assert.true(error instanceof TypeError, 'S15.5.4.11_A1_T16 #2');
}
assert.same('asdf'.replace(RegExp('', 'g'), '1'), '1a1s1d1f1', 'S15.5.4.11_A1_T17');
assert.same('She sells seashells by the seashore.'.replace(/sh/g, 'sch'), 'She sells seaschells by the seaschore.', 'S15.5.4.11_A2_T1');
assert.same('She sells seashells by the seashore.'.replace(/sh/g, '$$sch'), 'She sells sea$schells by the sea$schore.', 'S15.5.4.11_A2_T2');
assert.same('She sells seashells by the seashore.'.replace(/sh/g, '$&sch'), 'She sells seashschells by the seashschore.', 'S15.5.4.11_A2_T3');
assert.same('She sells seashells by the seashore.'.replace(/sh/g, '$`sch'), 'She sells seaShe sells seaschells by the seaShe sells seashells by the seaschore.', 'S15.5.4.11_A2_T4');
assert.same('She sells seashells by the seashore.'.replace(/sh/g, "$'sch"), 'She sells seaells by the seashore.schells by the seaore.schore.', 'S15.5.4.11_A2_T5');
assert.same('She sells seashells by the seashore.'.replace(/sh/, 'sch'), 'She sells seaschells by the seashore.', 'S15.5.4.11_A2_T6');
assert.same('She sells seashells by the seashore.'.replace(/sh/, '$$sch'), 'She sells sea$schells by the seashore.', 'S15.5.4.11_A2_T7');
assert.same('She sells seashells by the seashore.'.replace(/sh/, '$&sch'), 'She sells seashschells by the seashore.', 'S15.5.4.11_A2_T8');
assert.same('She sells seashells by the seashore.'.replace(/sh/, '$`sch'), 'She sells seaShe sells seaschells by the seashore.', 'S15.5.4.11_A2_T9');
assert.same('She sells seashells by the seashore.'.replace(/sh/, "$'sch"), 'She sells seaells by the seashore.schells by the seashore.', 'S15.5.4.11_A2_T10');
assert.same('uid=31'.replace(/(uid=)(\d+)/, '$1115'), 'uid=115', 'S15.5.4.11_A3_T1');
assert.same('uid=31'.replace(/(uid=)(\d+)/, '$11A15'), 'uid=1A15', 'S15.5.4.11_A3_T3');
assert.same('abc12 def34'.replace(/([a-z]+)(\d+)/, (a, b, c) => c + b), '12abc def34', 'S15.5.4.11_A4_T1');
// eslint-disable-next-line regexp/optimal-quantifier-concatenation -- required for testing
assert.same('aaaaaaaaaa,aaaaaaaaaaaaaaa'.replace(/^(a+)\1*,\1+$/, '$1'), 'aaaaa', 'S15.5.4.11_A5_T1');
// https://github.com/zloirock/core-js/issues/471
// eslint-disable-next-line regexp/no-useless-dollar-replacements, regexp/strict -- required for testing
assert.same('{price} Retail'.replace(/{price}/g, '$25.00'), '$25.00 Retail');
// eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
assert.same('a'.replace(/(.)/, '$0'), '$0');
assert.throws(() => ''.replace.call(Symbol('replace test'), /./, ''), 'throws on symbol context');
};
QUnit.test('String#replace regression', run);
QUnit.test('RegExp#@@replace appearance', assert => {
const replace = /./[Symbol.replace];
assert.isFunction(replace);
// assert.name(replace, '[Symbol.replace]');
assert.arity(replace, 2);
assert.looksNative(replace);
assert.nonEnumerable(RegExp.prototype, Symbol.replace);
});
QUnit.test('RegExp#@@replace basic behavior', assert => {
assert.same(/([a-z]+)(\d+)/[Symbol.replace]('abc12 def34', (a, b, c) => c + b), '12abc def34');
});
QUnit.test('String.replace delegates to @@replace', assert => {
const string = STRICT ? 'string' : Object('string');
const number = STRICT ? 42 : Object(42);
const object = {};
object[Symbol.replace] = function (a, b) {
return { a, b };
};
assert.same(string.replace(object, 42).a, string);
assert.same(string.replace(object, 42).b, 42);
assert.same(''.replace.call(number, object, 42).a, number);
assert.same(''.replace.call(number, object, 42).b, 42);
const regexp = /./;
regexp[Symbol.replace] = function (a, b) {
return { a, b };
};
assert.same(string.replace(regexp, 42).a, string);
assert.same(string.replace(regexp, 42).b, 42);
assert.same(''.replace.call(number, regexp, 42).a, number);
assert.same(''.replace.call(number, regexp, 42).b, 42);
});
QUnit.test('RegExp#@@replace delegates to exec', assert => {
const exec = function (...args) {
execCalled = true;
return /./.exec.apply(this, args);
};
let execCalled = false;
let re = /[ac]/;
re.exec = exec;
assert.deepEqual(re[Symbol.replace]('abc', 'f'), 'fbc');
assert.true(execCalled);
assert.same(re.lastIndex, 0);
execCalled = false;
re = /[ac]/g;
re.exec = exec;
assert.deepEqual(re[Symbol.replace]('abc', 'f'), 'fbf');
assert.true(execCalled);
assert.same(re.lastIndex, 0);
re = /a/;
// Not a function, should be ignored
re.exec = 3;
assert.deepEqual(re[Symbol.replace]('abc', 'f'), 'fbc');
re = /a/;
// Does not return an object, should throw
re.exec = () => 3;
assert.throws(() => re[Symbol.replace]('abc', 'f'));
});
QUnit.test('RegExp#@@replace correctly handles substitutions', assert => {
const re = /./;
re.exec = function () {
const result = ['23', '7'];
result.groups = { '!!!': '7' };
result.index = 1;
return result;
};
// eslint-disable-next-line regexp/no-useless-dollar-replacements -- false positive
assert.same('1234'.replace(re, '$1'), '174');
// eslint-disable-next-line regexp/no-useless-dollar-replacements -- required for testing
assert.same('1234'.replace(re, '$<!!!>'), '174');
assert.same('1234'.replace(re, '$`'), '114');
assert.same('1234'.replace(re, "$'"), '144');
assert.same('1234'.replace(re, '$$'), '1$4');
assert.same('1234'.replace(re, '$&'), '1234');
// eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
assert.same('1234'.replace(re, '$x'), '1$x4');
let args;
assert.same('1234'.replace(re, (...$args) => {
args = $args;
return 'x';
}), '1x4');
assert.deepEqual(args, ['23', '7', 1, '1234', { '!!!': '7' }]);
});
QUnit.test('RegExp#@@replace implementation', patchRegExp$exec(run));
QUnit.test('RegExp#@@replace global+unicode empty match at string end', assert => {
// eslint-disable-next-line regexp/no-empty-group -- testing
assert.same('abc'.replace(/(?:)/gu, '-'), '-a-b-c-', 'does not infinite loop on global+unicode empty match');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.search.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals -- required for testing */
import { GLOBAL, STRICT } from '../helpers/constants.js';
import { patchRegExp$exec } from '../helpers/helpers.js';
const Symbol = GLOBAL.Symbol || {};
const run = assert => {
assert.isFunction(''.search);
assert.arity(''.search, 1);
assert.name(''.search, 'search');
assert.looksNative(''.search);
assert.nonEnumerable(String.prototype, 'search');
let instance = Object(true);
instance.search = String.prototype.search;
assert.same(instance.search(true), 0, 'S15.5.4.12_A1_T1');
instance = Object(false);
instance.search = String.prototype.search;
assert.same(instance.search(false), 0, 'S15.5.4.12_A1_T2');
assert.same(''.search(), 0, 'S15.5.4.12_A1_T4 #1');
assert.same('--undefined--'.search(), 0, 'S15.5.4.12_A1_T4 #2');
assert.same('gnulluna'.search(null), 1, 'S15.5.4.12_A1_T5');
assert.same(Object('undefined').search(undefined), 0, 'S15.5.4.12_A1_T6');
assert.same('undefined'.search(undefined), 0, 'S15.5.4.12_A1_T7');
assert.same(String({
toString() { /* empty */ },
}).search(undefined), 0, 'S15.5.4.12_A1_T8');
assert.same('ssABB\u0041BABAB'.search({
toString() {
return '\u0041B';
},
}), 2, 'S15.5.4.12_A1_T10');
try {
'ABB\u0041BABAB'.search({
toString() {
throw new Error('intostr');
},
});
assert.avoid('S15.5.4.12_A1_T11 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intostr', 'S15.5.4.12_A1_T11 #2');
}
try {
Object('ABB\u0041BABAB').search({
toString() {
return {};
},
valueOf() {
throw new Error('intostr');
},
});
assert.avoid('S15.5.4.12_A1_T12 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intostr', 'S15.5.4.12_A1_T12 #2');
}
assert.same('ABB\u0041B\u0031ABAB\u0031BBAA'.search({
toString() {
return {};
},
valueOf() {
return 1;
},
}), 5, 'S15.5.4.12_A1_T13');
assert.same('ABB\u0041BABAB\u0037\u0037BBAA'.search(RegExp('77')), 9, 'S15.5.4.12_A1_T14');
assert.same(Object('test string').search('string'), 5, 'S15.5.4.12_A2_T1');
assert.same(Object('test string').search('String'), -1, 'S15.5.4.12_A2_T2');
assert.same(Object('test string').search(/string/i), 5, 'S15.5.4.12_A2_T3');
assert.same(Object('test string').search(/Four/), -1, 'S15.5.4.12_A2_T4');
assert.same(Object('one two three four five').search(/four/), 14, 'S15.5.4.12_A2_T5');
assert.same(Object('test string').search('nonexistent'), -1, 'S15.5.4.12_A2_T6');
assert.same(Object('test string probe').search('string pro'), 5, 'S15.5.4.12_A2_T7');
let string = Object('power of the power of the power of the power of the power of the power of the great sword');
assert.same(string.search(/the/), string.search(/the/g), 'S15.5.4.12_A3_T1');
string = Object('power \u006F\u0066 the power of the power \u006F\u0066 the power of the power \u006F\u0066 the power of the great sword');
assert.same(string.search(/of/), string.search(/of/g), 'S15.5.4.12_A3_T2');
assert.throws(() => ''.search.call(Symbol('search test'), /./), 'throws on symbol context');
};
QUnit.test('String#search regression', run);
QUnit.test('RegExp#@@search appearance', assert => {
const search = /./[Symbol.search];
assert.isFunction(search);
// assert.name(search, '[Symbol.search]');
assert.arity(search, 1);
assert.looksNative(search);
assert.nonEnumerable(RegExp.prototype, Symbol.search);
});
QUnit.test('RegExp#@@search basic behavior', assert => {
assert.same(/four/[Symbol.search]('one two three four five'), 14);
assert.same(/Four/[Symbol.search]('one two three four five'), -1);
});
QUnit.test('String#search delegates to @@search', assert => {
const string = STRICT ? 'string' : Object('string');
const number = STRICT ? 42 : Object(42);
const object = {};
object[Symbol.search] = function (it) {
return { value: it };
};
assert.same(string.search(object).value, string);
assert.same(''.search.call(number, object).value, number);
const regexp = /./;
regexp[Symbol.search] = function (it) {
return { value: it };
};
assert.same(string.search(regexp).value, string);
assert.same(''.search.call(number, regexp).value, number);
});
QUnit.test('RegExp#@@search delegates to exec', assert => {
let execCalled = false;
let re = /b/;
re.lastIndex = 7;
re.exec = function (...args) {
execCalled = true;
return /./.exec.apply(this, args);
};
assert.deepEqual(re[Symbol.search]('abc'), 1);
assert.true(execCalled);
assert.same(re.lastIndex, 7);
re = /b/;
// Not a function, should be ignored
re.exec = 3;
assert.deepEqual(re[Symbol.search]('abc'), 1);
re = /b/;
// Does not return an object, should throw
re.exec = () => 3;
assert.throws(() => re[Symbol.search]('abc'));
});
QUnit.test('RegExp#@@search implementation', patchRegExp$exec(run));
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.small.js
|
JavaScript
|
QUnit.test('String#small', assert => {
const { small } = String.prototype;
assert.isFunction(small);
assert.arity(small, 0);
assert.name(small, 'small');
assert.looksNative(small);
assert.nonEnumerable(String.prototype, 'small');
assert.same('a'.small(), '<small>a</small>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => small.call(Symbol('small test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.split.js
|
JavaScript
|
/* eslint-disable prefer-regex-literals -- required for testing */
/* eslint-disable regexp/no-empty-group, regexp/no-empty-capturing-group -- required for testing */
/* eslint-disable regexp/optimal-lookaround-quantifier, regexp/no-lazy-ends -- required for testing */
import { GLOBAL, NATIVE, STRICT } from '../helpers/constants.js';
import { patchRegExp$exec } from '../helpers/helpers.js';
const Symbol = GLOBAL.Symbol || {};
const run = assert => {
assert.isFunction(''.split);
assert.arity(''.split, 2);
assert.name(''.split, 'split');
assert.looksNative(''.split);
assert.nonEnumerable(String.prototype, 'split');
assert.arrayEqual('ab'.split(), ['ab'], 'If "separator" is undefined must return Array with one String - "this" string');
assert.arrayEqual('ab'.split(undefined), ['ab'], 'If "separator" is undefined must return Array with one String - "this" string');
assert.arrayEqual('ab'.split(undefined, 0), [], 'If "separator" is undefined and "limit" set to 0 must return Array[]');
assert.arrayEqual(''.split(), [''], "''.split() results in ['']");
assert.arrayEqual(''.split(/./), [''], "''.split(/./) results in ['']");
assert.arrayEqual(''.split(/.?/), [], "''.split(/.?/) results in []");
assert.arrayEqual(''.split(/.??/), [], "''.split(/.??/) results in []");
assert.arrayEqual('ab'.split(/a*/), ['', 'b'], "'ab'.split(/a*/) results in ['', 'b']");
assert.arrayEqual('ab'.split(/a*?/), ['a', 'b'], "'ab'.split(/a*?/) results in ['a', 'b']");
// eslint-disable-next-line regexp/no-useless-non-capturing-group -- required for testing
assert.arrayEqual('ab'.split(/(?:ab)/), ['', ''], "'ab'.split(/(?:ab)/) results in ['', '']");
assert.arrayEqual('ab'.split(/(?:ab)*/), ['', ''], "'ab'.split(/(?:ab)*/) results in ['', '']");
assert.arrayEqual('ab'.split(/(?:ab)*?/), ['a', 'b'], "'ab'.split(/(?:ab)*?/) results in ['a', 'b']");
assert.arrayEqual('test'.split(''), ['t', 'e', 's', 't'], "'test'.split('') results in ['t', 'e', 's', 't']");
assert.arrayEqual('test'.split(), ['test'], "'test'.split() results in ['test']");
assert.arrayEqual('111'.split(1), ['', '', '', ''], "'111'.split(1) results in ['', '', '', '']");
assert.arrayEqual('test'.split(/(?:)/, 2), ['t', 'e'], "'test'.split(/(?:)/, 2) results in ['t', 'e']");
assert.arrayEqual('test'.split(/(?:)/, -1), ['t', 'e', 's', 't'], "'test'.split(/(?:)/, -1) results in ['t', 'e', 's', 't']");
assert.arrayEqual('test'.split(/(?:)/, undefined), ['t', 'e', 's', 't'], "'test'.split(/(?:)/, undefined) results in ['t', 'e', 's', 't']");
assert.arrayEqual('test'.split(/(?:)/, null), [], "'test'.split(/(?:)/, null) results in []");
assert.arrayEqual('test'.split(/(?:)/, NaN), [], "'test'.split(/(?:)/, NaN) results in []");
assert.arrayEqual('test'.split(/(?:)/, true), ['t'], "'test'.split(/(?:)/, true) results in ['t']");
assert.arrayEqual('test'.split(/(?:)/, '2'), ['t', 'e'], "'test'.split(/(?:)/, '2') results in ['t', 'e']");
assert.arrayEqual('test'.split(/(?:)/, 'two'), [], "'test'.split(/(?:)/, 'two') results in []");
assert.arrayEqual('a'.split(/-/), ['a'], "'a'.split(/-/) results in ['a']");
assert.arrayEqual('a'.split(/-?/), ['a'], "'a'.split(/-?/) results in ['a']");
assert.arrayEqual('a'.split(/-??/), ['a'], "'a'.split(/-??/) results in ['a']");
assert.arrayEqual('a'.split(/a/), ['', ''], "'a'.split(/a/) results in ['', '']");
assert.arrayEqual('a'.split(/a?/), ['', ''], "'a'.split(/a?/) results in ['', '']");
assert.arrayEqual('a'.split(/a??/), ['a'], "'a'.split(/a??/) results in ['a']");
assert.arrayEqual('ab'.split(/-/), ['ab'], "'ab'.split(/-/) results in ['ab']");
assert.arrayEqual('ab'.split(/-?/), ['a', 'b'], "'ab'.split(/-?/) results in ['a', 'b']");
assert.arrayEqual('ab'.split(/-??/), ['a', 'b'], "'ab'.split(/-??/) results in ['a', 'b']");
assert.arrayEqual('a-b'.split(/-/), ['a', 'b'], "'a-b'.split(/-/) results in ['a', 'b']");
assert.arrayEqual('a-b'.split(/-?/), ['a', 'b'], "'a-b'.split(/-?/) results in ['a', 'b']");
assert.arrayEqual('a-b'.split(/-??/), ['a', '-', 'b'], "'a-b'.split(/-??/) results in ['a', '-', 'b']");
assert.arrayEqual('a--b'.split(/-/), ['a', '', 'b'], "'a--b'.split(/-/) results in ['a', '', 'b']");
assert.arrayEqual('a--b'.split(/-?/), ['a', '', 'b'], "'a--b'.split(/-?/) results in ['a', '', 'b']");
assert.arrayEqual('a--b'.split(/-??/), ['a', '-', '-', 'b'], "'a--b'.split(/-??/) results in ['a', '-', '-', 'b']");
assert.arrayEqual(''.split(/()()/), [], "''.split(/()()/) results in []");
assert.arrayEqual('.'.split(/()()/), ['.'], "'.'.split(/()()/) results in ['.']");
assert.arrayEqual('.'.split(/(.?)(.?)/), ['', '.', '', ''], "'.'.split(/(.?)(.?)/) results in ['', '.', '', '']");
assert.arrayEqual('.'.split(/(.??)(.??)/), ['.'], "'.'.split(/(.??)(.??)/) results in ['.']");
// eslint-disable-next-line regexp/optimal-quantifier-concatenation -- ignore
assert.arrayEqual('.'.split(/(.)?(.)?/), ['', '.', undefined, ''], "'.'.split(/(.)?(.)?/) results in ['', '.', undefined, '']");
assert.arrayEqual('A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\/)?([^<>]+)>/), ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', ''], "'A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\\/)?([^<>]+)>/) results in ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']");
assert.arrayEqual('tesst'.split(/(s)*/), ['t', undefined, 'e', 's', 't'], "'tesst'.split(/(s)*/) results in ['t', undefined, 'e', 's', 't']");
assert.arrayEqual('tesst'.split(/(s)*?/), ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't'], "'tesst'.split(/(s)*?/) results in ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']");
assert.arrayEqual('tesst'.split(/(s*)/), ['t', '', 'e', 'ss', 't'], "'tesst'.split(/(s*)/) results in ['t', '', 'e', 'ss', 't']");
assert.arrayEqual('tesst'.split(/(s*?)/), ['t', '', 'e', '', 's', '', 's', '', 't'], "'tesst'.split(/(s*?)/) results in ['t', '', 'e', '', 's', '', 's', '', 't']");
assert.arrayEqual('tesst'.split(/s*/), ['t', 'e', 't'], "'tesst'.split(/(?:s)*/) results in ['t', 'e', 't']");
assert.arrayEqual('tesst'.split(/(?=s+)/), ['te', 's', 'st'], "'tesst'.split(/(?=s+)/) results in ['te', 's', 'st']");
assert.arrayEqual('test'.split('t'), ['', 'es', ''], "'test'.split('t') results in ['', 'es', '']");
assert.arrayEqual('test'.split('es'), ['t', 't'], "'test'.split('es') results in ['t', 't']");
assert.arrayEqual('test'.split(/t/), ['', 'es', ''], "'test'.split(/t/) results in ['', 'es', '']");
assert.arrayEqual('test'.split(/es/), ['t', 't'], "'test'.split(/es/) results in ['t', 't']");
assert.arrayEqual('test'.split(/(t)/), ['', 't', 'es', 't', ''], "'test'.split(/(t)/) results in ['', 't', 'es', 't', '']");
assert.arrayEqual('test'.split(/(es)/), ['t', 'es', 't'], "'test'.split(/(es)/) results in ['t', 'es', 't']");
assert.arrayEqual('test'.split(/(t)(e)(s)(t)/), ['', 't', 'e', 's', 't', ''], "'test'.split(/(t)(e)(s)(t)/) results in ['', 't', 'e', 's', 't', '']");
assert.arrayEqual('.'.split(/(((.((.??)))))/), ['', '.', '.', '.', '', '', ''], "'.'.split(/(((.((.??)))))/) results in ['', '.', '.', '.', '', '', '']");
assert.arrayEqual('.'.split(/(((((.??)))))/), ['.'], "'.'.split(/(((((.??)))))/) results in ['.']");
assert.arrayEqual('a b c d'.split(/ /, -(2 ** 32) + 1), ['a'], "'a b c d'.split(/ /, -(2 ** 32) + 1) results in ['a']");
assert.arrayEqual('a b c d'.split(/ /, 2 ** 32 + 1), ['a'], "'a b c d'.split(/ /, 2 ** 32 + 1) results in ['a']");
assert.arrayEqual('a b c d'.split(/ /, Infinity), [], "'a b c d'.split(/ /, Infinity) results in []");
let instance = Object(true);
instance.split = String.prototype.split;
let split = instance.split(true, false);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T1 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T1 #2');
assert.same(split.length, 0, 'S15.5.4.14_A1_T1 #3');
instance = Object(false);
instance.split = String.prototype.split;
split = instance.split(false, 0, null);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T2 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T2 #2');
assert.same(split.length, 0, 'S15.5.4.14_A1_T2 #3');
split = ''.split();
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T4 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T4 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T4 #3');
assert.same(split[0], '', 'S15.5.4.14_A1_T4 #4');
split = 'gnulluna'.split(null);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T5 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T5 #2');
assert.same(split.length, 2, 'S15.5.4.14_A1_T5 #3');
assert.same(split[0], 'g', 'S15.5.4.14_A1_T5 #4');
assert.same(split[1], 'una', 'S15.5.4.14_A1_T5 #5');
if (NATIVE) {
split = Object('1undefined').split(undefined);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T6 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T6 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T6 #3');
assert.same(split[0], '1undefined', 'S15.5.4.14_A1_T6 #4');
split = 'undefinedd'.split(undefined);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T7 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T7 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T7 #3');
assert.same(split[0], 'undefinedd', 'S15.5.4.14_A1_T7 #4');
split = String({
toString() { /* empty */ },
}).split(undefined);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T8 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T8 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T8 #3');
assert.same(split[0], 'undefined', 'S15.5.4.14_A1_T8 #4');
}
split = new String({
valueOf() { /* empty */ },
toString: undefined,
}).split(() => { /* empty */ });
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T9 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T9 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T9 #3');
assert.same(split[0], 'undefined', 'S15.5.4.14_A1_T9 #4');
split = 'ABB\u0041BABAB'.split({
toString() {
return '\u0042B';
},
}, {
valueOf() {
return true;
},
});
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T10 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T10 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T10 #3');
assert.same(split[0], 'A', 'S15.5.4.14_A1_T10 #4');
try {
'ABB\u0041BABAB'.split({
toString() {
return '\u0041B';
},
}, {
valueOf() {
throw new Error('intointeger');
},
});
assert.avoid('S15.5.4.14_A1_T11 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intointeger', 'S15.5.4.14_A1_T11 #2');
}
if (NATIVE) {
try {
new String('ABB\u0041BABAB').split({
toString() {
return '\u0041B';
},
}, {
valueOf() {
return {};
},
toString() {
throw new Error('intointeger');
},
});
assert.avoid('S15.5.4.14_A1_T12 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intointeger', 'S15.5.4.14_A1_T12 #2');
}
}
split = 'ABB\u0041BABAB\u0042cc^^\u0042Bvv%%B\u0042xxx'.split({
toString() {
return '\u0042\u0042';
},
}, {
valueOf() {
return {};
},
toString() {
return '2';
},
});
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T13 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T13 #2');
assert.same(split.length, 2, 'S15.5.4.14_A1_T13 #3');
assert.same(split[0], 'A', 'S15.5.4.14_A1_T13 #4');
assert.same(split[1], 'ABABA', 'S15.5.4.14_A1_T13 #5');
if (NATIVE) {
try {
instance = Object(10001.10001);
instance.split = String.prototype.split;
instance.split({
toString() {
throw new Error('intostr');
},
}, {
valueOf() {
throw new Error('intoint');
},
});
assert.avoid('S15.5.4.14_A1_T14 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intoint', 'S15.5.4.14_A1_T14 #2');
}
try {
class F {
constructor(value) {
this.value = value;
}
valueOf() {
return `${ this.value }`;
}
toString() {
return new Number();
}
}
F.prototype.split = String.prototype.split;
new F().split({
toString() {
return {};
},
valueOf() {
throw new Error('intostr');
},
}, {
valueOf() {
throw new Error('intoint');
},
});
assert.avoid('S15.5.4.14_A1_T15 #1 lead to throwing exception');
} catch (error) {
assert.same(error.message, 'intoint', 'S15.5.4.14_A1_T15 #2');
}
}
try {
String.prototype.split.call(6776767677.006771, {
toString() {
return /77/g;
},
});
assert.avoid('S15.5.4.14_A1_T16 #1 lead to throwing exception');
} catch (error) {
assert.true(error instanceof TypeError, 'S15.5.4.14_A1_T16 #2');
}
split = String.prototype.split.call(6776767677.006771, /77/g);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T17 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T17 #2');
assert.same(split.length, 4, 'S15.5.4.14_A1_T17 #3');
assert.same(split[0], '6', 'S15.5.4.14_A1_T17 #4');
assert.same(split[1], '67676', 'S15.5.4.14_A1_T17 #5');
assert.same(split[2], '.006', 'S15.5.4.14_A1_T17 #6');
assert.same(split[3], '1', 'S15.5.4.14_A1_T17 #7');
split = String.prototype.split.call(6776767677.006771, /00/, 1);
assert.same(typeof split, 'object', 'S15.5.4.14_A1_T18 #1');
assert.same(split.constructor, Array, 'S15.5.4.14_A1_T18 #2');
assert.same(split.length, 1, 'S15.5.4.14_A1_T18 #3');
assert.same(split[0], '6776767677.', 'S15.5.4.14_A1_T18 #4');
split = Object('one,two,three,four,five').split(',');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T1 #1');
assert.same(split.length, 5, 'S15.5.4.14_A2_T1 #2');
assert.same(split[0], 'one', 'S15.5.4.14_A2_T1 #3');
assert.same(split[1], 'two', 'S15.5.4.14_A2_T1 #4');
assert.same(split[2], 'three', 'S15.5.4.14_A2_T1 #5');
assert.same(split[3], 'four', 'S15.5.4.14_A2_T1 #6');
assert.same(split[4], 'five', 'S15.5.4.14_A2_T1 #7');
split = Object('one two three four five').split(' ');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T2 #1');
assert.same(split.length, 5, 'S15.5.4.14_A2_T2 #2');
assert.same(split[0], 'one', 'S15.5.4.14_A2_T2 #3');
assert.same(split[1], 'two', 'S15.5.4.14_A2_T2 #4');
assert.same(split[2], 'three', 'S15.5.4.14_A2_T2 #5');
assert.same(split[3], 'four', 'S15.5.4.14_A2_T2 #6');
assert.same(split[4], 'five', 'S15.5.4.14_A2_T2 #7');
split = Object('one two three four five').split(RegExp(' '), 2);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T3 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T3 #2');
assert.same(split[0], 'one', 'S15.5.4.14_A2_T3 #3');
assert.same(split[1], 'two', 'S15.5.4.14_A2_T3 #4');
split = Object('one two three').split('');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T4 #1');
assert.same(split.length, 'one two three'.length, 'S15.5.4.14_A2_T4 #2');
assert.same(split[0], 'o', 'S15.5.4.14_A2_T4 #3');
assert.same(split[1], 'n', 'S15.5.4.14_A2_T4 #4');
assert.same(split[11], 'e', 'S15.5.4.14_A2_T4 #5');
assert.same(split[12], 'e', 'S15.5.4.14_A2_T4 #6');
split = Object('one-1,two-2,four-4').split(/,/);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T5 #1');
assert.same(split.length, 3, 'S15.5.4.14_A2_T5 #2');
assert.same(split[0], 'one-1', 'S15.5.4.14_A2_T5 #3');
assert.same(split[1], 'two-2', 'S15.5.4.14_A2_T5 #4');
assert.same(split[2], 'four-4', 'S15.5.4.14_A2_T5 #5');
let string = Object('one-1 two-2 three-3');
split = string.split('');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T6 #1');
assert.same(split.length, string.length, 'S15.5.4.14_A2_T6 #2');
for (let i = 0, { length } = split; i < length; ++i) {
assert.same(split[i], string.charAt(i), `S15.5.4.14_A2_T6 #${ i + 3 }`);
}
if (NATIVE) {
string = 'thisundefinedisundefinedaundefinedstringundefinedobject';
split = string.split(undefined);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T7 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T7 #2');
assert.same(split[0], string, 'S15.5.4.14_A2_T7 #3');
}
string = 'thisnullisnullanullstringnullobject';
let expected = ['this', 'is', 'a', 'string', 'object'];
split = string.split(null);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T8 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T8 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T8 #${ i + 3 }`);
}
string = 'thistrueistrueatruestringtrueobject';
expected = ['this', 'is', 'a', 'string', 'object'];
split = string.split(true);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T9 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T9 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T9 #${ i + 3 }`);
}
string = 'this123is123a123string123object';
expected = ['this', 'is', 'a', 'string', 'object'];
split = string.split(123);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T10 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T10 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T10 #${ i + 3 }`);
}
split = Object('one-1,two-2,four-4').split(':');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T11 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T11 #2');
assert.same(split[0], 'one-1,two-2,four-4', 'S15.5.4.14_A2_T11 #3');
split = Object('one-1 two-2 four-4').split('r-42');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T12 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T12 #2');
assert.same(split[0], 'one-1 two-2 four-4', 'S15.5.4.14_A2_T12 #3');
split = Object('one-1 two-2 four-4').split('-4');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T13 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T13 #2');
assert.same(split[0], 'one-1 two-2 four', 'S15.5.4.14_A2_T13 #3');
assert.same(split[1], '', 'S15.5.4.14_A2_T13 #4');
split = Object('one-1 two-2 four-4').split('on');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T14 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T14 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T14 #3');
assert.same(split[1], 'e-1 two-2 four-4', 'S15.5.4.14_A2_T14 #4');
split = new String().split('');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T15 #1');
assert.same(split.length, 0, 'S15.5.4.14_A2_T15 #2');
assert.same(split[0], undefined, 'S15.5.4.14_A2_T15 #3');
split = new String().split(' ');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T16 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T16 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T16 #3');
split = Object(' ').split('');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T18 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T18 #2');
assert.same(split[0], ' ', 'S15.5.4.14_A2_T18 #3');
split = Object(' ').split(' ');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T19 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T19 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T19 #3');
assert.same(split[1], '', 'S15.5.4.14_A2_T19 #4');
split = ''.split('x');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T19 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T19 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T19 #3');
string = Object('one-1 two-2 three-3');
split = string.split(new RegExp());
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T20 #1');
assert.same(split.length, string.length, 'S15.5.4.14_A2_T20 #2');
for (let i = 0, { length } = split; i < length; ++i) {
assert.same(split[i], string.charAt(i), `S15.5.4.14_A2_T20 #${ i + 3 }`);
}
split = Object('hello').split('ll');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T21 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T21 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A2_T21 #3');
assert.same(split[1], 'o', 'S15.5.4.14_A2_T21 #4');
split = Object('hello').split('l');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T22 #1');
assert.same(split.length, 3, 'S15.5.4.14_A2_T22 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A2_T22 #3');
assert.same(split[1], '', 'S15.5.4.14_A2_T22 #4');
assert.same(split[2], 'o', 'S15.5.4.14_A2_T22 #5');
split = Object('hello').split('x');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T23 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T23 #2');
assert.same(split[0], 'hello', 'S15.5.4.14_A2_T23 #3');
split = Object('hello').split('h');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T24 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T24 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T24 #3');
assert.same(split[1], 'ello', 'S15.5.4.14_A2_T24 #4');
split = Object('hello').split('o');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T25 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T25 #2');
assert.same(split[0], 'hell', 'S15.5.4.14_A2_T25 #3');
assert.same(split[1], '', 'S15.5.4.14_A2_T25 #4');
split = Object('hello').split('hello');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T26 #1');
assert.same(split.length, 2, 'S15.5.4.14_A2_T26 #2');
assert.same(split[0], '', 'S15.5.4.14_A2_T26 #3');
assert.same(split[1], '', 'S15.5.4.14_A2_T26 #4');
split = Object('hello').split(undefined);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T27 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T27 #2');
assert.same(split[0], 'hello', 'S15.5.4.14_A2_T27 #3');
split = Object('hello').split('hellothere');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T28 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T28 #2');
assert.same(split[0], 'hello', 'S15.5.4.14_A2_T28 #3');
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1);
expected = ['', '00', '', '', '', '22', '33', '44', '60'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T29 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T29 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T29 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 1);
expected = [''];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T30 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T30 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T30 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 2);
expected = ['', '00'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T31 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T31 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T31 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 0);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T32 #1');
assert.same(split.length, 0, 'S15.5.4.14_A2_T32 #2');
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 100);
expected = ['', '00', '', '', '', '22', '33', '44', '60'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T33 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T33 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T33 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, undefined);
expected = ['', '00', '', '', '', '22', '33', '44', '60'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T34 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T34 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T34 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 2 ** 32 - 1);
expected = ['', '00', '', '', '', '22', '33', '44', '60'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T35 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T35 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T35 #${ i + 3 }`);
}
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, 'boo');
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T36 #1');
assert.same(split.length, 0, 'S15.5.4.14_A2_T36 #2');
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, -(2 ** 32) + 1);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T37 #1');
assert.arrayEqual(split, [''], 'S15.5.4.14_A2_T37 #2');
instance = Object(100111122133144160);
instance.split = String.prototype.split;
split = instance.split(1, NaN);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T38 #1');
assert.same(split.length, 0, 'S15.5.4.14_A2_T38 #2');
split = Object('hello').split('l', 0);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T39 #1');
assert.same(split.length, 0, 'S15.5.4.14_A2_T39 #2');
split = Object('hello').split('l', 1);
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T40 #1');
assert.same(split.length, 1, 'S15.5.4.14_A2_T40 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A2_T40 #3');
split = Object('hello').split('l', 2);
expected = ['he', ''];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T41 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T41 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T41 #${ i + 3 }`);
}
split = Object('hello').split('l', 3);
expected = ['he', '', 'o'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T42 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T42 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T42 #${ i + 3 }`);
}
split = Object('hello').split('l', 4);
expected = ['he', '', 'o'];
assert.same(split.constructor, Array, 'S15.5.4.14_A2_T43 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A2_T43 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A2_T43 #${ i + 3 }`);
}
split = Object('one,two,three,four,five').split();
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T1 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T1 #2');
assert.same(split[0], 'one,two,three,four,five', 'S15.5.4.14_A3_T1 #3');
split = String.prototype.split.call({});
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T2 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T2 #2');
assert.same(split[0], '[object Object]', 'S15.5.4.14_A3_T2 #3');
split = String.prototype.split.call({
toString() {
return 'function(){}';
},
});
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T3 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T3 #2');
assert.same(split[0], 'function(){}', 'S15.5.4.14_A3_T3 #3');
split = String.prototype.split.call(Object(NaN));
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T4 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T4 #2');
assert.same(split[0], 'NaN', 'S15.5.4.14_A3_T4 #3');
split = String.prototype.split.call(Object(-1234567890));
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T5 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T5 #2');
assert.same(split[0], '-1234567890', 'S15.5.4.14_A3_T5 #3');
instance = Object(-1e21);
split = String.prototype.split.call(instance);
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T6 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T6 #2');
assert.same(split[0], instance.toString(), 'S15.5.4.14_A3_T6 #3');
split = String.prototype.split.call(Math);
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T7 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T7 #2');
assert.same(split[0], '[object Math]', 'S15.5.4.14_A3_T7 #3');
split = String.prototype.split.call([1, 2, 3, 4, 5]);
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T8 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T8 #2');
assert.same(split[0], '1,2,3,4,5', 'S15.5.4.14_A3_T8 #3');
split = String.prototype.split.call(Object(false));
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T9 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T9 #2');
assert.same(split[0], 'false', 'S15.5.4.14_A3_T9 #3');
split = String.prototype.split.call(new String());
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T10 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T10 #2');
assert.same(split[0], '', 'S15.5.4.14_A3_T10 #3');
split = String.prototype.split.call(Object(' '));
assert.same(split.constructor, Array, 'S15.5.4.14_A3_T11 #1');
assert.same(split.length, 1, 'S15.5.4.14_A3_T11 #2');
assert.same(split[0], ' ', 'S15.5.4.14_A3_T11 #3');
if (NATIVE) {
split = Object('hello').split(/l/);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T1 #1');
assert.same(split.length, 3, 'S15.5.4.14_A4_T1 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T1 #3');
assert.same(split[1], '', 'S15.5.4.14_A4_T1 #4');
assert.same(split[2], 'o', 'S15.5.4.14_A4_T1 #5');
}
split = Object('hello').split(/l/, 0);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T2 #1');
assert.same(split.length, 0, 'S15.5.4.14_A4_T2 #2');
split = Object('hello').split(/l/, 1);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T3 #1');
assert.same(split.length, 1, 'S15.5.4.14_A4_T3 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T3 #3');
if (NATIVE) {
split = Object('hello').split(/l/, 2);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T4 #1');
assert.same(split.length, 2, 'S15.5.4.14_A4_T4 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T4 #3');
assert.same(split[1], '', 'S15.5.4.14_A4_T4 #4');
split = Object('hello').split(/l/, 3);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T5 #1');
assert.same(split.length, 3, 'S15.5.4.14_A4_T5 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T5 #3');
assert.same(split[1], '', 'S15.5.4.14_A4_T5 #4');
assert.same(split[2], 'o', 'S15.5.4.14_A4_T5 #5');
split = Object('hello').split(/l/, 4);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T6 #1');
assert.same(split.length, 3, 'S15.5.4.14_A4_T6 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T6 #3');
assert.same(split[1], '', 'S15.5.4.14_A4_T6 #4');
assert.same(split[2], 'o', 'S15.5.4.14_A4_T6 #5');
split = Object('hello').split(/l/, undefined);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T7 #1');
assert.same(split.length, 3, 'S15.5.4.14_A4_T7 #2');
assert.same(split[0], 'he', 'S15.5.4.14_A4_T7 #3');
assert.same(split[1], '', 'S15.5.4.14_A4_T7 #4');
assert.same(split[2], 'o', 'S15.5.4.14_A4_T7 #5');
}
split = Object('hello').split(/l/, 'hi');
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T8 #1');
assert.same(split.length, 0, 'S15.5.4.14_A4_T8 #2');
split = Object('hello').split(new RegExp());
expected = ['h', 'e', 'l', 'l', 'o'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T10 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T10 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T10 #${ i + 3 }`);
}
split = Object('hello').split(new RegExp(), 0);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T11 #1');
assert.same(split.length, 0, 'S15.5.4.14_A4_T11 #2');
split = Object('hello').split(new RegExp(), 1);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T12 #1');
assert.same(split.length, 1, 'S15.5.4.14_A4_T12 #2');
assert.same(split[0], 'h', 'S15.5.4.14_A4_T12 #3');
split = Object('hello').split(new RegExp(), 2);
expected = ['h', 'e'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T13 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T13 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T13 #${ i + 3 }`);
}
split = Object('hello').split(new RegExp(), 3);
expected = ['h', 'e', 'l'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T14 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T14 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T14 #${ i + 3 }`);
}
split = Object('hello').split(new RegExp(), 4);
expected = ['h', 'e', 'l', 'l'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T15 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T15 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T15 #${ i + 3 }`);
}
split = Object('hello').split(new RegExp(), undefined);
expected = ['h', 'e', 'l', 'l', 'o'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T16 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T16 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T16 #${ i + 3 }`);
}
split = Object('hello').split(new RegExp(), 'hi');
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T18 #1');
assert.same(split.length, 0, 'S15.5.4.14_A4_T18 #2');
split = Object('a b c de f').split(/\s/);
expected = ['a', 'b', 'c', 'de', 'f'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T19 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T19 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T19 #${ i + 3 }`);
}
split = Object('a b c de f').split(/\s/, 3);
expected = ['a', 'b', 'c'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T20 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T20 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T20 #${ i + 3 }`);
}
split = Object('a b c de f').split(/X/);
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T21 #1');
assert.same(split.length, 1, 'S15.5.4.14_A4_T21 #2');
assert.same(split[0], 'a b c de f', 'S15.5.4.14_A4_T21 #3');
split = Object('dfe23iu 34 =+65--').split(/\d+/);
expected = ['dfe', 'iu ', ' =+', '--'];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T22 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T22 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T22 #${ i + 3 }`);
}
if (NATIVE) {
split = Object('abc').split(/[a-z]/);
expected = ['', '', '', ''];
assert.same(split.constructor, Array, 'S15.5.4.14_A4_T24 #1');
assert.same(split.length, expected.length, 'S15.5.4.14_A4_T24 #2');
for (let i = 0, { length } = expected; i < length; ++i) {
assert.same(expected[i], split[i], `S15.5.4.14_A4_T24 #${ i + 3 }`);
}
}
assert.throws(() => ''.split.call(Symbol('aplit test'), /./), 'throws on symbol context');
};
QUnit.test('String#split regression', run);
QUnit.test('RegExp#@@split appearance', assert => {
const split = /./[Symbol.split];
assert.isFunction(split);
// assert.name(split, '[Symbol.split]');
assert.arity(split, 2);
assert.looksNative(split);
assert.nonEnumerable(RegExp.prototype, Symbol.split);
});
QUnit.test('RegExp#@@split basic behavior', assert => {
assert.same(/\s/[Symbol.split]('a b c de f').length, 5);
assert.same(/\s/[Symbol.split]('a b c de f', undefined).length, 5);
assert.same(/\s/[Symbol.split]('a b c de f', 1).length, 1);
assert.same(/\s/[Symbol.split]('a b c de f', 10).length, 5);
});
QUnit.test('String#split delegates to @@split', assert => {
const string = STRICT ? 'string' : Object('string');
const number = STRICT ? 42 : Object(42);
const object = {};
object[Symbol.split] = function (a, b) {
return { a, b };
};
assert.same(string.split(object, 42).a, string);
assert.same(string.split(object, 42).b, 42);
assert.same(''.split.call(number, object, 42).a, number);
assert.same(''.split.call(number, object, 42).b, 42);
const regexp = /./;
regexp[Symbol.split] = function (a, b) {
return { a, b };
};
assert.same(string.split(regexp, 42).a, string);
assert.same(string.split(regexp, 42).b, 42);
assert.same(''.split.call(number, regexp, 42).a, number);
assert.same(''.split.call(number, regexp, 42).b, 42);
});
QUnit.test('RegExp#@@split delegates to exec', assert => {
let execCalled = false;
let speciesCalled = false;
let execSpeciesCalled = false;
const re = /[24]/;
re.exec = function (...args) {
execCalled = true;
return /./.exec.apply(this, args);
};
re.constructor = {
// eslint-disable-next-line object-shorthand -- constructor
[Symbol.species]: function (source, flags) {
const re2 = new RegExp(source, flags);
speciesCalled = true;
re2.exec = function (...args) {
execSpeciesCalled = true;
return /./.exec.apply(this, args);
};
return re2;
},
};
assert.deepEqual(re[Symbol.split]('123451234'), ['1', '3', '51', '3', '']);
assert.false(execCalled);
assert.true(speciesCalled);
assert.true(execSpeciesCalled);
re.constructor = {
// eslint-disable-next-line object-shorthand -- constructor
[Symbol.species]: function (source, flags) {
const re2 = new RegExp(source, flags);
// Not a function, should be ignored
re2.exec = 3;
return re2;
},
};
assert.deepEqual(re[Symbol.split]('123451234'), ['1', '3', '51', '3', '']);
re.constructor = {
// eslint-disable-next-line object-shorthand -- constructor
[Symbol.species]: function (source, flags) {
const re2 = new RegExp(source, flags);
// Does not return an object, should throw
re2.exec = () => 3;
return re2;
},
};
assert.throws(() => re[Symbol.split]('123451234'));
});
QUnit.test('RegExp#@@split implementation', patchRegExp$exec(run));
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.starts-with.js
|
JavaScript
|
import { GLOBAL, STRICT } from '../helpers/constants.js';
const Symbol = GLOBAL.Symbol || {};
QUnit.test('String#startsWith', assert => {
const { startsWith } = String.prototype;
assert.isFunction(startsWith);
assert.arity(startsWith, 1);
assert.name(startsWith, 'startsWith');
assert.looksNative(startsWith);
assert.nonEnumerable(String.prototype, 'startsWith');
assert.true('undefined'.startsWith());
assert.false('undefined'.startsWith(null));
assert.true('abc'.startsWith(''));
assert.true('abc'.startsWith('a'));
assert.true('abc'.startsWith('ab'));
assert.false('abc'.startsWith('bc'));
assert.true('abc'.startsWith('', NaN));
assert.true('abc'.startsWith('a', -1));
assert.false('abc'.startsWith('a', 1));
assert.false('abc'.startsWith('a', Infinity));
assert.true('abc'.startsWith('b', true));
assert.true('abc'.startsWith('a', 'x'));
if (typeof Symbol == 'function' && !Symbol.sham) {
const symbol = Symbol('startsWith test');
assert.throws(() => startsWith.call(symbol, 'b'), 'throws on symbol context');
assert.throws(() => startsWith.call('a', symbol), 'throws on symbol argument');
}
if (STRICT) {
assert.throws(() => startsWith.call(null, '.'), TypeError);
assert.throws(() => startsWith.call(undefined, '.'), TypeError);
}
const regexp = /./;
assert.throws(() => '/./'.startsWith(regexp), TypeError);
regexp[Symbol.match] = false;
assert.notThrows(() => '/./'.startsWith(regexp));
const object = {};
assert.notThrows(() => '[object Object]'.startsWith(object));
object[Symbol.match] = true;
assert.throws(() => '[object Object]'.startsWith(object), TypeError);
// side-effect ordering: ToString(searchString) should happen before ToIntegerOrInfinity(position)
const order = [];
'abc'.startsWith(
{ toString() { order.push('search'); return 'a'; } },
{ valueOf() { order.push('pos'); return 0; } },
);
assert.deepEqual(order, ['search', 'pos'], 'ToString(searchString) before ToIntegerOrInfinity(position)');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.strike.js
|
JavaScript
|
QUnit.test('String#strike', assert => {
const { strike } = String.prototype;
assert.isFunction(strike);
assert.arity(strike, 0);
assert.name(strike, 'strike');
assert.looksNative(strike);
assert.nonEnumerable(String.prototype, 'strike');
assert.same('a'.strike(), '<strike>a</strike>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => strike.call(Symbol('strike test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.sub.js
|
JavaScript
|
QUnit.test('String#sub', assert => {
const { sub } = String.prototype;
assert.isFunction(sub);
assert.arity(sub, 0);
assert.name(sub, 'sub');
assert.looksNative(sub);
assert.nonEnumerable(String.prototype, 'sub');
assert.same('a'.sub(), '<sub>a</sub>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => sub.call(Symbol('sub test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.substr.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#substr', assert => {
const { substr } = String.prototype;
assert.isFunction(substr);
assert.arity(substr, 2);
assert.name(substr, 'substr');
assert.looksNative(substr);
assert.nonEnumerable(String.prototype, 'substr');
assert.same('12345'.substr(1, 3), '234');
assert.same('ab'.substr(-1), 'b');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => substr.call(Symbol('substr test'), 1, 3), 'throws on symbol context');
}
if (STRICT) {
assert.throws(() => substr.call(null, 1, 3), TypeError, 'Throws on null as `this`');
assert.throws(() => substr.call(undefined, 1, 3), TypeError, 'Throws on undefined as `this`');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.sup.js
|
JavaScript
|
QUnit.test('String#sup', assert => {
const { sup } = String.prototype;
assert.isFunction(sup);
assert.arity(sup, 0);
assert.name(sup, 'sup');
assert.looksNative(sup);
assert.nonEnumerable(String.prototype, 'sup');
assert.same('a'.sup(), '<sup>a</sup>', 'lower case');
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => sup.call(Symbol('sup test')), 'throws on symbol context');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.to-well-formed.js
|
JavaScript
|
import { STRICT } from '../helpers/constants.js';
QUnit.test('String#toWellFormed', assert => {
const { toWellFormed } = String.prototype;
assert.isFunction(toWellFormed);
assert.arity(toWellFormed, 0);
assert.name(toWellFormed, 'toWellFormed');
assert.looksNative(toWellFormed);
assert.nonEnumerable(String.prototype, 'toWellFormed');
assert.same(toWellFormed.call('a'), 'a', 'a');
assert.same(toWellFormed.call('abc'), 'abc', 'abc');
assert.same(toWellFormed.call('💩'), '💩', '💩');
assert.same(toWellFormed.call('💩b'), '💩b', '💩b');
assert.same(toWellFormed.call('a💩'), 'a💩', '💩');
assert.same(toWellFormed.call('a💩b'), 'a💩b', 'a💩b');
assert.same(toWellFormed.call('💩a💩'), '💩a💩');
assert.same(toWellFormed.call('\uD83D'), '\uFFFD', '\uD83D');
assert.same(toWellFormed.call('\uDCA9'), '\uFFFD', '\uDCA9');
assert.same(toWellFormed.call('\uDCA9\uD83D'), '\uFFFD\uFFFD', '\uDCA9\uD83D');
assert.same(toWellFormed.call('a\uD83D'), 'a\uFFFD', 'a\uFFFD');
assert.same(toWellFormed.call('\uDCA9a'), '\uFFFDa', '\uDCA9a');
assert.same(toWellFormed.call('a\uD83Da'), 'a\uFFFDa', 'a\uD83Da');
assert.same(toWellFormed.call('a\uDCA9a'), 'a\uFFFDa', 'a\uDCA9a');
assert.same(toWellFormed.call({
toString() {
return 'abc';
},
}), 'abc', 'conversion #1');
assert.same(toWellFormed.call(1), '1', 'conversion #2');
if (STRICT) {
assert.throws(() => toWellFormed.call(null), TypeError, 'coercible #1');
assert.throws(() => toWellFormed.call(undefined), TypeError, 'coercible #2');
}
assert.throws(() => toWellFormed.call(Symbol('toWellFormed test')), 'throws on symbol context');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.trim-end.js
|
JavaScript
|
import { STRICT, WHITESPACES } from '../helpers/constants.js';
QUnit.test('String#trimEnd', assert => {
const { trimEnd, trimRight } = String.prototype;
assert.isFunction(trimEnd);
assert.arity(trimEnd, 0);
assert.name(trimEnd, 'trimEnd');
assert.looksNative(trimEnd);
assert.nonEnumerable(String.prototype, 'trimEnd');
assert.same(trimEnd, trimRight, 'same #trimRight');
assert.same(' \n q w e \n '.trimEnd(), ' \n q w e', 'removes whitespaces at right side of string');
assert.same(WHITESPACES.trimEnd(), '', 'removes all whitespaces');
assert.same('\u200B\u0085'.trimEnd(), '\u200B\u0085', "shouldn't remove this symbols");
assert.throws(() => trimEnd.call(Symbol('trimEnd test')), 'throws on symbol context');
if (STRICT) {
assert.throws(() => trimEnd.call(null, 0), TypeError);
assert.throws(() => trimEnd.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.trim-left.js
|
JavaScript
|
/* eslint-disable unicorn/prefer-string-trim-start-end -- required for testing */
import { STRICT, WHITESPACES } from '../helpers/constants.js';
QUnit.test('String#trimLeft', assert => {
const { trimLeft } = String.prototype;
assert.isFunction(trimLeft);
assert.arity(trimLeft, 0);
assert.name(trimLeft, 'trimStart');
assert.looksNative(trimLeft);
assert.nonEnumerable(String.prototype, 'trimLeft');
assert.same(' \n q w e \n '.trimLeft(), 'q w e \n ', 'removes whitespaces at left side of string');
assert.same(WHITESPACES.trimLeft(), '', 'removes all whitespaces');
assert.same('\u200B\u0085'.trimLeft(), '\u200B\u0085', "shouldn't remove this symbols");
assert.throws(() => trimLeft.call(Symbol('trimLeft test')), 'throws on symbol context');
if (STRICT) {
assert.throws(() => trimLeft.call(null, 0), TypeError);
assert.throws(() => trimLeft.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.trim-right.js
|
JavaScript
|
/* eslint-disable unicorn/prefer-string-trim-start-end -- required for testing */
import { STRICT, WHITESPACES } from '../helpers/constants.js';
QUnit.test('String#trimRight', assert => {
const { trimRight } = String.prototype;
assert.isFunction(trimRight);
assert.arity(trimRight, 0);
assert.name(trimRight, 'trimEnd');
assert.looksNative(trimRight);
assert.nonEnumerable(String.prototype, 'trimRight');
assert.same(' \n q w e \n '.trimRight(), ' \n q w e', 'removes whitespaces at right side of string');
assert.same(WHITESPACES.trimRight(), '', 'removes all whitespaces');
assert.same('\u200B\u0085'.trimRight(), '\u200B\u0085', "shouldn't remove this symbols");
assert.throws(() => trimRight.call(Symbol('trimRight test')), 'throws on symbol context');
if (STRICT) {
assert.throws(() => trimRight.call(null, 0), TypeError);
assert.throws(() => trimRight.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.trim-start.js
|
JavaScript
|
import { STRICT, WHITESPACES } from '../helpers/constants.js';
QUnit.test('String#trimStart', assert => {
const { trimStart, trimLeft } = String.prototype;
assert.isFunction(trimStart);
assert.arity(trimStart, 0);
assert.name(trimStart, 'trimStart');
assert.looksNative(trimStart);
assert.nonEnumerable(String.prototype, 'trimStart');
assert.same(trimStart, trimLeft, 'same #trimLeft');
assert.same(' \n q w e \n '.trimStart(), 'q w e \n ', 'removes whitespaces at left side of string');
assert.same(WHITESPACES.trimStart(), '', 'removes all whitespaces');
assert.same('\u200B\u0085'.trimStart(), '\u200B\u0085', "shouldn't remove this symbols");
assert.throws(() => trimStart.call(Symbol('trimStart test')), 'throws on symbol context');
if (STRICT) {
assert.throws(() => trimStart.call(null, 0), TypeError);
assert.throws(() => trimStart.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.string.trim.js
|
JavaScript
|
import { STRICT, WHITESPACES } from '../helpers/constants.js';
QUnit.test('String#trim', assert => {
const { trim } = String.prototype;
assert.isFunction(''.trim);
assert.arity(trim, 0);
assert.name(trim, 'trim');
assert.looksNative(trim);
assert.nonEnumerable(String.prototype, 'trim');
assert.same(' \n q w e \n '.trim(), 'q w e', 'removes whitespaces at left & right side of string');
assert.same(WHITESPACES.trim(), '', 'removes all whitespaces');
assert.same('\u200B\u0085'.trim(), '\u200B\u0085', "shouldn't remove this symbols");
if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => trim.call(Symbol('trim test')), 'throws on symbol context');
}
if (STRICT) {
assert.throws(() => trim.call(null, 0), TypeError);
assert.throws(() => trim.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.suppressed-error.constructor.js
|
JavaScript
|
/* eslint-disable unicorn/throw-new-error -- testing */
QUnit.test('SuppressedError', assert => {
assert.isFunction(SuppressedError);
assert.arity(SuppressedError, 3);
assert.name(SuppressedError, 'SuppressedError');
assert.looksNative(SuppressedError);
assert.true(new SuppressedError() instanceof SuppressedError);
assert.true(new SuppressedError() instanceof Error);
assert.true(SuppressedError() instanceof SuppressedError);
assert.true(SuppressedError() instanceof Error);
assert.same(SuppressedError().error, undefined);
assert.same(SuppressedError().suppressed, undefined);
assert.same(SuppressedError().message, '');
assert.same(SuppressedError().cause, undefined);
assert.false('cause' in SuppressedError());
assert.same(SuppressedError().name, 'SuppressedError');
assert.same(new SuppressedError().error, undefined);
assert.same(new SuppressedError().suppressed, undefined);
assert.same(new SuppressedError().message, '');
assert.same(new SuppressedError().cause, undefined);
assert.false('cause' in new SuppressedError());
assert.same(new SuppressedError().name, 'SuppressedError');
const error1 = SuppressedError(1, 2, 3, { cause: 4 });
assert.same(error1.error, 1);
assert.same(error1.suppressed, 2);
assert.same(error1.message, '3');
assert.same(error1.cause, undefined);
assert.false('cause' in error1);
assert.same(error1.name, 'SuppressedError');
const error2 = new SuppressedError(1, 2, 3, { cause: 4 });
assert.same(error2.error, 1);
assert.same(error2.suppressed, 2);
assert.same(error2.message, '3');
assert.same(error2.cause, undefined);
assert.false('cause' in error2);
assert.same(error2.name, 'SuppressedError');
assert.throws(() => SuppressedError(1, 2, Symbol('SuppressedError constructor test')), 'throws on symbol as a message');
assert.same({}.toString.call(SuppressedError()), '[object Error]', 'Object#toString');
assert.same(SuppressedError.prototype.constructor, SuppressedError, 'prototype constructor');
// eslint-disable-next-line no-prototype-builtins -- safe
assert.false(SuppressedError.prototype.hasOwnProperty('cause'), 'prototype has not cause');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.async-dispose.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Symbol.asyncDispose', assert => {
assert.true('asyncDispose' in Symbol, 'Symbol.asyncDispose available');
assert.true(Object(Symbol.asyncDispose) instanceof Symbol, 'Symbol.asyncDispose is symbol');
// Node 20.4.0 add `Symbol.asyncDispose`, but with incorrect descriptor
// https://github.com/nodejs/node/issues/48699
if (DESCRIPTORS) {
const descriptor = Object.getOwnPropertyDescriptor(Symbol, 'asyncDispose');
assert.false(descriptor.enumerable, 'non-enumerable');
assert.false(descriptor.writable, 'non-writable');
assert.false(descriptor.configurable, 'non-configurable');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.async-iterator.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Symbol.asyncIterator', assert => {
assert.true('asyncIterator' in Symbol, 'Symbol.asyncIterator available');
assert.nonEnumerable(Symbol, 'asyncIterator');
assert.true(Object(Symbol.asyncIterator) instanceof Symbol, 'Symbol.asyncIterator is symbol');
if (DESCRIPTORS) {
const descriptor = Object.getOwnPropertyDescriptor(Symbol, 'asyncIterator');
assert.false(descriptor.enumerable, 'non-enumerable');
assert.false(descriptor.writable, 'non-writable');
assert.false(descriptor.configurable, 'non-configurable');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.constructor.js
|
JavaScript
|
import { DESCRIPTORS, GLOBAL, NATIVE } from '../helpers/constants.js';
const {
defineProperty,
defineProperties,
getOwnPropertyDescriptor,
getOwnPropertyNames,
getOwnPropertySymbols,
keys,
create,
} = Object;
const { ownKeys } = GLOBAL.Reflect || {};
QUnit.test('Symbol', assert => {
assert.isFunction(Symbol);
if (NATIVE) assert.same(Symbol.length, 0, 'arity is 0');
assert.name(Symbol, 'Symbol');
assert.looksNative(Symbol);
const symbol1 = Symbol('symbol');
const symbol2 = Symbol('symbol');
assert.notSame(symbol1, symbol2, 'Symbol("symbol") !== Symbol("symbol")');
const object = {};
object[symbol1] = 42;
assert.same(object[symbol1], 42, 'Symbol() work as key');
assert.notSame(object[symbol2], 42, 'Various symbols from one description are various keys');
// assert.throws(() => Symbol(Symbol('foo')), 'throws on symbol argument');
if (DESCRIPTORS) {
let count = 0;
// eslint-disable-next-line no-unused-vars -- required for testing
for (const key in object) count++;
assert.same(count, 0, 'object[Symbol()] is not enumerable');
}
});
QUnit.test('Symbol as global key', assert => {
const TEXT = 'test global symbol key';
const symbol = Symbol(TEXT);
GLOBAL[symbol] = TEXT;
assert.same(GLOBAL[symbol], TEXT, TEXT);
});
QUnit.test('Well-known Symbols', assert => {
const wks = [
'hasInstance',
'isConcatSpreadable',
'iterator',
'match',
'matchAll',
'replace',
'search',
'species',
'split',
'toPrimitive',
'toStringTag',
'unscopables',
];
for (const name of wks) {
assert.true(name in Symbol, `Symbol.${ name } available`);
assert.true(Object(Symbol[name]) instanceof Symbol, `Symbol.${ name } is symbol`);
if (DESCRIPTORS) {
const descriptor = getOwnPropertyDescriptor(Symbol, name);
assert.false(descriptor.enumerable, 'non-enumerable');
assert.false(descriptor.writable, 'non-writable');
assert.false(descriptor.configurable, 'non-configurable');
}
}
});
QUnit.test('Symbol#@@toPrimitive', assert => {
const symbol = Symbol('Symbol#@@toPrimitive test');
assert.isFunction(Symbol.prototype[Symbol.toPrimitive]);
assert.same(symbol, symbol[Symbol.toPrimitive](), 'works');
});
QUnit.test('Symbol#@@toStringTag', assert => {
assert.same(Symbol.prototype[Symbol.toStringTag], 'Symbol', 'Symbol::@@toStringTag is `Symbol`');
});
if (DESCRIPTORS) {
QUnit.test('Symbols & descriptors', assert => {
const d = Symbol('d');
const e = Symbol('e');
const f = Symbol('f');
const i = Symbol('i');
const j = Symbol('j');
const prototype = { g: 'g' };
prototype[i] = 'i';
defineProperty(prototype, 'h', {
value: 'h',
});
defineProperty(prototype, 'j', {
value: 'j',
});
const object = create(prototype);
object.a = 'a';
object[d] = 'd';
defineProperty(object, 'b', {
value: 'b',
});
defineProperty(object, 'c', {
value: 'c',
enumerable: true,
});
defineProperty(object, e, {
configurable: true,
writable: true,
value: 'e',
});
const descriptor = {
value: 'f',
enumerable: true,
};
defineProperty(object, f, descriptor);
assert.true(descriptor.enumerable, 'defineProperty not changes descriptor object');
assert.deepEqual(getOwnPropertyDescriptor(object, 'a'), {
configurable: true,
writable: true,
enumerable: true,
value: 'a',
}, 'getOwnPropertyDescriptor a');
assert.deepEqual(getOwnPropertyDescriptor(object, 'b'), {
configurable: false,
writable: false,
enumerable: false,
value: 'b',
}, 'getOwnPropertyDescriptor b');
assert.deepEqual(getOwnPropertyDescriptor(object, 'c'), {
configurable: false,
writable: false,
enumerable: true,
value: 'c',
}, 'getOwnPropertyDescriptor c');
assert.deepEqual(getOwnPropertyDescriptor(object, d), {
configurable: true,
writable: true,
enumerable: true,
value: 'd',
}, 'getOwnPropertyDescriptor d');
assert.deepEqual(getOwnPropertyDescriptor(object, e), {
configurable: true,
writable: true,
enumerable: false,
value: 'e',
}, 'getOwnPropertyDescriptor e');
assert.deepEqual(getOwnPropertyDescriptor(object, f), {
configurable: false,
writable: false,
enumerable: true,
value: 'f',
}, 'getOwnPropertyDescriptor f');
assert.same(getOwnPropertyDescriptor(object, 'g'), undefined, 'getOwnPropertyDescriptor g');
assert.same(getOwnPropertyDescriptor(object, 'h'), undefined, 'getOwnPropertyDescriptor h');
assert.same(getOwnPropertyDescriptor(object, i), undefined, 'getOwnPropertyDescriptor i');
assert.same(getOwnPropertyDescriptor(object, j), undefined, 'getOwnPropertyDescriptor j');
assert.same(getOwnPropertyDescriptor(object, 'k'), undefined, 'getOwnPropertyDescriptor k');
assert.false(getOwnPropertyDescriptor(Object.prototype, 'toString').enumerable, 'getOwnPropertyDescriptor on Object.prototype');
assert.same(getOwnPropertyDescriptor(Object.prototype, d), undefined, 'getOwnPropertyDescriptor on Object.prototype missed symbol');
assert.same(keys(object).length, 2, 'Object.keys');
assert.same(getOwnPropertyNames(object).length, 3, 'Object.getOwnPropertyNames');
assert.same(getOwnPropertySymbols(object).length, 3, 'Object.getOwnPropertySymbols');
assert.same(ownKeys(object).length, 6, 'Reflect.ownKeys');
delete object[e];
object[e] = 'e';
assert.deepEqual(getOwnPropertyDescriptor(object, e), {
configurable: true,
writable: true,
enumerable: true,
value: 'e',
}, 'redefined non-enum key');
const g = Symbol('g');
defineProperty(object, g, { configurable: true, enumerable: true, writable: true, value: 1 });
defineProperty(object, g, { value: 2 });
assert.deepEqual(getOwnPropertyDescriptor(object, g), {
configurable: true,
writable: true,
enumerable: true,
value: 2,
}, 'redefine with partial descriptor preserves enumerable');
});
QUnit.test('Symbols & Object.defineProperties', assert => {
const c = Symbol('c');
const d = Symbol('d');
const descriptors = {
a: {
value: 'a',
},
};
descriptors[c] = {
value: 'c',
};
defineProperty(descriptors, 'b', {
value: {
value: 'b',
},
});
defineProperty(descriptors, d, {
value: {
value: 'd',
},
});
const object = defineProperties({}, descriptors);
assert.same(object.a, 'a', 'a');
assert.same(object.b, undefined, 'b');
assert.same(object[c], 'c', 'c');
assert.same(object[d], undefined, 'd');
});
QUnit.test('Symbols & Object.create', assert => {
const c = Symbol('c');
const d = Symbol('d');
const descriptors = {
a: {
value: 'a',
},
};
descriptors[c] = {
value: 'c',
};
defineProperty(descriptors, 'b', {
value: {
value: 'b',
},
});
defineProperty(descriptors, d, {
value: {
value: 'd',
},
});
const object = create(null, descriptors);
assert.same(object.a, 'a', 'a');
assert.same(object.b, undefined, 'b');
assert.same(object[c], 'c', 'c');
assert.same(object[d], undefined, 'd');
});
const constructors = ['Map', 'Set', 'Promise'];
for (const name of constructors) {
QUnit.test(`${ name }@@species`, assert => {
assert.same(GLOBAL[name][Symbol.species], GLOBAL[name], `${ name }@@species === ${ name }`);
const Subclass = create(GLOBAL[name]);
assert.same(Subclass[Symbol.species], Subclass, `${ name } subclass`);
});
}
QUnit.test('Array@@species', assert => {
assert.same(Array[Symbol.species], Array, 'Array@@species === Array');
const Subclass = create(Array);
assert.same(Subclass[Symbol.species], Subclass, 'Array subclass');
});
QUnit.test('Symbol.sham flag', assert => {
assert.same(Symbol.sham, typeof Symbol('Symbol.sham flag test') == 'symbol' ? undefined : true);
});
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.description.js
|
JavaScript
|
/* eslint-disable symbol-description -- required for testing */
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Symbol#description', assert => {
assert.same(Symbol('foo').description, 'foo');
assert.same(Symbol('').description, '');
assert.same(Symbol(')').description, ')');
assert.same(Symbol({}).description, '[object Object]');
assert.same(Symbol(null).description, 'null');
assert.same(Symbol(undefined).description, undefined);
assert.same(Symbol().description, undefined);
assert.same(Object(Symbol('foo')).description, 'foo');
assert.same(Object(Symbol()).description, undefined);
if (DESCRIPTORS) {
assert.false(Object.hasOwn(Symbol('foo'), 'description'));
const descriptor = Object.getOwnPropertyDescriptor(Symbol.prototype, 'description');
assert.false(descriptor.enumerable);
assert.true(descriptor.configurable);
assert.same(typeof descriptor.get, 'function');
}
if (typeof Symbol() == 'symbol') {
assert.same(Symbol('foo').toString(), 'Symbol(foo)');
assert.same(String(Symbol('foo')), 'Symbol(foo)');
assert.same(Symbol('').toString(), 'Symbol()');
assert.same(String(Symbol('')), 'Symbol()');
assert.same(Symbol().toString(), 'Symbol()');
assert.same(String(Symbol()), 'Symbol()');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.dispose.js
|
JavaScript
|
import { DESCRIPTORS } from '../helpers/constants.js';
QUnit.test('Symbol.dispose', assert => {
assert.true('dispose' in Symbol, 'Symbol.dispose available');
assert.true(Object(Symbol.dispose) instanceof Symbol, 'Symbol.dispose is symbol');
// Node 20.4.0 add `Symbol.dispose`, but with incorrect descriptor
// https://github.com/nodejs/node/issues/48699
if (DESCRIPTORS) {
const descriptor = Object.getOwnPropertyDescriptor(Symbol, 'dispose');
assert.false(descriptor.enumerable, 'non-enumerable');
assert.false(descriptor.writable, 'non-writable');
assert.false(descriptor.configurable, 'non-configurable');
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.for.js
|
JavaScript
|
import { NATIVE } from '../helpers/constants.js';
QUnit.test('Symbol.for', assert => {
assert.isFunction(Symbol.for, 'Symbol.for is function');
assert.nonEnumerable(Symbol, 'for');
assert.arity(Symbol.for, 1, 'Symbol.for arity is 1');
if (NATIVE) assert.name(Symbol.for, 'for', 'Symbol.for.name is "for"');
assert.looksNative(Symbol.for, 'Symbol.for looks like native');
const symbol = Symbol.for('foo');
assert.same(Symbol.for('foo'), symbol, 'registry');
assert.true(Object(symbol) instanceof Symbol, 'returns symbol');
assert.throws(() => Symbol.for(Symbol('foo')), 'throws on symbol argument');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.symbol.key-for.js
|
JavaScript
|
QUnit.test('Symbol.keyFor', assert => {
assert.isFunction(Symbol.keyFor, 'Symbol.keyFor is function');
assert.nonEnumerable(Symbol, 'keyFor');
assert.arity(Symbol.keyFor, 1, 'Symbol.keyFor arity is 1');
assert.name(Symbol.keyFor, 'keyFor', 'Symbol.keyFor.name is "keyFor"');
assert.looksNative(Symbol.keyFor, 'Symbol.keyFor looks like native');
assert.same(Symbol.keyFor(Symbol.for('foo')), 'foo');
assert.same(Symbol.keyFor(Symbol('foo')), undefined);
assert.throws(() => Symbol.keyFor('foo'), 'throws on non-symbol');
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.typed-array.at.js
|
JavaScript
|
import { DESCRIPTORS, TYPED_ARRAYS } from '../helpers/constants.js';
if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.indexOf', assert => {
// we can't implement %TypedArrayPrototype% in all engines, so run all tests for each typed array constructor
for (const { name, TypedArray } of TYPED_ARRAYS) {
const { at } = TypedArray.prototype;
assert.isFunction(at, `${ name }::at is function`);
assert.arity(at, 1, `${ name }::at arity is 1`);
assert.name(at, 'at', `${ name }::at name is 'at'`);
assert.looksNative(at, `${ name }::at looks native`);
assert.same(1, new TypedArray([1, 2, 3]).at(0));
assert.same(2, new TypedArray([1, 2, 3]).at(1));
assert.same(3, new TypedArray([1, 2, 3]).at(2));
assert.same(undefined, new TypedArray([1, 2, 3]).at(3));
assert.same(3, new TypedArray([1, 2, 3]).at(-1));
assert.same(2, new TypedArray([1, 2, 3]).at(-2));
assert.same(1, new TypedArray([1, 2, 3]).at(-3));
assert.same(undefined, new TypedArray([1, 2, 3]).at(-4));
assert.same(1, new TypedArray([1, 2, 3]).at(0.4));
assert.same(1, new TypedArray([1, 2, 3]).at(0.5));
assert.same(1, new TypedArray([1, 2, 3]).at(0.6));
assert.same(1, new TypedArray([1]).at(NaN));
assert.same(1, new TypedArray([1]).at());
assert.same(1, new TypedArray([1, 2, 3]).at(-0));
assert.throws(() => at.call({ 0: 1, length: 1 }, 0), TypeError);
assert.throws(() => at.call(null, 0), TypeError);
assert.throws(() => at.call(undefined, 0), TypeError);
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.typed-array.constructors.js
|
JavaScript
|
import { DESCRIPTORS, NATIVE, TYPED_ARRAYS } from '../helpers/constants.js';
import { createIterable } from '../helpers/helpers.js';
const { keys, getOwnPropertyDescriptor, getPrototypeOf, defineProperty, assign } = Object;
if (DESCRIPTORS) {
for (const { name, TypedArray, bytes } of TYPED_ARRAYS) {
QUnit.test(`${ name } constructor`, assert => {
assert.isFunction(TypedArray);
assert.arity(TypedArray, 3);
assert.name(TypedArray, name);
// Safari 5 bug
if (NATIVE) assert.looksNative(TypedArray);
assert.same(TypedArray.BYTES_PER_ELEMENT, bytes, `${ name }.BYTES_PER_ELEMENT`);
let array = new TypedArray(4);
assert.same(array.BYTES_PER_ELEMENT, bytes, '#BYTES_PER_ELEMENT');
assert.same(array.byteOffset, 0, `${ name }#byteOffset, passed number`);
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed number');
assert.arrayEqual(array, [0, 0, 0, 0], 'correct values, passed number');
assert.notThrows(() => {
// throws in IE / Edge / FF
array = new TypedArray('0x4');
assert.same(array.byteOffset, 0, '#byteOffset, passed string');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed string');
assert.arrayEqual(array, [0, 0, 0, 0], 'correct values, passed string');
return true;
}, 'passed string');
assert.notThrows(() => {
// throws in IE / Edge / FF
array = new TypedArray(true);
assert.same(array.byteOffset, 0, '#byteOffset, passed boolean');
assert.same(array.byteLength, 1 * bytes, '#byteLength, passed boolean');
assert.arrayEqual(array, [0], 'correct values, passed boolean');
return true;
}, 'passed boolean');
assert.notThrows(() => {
array = new TypedArray();
assert.same(array.byteOffset, 0, '#byteOffset, without arguments');
assert.same(array.byteLength, 0, '#byteLength, without arguments');
assert.arrayEqual(array, [], 'correct values, without arguments');
return true;
}, 'without arguments');
assert.notThrows(() => {
array = new TypedArray(undefined);
assert.same(array.byteOffset, 0, '#byteOffset, passed undefined');
assert.same(array.byteLength, 0, '#byteLength, passed undefined');
assert.arrayEqual(array, [], 'correct values, passed undefined');
return true;
}, 'passed undefined');
assert.notThrows(() => {
array = new TypedArray(-0);
assert.same(array.byteOffset, 0, '#byteOffset, passed -0');
assert.same(array.byteLength, 0, '#byteLength, passed -0');
assert.arrayEqual(array, [], 'correct values, passed -0');
return true;
}, 'passed -0');
assert.notThrows(() => {
array = new TypedArray(NaN);
assert.same(array.byteOffset, 0, '#byteOffset, passed NaN');
assert.same(array.byteLength, 0, '#byteLength, passed NaN');
assert.arrayEqual(array, [], 'correct values, passed NaN');
return true;
}, 'passed NaN');
assert.notThrows(() => {
array = new TypedArray(1.5);
assert.same(array.byteOffset, 0, '#byteOffset, passed 1.5');
assert.same(array.byteLength, 1 * bytes, '#byteLength, passed 1.5');
assert.arrayEqual(array, [0], 'correct values, passed 1.5');
return true;
}, 'passed 1.5');
if (NATIVE) assert.throws(() => new TypedArray(-1), RangeError, 'throws on -1');
assert.notThrows(() => {
array = new TypedArray(null);
assert.same(array.byteOffset, 0, '#byteOffset, passed null');
assert.same(array.byteLength, 0, '#byteLength, passed null');
assert.arrayEqual(array, [], 'correct values, passed null');
return true;
}, 'passed null');
array = new TypedArray([1, 2, 3, 4]);
assert.same(array.byteOffset, 0, '#byteOffset, passed array');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed array');
assert.arrayEqual(array, [1, 2, 3, 4], 'correct values, passed array');
array = new TypedArray({
0: 1,
1: 2,
2: 3,
3: 4,
length: 4,
});
assert.same(array.byteOffset, 0, '#byteOffset, passed array-like');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed array-like');
assert.arrayEqual(array, [1, 2, 3, 4], 'correct values, passed array-like');
assert.notThrows(() => {
// throws in IE / Edge
array = new TypedArray({});
assert.same(array.byteOffset, 0, '#byteOffset, passed empty object (also array-like case)');
assert.same(array.byteLength, 0, '#byteLength, passed empty object (also array-like case)');
assert.arrayEqual(array, [], 'correct values, passed empty object (also array-like case)');
return true;
}, 'passed empty object (also array-like case)');
assert.notThrows(() => {
array = new TypedArray(createIterable([1, 2, 3, 4]));
assert.same(array.byteOffset, 0, '#byteOffset, passed iterable');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed iterable');
assert.arrayEqual(array, [1, 2, 3, 4], 'correct values, passed iterable');
return true;
}, 'passed iterable');
assert.notThrows(() => {
array = new TypedArray([{ valueOf() { return 2; } }]);
assert.same(array.byteOffset, 0, '#byteOffset, passed array with object convertible to primitive');
assert.same(array.byteLength, bytes, '#byteLength, passed array with object convertible to primitive');
assert.arrayEqual(array, [2], 'correct values, passed array with object convertible to primitive');
return true;
}, 'passed array with object convertible to primitive');
assert.notThrows(() => {
array = new TypedArray(createIterable([{ valueOf() { return 2; } }]));
assert.same(array.byteOffset, 0, '#byteOffset, passed iterable with object convertible to primitive');
assert.same(array.byteLength, bytes, '#byteLength, passed iterable with object convertible to primitive');
assert.arrayEqual(array, [2], 'correct values, passed iterable with object convertible to primitive');
return true;
}, 'passed iterable with object convertible to primitive');
array = new TypedArray(new TypedArray([1, 2, 3, 4]));
assert.same(array.byteOffset, 0, '#byteOffset, passed typed array');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed typed array');
assert.arrayEqual(array, [1, 2, 3, 4], 'correct values, passed typed array');
const fake = new TypedArray([1, 2, 3, 4]);
fake[Symbol.iterator] = function () {
return createIterable([4, 3, 2, 1])[Symbol.iterator]();
};
array = new TypedArray(fake);
assert.same(array.byteOffset, 0, '#byteOffset, passed typed array with custom iterator');
assert.same(array.byteLength, 4 * bytes, '#byteLength, passed typed array with custom iterator');
// https://code.google.com/p/v8/issues/detail?id=4552
assert.arrayEqual(array, [1, 2, 3, 4], 'correct values, passed typed array with custom iterator');
array = new TypedArray(new ArrayBuffer(8));
assert.same(array.byteOffset, 0, '#byteOffset, passed buffer');
assert.same(array.byteLength, 8, '#byteLength, passed buffer');
assert.same(array.length, 8 / bytes, 'correct length, passed buffer');
array = new TypedArray(new ArrayBuffer(16), 8);
assert.same(array.byteOffset, 8, '#byteOffset, passed buffer and byteOffset');
assert.same(array.byteLength, 8, '#byteLength, passed buffer and byteOffset');
assert.same(array.length, 8 / bytes, 'correct length, passed buffer and byteOffset');
array = new TypedArray(new ArrayBuffer(24), 8, 8 / bytes);
assert.same(array.byteOffset, 8, '#byteOffset, passed buffer, byteOffset and length');
assert.same(array.byteLength, 8, '#byteLength, passed buffer, byteOffset and length');
assert.same(array.length, 8 / bytes, 'correct length, passed buffer, byteOffset and length');
array = new TypedArray(new ArrayBuffer(8), undefined);
assert.same(array.byteOffset, 0, '#byteOffset, passed buffer and undefined');
assert.same(array.byteLength, 8, '#byteLength, passed buffer and undefined');
assert.same(array.length, 8 / bytes, 'correct length, passed buffer and undefined');
array = new TypedArray(new ArrayBuffer(16), 8, undefined);
assert.same(array.byteOffset, 8, '#byteOffset, passed buffer, byteOffset and undefined');
assert.same(array.byteLength, 8, '#byteLength, passed buffer, byteOffset and undefined');
assert.same(array.length, 8 / bytes, 'correct length, passed buffer, byteOffset and undefined');
array = new TypedArray(new ArrayBuffer(8), 8);
assert.same(array.byteOffset, 8, '#byteOffset, passed buffer and byteOffset with buffer length');
assert.same(array.byteLength, 0, '#byteLength, passed buffer and byteOffset with buffer length');
assert.arrayEqual(array, [], 'correct values, passed buffer and byteOffset with buffer length');
// FF bug - TypeError instead of RangeError
assert.throws(() => new TypedArray(new ArrayBuffer(8), -1), RangeError, 'If offset < 0, throw a RangeError exception');
if (bytes !== 1) {
// FF bug - TypeError instead of RangeError
assert.throws(() => new TypedArray(new ArrayBuffer(8), 3), RangeError, 'If offset modulo elementSize ≠ 0, throw a RangeError exception');
}
if (NATIVE) {
if (bytes !== 1) {
// fails in Opera 12
assert.throws(() => new TypedArray(new ArrayBuffer(9)), RangeError, 'If bufferByteLength modulo elementSize ≠ 0, throw a RangeError exception');
}
assert.throws(() => new TypedArray(new ArrayBuffer(8), 16), RangeError, 'If newByteLength < 0, throw a RangeError exception');
assert.throws(() => new TypedArray(new ArrayBuffer(24), 8, 24), RangeError, 'If offset+newByteLength > bufferByteLength, throw a RangeError exception');
} else { // FF bug - TypeError instead of RangeError
assert.throws(() => new TypedArray(new ArrayBuffer(8), 16), 'If newByteLength < 0, throw a RangeError exception');
assert.throws(() => new TypedArray(new ArrayBuffer(24), 8, 24), 'If offset+newByteLength > bufferByteLength, throw a RangeError exception');
}
// eslint-disable-next-line sonarjs/inconsistent-function-call -- required for testing
assert.throws(() => TypedArray(1), TypeError, 'throws without `new`');
assert.same(TypedArray[Symbol.species], TypedArray, '@@species');
});
QUnit.test(`${ name } descriptors`, assert => {
const array = new TypedArray(2);
const descriptor = getOwnPropertyDescriptor(array, 0);
const base = NATIVE ? {
writable: true,
enumerable: true,
configurable: false,
} : {
writable: descriptor.writable,
enumerable: true,
configurable: descriptor.configurable,
};
assert.deepEqual(getOwnPropertyDescriptor(array, 0), assign({
value: 0,
}, base), 'Object.getOwnPropertyDescriptor');
if (NATIVE) {
// fails in old WebKit
assert.arrayEqual(keys(array), ['0', '1'], 'Object.keys');
const results = [];
for (const key in array) results.push(key);
// fails in old WebKit
assert.arrayEqual(results, ['0', '1'], 'for-in');
defineProperty(array, 0, {
value: 1,
writable: true,
enumerable: true,
configurable: false,
});
array[0] = array[1] = 2.5;
assert.deepEqual(getOwnPropertyDescriptor(array, 0), assign({
value: array[1],
}, base), 'Object.defineProperty, valid descriptor #1');
defineProperty(array, 0, {
value: 1,
});
array[0] = array[1] = 3.5;
assert.deepEqual(getOwnPropertyDescriptor(array, 0), assign({
value: array[1],
}, base), 'Object.defineProperty, valid descriptor #2');
assert.throws(() => defineProperty(array, 0, {
value: 2,
writable: false,
enumerable: true,
configurable: false,
}), 'Object.defineProperty, invalid descriptor #1');
assert.throws(() => defineProperty(array, 0, {
value: 2,
writable: true,
enumerable: false,
configurable: false,
}), 'Object.defineProperty, invalid descriptor #2');
assert.throws(() => defineProperty(array, 0, {
get() {
return 2;
},
}), 'Object.defineProperty, invalid descriptor #3');
}
assert.throws(() => defineProperty(array, 0, {
value: 2,
get() {
return 2;
},
}), 'Object.defineProperty, invalid descriptor #4');
});
QUnit.test(`${ name } @@toStringTag`, assert => {
const TypedArrayPrototype = getPrototypeOf(TypedArray.prototype);
const descriptor = getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag);
const getter = descriptor.get;
assert.isFunction(getter);
assert.same(getter.call(new Int8Array(1)), 'Int8Array');
assert.same(getter.call(new TypedArray(1)), name);
assert.same(getter.call([]), undefined);
assert.same(getter.call({}), undefined);
assert.same(getter.call(), undefined);
});
QUnit.test(`${ name }.sham`, assert => {
if (TypedArray.sham) assert.required(`${ name }.sham flag exists`);
else assert.required(`${ name }.sham flag missed`);
});
}
}
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.typed-array.copy-within.js
|
JavaScript
|
import { DESCRIPTORS, TYPED_ARRAYS } from '../helpers/constants.js';
if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.copyWithin', assert => {
// we can't implement %TypedArrayPrototype% in all engines, so run all tests for each typed array constructor
for (const { name, TypedArray } of TYPED_ARRAYS) {
const { copyWithin } = TypedArray.prototype;
assert.isFunction(copyWithin, `${ name }::copyWithin is function`);
assert.arity(copyWithin, 2, `${ name }::copyWithin arity is 2`);
assert.name(copyWithin, 'copyWithin', `${ name }::copyWithin name is 'copyWithin'`);
assert.looksNative(copyWithin, `${ name }::copyWithin looks native`);
const array = new TypedArray(5);
assert.same(array.copyWithin(0), array, 'return this');
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(0, 3), [4, 5, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(1, 3), [1, 4, 5, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(1, 2), [1, 3, 4, 5, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(2, 2), [1, 2, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(0, 3, 4), [4, 2, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(1, 3, 4), [1, 4, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(1, 2, 4), [1, 3, 4, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(0, -2), [4, 5, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(0, -2, -1), [4, 2, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(-4, -3, -2), [1, 3, 3, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(-4, -3, -1), [1, 3, 4, 4, 5]);
assert.arrayEqual(new TypedArray([1, 2, 3, 4, 5]).copyWithin(-4, -3), [1, 3, 4, 5, 5]);
assert.throws(() => copyWithin.call([0], 1), "isn't generic");
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
| |
tests/unit-global/es.typed-array.every.js
|
JavaScript
|
import { DESCRIPTORS, TYPED_ARRAYS } from '../helpers/constants.js';
if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.every', assert => {
// we can't implement %TypedArrayPrototype% in all engines, so run all tests for each typed array constructor
for (const { name, TypedArray } of TYPED_ARRAYS) {
const { every } = TypedArray.prototype;
assert.isFunction(every, `${ name }::every is function`);
assert.arity(every, 1, `${ name }::every arity is 1`);
assert.name(every, 'every', `${ name }::every name is 'every'`);
assert.looksNative(every, `${ name }::every looks native`);
const array = new TypedArray([1]);
const context = {};
array.every(function (value, key, that) {
assert.same(arguments.length, 3, 'correct number of callback arguments');
assert.same(value, 1, 'correct value in callback');
assert.same(key, 0, 'correct index in callback');
assert.same(that, array, 'correct link to array in callback');
assert.same(this, context, 'correct callback context');
}, context);
assert.true(new TypedArray([1, 2, 3]).every(it => typeof it == 'number'));
assert.true(new TypedArray([1, 2, 3]).every(it => it < 4));
assert.false(new TypedArray([1, 2, 3]).every(it => it < 3));
assert.false(new TypedArray([1, 2, 3]).every(it => typeof it == 'string'));
assert.true(new TypedArray([1, 2, 3]).every(function () {
return +this === 1;
}, 1));
let values = '';
let keys = '';
new TypedArray([1, 2, 3]).every((value, key) => {
values += value;
keys += key;
return true;
});
assert.same(values, '123');
assert.same(keys, '012');
assert.throws(() => every.call([0], () => { /* empty */ }), "isn't generic");
}
});
|
zloirock/core-js
| 25,418
|
Standard Library
|
JavaScript
|
zloirock
|
Denis Pushkarev
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.