-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
751604b
commit 8705a73
Showing
4 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/built-ins/Temporal/Instant/prototype/add/disallowed-duration-units.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters