|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js"); |
| 7 | + |
| 8 | +// NOTE: \u00C0 is U+00C0 LATIN CAPITAL LETTER A WITH GRAVE |
| 9 | +// NOTE: \u00E4 is U+00E4 LATIN SMALL LETTER A WITH DIAERESIS |
| 10 | +var tests = [ |
| 11 | + { |
| 12 | + name: "Test Valid Options Resolution", |
| 13 | + body: function () { |
| 14 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "variant" }).resolvedOptions().sensitivity, "variant", "Ensure that variant sensitivity is interpreted correctly."); |
| 15 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "case" }).resolvedOptions().sensitivity, "case", "Ensure that case sensitivity is interpreted correctly."); |
| 16 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "accent" }).resolvedOptions().sensitivity, "accent", "Ensure that accent sensitivity is interpreted correctly."); |
| 17 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "base" }).resolvedOptions().sensitivity, "base", "Ensure that base sensitivity is interpreted correctly."); |
| 18 | + |
| 19 | + assert.areEqual(new Intl.Collator("de-DE", { collation: "phonebk" }).resolvedOptions().collation, "default", "Ensure that collation option is ignored."); |
| 20 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk").resolvedOptions().collation, "phonebk", "Ensure that collation unicode extension is interpreted correctly (with absent options)."); |
| 21 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", undefined).resolvedOptions().collation, "phonebk", "Ensure that collation unicode extension is interpreted correctly (with undefined options)."); |
| 22 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", {}).resolvedOptions().collation, "phonebk", "Ensure that collation unicode extension is interpreted correctly (with empty options)."); |
| 23 | + |
| 24 | + assert.areEqual(new Intl.Collator("de-DE").resolvedOptions().numeric, false, "Ensure numeric set to false implicitly."); |
| 25 | + assert.areEqual(new Intl.Collator("de-DE", { numeric: false }).resolvedOptions().numeric, false, "Ensure numeric set to false explicitly."); |
| 26 | + assert.areEqual(new Intl.Collator("de-DE", { numeric: true }).resolvedOptions().numeric, true, "Ensure numeric set to true."); |
| 27 | + |
| 28 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", { numeric: false }).resolvedOptions().collation, "phonebk", "Mixed -u-co-phonebk and numeric option false (collation)."); |
| 29 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", { numeric: false }).resolvedOptions().numeric, false, "Mixed -u-co-phonebk and numeric option false (numeric)."); |
| 30 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", { numeric: true }).resolvedOptions().collation, "phonebk", "Mixed -u-co-phonebk and numeric option true (collation)."); |
| 31 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", { numeric: true }).resolvedOptions().numeric, true, "Mixed -u-co-phonebk and numeric option true (numeric)."); |
| 32 | + |
| 33 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true").resolvedOptions().numeric, true, "Ensure that -u-kn-true is interpreted correctly."); |
| 34 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false").resolvedOptions().numeric, false, "Ensure that -u-kn-false is interpreted correctly."); |
| 35 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true-co-phonebk").resolvedOptions().numeric, true, "Ensure that -u-kn-true-co-phonebk is interpreted correctly (numeric)."); |
| 36 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true-co-phonebk").resolvedOptions().collation, "phonebk", "Ensure that -u-kn-true-co-phonebk is interpreted correctly (collation)."); |
| 37 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false-co-phonebk").resolvedOptions().numeric, false, "Ensure that -u-kn-false-co-phonebk is interpreted correctly (numeric)."); |
| 38 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false-co-phonebk").resolvedOptions().collation, "phonebk", "Ensure that -u-kn-false-co-phonebk is interpreted correctly (collation)."); |
| 39 | + } |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Test Valid Options Behavior", |
| 43 | + body: function () { |
| 44 | + assert.areEqual(new Intl.Collator("en-US").compare("a", "A"), -1, "Comparing with default options."); |
| 45 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "variant" }).compare("a", "A"), -1, "Comparing with variant sensitivity."); |
| 46 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "variant" }).compare("\u00C0", "A"), 1, "Comparing with variant sensitivity."); |
| 47 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "variant" }).compare("a", "b"), -1, "Comparing with variant sensitivity."); |
| 48 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "case" }).compare("a", "A"), -1, "Comparing with case sensitivity."); |
| 49 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "case" }).compare("a", "b"), -1, "Comparing with case sensitivity."); |
| 50 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "case" }).compare("\u00C0", "A"), 0, "Comparing with case sensitivity."); |
| 51 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "accent" }).compare("\u00C0", "A"), 1, "Comparing with accent sensitivity."); |
| 52 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "accent" }).compare("a", "A"), 0, "Comparing with accent sensitivity."); |
| 53 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "accent" }).compare("a", "b"), -1, "Comparing with accent sensitivity."); |
| 54 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "base" }).compare("a", "A"), 0, "Comparing with base sensitivity."); |
| 55 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "base" }).compare("\u00C0", "A"), 0, "Comparing with base sensitivity."); |
| 56 | + assert.areEqual(new Intl.Collator("en-US", { sensitivity: "base" }).compare("a", "b"), -1, "Comparing with base sensitivity."); |
| 57 | + |
| 58 | + assert.areEqual(new Intl.Collator("de-DE", { collation: "phonebk" }).compare("\u00e4b", "ada"), -1, "Comparing with default collation, option ignored."); |
| 59 | + assert.areEqual(new Intl.Collator("de-DE", { collation: "phonebk" }).compare("äb", "ada"), -1, "Comparing with default collation, option ignored (using explicit ä character)."); |
| 60 | + |
| 61 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", {}).compare("\u00e4b", "ada"), 1, "Comparing with collation unicode extension phonebk."); |
| 62 | + assert.areEqual(new Intl.Collator("de-DE-u-co-phonebk", {}).compare("äb", "ada"), 1, "Comparing with collation unicode extension phonebk (using explicit ä character)."); |
| 63 | + assert.areEqual(new Intl.Collator("de-DE", {}).compare("\u00e4b", "ada"), -1, "Comparing without collation option of phonebk."); |
| 64 | + assert.areEqual(new Intl.Collator("de-DE", {}).compare("äb", "ada"), -1, "Comparing without collation option of phonebk (using explicit ä character)."); |
| 65 | + |
| 66 | + assert.areEqual(new Intl.Collator("de-DE", { numeric: true }).compare("21", "100"), -1, "Comparing with numeric option set to true."); |
| 67 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true", {}).compare("21", "100"), -1, "Comparing with numeric unicode extension set to true."); |
| 68 | + assert.areEqual(new Intl.Collator("de-DE", { numeric: false }).compare("21", "100"), 1, "Comparing with numeric option set to false."); |
| 69 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false", {}).compare("21", "100"), 1, "Comparing with numeric unicode extension set to false."); |
| 70 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true-co-phonebk", {}).compare("21", "100"), -1, "Comparing with collation set to phonebk and numeric set to true."); |
| 71 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-true-co-phonebk", {}).compare("\u00e4b", "ada"), 1, "Comparing with collation set to phonebk and numeric set to true."); |
| 72 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false-co-phonebk", {}).compare("21", "100"), 1, "Comparing with collation set to phonebk and numeric set to false."); |
| 73 | + assert.areEqual(new Intl.Collator("de-DE-u-kn-false-co-phonebk", {}).compare("\u00e4b", "ada"), 1, "Comparing with collation set to phonebk and numeric set to false."); |
| 74 | + assert.areEqual(new Intl.Collator("en-US", { ignorePunctuation: true }).compare("aa", "a!a"), 0, "Comparing with ignore punctuation set to true."); |
| 75 | + assert.areEqual(new Intl.Collator("en-US", { ignorePunctuation: false }).compare("aa", "a!a"), 1, "Comparing with ignore punctuation set to true."); |
| 76 | + } |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Test Invalid Options", |
| 80 | + body: function () { |
| 81 | + function verifyCollatorException(locale, options, expectingInvalidOption, validValuesStr) { |
| 82 | + try { |
| 83 | + //Since minute and second aren't supported alone; doing this to prevent that exception. |
| 84 | + new Intl.Collator(locale, options); |
| 85 | + assert.fail("Exception was expected. Option: " + expectingInvalidOption + "; options passed in: " + JSON.stringify(options)); |
| 86 | + } |
| 87 | + catch (e) { |
| 88 | + if (!(e instanceof RangeError || e instanceof TypeError)) { |
| 89 | + assert.fail("Incorrect exception was thrown."); |
| 90 | + } |
| 91 | + assert.isTrue(e.message.indexOf(validValuesStr) !== -1, |
| 92 | + "Exception didn't have the correct valid values when testing option:" + expectingInvalidOption + |
| 93 | + ".\nMessage: " + e.message + |
| 94 | + "\nSearched For:" + validValuesStr); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + verifyCollatorException("en-US-u-kf-invalid", {}, "caseFirst", "['upper', 'lower', 'false']"); |
| 99 | + verifyCollatorException("en-US", { caseFirst: "invalid" }, "caseFirst", "['upper', 'lower', 'false']"); |
| 100 | + |
| 101 | + assert.areEqual(new Intl.Collator("en-US", { numeric: "blah" }).resolvedOptions().numeric, true, "Testing invalid numeric option."); |
| 102 | + assert.areEqual(new Intl.Collator("en-US-u-kn-blah", {}).resolvedOptions().numeric, false, "Testing invalid numeric option."); |
| 103 | + assert.areEqual(new Intl.Collator("en-US", { ignorePunctuation: "blah" }).resolvedOptions().ignorePunctuation, true, "Testing invalid ignorePunctuation option."); |
| 104 | + assert.areEqual(new Intl.Collator("en-US", { collation: "blah" }).resolvedOptions().collation, "default", "Testing invalid collation option."); |
| 105 | + assert.areEqual(new Intl.Collator("en-US-u-co-blah", {}).resolvedOptions().collation, "default", "Testing invalid colation option."); |
| 106 | + } |
| 107 | + } |
| 108 | +]; |
| 109 | + |
| 110 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments