Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Temporal.Instant.p*.subtract #3076

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/built-ins/Temporal/Instant/prototype/subtract/normal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.subtract
description: Temporal.Instant.prototype.subtract() return correct value with Temporal.Instant object.
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]
---*/
let i1 = new Temporal.Instant(50000n);
assert.sameValue(-2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,0,3,2,1)).epochNanoseconds);

assert.sameValue(BigInt(-4 * 1e9) - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,4,3,2,1)).epochNanoseconds);

assert.sameValue(BigInt(5 * 60 + 4) * -1000000000n - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,5,4,3,2,1)).epochNanoseconds);

assert.sameValue(BigInt(6 * 3600 + 5 * 60 + 4) * -1000000000n - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,6,5,4,3,2,1)).epochNanoseconds);

assert.sameValue(3052001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,0,-3,-2,-1)).epochNanoseconds);

assert.sameValue(BigInt(4 * 1e9) + 3052001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,-4,-3,-2,-1)).epochNanoseconds);

assert.sameValue(BigInt(5 * 60 + 4) * 1000000000n + 3052001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,-5,-4,-3,-2,-1)).epochNanoseconds);

assert.sameValue(BigInt(6 * 3600 + 5 * 60 + 4) * 1000000000n + 3052001n,
i1.subtract(new Temporal.Duration(0,0,0,0,-6,-5,-4,-3,-2,-1)).epochNanoseconds);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.subtract
description: Temporal.Instant.prototype.subtract() throws RangeError while the result is too big or too small.
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]
---*/
let i1 = new Temporal.Instant(-86400n * 99999999999999999n);
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 0, 0, 0, 999999999)),
"subtract show throw from AddInstant while the result is out of range");

let i2 = new Temporal.Instant(86400n * 99999999999999999n);
assert.throws(RangeError, () => i2.subtract(new Temporal.Duration(0, 0, 0, 0, -999999999)),
"subtract show throw from AddInstant while the result is out of range");
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.subtract
description: Temporal.Instant.prototype.subtract() throws RangeError while the duration has
non zeros 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]
---*/
let i1 = new Temporal.Instant(500000n);
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(1)),
"subtract throw RangeError while the duration has non zero years");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 2)),
"subtract throw RangeError while the duration has non zero months");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 0, 3)),
"subtract throw RangeError while the duration has non zero weeks");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 0, 0, 4)),
"subtract throw RangeError while the duration has non zero days");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(-1)),
"subtract throw RangeError while the duration has non zero years");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, -2)),
"subtract throw RangeError while the duration has non zero months");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 0, -3)),
"subtract throw RangeError while the duration has non zero weeks");
assert.throws(RangeError, () => i1.subtract(new Temporal.Duration(0, 0, 0, -4)),
"subtract throw RangeError while the duration has non zero days");
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.instant.prototype.subtract
description: Temporal.Instant.prototype.subtract() throws TypeError while instant does
not have [[InitializedTemporalInstant]].
info: |
1. Let instant be the this value.
2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
features: [Temporal]
---*/
let i1 = new Temporal.Instant(50000n);

// Test RequireInternalSlot Throw TypeError
let badInstant = { subtract: i1.subtract };
assert.throws(TypeError, () => badInstant.subtract(new Temporal.Duration(0, 0, 0, 0, 5)),
"Throw TypeError while instant does not have [[InitializedTemporalInstant]]");