Intl.RelativeTimeFormat: Add some tests for format, formatToParts, and resolvedOptions.

This commit is contained in:
Ms2ger 2018-07-27 12:32:03 +02:00 committed by Rick Waldron
parent 8e15f532e1
commit 604df708b3
8 changed files with 261 additions and 0 deletions

View File

@ -0,0 +1,40 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.format
description: Checks the handling of plural unit arguments to Intl.RelativeTimeFormat.prototype.format().
info: |
SingularRelativeTimeUnit ( unit )
2. If unit is "seconds", return "second".
3. If unit is "minutes", return "minute".
4. If unit is "hours", return "hour".
5. If unit is "days", return "day".
6. If unit is "weeks", return "week".
7. If unit is "months", return "month".
8. If unit is "quarters", return "quarter".
9. If unit is "years", return "year".
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.format, "function", "format should be supported");
const units = [
"second",
"minute",
"hour",
"day",
"week",
"month",
"quarter",
"year",
];
for (const unit of units) {
assert.sameValue(rtf.format(3, unit + "s"), rtf.format(3, unit),
`Should support unit ${unit}s as a synonym for ${unit}`)
}

View File

@ -0,0 +1,36 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.format
description: Checks the handling of invalid value arguments to Intl.RelativeTimeFormat.prototype.format().
info: |
Intl.RelativeTimeFormat.prototype.format( value, unit )
3. Let value be ? ToNumber(value).
PartitionRelativeTimePattern ( relativeTimeFormat, value, unit )
4. If isFinite(value) is false, then throw a RangeError exception.
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.format, "function", "format should be supported");
const values = [
[undefined, "undefined"],
[NaN, "NaN"],
[Infinity, "Infinity"],
[-Infinity, "-Infinity"],
["string", '"string"'],
[{}, "empty object"],
[{ toString() { return NaN; }, valueOf: undefined }, "object with toString"],
[{ valueOf() { return NaN; }, toString: undefined }, "object with valueOf"],
];
for (const [value, name] of values) {
assert.throws(RangeError, () => rtf.format(value, "second"), name);
}

View File

@ -0,0 +1,20 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.format
description: Checks the handling of invalid value arguments to Intl.RelativeTimeFormat.prototype.format().
info: |
Intl.RelativeTimeFormat.prototype.format( value, unit )
3. Let value be ? ToNumber(value).
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.format, "function", "format should be supported");
const symbol = Symbol();
assert.throws(TypeError, () => rtf.format(symbol, "second"));

View File

@ -0,0 +1,52 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.formatToParts
description: Checks the handling of plural unit arguments to Intl.RelativeTimeFormat.prototype.formatToParts().
info: |
SingularRelativeTimeUnit ( unit )
2. If unit is "seconds", return "second".
3. If unit is "minutes", return "minute".
4. If unit is "hours", return "hour".
5. If unit is "days", return "day".
6. If unit is "weeks", return "week".
7. If unit is "months", return "month".
8. If unit is "quarters", return "quarter".
9. If unit is "years", return "year".
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported");
const units = [
"second",
"minute",
"hour",
"day",
"week",
"month",
"quarter",
"year",
];
for (const unit of units) {
const plural = rtf.formatToParts(3, unit + "s");
const singular = rtf.formatToParts(3, unit);
assert.sameValue(plural.length, singular.length,
`Should support unit ${unit}s as a synonym for ${unit}: length`);
for (let i = 0; i < plural.length; ++i) {
assert.sameValue(plural[i].type, singular[i].type,
`Should support unit ${unit}s as a synonym for ${unit}: [${i}].type`);
assert.sameValue(plural[i].value, singular[i].value,
`Should support unit ${unit}s as a synonym for ${unit}: [${i}].value`);
assert.sameValue(plural[i].unit, singular[i].unit,
`Should support unit ${unit}s as a synonym for ${unit}: [${i}].unit`);
}
}

View File

@ -0,0 +1,36 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.formatToParts
description: Checks the handling of invalid value arguments to Intl.RelativeTimeFormat.prototype.formatToParts().
info: |
Intl.RelativeTimeFormat.prototype.formatToParts( value, unit )
3. Let value be ? ToNumber(value).
PartitionRelativeTimePattern ( relativeTimeFormat, value, unit )
4. If isFinite(value) is false, then throw a RangeError exception.
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported");
const values = [
[undefined, "undefined"],
[NaN, "NaN"],
[Infinity, "Infinity"],
[-Infinity, "-Infinity"],
["string", '"string"'],
[{}, "empty object"],
[{ toString() { return NaN; }, valueOf: undefined }, "object with toString"],
[{ valueOf() { return NaN; }, toString: undefined }, "object with valueOf"],
];
for (const [value, name] of values) {
assert.throws(RangeError, () => rtf.formatToParts(value, "second"), name);
}

View File

@ -0,0 +1,20 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.formatToParts
description: Checks the handling of invalid value arguments to Intl.RelativeTimeFormat.prototype.formatToParts().
info: |
Intl.RelativeTimeFormat.prototype.formatToParts( value, unit )
3. Let value be ? ToNumber(value).
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-US");
assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported");
const symbol = Symbol();
assert.throws(TypeError, () => rtf.formatToParts(symbol, "second"));

View File

@ -0,0 +1,17 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.resolvedOptions
description: Verifies that the return value of Intl.RelativeTimeFormat.prototype.resolvedOptions() is not cached.
info: |
Intl.RelativeTimeFormat.prototype.resolvedOptions ()
4. Let options be ! ObjectCreate(%ObjectPrototype%).
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-us");
const options1 = rtf.resolvedOptions();
const options2 = rtf.resolvedOptions();
assert.notSameValue(options1, options2, "Should create a new object each time.");

View File

@ -0,0 +1,40 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.RelativeTimeFormat.prototype.resolvedOptions
description: Checks the properties of the result of Intl.RelativeTimeFormat.prototype.resolvedOptions().
info: |
Intl.RelativeTimeFormat.prototype.resolvedOptions ()
4. Let options be ! ObjectCreate(%ObjectPrototype%).
5. For each row of Table 1, except the header row, do
d. Perform ! CreateDataPropertyOrThrow(options, p, v).
includes: [propertyHelper.js]
features: [Intl.RelativeTimeFormat]
---*/
const rtf = new Intl.RelativeTimeFormat("en-us", { "style": "short", "numeric": "auto" });
const options = rtf.resolvedOptions();
assert.sameValue(Object.getPrototypeOf(options), Object.prototype, "Prototype");
verifyProperty(options, "locale", {
value: "en-US",
writable: true,
enumerable: true,
configurable: true,
});
verifyProperty(options, "style", {
value: "short",
writable: true,
enumerable: true,
configurable: true,
});
verifyProperty(options, "numeric", {
value: "auto",
writable: true,
enumerable: true,
configurable: true,
});