Duration.from.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const expectDurationOneToTen = duration => {
  2. expect(duration.years).toBe(1);
  3. expect(duration.months).toBe(2);
  4. expect(duration.weeks).toBe(3);
  5. expect(duration.days).toBe(4);
  6. expect(duration.hours).toBe(5);
  7. expect(duration.minutes).toBe(6);
  8. expect(duration.seconds).toBe(7);
  9. expect(duration.milliseconds).toBe(8);
  10. expect(duration.microseconds).toBe(9);
  11. expect(duration.nanoseconds).toBe(10);
  12. };
  13. describe("correct behavior", () => {
  14. test("length is 1", () => {
  15. expect(Temporal.Duration.from).toHaveLength(1);
  16. });
  17. test("Duration instance argument", () => {
  18. const duration = Temporal.Duration.from(
  19. new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  20. );
  21. expectDurationOneToTen(duration);
  22. });
  23. test("Duration-like object argument", () => {
  24. const duration = Temporal.Duration.from({
  25. years: 1,
  26. months: 2,
  27. weeks: 3,
  28. days: 4,
  29. hours: 5,
  30. minutes: 6,
  31. seconds: 7,
  32. milliseconds: 8,
  33. microseconds: 9,
  34. nanoseconds: 10,
  35. });
  36. expectDurationOneToTen(duration);
  37. });
  38. test("Duration string argument", () => {
  39. const duration = Temporal.Duration.from("P1Y2M3W4DT5H6M7.008009010S");
  40. expectDurationOneToTen(duration);
  41. });
  42. });
  43. describe("errors", () => {
  44. test("Invalid duration-like object", () => {
  45. expect(() => {
  46. Temporal.Duration.from({});
  47. }).toThrowWithMessage(TypeError, "Invalid duration-like object");
  48. });
  49. test("Invalid duration property value", () => {
  50. expect(() => {
  51. Temporal.Duration.from({ years: 1.23 });
  52. }).toThrowWithMessage(
  53. RangeError,
  54. "Invalid value for duration property 'years': must be an integer, got 1.2" // ...29999999999999 - let's not include that in the test :^)
  55. );
  56. expect(() => {
  57. Temporal.Duration.from({ years: "foo" });
  58. }).toThrowWithMessage(
  59. RangeError,
  60. "Invalid value for duration property 'years': must be an integer, got foo"
  61. );
  62. expect(() => {
  63. Temporal.Duration.from({ years: NaN });
  64. }).toThrowWithMessage(
  65. RangeError,
  66. "Invalid value for duration property 'years': must be an integer, got NaN"
  67. );
  68. });
  69. test("invalid duration string", () => {
  70. expect(() => {
  71. Temporal.Duration.from("foo");
  72. }).toThrowWithMessage(RangeError, "Invalid duration string 'foo'");
  73. });
  74. test("invalid duration string: fractional hours proceeded by minutes or seconds", () => {
  75. const values = [
  76. "PT1.23H1M",
  77. "PT1.23H1.23M",
  78. "PT1.23H1S",
  79. "PT1.23H1.23S",
  80. "PT1.23H1M1S",
  81. "PT1.23H1M1.23S",
  82. "PT1.23H1.23M1S",
  83. "PT1.23H1.23M1.23S",
  84. ];
  85. for (const value of values) {
  86. expect(() => {
  87. Temporal.Duration.from(value);
  88. }).toThrowWithMessage(RangeError, `Invalid duration string '${value}'`);
  89. }
  90. });
  91. test("invalid duration string: fractional minutes proceeded by seconds", () => {
  92. const values = ["PT1.23M1S", "PT1.23M1.23S"];
  93. for (const value of values) {
  94. expect(() => {
  95. Temporal.Duration.from(value);
  96. }).toThrowWithMessage(RangeError, `Invalid duration string '${value}'`);
  97. }
  98. });
  99. test("invalid duration string: exceed duration limits", () => {
  100. const values = [
  101. "P4294967296Y", // abs(years) >= 2**32
  102. "P4294967296M", // abs(months) >= 2**32
  103. "P4294967296W", // abs(weeks) >= 2**32
  104. "P104249991375D", // days >= 2*53 seconds
  105. "PT2501999792984H", // hours >= 2*53 seconds
  106. "PT150119987579017M", // minutes >= 2*53 seconds
  107. "PT9007199254740992S", // seconds >= 2*53 seconds
  108. ];
  109. for (const value of values) {
  110. expect(() => {
  111. Temporal.Duration.from(value);
  112. }).toThrowWithMessage(RangeError, `Invalid duration`);
  113. expect(() => {
  114. Temporal.Duration.from("-" + value);
  115. }).toThrowWithMessage(RangeError, `Invalid duration`);
  116. }
  117. });
  118. });