mirror of
https://github.com/tc39/test262.git
synced 2025-04-08 19:35:28 +02:00
Temporal: Add some tests for Duration#round.
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
This commit is contained in:
parent
69fd39b463
commit
7099acc882
38
test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js
vendored
Normal file
38
test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-default.js
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
// 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.duration.prototype.round
|
||||
description: assumes a different default for largestUnit if smallestUnit is larger than the default
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const relativeTo = Temporal.PlainDate.from("2020-01-01");
|
||||
const almostYear = Temporal.Duration.from({ days: 364 });
|
||||
TemporalHelpers.assertDuration(almostYear.round({ smallestUnit: "years", relativeTo }),
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
|
||||
const almostMonth = Temporal.Duration.from({ days: 27 });
|
||||
TemporalHelpers.assertDuration(almostMonth.round({ smallestUnit: "months", relativeTo }),
|
||||
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "months");
|
||||
const almostWeek = Temporal.Duration.from({ days: 6 });
|
||||
TemporalHelpers.assertDuration(almostWeek.round({ smallestUnit: "weeks", relativeTo }),
|
||||
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "weeks");
|
||||
const almostDay = Temporal.Duration.from({ seconds: 86399 });
|
||||
TemporalHelpers.assertDuration(almostDay.round({ smallestUnit: "days" }),
|
||||
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "days");
|
||||
const almostHour = Temporal.Duration.from({ seconds: 3599 });
|
||||
TemporalHelpers.assertDuration(almostHour.round({ smallestUnit: "hours" }),
|
||||
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "hours");
|
||||
const almostMinute = Temporal.Duration.from({ seconds: 59 });
|
||||
TemporalHelpers.assertDuration(almostMinute.round({ smallestUnit: "minutes" }),
|
||||
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "minutes");
|
||||
const almostSecond = Temporal.Duration.from({ nanoseconds: 999999999 });
|
||||
TemporalHelpers.assertDuration(almostSecond.round({ smallestUnit: "seconds" }),
|
||||
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "seconds");
|
||||
const almostMillisecond = Temporal.Duration.from({ nanoseconds: 999999 });
|
||||
TemporalHelpers.assertDuration(almostMillisecond.round({ smallestUnit: "milliseconds" }),
|
||||
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "milliseconds");
|
||||
const almostMicrosecond = Temporal.Duration.from({ nanoseconds: 999 });
|
||||
TemporalHelpers.assertDuration(almostMicrosecond.round({ smallestUnit: "microseconds" }),
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "microseconds");
|
20
test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js
vendored
Normal file
20
test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-mismatch.js
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
// 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.duration.prototype.round
|
||||
description: RangeError thrown when smallestUnit is larger than largestUnit
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const d = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
|
||||
const relativeTo = Temporal.PlainDate.from('2020-01-01');
|
||||
const units = ["years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"];
|
||||
for (let largestIdx = 1; largestIdx < units.length; largestIdx++) {
|
||||
for (let smallestIdx = 0; smallestIdx < largestIdx; smallestIdx++) {
|
||||
const largestUnit = units[largestIdx];
|
||||
const smallestUnit = units[smallestIdx];
|
||||
assert.throws(RangeError, () => d.round({ largestUnit, smallestUnit, relativeTo }),
|
||||
`${smallestUnit} > ${largestUnit}`);
|
||||
}
|
||||
}
|
34
test/built-ins/Temporal/Duration/prototype/round/smallestunit.js
vendored
Normal file
34
test/built-ins/Temporal/Duration/prototype/round/smallestunit.js
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
// 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.duration.prototype.round
|
||||
description: smallestUnit should be taken into account
|
||||
includes: [temporalHelpers.js]
|
||||
features: [Temporal]
|
||||
---*/
|
||||
|
||||
const d = Temporal.Duration.from({
|
||||
days: 1,
|
||||
hours: 2,
|
||||
minutes: 3,
|
||||
seconds: 4,
|
||||
milliseconds: 5,
|
||||
microseconds: 6,
|
||||
nanoseconds: 7
|
||||
});
|
||||
const tests = {
|
||||
'day': [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
||||
'hour': [0, 0, 0, 1, 2, 0, 0, 0, 0, 0],
|
||||
'minute': [0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
|
||||
'second': [0, 0, 0, 1, 2, 3, 4, 0, 0, 0],
|
||||
'millisecond': [0, 0, 0, 1, 2, 3, 4, 5, 0, 0],
|
||||
'microsecond': [0, 0, 0, 1, 2, 3, 4, 5, 6, 0],
|
||||
'nanosecond': [0, 0, 0, 1, 2, 3, 4, 5, 6, 7],
|
||||
};
|
||||
for (const [smallestUnit, expected] of Object.entries(tests)) {
|
||||
TemporalHelpers.assertDuration(d.round(smallestUnit), ...expected,
|
||||
`"${smallestUnit}" should work as argument`);
|
||||
TemporalHelpers.assertDuration(d.round({ smallestUnit }), ...expected,
|
||||
`"${smallestUnit}" should work in option bag`);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user