From 439ac8c2dc6cf278f2d46aac32050df01a1a95eb Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 24 Dec 2021 12:46:34 +0100 Subject: [PATCH] Add tests for PlainDate.compare(). --- .../PlainDate/compare/argument-object.js | 22 +++++++++++ .../PlainDate/compare/argument-string.js | 19 ++++++++++ .../Temporal/PlainDate/compare/basic.js | 16 ++++++++ .../Temporal/PlainDate/compare/calendar.js | 37 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 test/built-ins/Temporal/PlainDate/compare/argument-object.js create mode 100644 test/built-ins/Temporal/PlainDate/compare/argument-string.js create mode 100644 test/built-ins/Temporal/PlainDate/compare/basic.js create mode 100644 test/built-ins/Temporal/PlainDate/compare/calendar.js diff --git a/test/built-ins/Temporal/PlainDate/compare/argument-object.js b/test/built-ins/Temporal/PlainDate/compare/argument-object.js new file mode 100644 index 0000000000..fd5c6dd16c --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/compare/argument-object.js @@ -0,0 +1,22 @@ +// 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.plaindate.compare +description: basic object coercion in arguments +features: [Temporal] +---*/ + +const d1 = Temporal.PlainDate.from("1976-11-18"); +const d2 = Temporal.PlainDate.from("2019-06-30"); + +assert.sameValue(Temporal.PlainDate.compare({ year: 1976, month: 11, day: 18 }, d2), -1, "first argument"); +assert.sameValue(Temporal.PlainDate.compare({ year: 2019, month: 6, day: 30 }, d2), 0, "first argument"); +assert.sameValue(Temporal.PlainDate.compare({ year: 2024, month: 1, day: 12 }, d2), 1, "first argument"); + +assert.sameValue(Temporal.PlainDate.compare(d1, { year: 2024, month: 1, day: 12 }), -1, "second argument"); +assert.sameValue(Temporal.PlainDate.compare(d1, { year: 1976, month: 11, day: 18 }), 0, "second argument"); +assert.sameValue(Temporal.PlainDate.compare(d1, { year: 1926, month: 7, day: 7 }), 1, "second argument"); + +assert.throws(TypeError, () => Temporal.PlainDate.compare({ year: 1976 }, d2), "only year in first argument"); +assert.throws(TypeError, () => Temporal.PlainDate.compare(d1, { year: 2019 }), "only year in second argument"); diff --git a/test/built-ins/Temporal/PlainDate/compare/argument-string.js b/test/built-ins/Temporal/PlainDate/compare/argument-string.js new file mode 100644 index 0000000000..d683f47225 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/compare/argument-string.js @@ -0,0 +1,19 @@ +// 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.plaindate.compare +description: basic string coercion in arguments +features: [Temporal] +---*/ + +const d1 = Temporal.PlainDate.from("1976-11-18"); +const d2 = Temporal.PlainDate.from("2019-06-30"); + +assert.sameValue(Temporal.PlainDate.compare("1976-11-18", d2), -1, "first argument"); +assert.sameValue(Temporal.PlainDate.compare("2019-06-30", d2), 0, "first argument"); +assert.sameValue(Temporal.PlainDate.compare("2024-01-12", d2), 1, "first argument"); + +assert.sameValue(Temporal.PlainDate.compare(d1, "2019-06-30"), -1, "second argument"); +assert.sameValue(Temporal.PlainDate.compare(d1, "1976-11-18"), 0, "second argument"); +assert.sameValue(Temporal.PlainDate.compare(d1, "1926-07-07"), 1, "second argument"); diff --git a/test/built-ins/Temporal/PlainDate/compare/basic.js b/test/built-ins/Temporal/PlainDate/compare/basic.js new file mode 100644 index 0000000000..5de74528d8 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/compare/basic.js @@ -0,0 +1,16 @@ +// 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.plaindate.compare +description: basic tests +features: [Temporal] +---*/ + +const d1 = Temporal.PlainDate.from("1976-11-18"); +const d2 = Temporal.PlainDate.from("2019-06-30"); +const d3 = Temporal.PlainDate.from("2019-06-30"); +assert.sameValue(Temporal.PlainDate.compare(d1, d1), 0, "same object"); +assert.sameValue(Temporal.PlainDate.compare(d1, d2), -1, "earlier"); +assert.sameValue(Temporal.PlainDate.compare(d2, d1), 1, "later"); +assert.sameValue(Temporal.PlainDate.compare(d2, d3), 0, "same date"); diff --git a/test/built-ins/Temporal/PlainDate/compare/calendar.js b/test/built-ins/Temporal/PlainDate/compare/calendar.js new file mode 100644 index 0000000000..250bffa98c --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/compare/calendar.js @@ -0,0 +1,37 @@ +// 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.plaindate.compare +description: basic tests +features: [Temporal] +---*/ + +class CalendarTraceToString extends Temporal.Calendar { + constructor(id) { + super("iso8601"); + this.id_ = id; + this.calls = 0; + } + toString() { + ++this.calls; + return this.id_; + } +}; + +const calendar1 = new CalendarTraceToString("a"); +const date1 = new Temporal.PlainDate(1914, 2, 23, calendar1); + +const calendar2 = new CalendarTraceToString("a"); +const date2 = new Temporal.PlainDate(1914, 2, 23, calendar2); + +const calendar3 = new CalendarTraceToString("b"); +const date3 = new Temporal.PlainDate(1914, 2, 23, calendar3); + +assert.sameValue(Temporal.PlainDate.compare(date1, date1), 0, "same object"); +assert.sameValue(Temporal.PlainDate.compare(date1, date2), 0, "same date"); +assert.sameValue(Temporal.PlainDate.compare(date1, date3), 0, "same date, different calendar"); + +assert.sameValue(calendar1.calls, 0, "calendar1 toString() calls"); +assert.sameValue(calendar2.calls, 0, "calendar2 toString() calls"); +assert.sameValue(calendar3.calls, 0, "calendar3 toString() calls");