Duration.from.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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("NaN value becomes zero", () => {
  39. // NOTE: NaN does *not* throw a RangeError anymore - which is questionable, IMO - as of:
  40. // https://github.com/tc39/proposal-temporal/commit/8c854507a52efbc6e9eb2642f0f928df38e5c021
  41. const duration = Temporal.Duration.from({ years: "foo" });
  42. expect(duration.years).toBe(0);
  43. });
  44. test("Duration string argument", () => {
  45. // FIXME: yes, this needs 11 instead of 10 for nanoseconds for the test to pass.
  46. // See comment in parse_temporal_duration_string().
  47. const duration = Temporal.Duration.from("P1Y2M3W4DT5H6M7.008009011S");
  48. expectDurationOneToTen(duration);
  49. });
  50. });
  51. describe("errors", () => {
  52. test("Invalid duration-like object", () => {
  53. expect(() => {
  54. Temporal.Duration.from({});
  55. }).toThrowWithMessage(TypeError, "Invalid duration-like object");
  56. });
  57. test("Invalid duration property value", () => {
  58. expect(() => {
  59. Temporal.Duration.from({ years: 1.23 });
  60. }).toThrowWithMessage(
  61. RangeError,
  62. "Invalid value for duration property 'years': must be an integer, got 1.2" // ...29999999999999 - let's not include that in the test :^)
  63. );
  64. });
  65. test("invalid duration string", () => {
  66. expect(() => {
  67. Temporal.Duration.from("foo");
  68. }).toThrowWithMessage(RangeError, "Invalid duration string 'foo'");
  69. });
  70. test("invalid duration string: fractional hours proceeded by minutes or seconds", () => {
  71. const values = [
  72. "PT1.23H1M",
  73. "PT1.23H1.23M",
  74. "PT1.23H1S",
  75. "PT1.23H1.23S",
  76. "PT1.23H1M1S",
  77. "PT1.23H1M1.23S",
  78. "PT1.23H1.23M1S",
  79. "PT1.23H1.23M1.23S",
  80. ];
  81. for (const value of values) {
  82. expect(() => {
  83. Temporal.Duration.from(value);
  84. }).toThrowWithMessage(
  85. RangeError,
  86. `Invalid duration string '${value}': fractional hours must not be proceeded by minutes or seconds`
  87. );
  88. }
  89. });
  90. test("invalid duration string: fractional minutes proceeded by seconds", () => {
  91. const values = ["PT1.23M1S", "PT1.23M1.23S"];
  92. for (const value of values) {
  93. expect(() => {
  94. Temporal.Duration.from(value);
  95. }).toThrowWithMessage(
  96. RangeError,
  97. `Invalid duration string '${value}': fractional minutes must not be proceeded by seconds`
  98. );
  99. }
  100. });
  101. });