summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Temporal/Calendar/prototype/monthDayFromFields/overflow-constrain.js
blob: 072e99e3f7f4980be8327bebd314bec7ca92bfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.monthdayfromfields
description: Temporal.Calendar.prototype.monthDayFromFields will return correctly with data and overflow set to 'constrain'.
info: |
  1. Let calendar be the this value.
  2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
  3. Assert: calendar.[[Identifier]] is "iso8601".
  4. If Type(fields) is not Object, throw a TypeError exception.
  5. Set options to ? GetOptionsObject(options).
  6. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », « "day" »).
  7. Let overflow be ? ToTemporalOverflow(options).
  8. Perform ? ISOResolveMonth(fields).
  9. Let result be ? ISOMonthDayFromFields(fields, overflow).
  10. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], "iso8601", result.[[ReferenceISOYear]]).
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601");
const opt = { overflow: "constrain" };

let result = cal.monthDayFromFields({ year: 2021, month: 13, day: 1 }, opt);
TemporalHelpers.assertPlainMonthDay(result, "M12", 1, "month 13 is constrained to 12");

result = cal.monthDayFromFields({ year: 2021, month: 999999, day: 500 }, opt);
TemporalHelpers.assertPlainMonthDay(result, "M12", 31, "month 999999 is constrained to 12 and day 500 is constrained to 31");

[-99999, -1, 0].forEach((month) => {
  assert.throws(
    RangeError,
    () => cal.monthDayFromFields({ year: 2021, month, day: 1 }, opt),
    `Month ${month} is out of range for 2021 even with overflow: constrain`
  );
});

TemporalHelpers.ISOMonths.forEach(({ month, monthCode, daysInMonth }) => {
  const day = daysInMonth + 1;

  result = cal.monthDayFromFields({ month, day }, opt);
  TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth,
    `day is constrained from ${day} to ${daysInMonth} in month ${month}`);

  result = cal.monthDayFromFields({ month, day: 9001 }, opt);
  TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth,
    `day is constrained to ${daysInMonth} in month ${month}`);

  result = cal.monthDayFromFields({ monthCode, day }, opt);
  TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth,
    `day is constrained from ${day} to ${daysInMonth} in monthCode ${monthCode}`);

  result = cal.monthDayFromFields({ monthCode, day: 9001 }, opt);
  TemporalHelpers.assertPlainMonthDay(result, monthCode, daysInMonth,
    `day is constrained to ${daysInMonth} in monthCode ${monthCode}`);
});

[ ["month", 2], ["monthCode", "M02"] ].forEach(([ name, value ]) => {
  result = cal.monthDayFromFields({ year: 2020, [name]: value, day: 30 }, opt);
  TemporalHelpers.assertPlainMonthDay(result, "M02", 29, `${name} ${value} is constrained to 29 in leap year 2020`);

  result = cal.monthDayFromFields({ year: 2021, [name]: value, day: 29 }, opt);
  TemporalHelpers.assertPlainMonthDay(result, "M02", 28, `${name} ${value} is constrained to 28 in common year 2021`);
});

reportCompare(0, 0);