DateTimeFormatInvalidOptions.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 Invalid Options",
  10. body: function () {
  11. function verifyDTFException(locale, option, invalidValue, validValues) {
  12. if (validValues.indexOf(invalidValue) !== -1) {
  13. assert.fail("Test constructed incorrectly.");
  14. }
  15. try {
  16. // Since minute and second aren't supported alone; doing this to prevent that exception.
  17. var options = { hour: "numeric", minute: "numeric" };
  18. options[option] = invalidValue;
  19. new Intl.DateTimeFormat(locale, options);
  20. assert.fail("Exception was expected. Option: " + option + "; invalid value: " + invalidValue);
  21. }
  22. catch (e) {
  23. if (!(e instanceof RangeError)) {
  24. assert.fail("Incorrect exception was thrown.");
  25. }
  26. assert.isTrue(e.message.indexOf(validValues) !== -1,
  27. "Checking exception message for correct values string. Looking for: " + validValues +
  28. "\nMessage: " + e.message);
  29. }
  30. }
  31. verifyDTFException("en-US", "year", "long", "['2-digit', 'numeric']");
  32. verifyDTFException("en-US", "month", "false", "['2-digit', 'numeric', 'narrow', 'short', 'long']");
  33. verifyDTFException("en-US", "day", "long", "['2-digit', 'numeric']");
  34. verifyDTFException("en-US", "hour", "long", "['2-digit', 'numeric']");
  35. verifyDTFException("en-US", "minute", "long", "['2-digit', 'numeric']");
  36. verifyDTFException("en-US", "second", "long", "['2-digit', 'numeric']");
  37. verifyDTFException("en-US", "era", "numeric", "['narrow', 'short', 'long']");
  38. verifyDTFException("en-US", "localeMatcher", "long", "['lookup', 'best fit']");
  39. verifyDTFException("en-US", "formatMatcher", "long", "['basic', 'best fit']");
  40. assert.areEqual(new Intl.DateTimeFormat("en-US", { hour: "numeric", hour12: "asdasd" }).resolvedOptions().hour12, true, "Hour12 special invalid option treatment.");
  41. }
  42. }
  43. ];
  44. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });