DateTimeFormatOptions.js 6.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: \u200e is the U+200E LEFT-TO-RIGHT MARK
  7. var tests = [
  8. {
  9. name: "Test Valid Options",
  10. body: function () {
  11. assert.areEqual(new Intl.DateTimeFormat("en-US", { year: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e2000", "Formatting year as numeric.");
  12. assert.areEqual(new Intl.DateTimeFormat("en-US", { year: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e00", "Formatting year as 2-digit.");
  13. assert.areEqual(new Intl.DateTimeFormat("en-US", { month: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e2", "Formatting month as numeric.");
  14. assert.areEqual(new Intl.DateTimeFormat("en-US", { month: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e02", "Formatting month as 2-digit.");
  15. assert.areEqual(new Intl.DateTimeFormat("en-US", { month: "long" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200eFebruary", "Formatting month as 2-digit.");
  16. assert.areEqual(new Intl.DateTimeFormat("en-US", { month: "short" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200eFeb", "Formatting month as numeric.");
  17. assert.areEqual(new Intl.DateTimeFormat("en-US", { month: "narrow" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200eFeb", "Formatting month as narrow.");
  18. assert.areEqual(new Intl.DateTimeFormat("en-US", { day: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01", "Formatting day as 2-digit.");
  19. assert.areEqual(new Intl.DateTimeFormat("en-US", { day: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e1", "Formatting day as numeric.");
  20. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01\u200e \u200eAM", "Formatting hour as 2-digit.");
  21. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e1\u200e \u200eAM", "Formatting hour as numeric.");
  22. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "2-digit", minute: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01\u200e:\u200e01\u200e \u200eAM", "Formatting hour as 2-digit and minute as 2-digit.");
  23. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "2-digit", minute: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01\u200e:\u200e01\u200e \u200eAM", "Formatting hour as 2-digit and minute as numeric.");
  24. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "2-digit", minute: "2-digit", second: "2-digit" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01\u200e:\u200e01\u200e:\u200e01\u200e \u200eAM", "Formatting hour as 2-digit, minute as 2-digit and second as 2-digit.");
  25. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "2-digit", minute: "2-digit", second: "numeric" }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e01\u200e:\u200e01\u200e:\u200e01\u200e \u200eAM", "Formatting hour as 2-digit, minute as 2-digit and second as numeric.");
  26. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "numeric", hour12: true }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e1\u200e \u200eAM", "Formatting hour as numeric with hour12=true.");
  27. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "numeric", hour12: false }).format(new Date(2000, 1, 1, 1, 1, 1)), "\u200e1\u200e:\u200e00", "Formatting hour as numeric with hour12=false.");
  28. }
  29. },
  30. {
  31. name: "Test Invalid Options",
  32. body: function () {
  33. function verifyDTFException(locale, option, invalidValue, validValues) {
  34. if (validValues.indexOf(invalidValue) !== -1) {
  35. assert.fail("Test constructed incorrectly.");
  36. }
  37. try {
  38. // Since minute and second aren't supported alone; doing this to prevent that exception.
  39. var options = { hour: "numeric", minute: "numeric" };
  40. options[option] = invalidValue;
  41. new Intl.DateTimeFormat(locale, options);
  42. assert.fail("Exception was expected. Option: " + option + "; invalid value: " + invalidValue);
  43. }
  44. catch (e) {
  45. if (!(e instanceof RangeError)) {
  46. assert.fail("Incorrect exception was thrown.");
  47. }
  48. assert.isTrue(e.message.indexOf(validValues) !== -1,
  49. "Checking exception message for correct values string. Looking for: " + validValues +
  50. "\nMessage: " + e.message);
  51. }
  52. }
  53. verifyDTFException("en-US", "year", "long", "['2-digit', 'numeric']");
  54. verifyDTFException("en-US", "month", "false", "['2-digit', 'numeric', 'narrow', 'short', 'long']");
  55. verifyDTFException("en-US", "day", "long", "['2-digit', 'numeric']");
  56. verifyDTFException("en-US", "hour", "long", "['2-digit', 'numeric']");
  57. verifyDTFException("en-US", "minute", "long", "['2-digit', 'numeric']");
  58. verifyDTFException("en-US", "second", "long", "['2-digit', 'numeric']");
  59. verifyDTFException("en-US", "era", "numeric", "['narrow', 'short', 'long']");
  60. verifyDTFException("en-US", "localeMatcher", "long", "['lookup', 'best fit']");
  61. verifyDTFException("en-US", "formatMatcher", "long", "['basic', 'best fit']");
  62. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "numeric", hour12: "asdasd" }).resolvedOptions().hour12, true, "Hour12 special invalid option treatment.");
  63. }
  64. }
  65. ];
  66. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });