| 1 |
// Copyright 2018 André Bargull; Igalia, S.L. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-intl.locale |
| 6 |
description: > |
| 7 |
Verify valid language option values (various) |
| 8 |
info: | |
| 9 |
Intl.Locale( tag [, options] ) |
| 10 |
10. If options is undefined, then |
| 11 |
11. Else |
| 12 |
a. Let options be ? ToObject(options). |
| 13 |
12. Set tag to ? ApplyOptionsToTag(tag, options). |
| 14 |
|
| 15 |
ApplyOptionsToTag( tag, options ) |
| 16 |
... |
| 17 |
2. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception. |
| 18 |
3. Let language be ? GetOption(options, "language", "string", undefined, undefined). |
| 19 |
4. If language is not undefined, then |
| 20 |
a. If language does not match the unicode_language_subtag production, throw a RangeError exception. |
| 21 |
|
| 22 |
IsStructurallyValidLanguageTag ( locale ) |
| 23 |
|
| 24 |
The IsStructurallyValidLanguageTag abstract operation verifies that the |
| 25 |
locale argument (which must be a String value) |
| 26 |
|
| 27 |
represents a well-formed Unicode BCP 47 Locale Identifier" as specified in |
| 28 |
Unicode Technical Standard 35 section 3.2, or successor, |
| 29 |
|
| 30 |
features: [Intl.Locale] |
| 31 |
---*/ |
| 32 |
|
| 33 |
const validLanguageOptions = [ |
| 34 |
[{ toString() { return 'de' } }, 'de'], |
| 35 |
]; |
| 36 |
for (const [language, expected] of validLanguageOptions) { |
| 37 |
let expect = expected || 'en'; |
| 38 |
|
| 39 |
assert.sameValue( |
| 40 |
new Intl.Locale('en', {language}).toString(), |
| 41 |
expect, |
| 42 |
`new Intl.Locale('en', {language: "${language}"}).toString() returns "${expect}"` |
| 43 |
); |
| 44 |
|
| 45 |
expect = (expected || 'en') + '-US'; |
| 46 |
assert.sameValue( |
| 47 |
new Intl.Locale('en-US', {language}).toString(), |
| 48 |
expect, |
| 49 |
`new Intl.Locale('en-US', {language: "${language}"}).toString() returns "${expect}"` |
| 50 |
); |
| 51 |
|
| 52 |
assert.throws(RangeError, () => new Intl.Locale('en-els', {language})); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
const invalidLanguageOptions = [ |
| 57 |
null, |
| 58 |
'zh-cmn', |
| 59 |
'ZH-CMN', |
| 60 |
'abcd', |
| 61 |
]; |
| 62 |
for (const language of invalidLanguageOptions) { |
| 63 |
assert.throws(RangeError, () => new Intl.Locale('en', {language})); |
| 64 |
assert.throws(RangeError, () => new Intl.Locale('en-US', {language})); |
| 65 |
assert.throws(RangeError, () => new Intl.Locale('en-els', {language})); |
| 66 |
} |
| 67 |
|
| 68 |
reportCompare(0, 0); |