CollatorOptions.js 11 KB

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