Temporal: Coverage for Temporal.PlainDateTime

This commit is contained in:
André Bargull 2024-11-19 16:30:20 +01:00 committed by Philip Chimento
parent 22539853cd
commit 4ceee16fff
9 changed files with 326 additions and 0 deletions

View File

@ -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)
});

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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)}`
);
}

View File

@ -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)}`
);
}