-
Notifications
You must be signed in to change notification settings - Fork 516
Add tests for Intl.Locale.prototype.variants #4474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ const options = [ | |
| "language", | ||
| "script", | ||
| "region", | ||
| "variants", | ||
| "calendar", | ||
| "collation", | ||
| "hourCycle", | ||
|
|
||
54 changes: 54 additions & 0 deletions
54
test/intl402/Locale/constructor-options-variants-invalid.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2025 Richard Gibson. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-intl.locale | ||
| description: > | ||
| Checks error cases for the options argument to the Locale | ||
| constructor. | ||
| info: | | ||
| Intl.Locale( tag [, options] ) | ||
| 12. Set _tag_ to ? UpdateLanguageId(_tag_, _options_). | ||
|
|
||
| UpdateLanguageId ( tag, options ) | ||
| 8. Let _variants_ be ? GetOption(_options_, *"variants"*, ~string~, ~empty~, GetLocaleVariants(_baseName_)). | ||
| 9. If _variants_ is not *undefined*, then | ||
| a. If _variants_ cannot be matched by the <code>unicode_variant_subtag</code> Unicode locale nonterminal, throw a *RangeError* exception. | ||
|
|
||
| features: [Intl.Locale] | ||
| ---*/ | ||
|
|
||
| /* | ||
| unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}) | ||
| */ | ||
| const invalidVariantsOptions = [ | ||
| "", | ||
| "a", | ||
| "1", | ||
| "ab", | ||
| "2x", | ||
| "abc", | ||
| "3xy", | ||
| "abcd", | ||
| "abcdefghi", | ||
|
|
||
| // Value contains more than just the 'variants' production. | ||
| "GB-scouse", | ||
|
|
||
| // Value contains duplicates. | ||
| "fonipa-fonipa", | ||
| "fonipa-valencia-Fonipa", | ||
|
|
||
| // Value contains out-of-place dashes. | ||
| "-", | ||
| "-spanglis", | ||
| "spanglis-", | ||
| "-spanglis-oxendict", | ||
| "spanglis-oxendict-", | ||
| "spanglis--oxendict", | ||
| ]; | ||
| for (const variants of invalidVariantsOptions) { | ||
| assert.throws(RangeError, function() { | ||
| new Intl.Locale("en", {variants}); | ||
| }, `new Intl.Locale("en", {variants: "${variants}"}) throws RangeError`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2025 Richard Gibson. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-intl.locale | ||
| description: > | ||
| Checks error cases for the options argument to the Locale | ||
| constructor. | ||
| info: | | ||
| Intl.Locale( tag [, options] ) | ||
| 12. Set _tag_ to ? UpdateLanguageId(_tag_, _options_). | ||
|
|
||
| UpdateLanguageId ( tag, options ) | ||
| 8. Let _variants_ be ? GetOption(_options_, *"variants"*, ~string~, ~empty~, GetLocaleVariants(_baseName_)). | ||
| ... | ||
| 13. If _variants_ is not *undefined*, set _newTag_ to the string-concatenation of _newTag_, *"-"*, and _variants_. | ||
|
|
||
| features: [Intl.Locale] | ||
| ---*/ | ||
|
|
||
| const validVariantsOptions = [ | ||
| ['en', undefined, undefined], | ||
| ['en', 'spanglis', 'en-spanglis'], | ||
|
|
||
| // unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}) | ||
| ['xx', '1xyz', 'xx-1xyz'], | ||
| ['xx', '1234', 'xx-1234'], | ||
| ['xx', 'abcde', 'xx-abcde'], | ||
| ['xx', '12345678', 'xx-12345678'], | ||
| ['xx', '1xyz-1234-abcde-12345678', 'xx-1234-12345678-1xyz-abcde'], | ||
|
|
||
| // Canonicalization affects subtag ordering. | ||
| ['en', 'spanglis-oxendict', 'en-oxendict-spanglis'], | ||
| ]; | ||
| for (const [lang, variants, baseName] of validVariantsOptions) { | ||
| let options = { variants }; | ||
| let optionsRepr = `{variants: ${typeof variants === "string" ? `"${variants}"` : variants}}`; | ||
| let instance; | ||
| let expect; | ||
|
|
||
| instance = new Intl.Locale(lang, options); | ||
| expect = baseName || lang; | ||
| assert.sameValue(instance.toString(), expect, | ||
| `new Intl.Locale("${lang}", ${optionsRepr}).toString() returns "${expect}"`); | ||
|
|
||
| instance = new Intl.Locale(lang + '-fonipa', options); | ||
| expect = baseName || (lang + '-fonipa'); | ||
| assert.sameValue(instance.toString(), expect, | ||
| `new Intl.Locale("${lang}-fonipa", ${optionsRepr}).toString() returns "${expect}"`); | ||
|
|
||
| instance = new Intl.Locale(lang + '-u-ca-gregory', options); | ||
| expect = (baseName || lang) + '-u-ca-gregory'; | ||
| assert.sameValue(instance.toString(), expect, | ||
| `new Intl.Locale("${lang}-u-ca-gregory", ${optionsRepr}).toString() returns "${expect}"`); | ||
|
|
||
| instance = new Intl.Locale(lang + '-fonipa-u-ca-gregory', options); | ||
| expect = (baseName || (lang + '-fonipa')) + '-u-ca-gregory'; | ||
| assert.sameValue(instance.toString(), expect, | ||
| `new Intl.Locale("${lang}-fonipa-u-ca-gregory", ${optionsRepr}).toString() returns "${expect}"`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also test for leading, trailing, and consecutive
"-"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And test that duplicate variants throw: