Port tests for PlainYearMonth#{since,until}.

This commit is contained in:
Ms2ger 2022-02-03 18:01:01 +01:00 committed by Rick Waldron
parent 8848ee91e8
commit f98a00bc69
18 changed files with 350 additions and 6 deletions

View File

@ -20,3 +20,8 @@ TemporalHelpers.assertDuration(later.since(earlier, {}),
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (implicit, pos)");
TemporalHelpers.assertDuration(earlier.since(later, {}),
-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (implicit, neg)");
TemporalHelpers.assertDuration(later.since(earlier, () => {}),
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (arrow function, pos)");
TemporalHelpers.assertDuration(earlier.since(later, () => {}),
-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (arrow function, neg)");

View File

@ -11,11 +11,14 @@ features: [Temporal]
const earlier = new Temporal.PlainYearMonth(2019, 1);
const later = new Temporal.PlainYearMonth(2021, 9);
const laterSinceYear = later.since(earlier, { smallestUnit: 'years', roundingIncrement: 4, roundingMode: 'halfExpand' });
TemporalHelpers.assertDurationsEqual(laterSinceYear, Temporal.Duration.from('P4Y'), 'rounds to an increment of years');
const laterSinceYear = later.since(earlier, { smallestUnit: "years", roundingIncrement: 4, roundingMode: "halfExpand" });
TemporalHelpers.assertDuration(laterSinceYear,
/* years = */ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of years");
const laterSinceMixed = later.since(earlier, { smallestUnit: 'months', roundingIncrement: 5 });
TemporalHelpers.assertDurationsEqual(laterSinceMixed, Temporal.Duration.from('P2Y5M'), 'rounds to an increment of months mixed with years');
const laterSinceMixed = later.since(earlier, { smallestUnit: "months", roundingIncrement: 5 });
TemporalHelpers.assertDuration(laterSinceMixed,
/* years = */ 2, /* months = */ 5, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of months mixed with years");
const laterSinceMonth = later.since(earlier, { largestUnit: 'months', smallestUnit: 'months', roundingIncrement: 10 });
TemporalHelpers.assertDurationsEqual(laterSinceMonth, Temporal.Duration.from('P30M'), 'rounds to an increment of pure months');
const laterSinceMonth = later.since(earlier, { largestUnit: "months", smallestUnit: "months", roundingIncrement: 10 });
TemporalHelpers.assertDuration(laterSinceMonth,
0, /* months = */ 30, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of pure months");

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.plainyearmonth.prototype.since
description: Tests calculations with roundingMode "ceil".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "years", roundingMode: "ceil" }),
/* years = */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "years", roundingMode: "ceil" }),
/* years = */ -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "months", roundingMode: "ceil" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "months", roundingMode: "ceil" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.since
description: Tests calculations with roundingMode "floor".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "years", roundingMode: "floor" }),
/* years = */ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "years", roundingMode: "floor" }),
/* years = */ -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "months", roundingMode: "floor" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "months", roundingMode: "floor" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.since
description: Tests calculations with roundingMode "halfExpand".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "years", roundingMode: "halfExpand" }),
/* years = */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "years", roundingMode: "halfExpand" }),
/* years = */ -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "months", roundingMode: "halfExpand" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "months", roundingMode: "halfExpand" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.since
description: Tests calculations with roundingMode "trunc".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "years", roundingMode: "trunc" }),
/* years = */ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "years", roundingMode: "trunc" }),
/* years = */ -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.since(earlier, { smallestUnit: "months", roundingMode: "trunc" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
earlier.since(later, { smallestUnit: "months", roundingMode: "trunc" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.until
description: Calls to PYM.until cast arguments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const nov94 = new Temporal.PlainYearMonth(1994, 11);
const jun13 = new Temporal.PlainYearMonth(2013, 6);
const diff = nov94.until(jun13);
TemporalHelpers.assertDurationsEqual(nov94.until({ year: 2013, month: 6 }), diff, "Casts object argument");
TemporalHelpers.assertDurationsEqual(nov94.until("2013-06"), diff, "Casts string argument");

View File

@ -0,0 +1,13 @@
// 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.plainyearmonth.prototype.until
description: Calls to PYM.until throw when missing required arguments.
features: [Temporal]
---*/
const jun13 = new Temporal.PlainYearMonth(2013, 6);
assert.throws(TypeError, () => jun13.until({ year: 1994 }), 'Throws when missing required month');
assert.throws(TypeError, () => jun13.until({ month: 11 }), 'Throws when missing required year');

View File

@ -0,0 +1,25 @@
// 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.plainyearmonth.prototype.until
description: Until throws on to0-small largestUnit
features: [Temporal, arrow-function]
---*/
const earlier = new Temporal.PlainYearMonth(2019, 1);
const later = new Temporal.PlainYearMonth(2021, 9);
[
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds"
].forEach((largestUnit) => {
assert.throws(RangeError, () => earlier.until(later, { largestUnit }),
`throws on disallowed or invalid largestUnit: ${largestUnit}`);
});

View File

@ -20,3 +20,8 @@ TemporalHelpers.assertDuration(earlier.until(later, {}),
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (implicit, pos)");
TemporalHelpers.assertDuration(later.until(earlier, {}),
-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (implicit, neg)");
TemporalHelpers.assertDuration(earlier.until(later, () => {}),
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (arrow function, pos)");
TemporalHelpers.assertDuration(later.until(earlier, () => {}),
-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, "default largestUnit is year (arrow function, neg)");

View File

@ -0,0 +1,23 @@
// 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.plainyearmonth.prototype.until
description: Mixed calendars throw as invalid
features: [Temporal]
---*/
class customCal extends Temporal.Calendar {
constructor () {
super('iso8601');
}
toString() {
return "I am a secret cal.";
}
}
const ym1 = new Temporal.PlainYearMonth(2000, 1);
const ym2 = new Temporal.PlainYearMonth(2000, 1, new customCal());
assert.throws(RangeError, () => ym1.until(ym2), 'until throws with different calendars');

View File

@ -0,0 +1,22 @@
// 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.plainyearmonth.prototype.until
description: Verify that invalid options are handled correctly.
features: [Temporal]
---*/
const feb20 = new Temporal.PlainYearMonth(2020, 2);
const feb21 = new Temporal.PlainYearMonth(2021, 2);
[
null,
1,
"hello",
true,
Symbol("foo"),
1n
].forEach((badOption) =>
assert.throws(TypeError, () => feb20.until(feb21, badOption), `${String(badOption)} throws TypeError`)
);

View File

@ -0,0 +1,24 @@
// 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.plainyearmonth.prototype.until
description: Until rounding increments work as expected
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = new Temporal.PlainYearMonth(2019, 1);
const later = new Temporal.PlainYearMonth(2021, 9);
const laterSinceYear = earlier.until(later, { smallestUnit: "years", roundingIncrement: 4, roundingMode: "halfExpand" });
TemporalHelpers.assertDuration(laterSinceYear,
/* years = */ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of years");
const laterSinceMixed = earlier.until(later, { smallestUnit: "months", roundingIncrement: 5 });
TemporalHelpers.assertDuration(laterSinceMixed,
/* years = */ 2, /* months = */ 5, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of months mixed with years");
const laterSinceMonth = earlier.until(later, { largestUnit: "months", smallestUnit: "months", roundingIncrement: 10 });
TemporalHelpers.assertDuration(laterSinceMonth,
0, /* months = */ 30, 0, 0, 0, 0, 0, 0, 0, 0, "rounds to an increment of pure months");

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.plainyearmonth.prototype.until
description: Tests calculations with roundingMode "ceil".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "years", roundingMode: "ceil" }),
/* years = */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "years", roundingMode: "ceil" }),
/* years = */ -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "months", roundingMode: "ceil" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "months", roundingMode: "ceil" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.until
description: Tests calculations with roundingMode "floor".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "years", roundingMode: "floor" }),
/* years = */ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "years", roundingMode: "floor" }),
/* years = */ -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "months", roundingMode: "floor" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "months", roundingMode: "floor" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.until
description: Tests calculations with roundingMode "halfExpand".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "years", roundingMode: "halfExpand" }),
/* years = */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "years", roundingMode: "halfExpand" }),
/* years = */ -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "months", roundingMode: "halfExpand" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "months", roundingMode: "halfExpand" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");

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.plainyearmonth.prototype.until
description: Tests calculations with roundingMode "trunc".
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.PlainYearMonth.from("2019-01");
const later = Temporal.PlainYearMonth.from("2021-09");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "years", roundingMode: "trunc" }),
/* years = */ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "years", roundingMode: "trunc" }),
/* years = */ -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "years");
TemporalHelpers.assertDuration(
earlier.until(later, { smallestUnit: "months", roundingMode: "trunc" }),
/* years = */ 2, /* months = */ 8, 0, 0, 0, 0, 0, 0, 0, 0, "months");
TemporalHelpers.assertDuration(
later.until(earlier, { smallestUnit: "months", roundingMode: "trunc" }),
/* years = */ -2, /* months = */ -8, 0, 0, 0, 0, 0, 0, 0, 0, "months");