PluralRules.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. describe("errors", () => {
  2. test("called without new", () => {
  3. expect(() => {
  4. Intl.PluralRules();
  5. }).toThrowWithMessage(TypeError, "Intl.PluralRules constructor must be called with 'new'");
  6. });
  7. test("options is an invalid type", () => {
  8. expect(() => {
  9. new Intl.PluralRules("en", null);
  10. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  11. });
  12. test("localeMatcher option is invalid ", () => {
  13. expect(() => {
  14. new Intl.PluralRules("en", { localeMatcher: "hello!" });
  15. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
  16. });
  17. test("type option is invalid ", () => {
  18. expect(() => {
  19. new Intl.PluralRules("en", { type: "hello!" });
  20. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option type");
  21. });
  22. test("minimumIntegerDigits option is invalid ", () => {
  23. expect(() => {
  24. new Intl.PluralRules("en", { minimumIntegerDigits: 1n });
  25. }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  26. expect(() => {
  27. new Intl.PluralRules("en", { minimumIntegerDigits: "hello!" });
  28. }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
  29. expect(() => {
  30. new Intl.PluralRules("en", { minimumIntegerDigits: 0 });
  31. }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
  32. expect(() => {
  33. new Intl.PluralRules("en", { minimumIntegerDigits: 22 });
  34. }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
  35. });
  36. test("minimumFractionDigits option is invalid ", () => {
  37. expect(() => {
  38. new Intl.PluralRules("en", { minimumFractionDigits: 1n });
  39. }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  40. expect(() => {
  41. new Intl.PluralRules("en", { minimumFractionDigits: "hello!" });
  42. }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 0 and 20");
  43. expect(() => {
  44. new Intl.PluralRules("en", { minimumFractionDigits: -1 });
  45. }).toThrowWithMessage(RangeError, "Value -1 is NaN or is not between 0 and 20");
  46. expect(() => {
  47. new Intl.PluralRules("en", { minimumFractionDigits: 21 });
  48. }).toThrowWithMessage(RangeError, "Value 21 is NaN or is not between 0 and 20");
  49. });
  50. test("maximumFractionDigits option is invalid ", () => {
  51. expect(() => {
  52. new Intl.PluralRules("en", { maximumFractionDigits: 1n });
  53. }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  54. expect(() => {
  55. new Intl.PluralRules("en", { maximumFractionDigits: "hello!" });
  56. }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 0 and 20");
  57. expect(() => {
  58. new Intl.PluralRules("en", { maximumFractionDigits: -1 });
  59. }).toThrowWithMessage(RangeError, "Value -1 is NaN or is not between 0 and 20");
  60. expect(() => {
  61. new Intl.PluralRules("en", { maximumFractionDigits: 21 });
  62. }).toThrowWithMessage(RangeError, "Value 21 is NaN or is not between 0 and 20");
  63. expect(() => {
  64. new Intl.PluralRules("en", { minimumFractionDigits: 10, maximumFractionDigits: 5 });
  65. }).toThrowWithMessage(RangeError, "Minimum value 10 is larger than maximum value 5");
  66. });
  67. test("minimumSignificantDigits option is invalid ", () => {
  68. expect(() => {
  69. new Intl.PluralRules("en", { minimumSignificantDigits: 1n });
  70. }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  71. expect(() => {
  72. new Intl.PluralRules("en", { minimumSignificantDigits: "hello!" });
  73. }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
  74. expect(() => {
  75. new Intl.PluralRules("en", { minimumSignificantDigits: 0 });
  76. }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
  77. expect(() => {
  78. new Intl.PluralRules("en", { minimumSignificantDigits: 22 });
  79. }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
  80. });
  81. test("maximumSignificantDigits option is invalid ", () => {
  82. expect(() => {
  83. new Intl.PluralRules("en", { maximumSignificantDigits: 1n });
  84. }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
  85. expect(() => {
  86. new Intl.PluralRules("en", { maximumSignificantDigits: "hello!" });
  87. }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
  88. expect(() => {
  89. new Intl.PluralRules("en", { maximumSignificantDigits: 0 });
  90. }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
  91. expect(() => {
  92. new Intl.PluralRules("en", { maximumSignificantDigits: 22 });
  93. }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
  94. });
  95. });
  96. describe("normal behavior", () => {
  97. test("length is 0", () => {
  98. expect(Intl.PluralRules).toHaveLength(0);
  99. });
  100. test("all valid localeMatcher options", () => {
  101. ["lookup", "best fit"].forEach(localeMatcher => {
  102. expect(() => {
  103. new Intl.PluralRules("en", { localeMatcher: localeMatcher });
  104. }).not.toThrow();
  105. });
  106. });
  107. test("all valid type options", () => {
  108. ["cardinal", "ordinal"].forEach(type => {
  109. expect(() => {
  110. new Intl.PluralRules("en", { type: type });
  111. }).not.toThrow();
  112. });
  113. });
  114. test("all valid minimumIntegerDigits options", () => {
  115. for (let i = 1; i <= 21; ++i) {
  116. expect(() => {
  117. new Intl.PluralRules("en", { minimumIntegerDigits: i });
  118. }).not.toThrow();
  119. }
  120. });
  121. test("all valid minimumFractionDigits options", () => {
  122. for (let i = 0; i <= 20; ++i) {
  123. expect(() => {
  124. new Intl.PluralRules("en", { minimumFractionDigits: i });
  125. }).not.toThrow();
  126. }
  127. });
  128. test("all valid maximumFractionDigits options", () => {
  129. for (let i = 0; i <= 20; ++i) {
  130. expect(() => {
  131. new Intl.PluralRules("en", { maximumFractionDigits: i });
  132. }).not.toThrow();
  133. }
  134. });
  135. test("all valid minimumSignificantDigits options", () => {
  136. for (let i = 1; i <= 21; ++i) {
  137. expect(() => {
  138. new Intl.PluralRules("en", { minimumSignificantDigits: i });
  139. }).not.toThrow();
  140. }
  141. });
  142. test("all valid maximumSignificantDigits options", () => {
  143. for (let i = 1; i <= 21; ++i) {
  144. expect(() => {
  145. new Intl.PluralRules("en", { maximumSignificantDigits: i });
  146. }).not.toThrow();
  147. }
  148. });
  149. });