-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Temporal: Port some Duration.compare tests.
- Loading branch information
Showing
7 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
test/built-ins/Temporal/Duration/compare/relativeto-hour.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
test/built-ins/Temporal/Duration/compare/relativeto-month.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
14 changes: 14 additions & 0 deletions
14
test/built-ins/Temporal/Duration/compare/relativeto-propertybag-invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/built-ins/Temporal/Duration/compare/relativeto-year.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|