diff --git a/test/built-ins/Temporal/Duration/compare/argument-cast.js b/test/built-ins/Temporal/Duration/compare/argument-cast.js new file mode 100644 index 0000000000..5d2262bf0e --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/argument-cast.js @@ -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"); + diff --git a/test/built-ins/Temporal/Duration/compare/basic.js b/test/built-ins/Temporal/Duration/compare/basic.js new file mode 100644 index 0000000000..bcc7fadfdd --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/basic.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/compare/relativeto-hour.js b/test/built-ins/Temporal/Duration/compare/relativeto-hour.js new file mode 100644 index 0000000000..149885e1db --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/relativeto-hour.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/compare/relativeto-month.js b/test/built-ins/Temporal/Duration/compare/relativeto-month.js new file mode 100644 index 0000000000..cb0df5606d --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/relativeto-month.js @@ -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); diff --git a/test/built-ins/Temporal/Duration/compare/relativeto-propertybag-invalid.js b/test/built-ins/Temporal/Duration/compare/relativeto-propertybag-invalid.js new file mode 100644 index 0000000000..0e940ade56 --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/relativeto-propertybag-invalid.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/compare/relativeto-year.js b/test/built-ins/Temporal/Duration/compare/relativeto-year.js new file mode 100644 index 0000000000..7700d0f3db --- /dev/null +++ b/test/built-ins/Temporal/Duration/compare/relativeto-year.js @@ -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); diff --git a/test/intl402/Temporal/Duration/compare/relativeto-hour.js b/test/intl402/Temporal/Duration/compare/relativeto-hour.js new file mode 100644 index 0000000000..318c0ba4ac --- /dev/null +++ b/test/intl402/Temporal/Duration/compare/relativeto-hour.js @@ -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'); +