NumberFormatOptions.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. const tests = [
  7. {
  8. name: "Test invalid options",
  9. body: function () {
  10. function verifyNFException(locale, options, expectingInvalidOption, validValuesStr) {
  11. try {
  12. //Since minute and second aren't supported alone; doing this to prevent that exception.
  13. new Intl.NumberFormat(locale, options);
  14. assert.fail("Exception was expected. Option: " + expectingInvalidOption + "; options passed in: " + JSON.stringify(options));
  15. }
  16. catch (e) {
  17. if (!(e instanceof RangeError || e instanceof TypeError)) {
  18. assert.fail("Incorrect exception was thrown.");
  19. }
  20. assert.isTrue(e.message.indexOf(validValuesStr) !== -1,
  21. "Exception didn't have the correct valid values when testing option:" + expectingInvalidOption +
  22. ".\nMessage: " + e.message +
  23. "\nSearched For:" + validValuesStr);
  24. }
  25. }
  26. verifyNFException("en-US", { minimumSignificantDigits: -1 }, "minimumSignificantDigits", "[1 - 21]");
  27. verifyNFException("en-US", { maximumSignificantDigits: -1 }, "maximumSignificantDigits", "[1 - 21]");
  28. verifyNFException("en-US", { minimumFractionDigits: -1 }, "minimumFractionDigits", "[0 - 20]");
  29. verifyNFException("en-US", { maximumFractionDigits: -1 }, "maximumFractionDigits", "[0 - 20]");
  30. verifyNFException("en-US", { minimumIntegerDigits: -1 }, "minimumIntegerDigits", "[1 - 21]");
  31. verifyNFException("en-US", { minimumSignificantDigits: 22 }, "minimumSignificantDigits", "[1 - 21]");
  32. verifyNFException("en-US", { maximumSignificantDigits: 22 }, "maximumSignificantDigits", "[1 - 21]");
  33. verifyNFException("en-US", { minimumFractionDigits: 21 }, "minimumFractionDigits", "[0 - 20]");
  34. verifyNFException("en-US", { maximumFractionDigits: 21 }, "maximumFractionDigits", "[0 - 20]");
  35. verifyNFException("en-US", { minimumIntegerDigits: 22 }, "minimumIntegerDigits", "[1 - 21]");
  36. verifyNFException("en-US", { minimumSignificantDigits: 5, maximumSignificantDigits: 1 }, "maximumSignificantDigits", "[5 - 21]");
  37. verifyNFException("en-US", { minimumFractionDigits: 5, maximumFractionDigits: 1 }, "maximumFractionDigits", "[5 - 20]");
  38. verifyNFException("en-US", { style: "invalid" }, "style", "['decimal', 'percent', 'currency']");
  39. verifyNFException("en-US", { style: "currency" }, "style", "Currency code was not specified");
  40. verifyNFException("en-US", { style: "currency", currency: 5 }, "currency", "Currency code '5' is invalid");
  41. verifyNFException("en-US", { style: "currency", currency: "USD", currencyDisplay: "invalid" }, "currencyDisplay", "['code', 'symbol', 'name']");
  42. }
  43. }
  44. ];
  45. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });