SupportedLocalesOf.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. var tests = [
  7. {
  8. name: "Test Correct Errors",
  9. body: function () {
  10. assert.throws(function () { new Intl.Collator.supportedLocalesOf(); }, TypeError, "", "Function 'Intl.Collator.supportedLocalesOf' is not a constructor");
  11. assert.throws(function () { new Intl.NumberFormat.supportedLocalesOf(); }, TypeError, "", "Function 'Intl.NumberFormat.supportedLocalesOf' is not a constructor");
  12. assert.throws(function () { new Intl.DateTimeFormat.supportedLocalesOf(); }, TypeError, "", "Function 'Intl.DateTimeFormat.supportedLocalesOf' is not a constructor");
  13. const rangeErrorMessage = "Option value 'incorrect' for 'localeMatcher' is outside of valid range. Expected: ['best fit', 'lookup']";
  14. assert.throws(function () { Intl.Collator.supportedLocalesOf(["en-US"], { localeMatcher: "incorrect" }) }, RangeError, "", rangeErrorMessage);
  15. assert.throws(function () { Intl.NumberFormat.supportedLocalesOf(["en-US"], { localeMatcher: "incorrect" }) }, RangeError, "", rangeErrorMessage);
  16. assert.throws(function () { Intl.DateTimeFormat.supportedLocalesOf(["en-US"], { localeMatcher: "incorrect" }) }, RangeError, "", rangeErrorMessage);
  17. assert.throws(function () { Intl.Collator.supportedLocalesOf(null) }, TypeError, "", "Object expected");
  18. assert.throws(function () { Intl.NumberFormat.supportedLocalesOf(null) }, TypeError, "", "Object expected");
  19. assert.throws(function () { Intl.DateTimeFormat.supportedLocalesOf(null) }, TypeError, "", "Object expected");
  20. assert.throws(function () {
  21. var locales = { get length() { throw new Error("Intentional throw"); } };
  22. Intl.Collator.supportedLocalesOf(locales);
  23. console.log("Intentional throw didn't throw.");
  24. }, Error, "", "Intentional throw");
  25. }
  26. },
  27. {
  28. name: "",
  29. body: function () {
  30. const ctors = [Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat];
  31. function test(ctor) {
  32. assert.areEqual(ctor.supportedLocalesOf(["en"]).toString(), "en");
  33. assert.areEqual(ctor.supportedLocalesOf(["en"], { localeMatcher: "lookup" }).toString(), "en-US");
  34. assert.areEqual(ctor.supportedLocalesOf(["en"], { localeMatcher: "best fit" }).toString(), "en");
  35. assert.areEqual(ctor.supportedLocalesOf().length, 0);
  36. assert.areEqual(ctor.supportedLocalesOf(undefined, { localeMatcher: "lookup" }).length, 0);
  37. assert.areEqual(ctor.supportedLocalesOf(undefined, { localeMatcher: "best fit" }).length, 0);
  38. assert.areEqual(ctor.supportedLocalesOf.call({}, ["en"]).toString(), "en");
  39. assert.areEqual(ctor.supportedLocalesOf.call({}, ["en"], { localeMatcher: "lookup" }).toString(), "en-US");
  40. assert.areEqual(ctor.supportedLocalesOf.call({}, ["en"], { localeMatcher: "best fit" }).toString(), "en");
  41. assert.areEqual(ctor.supportedLocalesOf.bind({})(["en"]).toString(), "en");
  42. assert.areEqual(ctor.supportedLocalesOf.bind({})(["en"], { localeMatcher: "lookup" }).toString(), "en-US");
  43. assert.areEqual(ctor.supportedLocalesOf.bind({})(["en"], { localeMatcher: "best fit" }).toString(), "en");
  44. }
  45. ctors.forEach(test);
  46. }
  47. }
  48. ];
  49. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });