diff --git a/test/built-ins/Temporal/PlainDateTime/get-prototype-from-constructor-throws.js b/test/built-ins/Temporal/PlainDateTime/get-prototype-from-constructor-throws.js new file mode 100644 index 0000000000..9be9806ba9 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/get-prototype-from-constructor-throws.js @@ -0,0 +1,40 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime +description: > + OrdinaryCreateFromConstructor returns with an abrupt completion. +info: | + CreateTemporalDateTime ( isoDateTime, calendar [ , newTarget ] ) + + ... + 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, + "%Temporal.PlainDateTime.prototype%", « [[InitializedTemporalDateTime]], + [[ISODateTime]], [[Calendar]] »). + ... + + OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] ) + + ... + 2. Let proto be ? GetPrototypeFromConstructor(constructor, intrinsicDefaultProto). + ... + + GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto ) + + ... + 2. Let proto be ? Get(constructor, "prototype"). + ... + +features: [Temporal] +---*/ + +var newTarget = Object.defineProperty(function(){}.bind(), "prototype", { + get() { + throw new Test262Error(); + } +}); + +assert.throws(Test262Error, function() { + Reflect.construct(Temporal.PlainDateTime, [1970, 1, 1], newTarget) +}); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/add/throws-if-duration-days-too-large.js b/test/built-ins/Temporal/PlainDateTime/prototype/add/throws-if-duration-days-too-large.js new file mode 100644 index 0000000000..bcf703c4be --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/add/throws-if-duration-days-too-large.js @@ -0,0 +1,32 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.add +description: > + Throws duration days are too large. +info: | + Temporal.PlainDateTime.prototype.add ( temporalDurationLike [ , options ] ) + + ... + 3. Return ? AddDurationToDateTime(add, dateTime, temporalDurationLike, options). + + AddDurationToDateTime ( operation, dateTime, temporalDurationLike, options ) + + ... + 6. Let timeResult be AddTime(dateTime.[[ISODateTime]].[[Time]], internalDuration.[[Time]]). + 7. Let dateDuration be ? AdjustDateDurationRecord(internalDuration.[[Date]], timeResult.[[Days]]). + ... + +features: [Temporal] +---*/ + +const secondsPerDay = 24 * 60 * 60; +const maxSeconds = 2 ** 53 - 1; +const maxDays = Math.trunc(maxSeconds / secondsPerDay); +const maxHours = Math.trunc(((maxSeconds / secondsPerDay) % 1) * 24); + +let d = new Temporal.Duration(0, 0, 0, maxDays, maxHours); +let pdt = new Temporal.PlainDateTime(1970, 1, 1, 24 - maxHours); + +assert.throws(RangeError, () => pdt.add(d)); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/since/throws-if-rounded-date-outside-valid-iso-range.js b/test/built-ins/Temporal/PlainDateTime/prototype/since/throws-if-rounded-date-outside-valid-iso-range.js new file mode 100644 index 0000000000..4628a37b11 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/since/throws-if-rounded-date-outside-valid-iso-range.js @@ -0,0 +1,52 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.since +description: > + Throws if rounded date outside valid ISO date range. +info: | + Temporal.PlainDateTime.prototype.since ( other [ , options ] ) + + ... + 3. Return ? DifferenceTemporalPlainDateTime(since, dateTime, other, options). + + DifferenceTemporalPlainDateTime ( operation, dateTime, other, options ) + + ... + 6. Let internalDuration be ? DifferencePlainDateTimeWithRounding(dateTime.[[ISODateTime]], + other.[[ISODateTime]], dateTime.[[Calendar]], settings.[[LargestUnit]], + settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + ... + + DifferencePlainDateTimeWithRounding ( isoDateTime1, isoDateTime2, calendar, largestUnit, + roundingIncrement, smallestUnit, roundingMode ) + + ... + 5. Return ? RoundRelativeDuration(diff, destEpochNs, isoDateTime1, unset, calendar, + largestUnit, roundingIncrement, smallestUnit, roundingMode). + + RoundRelativeDuration ( duration, destEpochNs, isoDateTime, timeZone, calendar, + largestUnit, increment, smallestUnit, roundingMode ) + + ... + 5. If irregularLengthUnit is true, then + a. Let record be ? NudgeToCalendarUnit(sign, duration, destEpochNs, isoDateTime, + timeZone, calendar, increment, smallestUnit, roundingMode). + ... + + NudgeToCalendarUnit ( sign, duration, destEpochNs, isoDateTime, timeZone, calendar, + increment, unit, roundingMode ) + + ... + 8. Let end be ? CalendarDateAdd(calendar, isoDateTime.[[ISODate]], endDuration, constrain). + ... + +features: [Temporal] +---*/ + +var from = new Temporal.PlainDateTime(1970, 1, 1); +var to = new Temporal.PlainDateTime(1971, 1, 1); +var options = {roundingIncrement: 100_000_000, smallestUnit: "months"}; + +assert.throws(RangeError, () => from.since(to, options)); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/subtract/throws-if-duration-days-too-large.js b/test/built-ins/Temporal/PlainDateTime/prototype/subtract/throws-if-duration-days-too-large.js new file mode 100644 index 0000000000..44d818021d --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/subtract/throws-if-duration-days-too-large.js @@ -0,0 +1,32 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.subtract +description: > + Throws duration days are too large. +info: | + Temporal.PlainDateTime.prototype.subtract ( temporalDurationLike [ , options ] ) + + ... + 3. Return ? AddDurationToDateTime(subtract, dateTime, temporalDurationLike, options). + + AddDurationToDateTime ( operation, dateTime, temporalDurationLike, options ) + + ... + 6. Let timeResult be AddTime(dateTime.[[ISODateTime]].[[Time]], internalDuration.[[Time]]). + 7. Let dateDuration be ? AdjustDateDurationRecord(internalDuration.[[Date]], timeResult.[[Days]]). + ... + +features: [Temporal] +---*/ + +const secondsPerDay = 24 * 60 * 60; +const maxSeconds = 2 ** 53 - 1; +const maxDays = Math.trunc(maxSeconds / secondsPerDay); +const maxHours = Math.trunc(((maxSeconds / secondsPerDay) % 1) * 24); + +let d = new Temporal.Duration(0, 0, 0, -maxDays, -maxHours); +let pdt = new Temporal.PlainDateTime(1970, 1, 1, 24 - maxHours); + +assert.throws(RangeError, () => pdt.subtract(d)); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/until/throws-if-rounded-date-outside-valid-iso-range.js b/test/built-ins/Temporal/PlainDateTime/prototype/until/throws-if-rounded-date-outside-valid-iso-range.js new file mode 100644 index 0000000000..0a7f0258a2 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/until/throws-if-rounded-date-outside-valid-iso-range.js @@ -0,0 +1,52 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.until +description: > + Throws if rounded date outside valid ISO date range. +info: | + Temporal.PlainDateTime.prototype.until ( other [ , options ] ) + + ... + 3. Return ? DifferenceTemporalPlainDateTime(until, dateTime, other, options). + + DifferenceTemporalPlainDateTime ( operation, dateTime, other, options ) + + ... + 6. Let internalDuration be ? DifferencePlainDateTimeWithRounding(dateTime.[[ISODateTime]], + other.[[ISODateTime]], dateTime.[[Calendar]], settings.[[LargestUnit]], + settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + ... + + DifferencePlainDateTimeWithRounding ( isoDateTime1, isoDateTime2, calendar, largestUnit, + roundingIncrement, smallestUnit, roundingMode ) + + ... + 5. Return ? RoundRelativeDuration(diff, destEpochNs, isoDateTime1, unset, calendar, + largestUnit, roundingIncrement, smallestUnit, roundingMode). + + RoundRelativeDuration ( duration, destEpochNs, isoDateTime, timeZone, calendar, + largestUnit, increment, smallestUnit, roundingMode ) + + ... + 5. If irregularLengthUnit is true, then + a. Let record be ? NudgeToCalendarUnit(sign, duration, destEpochNs, isoDateTime, + timeZone, calendar, increment, smallestUnit, roundingMode). + ... + + NudgeToCalendarUnit ( sign, duration, destEpochNs, isoDateTime, timeZone, calendar, + increment, unit, roundingMode ) + + ... + 8. Let end be ? CalendarDateAdd(calendar, isoDateTime.[[ISODate]], endDuration, constrain). + ... + +features: [Temporal] +---*/ + +var from = new Temporal.PlainDateTime(1970, 1, 1); +var to = new Temporal.PlainDateTime(1971, 1, 1); +var options = {roundingIncrement: 100_000_000, smallestUnit: "months"}; + +assert.throws(RangeError, () => from.until(to, options)); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/with/throws-if-combined-date-time-outside-valid-iso-range.js b/test/built-ins/Temporal/PlainDateTime/prototype/with/throws-if-combined-date-time-outside-valid-iso-range.js new file mode 100644 index 0000000000..a77608f540 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/with/throws-if-combined-date-time-outside-valid-iso-range.js @@ -0,0 +1,19 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.with +description: > + Throws if combined date-time outside valid ISO date range. +info: | + Temporal.PlainDateTime.prototype.with ( temporalDateTimeLike [ , options ] ) + + ... + 17. Return ? CreateTemporalDateTime(result, calendar). +features: [Temporal] +---*/ + +var minDateTime = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1); +var zeroNanoseconds = {nanosecond: 0}; + +assert.throws(RangeError, () => minDateTime.with(zeroNanoseconds)); diff --git a/test/built-ins/Temporal/PlainDateTime/prototype/withPlainTime/throws-if-combined-date-time-outside-valid-iso-range.js b/test/built-ins/Temporal/PlainDateTime/prototype/withPlainTime/throws-if-combined-date-time-outside-valid-iso-range.js new file mode 100644 index 0000000000..51abded963 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/prototype/withPlainTime/throws-if-combined-date-time-outside-valid-iso-range.js @@ -0,0 +1,22 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime.prototype.withplaintime +description: > + Throws if combined date-time outside valid ISO date range. +info: | + Temporal.PlainDateTime.prototype.withPlainTime ( [ plainTimeLike ] ) + + 1. Let dateTime be the this value. + ... + 2. Let time be ? ToTimeRecordOrMidnight(plainTimeLike). + 4. Let isoDateTime be CombineISODateAndTimeRecord(dateTime.[[ISODateTime]].[[ISODate]], time). + 5. Return ? CreateTemporalDateTime(isoDateTime, dateTime.[[Calendar]]). +features: [Temporal] +---*/ + +var minDateTime = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1); +var midnight = new Temporal.PlainTime(); + +assert.throws(RangeError, () => minDateTime.withPlainTime(midnight)); diff --git a/test/built-ins/Temporal/PlainDateTime/throws-if-date-is-invalid.js b/test/built-ins/Temporal/PlainDateTime/throws-if-date-is-invalid.js new file mode 100644 index 0000000000..2b3e549d6b --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/throws-if-date-is-invalid.js @@ -0,0 +1,35 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime +description: > + Throws if any date value is outside the valid bounds. +info: | + Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute + [ , second [ , millisecond [ , microsecond + [ , nanosecond [ , calendar ] ] ] ] ] ] ] ) + + ... + 16. If IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception. + ... + +features: [Temporal] +---*/ + +var invalidArgs = [ + [1970, 0, 1], + [1970, 13, 1], + [1970, 1, 0], + [1970, 1, 32], + [1970, 2, 29], + [1972, 2, 30], +]; + +for (var args of invalidArgs) { + assert.throws( + RangeError, + () => new Temporal.PlainDateTime(...args), + `args = ${JSON.stringify(args)}` + ); +} diff --git a/test/built-ins/Temporal/PlainDateTime/throws-if-time-is-invalid.js b/test/built-ins/Temporal/PlainDateTime/throws-if-time-is-invalid.js new file mode 100644 index 0000000000..d13fdac191 --- /dev/null +++ b/test/built-ins/Temporal/PlainDateTime/throws-if-time-is-invalid.js @@ -0,0 +1,42 @@ +// Copyright (C) 2024 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.plaindatetime +description: > + Throws if any time value is outside the valid bounds. +info: | + Temporal.PlainDateTime ( isoYear, isoMonth, isoDay [ , hour [ , minute + [ , second [ , millisecond [ , microsecond + [ , nanosecond [ , calendar ] ] ] ] ] ] ] ) + + ... + 16. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) + is false, throw a RangeError exception. + ... + +features: [Temporal] +---*/ + +var invalidArgs = [ + [-1], + [24], + [0, -1], + [0, 60], + [0, 0, -1], + [0, 0, 60], + [0, 0, 0, -1], + [0, 0, 0, 1000], + [0, 0, 0, 0, -1], + [0, 0, 0, 0, 1000], + [0, 0, 0, 0, 0, -1], + [0, 0, 0, 0, 0, 1000], +]; + +for (var args of invalidArgs) { + assert.throws( + RangeError, + () => new Temporal.PlainDateTime(1970, 1, 1, ...args), + `args = ${JSON.stringify(args)}` + ); +}