Basics.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. function testRangeError(tag) {
  7. assert.throws(function () { Intl.getCanonicalLocales(tag) }, RangeError,
  8. `Tag '${tag}' should throw RangeError`,
  9. `Locale '${tag}' is not well-formed`);
  10. }
  11. function assertEachIsOneOf(actualList, expectedList, msg) {
  12. if (actualList.length === 0) {
  13. assert.fail("actual result list was empty");
  14. }
  15. for (a of actualList) {
  16. assert.isTrue(expectedList.includes(a), msg);
  17. }
  18. }
  19. let IntlNamespaces = [
  20. Intl.Collator,
  21. Intl.DateTimeFormat,
  22. Intl.NumberFormat
  23. ]
  24. // Tests of ECMA 402 Basic Functionality
  25. // Minor motivation is to exercise internal operations implemented with WinGlob and ICU.
  26. var tests = [
  27. // #sec-canonicalizelocalelist (JS `Intl.getCanonicalLocales`)
  28. {
  29. name: "#sec-isstructurallyvalidlanguagetag (C++ `IsWellFormedLanguageTag`): malformed inputs throw",
  30. body: function () {
  31. // * #sec-isstructurallyvalidlanguagetag: C++ `IsWellFormedLanguageTag`: not well-formed; should throw
  32. let malformed = ['中文', 'en-', '-us', 'en-us-latn'];
  33. malformed.forEach(testRangeError);
  34. }
  35. },
  36. {
  37. name: "#sec-canonicalizelanguagetag (C++ `NormalizeLanguageTag`)",
  38. body: function () {
  39. // * #sec-isstructurallyvalidlanguagetag: C++ `IsWellFormedLanguageTag`: input is well-formed
  40. // * (implies C++ `IsWellFormedLanguageTag` returned true and thus didn't throw)
  41. assert.areEqual(Intl.getCanonicalLocales(['en-us']), ['en-US']);
  42. assert.areEqual(Intl.getCanonicalLocales(['de-de']), ['de-DE']);
  43. assertEachIsOneOf(Intl.getCanonicalLocales(['ja-JP']), ['ja', 'ja-JP'], "Depending on WinGlob or ICU, one of these results");
  44. assertEachIsOneOf(Intl.getCanonicalLocales(['zh-cn']), ['zh-CN', 'zh-Hans-CN'], "Depending on WinGlob or ICU, one of these results");
  45. assertEachIsOneOf(
  46. Intl.getCanonicalLocales(
  47. [
  48. 'en-us', 'de-de', 'ja-JP', 'zh-cn'
  49. ]
  50. ),
  51. [
  52. 'en-US',
  53. 'de-DE',
  54. 'ja', 'ja-JP',
  55. 'zh-CN', 'zh-Hans-CN',
  56. ]
  57. );
  58. }
  59. },
  60. {
  61. name: "supportedLocalesOf",
  62. body: function () {
  63. IntlNamespaces.forEach(function(ns) {
  64. assertEachIsOneOf(ns.supportedLocalesOf(['en-us']), ['en', 'en-US']);
  65. assertEachIsOneOf(ns.supportedLocalesOf(['de-de']), ['de', 'de-DE']);
  66. assertEachIsOneOf(ns.supportedLocalesOf(['ja-JP']), ['ja', 'ja-JP']);
  67. assertEachIsOneOf(ns.supportedLocalesOf(['zh-cn']), ['zh', 'zh-CN', 'zh-Hans-CN']);
  68. assertEachIsOneOf(
  69. ns.supportedLocalesOf(
  70. [
  71. 'en-us', 'de-de', 'ja-JP', 'zh-cn'
  72. ]
  73. ),
  74. [
  75. 'en', 'en-US',
  76. 'de', 'de-DE',
  77. 'ja', 'ja-JP',
  78. 'zh', 'zh-CN', 'zh-Hans-CN',
  79. ]
  80. );
  81. });
  82. }
  83. }
  84. ];
  85. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });