Skip to content
Snippets Groups Projects
Commit 604df708 authored by Ms2ger's avatar Ms2ger Committed by Rick Waldron
Browse files

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

parent 8e15f532
No related branches found
No related tags found
No related merge requests found
// 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}`)
}
// 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);
}
// 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"));
// 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`);
}
}
// 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);
}
// 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"));
// 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.");
// 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,
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment