Temporal: Move ZonedDateTime/prototype/round tests and one other ZonedDateTime test out of staging

This commit is contained in:
Tim Chevalier 2024-10-29 14:31:22 -07:00 committed by Philip Chimento
parent 70ef1acd1c
commit de68177f59
9 changed files with 314 additions and 250 deletions

View File

@ -0,0 +1,68 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: round() rounds to various increments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
// "1976-11-18T16:00:00+01:00[+01:00]"
const expectedHours = new Temporal.ZonedDateTime(217177200000000000n, "+01:00");
// "1976-11-18T15:30:00+01:00[+01:00]"
const expectedMinutes = new Temporal.ZonedDateTime(217175400000000000n, "+01:00");
// "1976-11-18T15:23:30+01:00[+01:00]"
const expectedSeconds = new Temporal.ZonedDateTime(217175010000000000n, "+01:00");
// "1976-11-18T15:23:30.12+01:00[+01:00]");
const expectedMilliseconds = new Temporal.ZonedDateTime(217175010120000000n, "+01:00");
// "1976-11-18T15:23:30.12346+01:00[+01:00]");
const expectedMicroseconds = new Temporal.ZonedDateTime(217175010123460000n, "+01:00");
// "1976-11-18T15:23:30.12345679+01:00[+01:00]");
const expectedNanoseconds = new Temporal.ZonedDateTime(217175010123456790n, "+01:00");
// "1976-11-19T00:00:00+01:00[+01:00]");
const expected1Day = new Temporal.ZonedDateTime(217206000000000000n, "+01:00");
// rounds to an increment of hours
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "hour",
roundingIncrement: 4
}), expectedHours);
// rounds to an increment of minutes
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "minute",
roundingIncrement: 15
}), expectedMinutes);
// rounds to an increment of seconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "second",
roundingIncrement: 30
}), expectedSeconds);
// rounds to an increment of milliseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 10
}), expectedMilliseconds);
// rounds to an increment of microseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 10
}), expectedMicroseconds);
// rounds to an increment of nanoseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}), expectedNanoseconds);
// 1 day is a valid increment
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "day",
roundingIncrement: 1
}), expected1Day);

View File

@ -0,0 +1,26 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: Test various smallestUnit values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// const bal = Temporal.ZonedDateTime.from("1976-11-18T23:59:59.999999999+01:00[+01:00]");
const bal = new Temporal.ZonedDateTime(217205999999999999n, "+01:00");
// "1976-11-19T00:00:00+01:00[+01:00]"
const expected = new Temporal.ZonedDateTime(217206000000000000n, "+01:00");
[
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond"
].forEach(smallestUnit => {
TemporalHelpers.assertZonedDateTimesEqual(bal.round( { smallestUnit }),
expected);
});

View File

@ -0,0 +1,66 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: Throws on invalid increments.
features: [Temporal]
---*/
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "day",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 24
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 1000
}));

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: Throws without parameter.
features: [Temporal]
---*/
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
assert.throws(TypeError, () => zdt.round());

View File

@ -0,0 +1,16 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: Throws without required smallestUnit parameter.
features: [Temporal]
---*/
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
assert.throws(RangeError, () => zdt.round({}));
assert.throws(RangeError, () => zdt.round({
roundingIncrement: 1,
roundingMode: "ceil"
}));

View File

@ -0,0 +1,80 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.round
description: Validity of increments depends on divisibility.
features: [Temporal]
---*/
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
// valid hour increments divide into 24
const smallestUnit = "hour";
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
[
"minute",
"second"
].forEach(smallestUnit => {
// valid minutes/seconds increments divide into 60`, () => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});
[
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
// valid increments divide into 1000`
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});

View File

@ -0,0 +1,46 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.prototype.since
description: Reversibility of differences.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
/*
const earlier = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789-03:00[-03:00]");
const later = Temporal.ZonedDateTime.from("2019-10-29T10:46:38.271986102-03:00[-03:00]");
*/
const earlier = new Temporal.ZonedDateTime(217189410123456789n, "-03:00");
const later = new Temporal.ZonedDateTime(1572356798271986102n, "-03:00");
[
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
const diff = later.since(earlier, { largestUnit });
TemporalHelpers.assertDurationsEqual(earlier.since(later, { largestUnit }),
diff.negated())
TemporalHelpers.assertDurationsEqual(earlier.until(later, { largestUnit }),
diff);
// difference symmetrical with regard to negative durations
assert(earlier.subtract(diff.negated()).equals(later));
assert(later.add(diff.negated()).equals(earlier));
});
[
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
const diff1 = earlier.until(later, { largestUnit });
const diff2 = later.since(earlier, { largestUnit });
assert(earlier.add(diff1).equals(later));
assert(later.subtract(diff2).equals(earlier));
});

View File

@ -1,37 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-zoneddatetime-objects
description: Reversibility of differences
features: [Temporal]
---*/
var earlier = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789-03:00[-03:00]");
var later = Temporal.ZonedDateTime.from("2019-10-29T10:46:38.271986102-03:00[-03:00]");
[
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
var diff = later.since(earlier, { largestUnit });
assert.sameValue(`${ earlier.since(later, { largestUnit }) }`, `${ diff.negated() }`);
assert.sameValue(`${ earlier.until(later, { largestUnit }) }`, `${ diff }`);
// difference symmetrical with regard to negative durations
assert(earlier.subtract(diff.negated()).equals(later));
assert(later.add(diff.negated()).equals(earlier));
});
[
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
var diff1 = earlier.until(later, { largestUnit });
var diff2 = later.since(earlier, { largestUnit });
assert(earlier.add(diff1).equals(later));
assert(later.subtract(diff2).equals(earlier));
});

View File

@ -1,213 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-zoneddatetime-objects
description: Temporal.ZonedDateTime.prototype.round()
features: [Temporal]
---*/
var zdt = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789+01:00[+01:00]");
// throws without parameter
assert.throws(TypeError, () => zdt.round());
// throws without required smallestUnit parameter
assert.throws(RangeError, () => zdt.round({}));
assert.throws(RangeError, () => zdt.round({
roundingIncrement: 1,
roundingMode: "ceil"
}));
// throws on disallowed or invalid smallestUnit (string param)
[
"era",
"year",
"month",
"week",
"years",
"months",
"weeks",
"nonsense"
].forEach(smallestUnit => {
assert.throws(RangeError, () => zdt.round(smallestUnit));
});
// rounds to an increment of hours
assert.sameValue(`${ zdt.round({
smallestUnit: "hour",
roundingIncrement: 4
}) }`, "1976-11-18T16:00:00+01:00[+01:00]");
// rounds to an increment of minutes
assert.sameValue(`${ zdt.round({
smallestUnit: "minute",
roundingIncrement: 15
}) }`, "1976-11-18T15:30:00+01:00[+01:00]");
// rounds to an increment of seconds
assert.sameValue(`${ zdt.round({
smallestUnit: "second",
roundingIncrement: 30
}) }`, "1976-11-18T15:23:30+01:00[+01:00]");
// rounds to an increment of milliseconds
assert.sameValue(`${ zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 10
}) }`, "1976-11-18T15:23:30.12+01:00[+01:00]");
// rounds to an increment of microseconds
assert.sameValue(`${ zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 10
}) }`, "1976-11-18T15:23:30.12346+01:00[+01:00]");
// rounds to an increment of nanoseconds
assert.sameValue(`${ zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}) }`, "1976-11-18T15:23:30.12345679+01:00[+01:00]");
// 1 day is a valid increment
assert.sameValue(`${ zdt.round({
smallestUnit: "day",
roundingIncrement: 1
}) }`, "1976-11-19T00:00:00+01:00[+01:00]");
// valid hour increments divide into 24
var smallestUnit = "hour";
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
[
"minute",
"second"
].forEach(smallestUnit => {
// valid minutes/seconds increments divide into 60`, () => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});
[
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
// valid increments divide into 1000`
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "day",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 24
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 1000
}));
var bal = Temporal.ZonedDateTime.from("1976-11-18T23:59:59.999999999+01:00[+01:00]");
[
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond"
].forEach(smallestUnit => {
assert.sameValue(`${ bal.round({ smallestUnit }) }`, "1976-11-19T00:00:00+01:00[+01:00]");
});