From 65e75e4eed999ff8201fd1467e940fbb1731accf Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 20 Jun 2022 15:47:09 +0200 Subject: [PATCH] Temporal: Extend PlainDate#{since,until} coverage. (#3581) --- .../since/calendar-invalid-return.js | 36 +++++++++++++++++++ .../PlainDate/prototype/since/custom.js | 32 +++++++++++++++++ .../until/calendar-invalid-return.js | 36 +++++++++++++++++++ .../PlainDate/prototype/until/custom.js | 32 +++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 test/built-ins/Temporal/PlainDate/prototype/since/calendar-invalid-return.js create mode 100644 test/built-ins/Temporal/PlainDate/prototype/since/custom.js create mode 100644 test/built-ins/Temporal/PlainDate/prototype/until/calendar-invalid-return.js create mode 100644 test/built-ins/Temporal/PlainDate/prototype/until/custom.js diff --git a/test/built-ins/Temporal/PlainDate/prototype/since/calendar-invalid-return.js b/test/built-ins/Temporal/PlainDate/prototype/since/calendar-invalid-return.js new file mode 100644 index 0000000000..6ecd46d277 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/since/calendar-invalid-return.js @@ -0,0 +1,36 @@ +// 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.plaindate.prototype.since +description: Throw when the returned value from the calendar's dateUntil method is not a Duration. +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor(value) { + super("iso8601"); + this.value = value; + } + dateUntil() { + return this.value; + } +} + +const tests = [ + [undefined], + [null, "null"], + [true], + ["2000-05"], + [Symbol()], + [200005], + [200005n], + [{}, "plain object"], + [() => {}, "lambda"], + [Temporal.Duration, "Temporal.Duration"], + [Temporal.Duration.prototype, "Temporal.Duration.prototype"], +]; +for (const [test, description = typeof test] of tests) { + const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test)); + assert.throws(TypeError, () => plainDate.since("2022-06-20"), `Expected error with ${description}`); +} diff --git a/test/built-ins/Temporal/PlainDate/prototype/since/custom.js b/test/built-ins/Temporal/PlainDate/prototype/since/custom.js new file mode 100644 index 0000000000..d768512f54 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/since/custom.js @@ -0,0 +1,32 @@ +// 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.plaindate.prototype.since +description: Basic tests with custom calendar +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const result = new Temporal.Duration(1, 3, 5, 7, 9); +const options = {}; +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateUntil(...args) { + ++calls; + assert.sameValue(args.length, 3, "Three arguments"); + assert.sameValue(args[0], plainDate, "First argument"); + assert.sameValue(args[1], other, "Second argument"); + assert.sameValue(args[2].largestUnit, "day", "Third argument: largestUnit"); + return result; + } +} +const calendar = new CustomCalendar(); +const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar); +const other = new Temporal.PlainDate(2022, 6, 20, calendar); +TemporalHelpers.assertDuration(plainDate.since(other, options), + -1, -3, -5, -7, 0, 0, 0, 0, 0, 0, "result"); +assert.sameValue(calls, 1); diff --git a/test/built-ins/Temporal/PlainDate/prototype/until/calendar-invalid-return.js b/test/built-ins/Temporal/PlainDate/prototype/until/calendar-invalid-return.js new file mode 100644 index 0000000000..5e66a55532 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/until/calendar-invalid-return.js @@ -0,0 +1,36 @@ +// 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.plaindate.prototype.until +description: Throw when the returned value from the calendar's dateUntil method is not a Duration. +features: [Temporal] +---*/ + +class CustomCalendar extends Temporal.Calendar { + constructor(value) { + super("iso8601"); + this.value = value; + } + dateUntil() { + return this.value; + } +} + +const tests = [ + [undefined], + [null, "null"], + [true], + ["2000-05"], + [Symbol()], + [200005], + [200005n], + [{}, "plain object"], + [() => {}, "lambda"], + [Temporal.Duration, "Temporal.Duration"], + [Temporal.Duration.prototype, "Temporal.Duration.prototype"], +]; +for (const [test, description = typeof test] of tests) { + const plainDate = new Temporal.PlainDate(2000, 5, 2, new CustomCalendar(test)); + assert.throws(TypeError, () => plainDate.until("2022-06-20"), `Expected error with ${description}`); +} diff --git a/test/built-ins/Temporal/PlainDate/prototype/until/custom.js b/test/built-ins/Temporal/PlainDate/prototype/until/custom.js new file mode 100644 index 0000000000..1d332af5c6 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/prototype/until/custom.js @@ -0,0 +1,32 @@ +// 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.plaindate.prototype.until +description: Basic tests with custom calendar +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const result = new Temporal.Duration(1, 3, 5, 7, 9); +const options = {}; +let calls = 0; +class CustomCalendar extends Temporal.Calendar { + constructor() { + super("iso8601"); + } + dateUntil(...args) { + ++calls; + assert.sameValue(args.length, 3, "Three arguments"); + assert.sameValue(args[0], plainDate, "First argument"); + assert.sameValue(args[1], other, "Second argument"); + assert.sameValue(args[2].largestUnit, "day", "Third argument: largestUnit"); + return result; + } +} +const calendar = new CustomCalendar(); +const plainDate = new Temporal.PlainDate(1976, 11, 18, calendar); +const other = new Temporal.PlainDate(2022, 6, 20, calendar); +TemporalHelpers.assertDuration(plainDate.until(other, options), + 1, 3, 5, 7, 0, 0, 0, 0, 0, 0, "result"); +assert.sameValue(calls, 1);