From 8705a738cdc688d9190069c1e923faa16b3819e2 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Tue, 20 Jul 2021 23:34:57 -0700 Subject: [PATCH] Add test for Temporal.Instant.p*.add This was originally Frank's PR #3075. I rebased it, added assertion messages, did some reformatting, and combined with some existing tests. --- .../Temporal/Instant/prototype/add/basic.js | 72 +++++++++++++++++++ .../Instant/prototype/add/branding.js | 20 +++--- .../add/disallowed-duration-units.js | 31 ++++++++ .../prototype/add/result-out-of-range.js | 18 ++++- 4 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 test/built-ins/Temporal/Instant/prototype/add/basic.js create mode 100644 test/built-ins/Temporal/Instant/prototype/add/disallowed-duration-units.js diff --git a/test/built-ins/Temporal/Instant/prototype/add/basic.js b/test/built-ins/Temporal/Instant/prototype/add/basic.js new file mode 100644 index 00000000000..1b94ba1befb --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/add/basic.js @@ -0,0 +1,72 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.instant.prototype.add +description: Basic functionality of Temporal.Instant.prototype.add() +info: | + 1. Let instant be the this value. + 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]). + 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « "years", "months", "weeks", "days" »). + 4. Let ns be ? AddInstant(instant.[[EpochNanoseconds]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]). + 5. Return ! CreateTemporalInstant(ns). +features: [Temporal] +---*/ + +const inst = new Temporal.Instant(50000n); + +let result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 3, 2, 1)); +assert.sameValue( + 3052001n, + result.epochNanoseconds, + "add positive sub-seconds" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, 0, 4, 3, 2, 1)); +assert.sameValue( + BigInt(4 * 1e9) + 3052001n, + result.epochNanoseconds, + "add positive seconds" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, 5, 4, 3, 2, 1)); +assert.sameValue( + BigInt(5 * 60 + 4) * 1000000000n + 3052001n, + result.epochNanoseconds, + "add positive minutes" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 6, 5, 4, 3, 2, 1)); +assert.sameValue( + BigInt(6 * 3600 + 5 * 60 + 4) * 1000000000n + 3052001n, + result.epochNanoseconds, + "add positive hours" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -3, -2, -1)); +assert.sameValue( + -2952001n, + result.epochNanoseconds, + "add negative sub-seconds" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, 0, -4, -3, -2, -1)); +assert.sameValue( + BigInt(-4 * 1e9) - 2952001n, + result.epochNanoseconds, + "add negative seconds" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, 0, -5, -4, -3, -2, -1)); +assert.sameValue( + BigInt(5 * 60 + 4) * -1000000000n - 2952001n, + result.epochNanoseconds, + "add negative minutes" +); + +result = inst.add(new Temporal.Duration(0, 0, 0, 0, -6, -5, -4, -3, -2, -1)); +assert.sameValue( + BigInt(6 * 3600 + 5 * 60 + 4) * -1000000000n - 2952001n, + result.epochNanoseconds, + "add negative hours" +); diff --git a/test/built-ins/Temporal/Instant/prototype/add/branding.js b/test/built-ins/Temporal/Instant/prototype/add/branding.js index cb6b1158c6d..cccdbf63ef1 100644 --- a/test/built-ins/Temporal/Instant/prototype/add/branding.js +++ b/test/built-ins/Temporal/Instant/prototype/add/branding.js @@ -11,12 +11,14 @@ const add = Temporal.Instant.prototype.add; assert.sameValue(typeof add, "function"); -assert.throws(TypeError, () => add.call(undefined), "undefined"); -assert.throws(TypeError, () => add.call(null), "null"); -assert.throws(TypeError, () => add.call(true), "true"); -assert.throws(TypeError, () => add.call(""), "empty string"); -assert.throws(TypeError, () => add.call(Symbol()), "symbol"); -assert.throws(TypeError, () => add.call(1), "1"); -assert.throws(TypeError, () => add.call({}), "plain object"); -assert.throws(TypeError, () => add.call(Temporal.Instant), "Temporal.Instant"); -assert.throws(TypeError, () => add.call(Temporal.Instant.prototype), "Temporal.Instant.prototype"); +const arg = new Temporal.Duration(0, 0, 0, 0, 5); + +assert.throws(TypeError, () => add.call(undefined, arg), "undefined"); +assert.throws(TypeError, () => add.call(null, arg), "null"); +assert.throws(TypeError, () => add.call(true, arg), "true"); +assert.throws(TypeError, () => add.call("", arg), "empty string"); +assert.throws(TypeError, () => add.call(Symbol(), arg), "symbol"); +assert.throws(TypeError, () => add.call(1, arg), "1"); +assert.throws(TypeError, () => add.call({}, arg), "plain object"); +assert.throws(TypeError, () => add.call(Temporal.Instant, arg), "Temporal.Instant"); +assert.throws(TypeError, () => add.call(Temporal.Instant.prototype, arg), "Temporal.Instant.prototype"); diff --git a/test/built-ins/Temporal/Instant/prototype/add/disallowed-duration-units.js b/test/built-ins/Temporal/Instant/prototype/add/disallowed-duration-units.js new file mode 100644 index 00000000000..f6e35389cd8 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/add/disallowed-duration-units.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.instant.prototype.add +description: | + Temporal.Instant.prototype.add() throws RangeError when the duration has + non-zero years, months, weeks, or days. +info: | + 1. Let instant be the this value. + 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « "years", "months", "weeks", "days" »). +features: [Temporal] +---*/ + +const inst = new Temporal.Instant(500000n); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(1)), + "should throw RangeError when the duration has non-zero years (positive)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, 2)), + "should throw RangeError when the duration has non-zero months (positive)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, 0, 3)), + "should throw RangeError when the duration has non-zero weeks (positive)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, 0, 0, 4)), + "should throw RangeError when the duration has non-zero days (positive)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(-1)), + "should throw RangeError when the duration has non-zero years (negative)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, -2)), + "should throw RangeError when the duration has non-zero months (negative)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, 0, -3)), + "should throw RangeError when the duration has non-zero weeks (negative)"); +assert.throws(RangeError, () => inst.add(new Temporal.Duration(0, 0, 0, -4)), + "should throw RangeError when the duration has non-zero days (negative)"); diff --git a/test/built-ins/Temporal/Instant/prototype/add/result-out-of-range.js b/test/built-ins/Temporal/Instant/prototype/add/result-out-of-range.js index 63f6bba22a3..a2473233c8d 100644 --- a/test/built-ins/Temporal/Instant/prototype/add/result-out-of-range.js +++ b/test/built-ins/Temporal/Instant/prototype/add/result-out-of-range.js @@ -9,8 +9,22 @@ features: [Temporal] const fields = ["hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"]; -const instance = Temporal.Instant.fromEpochNanoseconds(8640000_000_000_000_000_000n); +const latest = Temporal.Instant.fromEpochNanoseconds(8640000_000_000_000_000_000n); fields.forEach((field) => { - assert.throws(RangeError, () => instance.add({ [field]: 1 })); + assert.throws( + RangeError, + () => latest.add({ [field]: 1 }), + `adding ${field} with result out of range (positive)` + ); +}); + +const earliest = Temporal.Instant.fromEpochNanoseconds(-8640000_000_000_000_000_000n); + +fields.forEach((field) => { + assert.throws( + RangeError, + () => earliest.add({ [field]: -1 }), + `adding ${field} with result out of range (negative)` + ); });