PlainYearMonth.from.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.PlainYearMonth.from).toHaveLength(1);
  4. });
  5. test("PlainYearMonth instance argument", () => {
  6. const plainYearMonth_ = new Temporal.PlainYearMonth(2021, 7);
  7. const plainYearMonth = Temporal.PlainYearMonth.from(plainYearMonth_);
  8. expect(plainYearMonth.year).toBe(2021);
  9. expect(plainYearMonth.month).toBe(7);
  10. expect(plainYearMonth.monthCode).toBe("M07");
  11. expect(plainYearMonth.daysInYear).toBe(365);
  12. expect(plainYearMonth.daysInMonth).toBe(31);
  13. expect(plainYearMonth.monthsInYear).toBe(12);
  14. expect(plainYearMonth.inLeapYear).toBeFalse();
  15. });
  16. test("fields object argument", () => {
  17. const object = {
  18. year: 2021,
  19. month: 7,
  20. };
  21. const plainYearMonth = Temporal.PlainYearMonth.from(object);
  22. expect(plainYearMonth.year).toBe(2021);
  23. expect(plainYearMonth.month).toBe(7);
  24. expect(plainYearMonth.monthCode).toBe("M07");
  25. expect(plainYearMonth.daysInYear).toBe(365);
  26. expect(plainYearMonth.daysInMonth).toBe(31);
  27. expect(plainYearMonth.monthsInYear).toBe(12);
  28. expect(plainYearMonth.inLeapYear).toBeFalse();
  29. });
  30. test("from year month string", () => {
  31. const plainYearMonth = Temporal.PlainYearMonth.from("2021-07");
  32. expect(plainYearMonth.year).toBe(2021);
  33. expect(plainYearMonth.month).toBe(7);
  34. expect(plainYearMonth.monthCode).toBe("M07");
  35. });
  36. test("from date time string", () => {
  37. const plainYearMonth = Temporal.PlainYearMonth.from("2021-07-06T23:42:01");
  38. expect(plainYearMonth.year).toBe(2021);
  39. expect(plainYearMonth.month).toBe(7);
  40. expect(plainYearMonth.monthCode).toBe("M07");
  41. expect(plainYearMonth.daysInYear).toBe(365);
  42. expect(plainYearMonth.daysInMonth).toBe(31);
  43. expect(plainYearMonth.monthsInYear).toBe(12);
  44. expect(plainYearMonth.inLeapYear).toBeFalse();
  45. });
  46. test("compares calendar name in year month string in lowercase", () => {
  47. const values = [
  48. "2023-02[u-ca=iso8601]",
  49. "2023-02[u-ca=isO8601]",
  50. "2023-02[u-ca=iSo8601]",
  51. "2023-02[u-ca=iSO8601]",
  52. "2023-02[u-ca=Iso8601]",
  53. "2023-02[u-ca=IsO8601]",
  54. "2023-02[u-ca=ISo8601]",
  55. "2023-02[u-ca=ISO8601]",
  56. ];
  57. for (const value of values) {
  58. expect(() => {
  59. Temporal.PlainYearMonth.from(value);
  60. }).not.toThrowWithMessage(
  61. RangeError,
  62. "YYYY-MM string format can only be used with the iso8601 calendar"
  63. );
  64. }
  65. });
  66. });
  67. describe("errors", () => {
  68. test("missing fields", () => {
  69. expect(() => {
  70. Temporal.PlainYearMonth.from({});
  71. }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
  72. expect(() => {
  73. Temporal.PlainYearMonth.from({ year: 0 });
  74. }).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
  75. expect(() => {
  76. Temporal.PlainYearMonth.from({ month: 1 });
  77. }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
  78. });
  79. test("invalid year month string", () => {
  80. expect(() => {
  81. Temporal.PlainYearMonth.from("foo");
  82. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  83. });
  84. test("string must not contain a UTC designator", () => {
  85. expect(() => {
  86. Temporal.PlainYearMonth.from("2021-07-06T23:42:01Z");
  87. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  88. });
  89. test("extended year must not be negative zero", () => {
  90. expect(() => {
  91. Temporal.PlainYearMonth.from("-000000-01");
  92. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  93. expect(() => {
  94. Temporal.PlainYearMonth.from("−000000-01"); // U+2212
  95. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  96. });
  97. test("can only use iso8601 calendar with year month strings", () => {
  98. expect(() => {
  99. Temporal.PlainYearMonth.from("2023-02[u-ca=iso8602]");
  100. }).toThrowWithMessage(RangeError, "Invalid calendar identifier 'iso8602'");
  101. expect(() => {
  102. Temporal.PlainYearMonth.from("2023-02[u-ca=ladybird]");
  103. }).toThrowWithMessage(RangeError, "Invalid calendar identifier 'ladybird'");
  104. });
  105. test("doesn't throw non-iso8601 calendar error when using a superset format string such as DateTime", () => {
  106. // NOTE: This will still throw, but only because "ladybird" is not a recognised calendar, not because of the string format restriction.
  107. try {
  108. Temporal.PlainYearMonth.from("2023-02-10T22:57[u-ca=ladybird]");
  109. } catch (e) {
  110. expect(e).toBeInstanceOf(RangeError);
  111. expect(e.message).not.toBe(
  112. "MM-DD string format can only be used with the iso8601 calendar"
  113. );
  114. expect(e.message).toBe("Invalid calendar identifier 'ladybird'");
  115. }
  116. });
  117. });