Skip to content

Commit

Permalink
Temporal: Port some Duration.compare tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger authored and ptomato committed Apr 29, 2022
1 parent dcd25e6 commit b649e6b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/built-ins/Temporal/Duration/compare/argument-cast.js
Original file line number Diff line number Diff line change
@@ -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.duration.compare
description: Strings and objects are supported arguments.
features: [Temporal]
---*/

assert.sameValue(Temporal.Duration.compare("PT12H", new Temporal.Duration()), 1,
"first argument string");
assert.sameValue(Temporal.Duration.compare({ hours: 12 }, new Temporal.Duration()), 1,
"first argument object");
assert.throws(TypeError, () => Temporal.Duration.compare({ hour: 12 }, new Temporal.Duration()),
"first argument missing property");

assert.sameValue(Temporal.Duration.compare(new Temporal.Duration(), "PT12H"), -1,
"second argument string");
assert.sameValue(Temporal.Duration.compare(new Temporal.Duration(), { hours: 12 }), -1,
"second argument object");
assert.throws(TypeError, () => Temporal.Duration.compare(new Temporal.Duration(), { hour: 12 }),
"second argument missing property");

assert.sameValue(Temporal.Duration.compare({ hours: 12, minute: 5 }, { hours: 12, day: 5 }), 0,
"ignores incorrect properties");

53 changes: 53 additions & 0 deletions test/built-ins/Temporal/Duration/compare/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.compare
description: Basic comparisons.
features: [Temporal]
---*/

const td1pos = new Temporal.Duration(0, 0, 0, 0, 5, 5, 5, 5, 5, 5);
const td2pos = new Temporal.Duration(0, 0, 0, 0, 5, 4, 5, 5, 5, 5);
const td1neg = new Temporal.Duration(0, 0, 0, 0, -5, -5, -5, -5, -5, -5);
const td2neg = new Temporal.Duration(0, 0, 0, 0, -5, -4, -5, -5, -5, -5);
assert.sameValue(Temporal.Duration.compare(td1pos, td1pos), 0,
"time units: equal");
assert.sameValue(Temporal.Duration.compare(td2pos, td1pos), -1,
"time units: smaller/larger");
assert.sameValue(Temporal.Duration.compare(td1pos, td2pos), 1,
"time units: larger/smaller");
assert.sameValue(Temporal.Duration.compare(td1neg, td1neg), 0,
"time units: negative/negative equal");
assert.sameValue(Temporal.Duration.compare(td2neg, td1neg), 1,
"time units: negative/negative smaller/larger");
assert.sameValue(Temporal.Duration.compare(td1neg, td2neg), -1,
"time units: negative/negative larger/smaller");
assert.sameValue(Temporal.Duration.compare(td1neg, td2pos), -1,
"time units: negative/positive");
assert.sameValue(Temporal.Duration.compare(td1pos, td2neg), 1,
"time units: positive/negative");

const dd1pos = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
const dd2pos = new Temporal.Duration(5, 5, 5, 5, 5, 4, 5, 5, 5, 5);
const dd1neg = new Temporal.Duration(-5, -5, -5, -5, -5, -5, -5, -5, -5, -5);
const dd2neg = new Temporal.Duration(-5, -5, -5, -5, -5, -4, -5, -5, -5, -5);
const relativeTo = Temporal.PlainDate.from("2017-01-01");
assert.throws(RangeError, () => Temporal.Duration.compare(dd1pos, dd2pos),
"date units: relativeTo is required");
assert.sameValue(Temporal.Duration.compare(dd1pos, dd1pos, { relativeTo }), 0,
"date units: equal");
assert.sameValue(Temporal.Duration.compare(dd2pos, dd1pos, { relativeTo }), -1,
"date units: smaller/larger");
assert.sameValue(Temporal.Duration.compare(dd1pos, dd2pos, { relativeTo }), 1,
"date units: larger/smaller");
assert.sameValue(Temporal.Duration.compare(dd1neg, dd1neg, { relativeTo }), 0,
"date units: negative/negative equal");
assert.sameValue(Temporal.Duration.compare(dd2neg, dd1neg, { relativeTo }), 1,
"date units: negative/negative smaller/larger");
assert.sameValue(Temporal.Duration.compare(dd1neg, dd2neg, { relativeTo }), -1,
"date units: negative/negative larger/smaller");
assert.sameValue(Temporal.Duration.compare(dd1neg, dd2pos, { relativeTo }), -1,
"date units: negative/positive");
assert.sameValue(Temporal.Duration.compare(dd1pos, dd2neg, { relativeTo }), 1,
"date units: positive/negative");
24 changes: 24 additions & 0 deletions test/built-ins/Temporal/Duration/compare/relativeto-hour.js
Original file line number Diff line number Diff line change
@@ -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.duration.compare
description: relativeTo with hours.
features: [Temporal]
---*/

const oneDay = new Temporal.Duration(0, 0, 0, 1);
const hours24 = new Temporal.Duration(0, 0, 0, 0, 24);
assert.sameValue(Temporal.Duration.compare(oneDay, hours24),
0,
"relativeTo not required for days");
assert.sameValue(
Temporal.Duration.compare(oneDay, hours24, { relativeTo: Temporal.PlainDate.from("2017-01-01") }),
0,
"relativeTo does not affect days if PlainDate");
assert.sameValue(Temporal.Duration.compare(oneDay, hours24, { relativeTo: "2019-11-03" }),
0,
"casts relativeTo to PlainDate from string");
assert.sameValue(Temporal.Duration.compare(oneDay, hours24, { relativeTo: { year: 2019, month: 11, day: 3 } }),
0,
"casts relativeTo to PlainDate from object");
17 changes: 17 additions & 0 deletions test/built-ins/Temporal/Duration/compare/relativeto-month.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.compare
description: relativeTo with months.
features: [Temporal]
---*/

const oneMonth = new Temporal.Duration(0, 1);
const days30 = new Temporal.Duration(0, 0, 0, 30);
assert.sameValue(
Temporal.Duration.compare(oneMonth, days30, { relativeTo: Temporal.PlainDate.from("2018-04-01") }), 0);
assert.sameValue(
Temporal.Duration.compare(oneMonth, days30, { relativeTo: Temporal.PlainDate.from("2018-03-01") }), 1);
assert.sameValue(
Temporal.Duration.compare(oneMonth, days30, { relativeTo: Temporal.PlainDate.from("2018-02-01") }), -1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.compare
description: Throws if a value in the relativeTo property bag is missing.
features: [Temporal]
---*/

const oneDay = new Temporal.Duration(0, 0, 0, 1);
const hours24 = new Temporal.Duration(0, 0, 0, 0, 24);
assert.throws(TypeError, () => Temporal.Duration.compare(oneDay, hours24, { relativeTo: { month: 11, day: 3 } }), "missing year");
assert.throws(TypeError, () => Temporal.Duration.compare(oneDay, hours24, { relativeTo: { year: 2019, month: 11 } }), "missing day");
assert.throws(TypeError, () => Temporal.Duration.compare(oneDay, hours24, { relativeTo: { year: 2019, day: 3 } }), "missing month");
15 changes: 15 additions & 0 deletions test/built-ins/Temporal/Duration/compare/relativeto-year.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.compare
description: relativeTo with years.
features: [Temporal]
---*/

const oneYear = new Temporal.Duration(1);
const days365 = new Temporal.Duration(0, 0, 0, 365);
assert.sameValue(
Temporal.Duration.compare(oneYear, days365, { relativeTo: Temporal.PlainDate.from("2017-01-01") }), 0);
assert.sameValue(
Temporal.Duration.compare(oneYear, days365, { relativeTo: Temporal.PlainDate.from("2016-01-01") }), 1);
31 changes: 31 additions & 0 deletions test/intl402/Temporal/Duration/compare/relativeto-hour.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.compare
description: relativeTo with hours.
features: [Temporal]
---*/

const oneDay = new Temporal.Duration(0, 0, 0, 1);
const hours24 = new Temporal.Duration(0, 0, 0, 0, 24);

assert.sameValue(
Temporal.Duration.compare(oneDay, hours24, { relativeTo: Temporal.ZonedDateTime.from('2017-01-01T00:00[America/Montevideo]') }),
0,
'relativeTo does not affect days if ZonedDateTime, and duration encompasses no DST change');
assert.sameValue(
Temporal.Duration.compare(oneDay, hours24, { relativeTo: Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]') }),
1,
'relativeTo does affect days if ZonedDateTime, and duration encompasses DST change');
assert.sameValue(
Temporal.Duration.compare(oneDay, hours24, { relativeTo: '2019-11-03T00:00[America/Vancouver]' }),
1,
'casts relativeTo to ZonedDateTime from string');
assert.sameValue(
Temporal.Duration.compare(oneDay, hours24, {
relativeTo: { year: 2019, month: 11, day: 3, timeZone: 'America/Vancouver' }
}),
1,
'casts relativeTo to ZonedDateTime from object');

0 comments on commit b649e6b

Please sign in to comment.