Add tests for Intl.DisplayNames#resolvedOptions (#2405)

This commit is contained in:
Leo Balter 2019-11-13 10:50:18 -05:00 committed by GitHub
parent 8f8b3374c2
commit aa9ba4b58f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 598 additions and 0 deletions

View File

@ -0,0 +1,80 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Default values for each option
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
3. Let options be ! ObjectCreate(%ObjectPrototype%).
4. For each row of Table 6, except the header row, in table order, do
a. Let p be the Property value of the current row.
b. Let v be the value of pr's internal slot whose name is the Internal Slot value of the current row.
c. If v is not undefined, then
i. Perform ! CreateDataPropertyOrThrow(options, p, v).
6. Return options.
Table 6: Resolved Options of DisplayNames Instances
[[Locale]]: "locale"
[[Style]]: "style"
[[Type]]: "type"
[[Fallback]]: "fallback"
Intl.DisplayNames ([ locales [ , options ]])
...
8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
...
10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt,
%DisplayNames%.[[RelevantExtensionKeys]]).
11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
...
13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency",
"weekday", "month", "quarter", "dayPeriod", "dateTimeField" », "language").
...
15. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
...
17. Set displayNames.[[Locale]] to the value of r.[[Locale]].
...
CreateDataProperty ( O, P, V )
...
3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true }.
...
locale: [en-US]
features: [Intl.DisplayNames]
includes: [propertyHelper.js]
---*/
var dn = new Intl.DisplayNames('en-US');
var options = dn.resolvedOptions();
verifyProperty(options, 'style', {
value: 'long',
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'type', {
value: 'language',
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'fallback', {
value: 'code',
writable: true,
enumerable: true,
configurable: true
});

View File

@ -0,0 +1,31 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Intl.DisplayNames.prototype.resolvedOptions.length is 0.
info: |
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: [Intl.DisplayNames]
---*/
verifyProperty(Intl.DisplayNames.prototype.resolvedOptions, "length", {
value: 0,
enumerable: false,
writable: false,
configurable: true,
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Intl.DisplayNames.prototype.resolvedOptions.name is "resolvedOptions".
info: |
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.
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: [Intl.DisplayNames]
---*/
verifyProperty(Intl.DisplayNames.prototype.resolvedOptions, "name", {
value: "resolvedOptions",
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,83 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Values for the fallback option
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
3. Let options be ! ObjectCreate(%ObjectPrototype%).
4. For each row of Table 6, except the header row, in table order, do
a. Let p be the Property value of the current row.
b. Let v be the value of pr's internal slot whose name is the Internal Slot value of the current row.
c. If v is not undefined, then
i. Perform ! CreateDataPropertyOrThrow(options, p, v).
6. Return options.
Table 6: Resolved Options of DisplayNames Instances
[[Locale]]: "locale"
[[Style]]: "style"
[[Type]]: "type"
[[Fallback]]: "fallback"
Intl.DisplayNames ([ locales [ , options ]])
...
8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
...
10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt,
%DisplayNames%.[[RelevantExtensionKeys]]).
11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
...
13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency",
"weekday", "month", "quarter", "dayPeriod", "dateTimeField" », "language").
...
15. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
...
17. Set displayNames.[[Locale]] to the value of r.[[Locale]].
...
CreateDataProperty ( O, P, V )
...
3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true }.
...
locale: [en-US]
features: [Intl.DisplayNames]
includes: [propertyHelper.js]
---*/
var fallbacks = ['code', 'none'];
fallbacks.forEach(fallback => {
var dn = new Intl.DisplayNames('en-US', { fallback });
var options = dn.resolvedOptions();
verifyProperty(options, 'fallback', {
value: fallback,
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'type', {
value: 'language',
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'style', {
value: 'long',
writable: true,
enumerable: true,
configurable: true
});
});

View File

@ -0,0 +1,83 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Values for the style option
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
3. Let options be ! ObjectCreate(%ObjectPrototype%).
4. For each row of Table 6, except the header row, in table order, do
a. Let p be the Property value of the current row.
b. Let v be the value of pr's internal slot whose name is the Internal Slot value of the current row.
c. If v is not undefined, then
i. Perform ! CreateDataPropertyOrThrow(options, p, v).
6. Return options.
Table 6: Resolved Options of DisplayNames Instances
[[Locale]]: "locale"
[[Style]]: "style"
[[Type]]: "type"
[[Fallback]]: "fallback"
Intl.DisplayNames ([ locales [ , options ]])
...
8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
...
10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt,
%DisplayNames%.[[RelevantExtensionKeys]]).
11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
...
13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency",
"weekday", "month", "quarter", "dayPeriod", "dateTimeField" », "language").
...
15. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
...
17. Set displayNames.[[Locale]] to the value of r.[[Locale]].
...
CreateDataProperty ( O, P, V )
...
3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true }.
...
locale: [en-US]
features: [Intl.DisplayNames]
includes: [propertyHelper.js]
---*/
var styles = ['narrow', 'short', 'long'];
styles.forEach(style => {
var dn = new Intl.DisplayNames('en-US', { style });
var options = dn.resolvedOptions();
verifyProperty(options, 'style', {
value: style,
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'type', {
value: 'language',
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'fallback', {
value: 'code',
writable: true,
enumerable: true,
configurable: true
});
});

View File

@ -0,0 +1,83 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Values for the type option
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
3. Let options be ! ObjectCreate(%ObjectPrototype%).
4. For each row of Table 6, except the header row, in table order, do
a. Let p be the Property value of the current row.
b. Let v be the value of pr's internal slot whose name is the Internal Slot value of the current row.
c. If v is not undefined, then
i. Perform ! CreateDataPropertyOrThrow(options, p, v).
6. Return options.
Table 6: Resolved Options of DisplayNames Instances
[[Locale]]: "locale"
[[Style]]: "style"
[[Type]]: "type"
[[Fallback]]: "fallback"
Intl.DisplayNames ([ locales [ , options ]])
...
8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
...
10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt,
%DisplayNames%.[[RelevantExtensionKeys]]).
11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
...
13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency",
"weekday", "month", "quarter", "dayPeriod", "dateTimeField" », "language").
...
15. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
...
17. Set displayNames.[[Locale]] to the value of r.[[Locale]].
...
CreateDataProperty ( O, P, V )
...
3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true }.
...
locale: [en-US]
features: [Intl.DisplayNames]
includes: [propertyHelper.js]
---*/
var types = ['language', 'region', 'script', 'currency', 'weekday', 'month', 'quarter', 'dayPeriod', 'dateTimeField'];
types.forEach(type => {
var dn = new Intl.DisplayNames('en-US', { type });
var options = dn.resolvedOptions();
verifyProperty(options, 'type', {
value: type,
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'fallback', {
value: 'code',
writable: true,
enumerable: true,
configurable: true
});
verifyProperty(options, 'style', {
value: 'long',
writable: true,
enumerable: true,
configurable: true
});
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Property descriptor of Intl.DisplayNames.prototype.resolvedOptions
info: |
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: [Intl.DisplayNames]
---*/
assert.sameValue(
typeof Intl.DisplayNames.prototype.resolvedOptions,
"function",
"`typeof Intl.DisplayNames.prototype.resolvedOptions` is `'function'`"
);
verifyProperty(Intl.DisplayNames.prototype, "resolvedOptions", {
writable: true,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,95 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Returns a new ordinary object on each call, with data properties containing values from internals
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
3. Let options be ! ObjectCreate(%ObjectPrototype%).
4. For each row of Table 6, except the header row, in table order, do
a. Let p be the Property value of the current row.
b. Let v be the value of pr's internal slot whose name is the Internal Slot value of the current row.
c. If v is not undefined, then
i. Perform ! CreateDataPropertyOrThrow(options, p, v).
6. Return options.
Table 6: Resolved Options of DisplayNames Instances
[[Locale]]: "locale"
[[Style]]: "style"
[[Type]]: "type"
[[Fallback]]: "fallback"
Intl.DisplayNames ([ locales [ , options ]])
...
8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
...
10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt,
%DisplayNames%.[[RelevantExtensionKeys]]).
11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
...
13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency",
"weekday", "month", "quarter", "dayPeriod", "dateTimeField" », "language").
...
15. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
...
17. Set displayNames.[[Locale]] to the value of r.[[Locale]].
...
CreateDataProperty ( O, P, V )
...
3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true,
[[Configurable]]: true }.
...
locale: [en-US]
features: [Intl.DisplayNames, Reflect]
includes: [propertyHelper.js, compareArray.js]
---*/
var dn = new Intl.DisplayNames('en-US');
var options = dn.resolvedOptions();
var other = dn.resolvedOptions();
assert.notSameValue(options, other, 'each call returns a new object');
assert.sameValue(Object.getPrototypeOf(options), Object.prototype, 'ordinary object #1');
assert.sameValue(Object.getPrototypeOf(other), Object.prototype, 'ordinary object #2');
assert.compareArray(
Reflect.ownKeys(options),
['locale', 'style', 'type', 'fallback'],
'all the data properties set to this object, in order of creation'
);
verifyProperty(options, 'locale', {
value: 'en-US',
writable: true,
enumerable: true,
configurable: true
});
var explicit = new Intl.DisplayNames('en', { localeMatcher: 'lookup' }).resolvedOptions();
assert.sameValue(
explicit.hasOwnProperty('localeMatcher'),
false,
'the localeMatcher option is not set, option was explicitly set'
);
var extra = new Intl.DisplayNames('en', { chaos: 'yes', random: 'sure', '0': 42 }).resolvedOptions();
assert.compareArray(
Reflect.ownKeys(extra),
['locale', 'style', 'type', 'fallback'],
'extra properties are not reflected in the resolvedOptions'
);

View File

@ -0,0 +1,47 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Throws a TypeError if this is not Object.
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
...
features: [Intl.DisplayNames, Symbol]
---*/
var resolvedOptions = Intl.DisplayNames.prototype.resolvedOptions;
assert.throws(TypeError, function() {
resolvedOptions();
}, 'direct call');
assert.throws(TypeError, function() {
resolvedOptions.call('en');
}, 'string');
assert.throws(TypeError, function() {
resolvedOptions.call(1);
}, 'number');
assert.throws(TypeError, function() {
resolvedOptions.call(null);
}, 'null');
assert.throws(TypeError, function() {
resolvedOptions.call(true);
}, 'true');
assert.throws(TypeError, function() {
resolvedOptions.call(false);
}, 'false');
var symbol = Symbol();
assert.throws(TypeError, function() {
resolvedOptions.call(symbol);
}, 'symbol');

View File

@ -0,0 +1,41 @@
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DisplayNames.prototype.resolvedOptions
description: >
Throws a TypeError if this does not have an [[InitializedDisplayNames]] internal slot.
info: |
Intl.DisplayNames.prototype.resolvedOptions ()
1. Let pr be the this value.
2. If Type(pr) is not Object or pr does not have an [[InitializedDisplayNames]] internal slot,
throw a TypeError exception.
...
features: [Intl.DisplayNames]
---*/
var resolvedOptions = Intl.DisplayNames.prototype.resolvedOptions;
assert.throws(TypeError, function() {
Intl.DisplayNames.prototype.resolvedOptions();
}, 'Intl.DisplayNames.prototype does not have the internal slot');
assert.throws(TypeError, function() {
resolvedOptions.call({});
}, 'ordinary object');
assert.throws(TypeError, function() {
resolvedOptions.call(Intl.DisplayNames);
}, 'Intl.DisplayNames does not have the internal slot');
assert.throws(TypeError, function() {
resolvedOptions.call(Intl);
}, 'Intl does not have the internal slot');
// Not DisplayNames!!!
var dtf = new Intl.DateTimeFormat();
assert.throws(TypeError, function() {
resolvedOptions.call(dtf);
}, 'resolvedOptions cannot be used with instances from different Intl ctors');