IntlPlatform.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if (Intl.platform === undefined) {
  7. WScript.Echo("This test must be run with -IntlPlatform to enable the Intl.platform object");
  8. WScript.Quit(1);
  9. } else if (WScript.Platform.INTL_LIBRARY === "winglob") {
  10. // Skip for winglob -- there is an equivalent test in ChakraFull for it
  11. WScript.Echo("pass");
  12. WScript.Quit(0);
  13. }
  14. var platform = Intl.platform;
  15. Intl.platform.useCaches = false;
  16. /**
  17. * Caches all methods on Intl.platform and restores them after the test executes
  18. */
  19. function testSingleConstructor(Ctor, test) {
  20. return function () {
  21. const methods = Object.getOwnPropertyNames(Intl.platform);
  22. const cache = {};
  23. for (const method of methods) {
  24. cache[method] = Intl.platform[method];
  25. }
  26. test(Ctor);
  27. for (const method of methods) {
  28. Intl.platform[method] = cache[method];
  29. }
  30. }
  31. }
  32. /**
  33. * Creates a test for each constructor
  34. */
  35. function testEachConstructor(name, test) {
  36. const ret = [];
  37. for (const Ctor of [Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat]) {
  38. ret.push({
  39. name: `${name} (using Intl.${Ctor.name})`,
  40. body: testSingleConstructor(Ctor, test)
  41. });
  42. }
  43. return ret;
  44. }
  45. const tests = [
  46. ...testEachConstructor("Changing the default locale", function (Ctor) {
  47. platform.getDefaultLocale = () => "de-DE";
  48. assert.areEqual("de-DE", new Ctor().resolvedOptions().locale, "Default locale is respected with undefined language tag");
  49. }),
  50. ...testEachConstructor("Limiting available locales", function (Ctor) {
  51. const isXLocaleAvailableMap = {
  52. Collator: "Collator",
  53. NumberFormat: "NF",
  54. DateTimeFormat: "DTF",
  55. };
  56. const mapped = isXLocaleAvailableMap[Ctor.name];
  57. assert.isNotUndefined(mapped, `Invalid test setup: no mapped name available for ${Ctor.name}`);
  58. platform[`is${mapped}LocaleAvailable`] = (langtag) => ["de", "de-DE", "en", "zh", "en-UK"].includes(langtag);
  59. platform.getDefaultLocale = () => "en-UK";
  60. assert.areEqual("en", new Ctor("en-US").resolvedOptions().locale, "en-US should fall back to en");
  61. assert.areEqual("zh", new Ctor("zh-Hans").resolvedOptions().locale, "zh-Hans should fall back to zh");
  62. assert.areEqual("de-DE", new Ctor("de-DE-gregory").resolvedOptions().locale, "de-DE-gregory should fall back to de-DE");
  63. assert.areEqual("en-UK", new Ctor("sp").resolvedOptions().locale, "An unknown language tag should fall back to the default");
  64. })
  65. ];
  66. testRunner.runTests(tests, { verbose: false });