mirror of https://github.com/tc39/test262.git
Initial tests for BigInt.prototype.toLocaleString(). (#2126)
Many of these tests are heavily based on the tests for Number.prototype.toLocaleString().
This commit is contained in:
parent
c03e14263e
commit
8e5ab69e8c
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2012-2019 Mozilla Corporation; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
Tests that BigInt.prototype.toLocaleString meets the requirements
|
||||
for built-in objects defined by the introduction of chapter 17 of
|
||||
the ECMAScript Language Specification.
|
||||
author: Norbert Lindenberg
|
||||
includes: [isConstructor.js]
|
||||
features: [Reflect.construct, BigInt]
|
||||
---*/
|
||||
|
||||
assert.sameValue(Object.prototype.toString.call(BigInt.prototype.toLocaleString), "[object Function]",
|
||||
"The [[Class]] internal property of a built-in function must be " +
|
||||
"\"Function\".");
|
||||
|
||||
assert(Object.isExtensible(BigInt.prototype.toLocaleString),
|
||||
"Built-in objects must be extensible.");
|
||||
|
||||
assert.sameValue(Object.getPrototypeOf(BigInt.prototype.toLocaleString), Function.prototype);
|
||||
|
||||
assert.sameValue(BigInt.prototype.toLocaleString.hasOwnProperty("prototype"), false,
|
||||
"Built-in functions that aren't constructors must not have a prototype property.");
|
||||
|
||||
assert.sameValue(isConstructor(BigInt.prototype.toLocaleString), false,
|
||||
"Built-in functions don't implement [[Construct]] unless explicitly specified.");
|
19
test/intl402/BigInt/prototype/toLocaleString/default-options-object-prototype.js
vendored
Normal file
19
test/intl402/BigInt/prototype/toLocaleString/default-options-object-prototype.js
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright (C) 2017-2019 Daniel Ehrenberg; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-initializenumberformat
|
||||
description: >
|
||||
Monkey-patching Object.prototype does not change the default
|
||||
options for NumberFormat as a null prototype is used.
|
||||
info: |
|
||||
InitializeNumberFormat ( numberFormat, locales, options )
|
||||
|
||||
1. If _options_ is *undefined*, then
|
||||
1. Let _options_ be ObjectCreate(*null*).
|
||||
locale: [en-US]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
Object.prototype.useGrouping = false;
|
||||
assert.sameValue(12345n.toLocaleString("en-US"), "12,345");
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright (C) 2019 Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: Checks basic behavior for BigInt.prototype.toLocaleString.
|
||||
locale: [en-US]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
const tests = [
|
||||
[0n, undefined, "0"],
|
||||
[-0n, undefined, "0"],
|
||||
[88776655n, { "maximumSignificantDigits": 4 }, "88,780,000"],
|
||||
[88776655n, { "maximumSignificantDigits": 4, "style": "percent" }, "8,878,000,000%"],
|
||||
[88776655n, { "minimumFractionDigits": 3 }, "88,776,655.000"],
|
||||
];
|
||||
|
||||
for (const [bigint, options, expected] of tests) {
|
||||
const result = bigint.toLocaleString("en-US", options);
|
||||
assert.sameValue(result, expected);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright (C) 2017-2019 André Bargull; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
BigInt.prototype.toLocaleString.length is 0.
|
||||
info: |
|
||||
BigInt.prototype.toLocaleString ( [ locales [ , options ] ] )
|
||||
|
||||
17 ECMAScript Standard Built-in Objects:
|
||||
|
||||
Every built-in function object, including constructors, has a length
|
||||
property whose value is an integer. Unless otherwise specified, this
|
||||
value is equal to the largest number of named arguments shown in the
|
||||
subclause headings for the function description. Optional parameters
|
||||
(which are indicated with brackets: [ ]) or rest parameters (which
|
||||
are shown using the form «...name») are not included in the default
|
||||
argument count.
|
||||
Unless otherwise specified, the length property of a built-in function
|
||||
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
|
||||
[[Configurable]]: true }.
|
||||
|
||||
includes: [propertyHelper.js]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
verifyProperty(BigInt.prototype.toLocaleString, "length", {
|
||||
value: 0,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
});
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright (C) 2019 Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
BigInt.prototype.toLocaleString.name is toLocaleString.
|
||||
info: |
|
||||
BigInt.prototype.toLocaleString ( [ locales [ , options ] ] )
|
||||
|
||||
17 ECMAScript Standard Built-in Objects:
|
||||
|
||||
Every built-in function object, including constructors, that is not
|
||||
identified as an anonymous function has a name property whose value
|
||||
is a String. For functions that are specified as properties of objects,
|
||||
the name value is the property name string used to access the function.
|
||||
|
||||
Unless otherwise specified, the name property of a built-in function
|
||||
object, if it exists, has the attributes { [[Writable]]: false,
|
||||
[[Enumerable]]: false, [[Configurable]]: true }.
|
||||
|
||||
includes: [propertyHelper.js]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
verifyProperty(BigInt.prototype.toLocaleString, "name", {
|
||||
value: "toLocaleString",
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
});
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright (C) 2019 Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: Checks the "toLocaleString" property of the BigInt prototype object.
|
||||
info: |
|
||||
BigInt.prototype.toLocaleString ( [ locales [ , options ] ] )
|
||||
|
||||
17 ECMAScript Standard Built-in Objects:
|
||||
|
||||
Every other data property described in clauses 18 through 26 and in
|
||||
Annex B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
|
||||
[[Configurable]]: true } unless otherwise specified.
|
||||
|
||||
includes: [propertyHelper.js]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
typeof BigInt.prototype.toLocaleString,
|
||||
"function",
|
||||
"typeof BigInt.prototype.toLocaleString is function"
|
||||
);
|
||||
|
||||
verifyProperty(BigInt.prototype, "toLocaleString", {
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
});
|
46
test/intl402/BigInt/prototype/toLocaleString/returns-same-results-as-NumberFormat.js
vendored
Normal file
46
test/intl402/BigInt/prototype/toLocaleString/returns-same-results-as-NumberFormat.js
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2012-2019 Mozilla Corporation; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
Tests that BigInt.prototype.toLocaleString produces the same
|
||||
results as Intl.BigIntFormat.
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
var inputs = [
|
||||
0n,
|
||||
-0n,
|
||||
1n,
|
||||
-1n,
|
||||
123n,
|
||||
-123n,
|
||||
12345n,
|
||||
-12345n,
|
||||
12344501000000000000000000000000000n,
|
||||
-12344501000000000000000000000000000n,
|
||||
];
|
||||
var localesInputs = [undefined, ["de"], ["th-u-nu-thai"], ["en"], ["ja-u-nu-jpanfin"], ["ar-u-nu-arab"]];
|
||||
var optionsInputs = [
|
||||
undefined,
|
||||
{style: "percent"},
|
||||
{style: "currency", currency: "EUR", currencyDisplay: "symbol"},
|
||||
{style: "currency", currency: "IQD", currencyDisplay: "symbol"},
|
||||
{style: "currency", currency: "KMF", currencyDisplay: "symbol"},
|
||||
{style: "currency", currency: "CLF", currencyDisplay: "symbol"},
|
||||
{useGrouping: false, minimumIntegerDigits: 3, minimumFractionDigits: 1, maximumFractionDigits: 3}
|
||||
];
|
||||
|
||||
for (const locales of localesInputs) {
|
||||
for (const options of optionsInputs) {
|
||||
const optionsString = options ? JSON.stringify(options) : String(options);
|
||||
const referenceNumberFormat = new Intl.NumberFormat(locales, options);
|
||||
for (const input of inputs) {
|
||||
const referenceFormatted = referenceNumberFormat.format(input);
|
||||
const formatted = input.toLocaleString(locales, options);
|
||||
assert.sameValue(formatted, referenceFormatted,
|
||||
`(Testing with locales ${locales}; options ${optionsString}.)`);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2013-2019 Mozilla Corporation; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
Tests that Number.prototype.toLocaleString uses the standard
|
||||
built-in Intl.NumberFormat constructor.
|
||||
includes: [testIntl.js]
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
taintDataProperty(Intl, "NumberFormat");
|
||||
0n.toLocaleString();
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2012-2019 Mozilla Corporation; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: Tests that toLocaleString handles "thisBigIntValue" correctly.
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
var invalidValues = [
|
||||
undefined,
|
||||
null,
|
||||
false,
|
||||
"5",
|
||||
Symbol(),
|
||||
5,
|
||||
-1234567.89,
|
||||
NaN,
|
||||
-Infinity,
|
||||
{valueOf: function () { return 5; }},
|
||||
{valueOf: function () { return 5n; }},
|
||||
];
|
||||
|
||||
for (const value of invalidValues) {
|
||||
assert.throws(TypeError, function() {
|
||||
BigInt.prototype.toLocaleString.call(value);
|
||||
}, `BigInt.prototype.toLocaleString did not throw with this = ${String(value)}.`);
|
||||
}
|
47
test/intl402/BigInt/prototype/toLocaleString/throws-same-exceptions-as-NumberFormat.js
vendored
Normal file
47
test/intl402/BigInt/prototype/toLocaleString/throws-same-exceptions-as-NumberFormat.js
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2012-2019 Mozilla Corporation; Igalia, S.L. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
esid: sec-bigint.prototype.tolocalestring
|
||||
description: >
|
||||
Tests that BigInt.prototype.toLocaleString throws the same
|
||||
exceptions as Intl.NumberFormat.
|
||||
features: [BigInt]
|
||||
---*/
|
||||
|
||||
var localesInputs = [null, [NaN], ["i"], ["de_DE"]];
|
||||
var optionsInputs = [
|
||||
{localeMatcher: null},
|
||||
{style: "invalid"},
|
||||
{style: "currency"},
|
||||
{style: "currency", currency: "ßP"},
|
||||
{maximumSignificantDigits: -Infinity}
|
||||
];
|
||||
|
||||
for (const locales of localesInputs) {
|
||||
var referenceError, error;
|
||||
try {
|
||||
var format = new Intl.NumberFormat(locales);
|
||||
} catch (e) {
|
||||
referenceError = e;
|
||||
}
|
||||
assert.notSameValue(referenceError, undefined, "Internal error: Expected exception was not thrown by Intl.NumberFormat for locales " + locales + ".");
|
||||
|
||||
assert.throws(referenceError.constructor, function() {
|
||||
var result = 0n.toLocaleString(locales);
|
||||
}, "BigInt.prototype.toLocaleString didn't throw exception for locales " + locales + ".");
|
||||
}
|
||||
|
||||
for (const options of optionsInputs) {
|
||||
var referenceError, error;
|
||||
try {
|
||||
var format = new Intl.NumberFormat([], options);
|
||||
} catch (e) {
|
||||
referenceError = e;
|
||||
}
|
||||
assert.notSameValue(referenceError, undefined, "Internal error: Expected exception was not thrown by Intl.NumberFormat for options " + JSON.stringify(options) + ".");
|
||||
|
||||
assert.throws(referenceError.constructor, function() {
|
||||
var result = 0n.toLocaleString([], options);
|
||||
}, "BigInt.prototype.toLocaleString didn't throw exception for options " + JSON.stringify(options) + ".");
|
||||
}
|
Loading…
Reference in New Issue