v8/test/mjsunit/temporal/duration-constructor.js
Frank Tang a91a6e1a23 [Temporal] Add some tests for Duration
Land some of the tests for Temporal.Duration
All marked as FAIL at this stage.

Bug: v8:11544
Change-Id: I4696edee7a2345133ed13c25aa720febe1f8fc69
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3087363
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76524}
2021-08-27 06:45:13 +00:00

94 lines
4.3 KiB
JavaScript

// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
d8.file.execute('test/mjsunit/temporal/temporal-helpers.js');
let d1 = new Temporal.Duration();
assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertDuration(d2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false);
// Test large number
let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5);
assertDuration(d3, 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false);
// Test negative values
let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10);
assertDuration(d4, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false);
// Test NaN
let d5 = new Temporal.Duration(NaN, NaN, NaN);
assertDuration(d5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
// 1. If NewTarget is undefined, then
// a. Throw a TypeError exception.
assertThrows(() => Temporal.Duration(), TypeError,
"Method invoked on an object that is not Temporal.Duration.");
// 1. Let number be ? ToNumber(argument).
assertDuration(new Temporal.Duration(undefined, 234, true, false, "567"),
0, 234, 1, 0, 567, 0, 0, 0, 0, 0, 1, false);
assertThrows(() => new Temporal.Duration(Symbol(123)), TypeError,
"Cannot convert a Symbol value to a number");
assertThrows(() => new Temporal.Duration(123n), TypeError,
"Cannot convert a BigInt value to a number");
// Test Infinity
// 7.5.4 IsValidDuration ( years, months, weeks, days, hours, minutes, seconds,
// milliseconds, microseconds, nanoseconds )
// a. If v is not finite, return false.
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, 5, 6, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, 5, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, 4, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, 3, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 2, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9,
-Infinity), RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8,
-Infinity), RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7,
-Infinity), RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -5, -6, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -5, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -4, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -3, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -2, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, -Infinity),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-Infinity),
RangeError, "Invalid time value");
// Sign different
assertThrows(() => new Temporal.Duration(1, -2),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(1, 0, -2),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(-1, 0, 0, 3),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 1, -1),
RangeError, "Invalid time value");
assertThrows(() => new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -1, 1),
RangeError, "Invalid time value");