Commit a91a6e1a authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[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: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76524}
parent 080bc4de
......@@ -43,7 +43,11 @@
##############################################################################
# Temporal tests to be implemented
# https://crbug.com/v8/11544
'temporal/*': [FAIL],
'temporal/plain*': [FAIL],
'temporal/duration*': [FAIL],
'temporal/calendar*': [FAIL],
'temporal/zoned*': [FAIL],
'temporal/instant*': [FAIL],
##############################################################################
# Open bugs.
......
// 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.abs(), 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.abs(), 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.abs(), 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.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false);
// 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();
let badDur = {add: d1.add};
assertThrows(() => badDur.add(d1), TypeError,
"Method Temporal.Duration.prototype.add called on incompatible receiver #<Object>");
let relativeToOptions = {relativeTo: "2021-08-01"};
let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertThrows(() => d2.add(d1), RangeError, "Invalid time value");
assertThrows(() => d1.add(d2), RangeError, "Invalid time value");
assertThrows(() => d2.add(d2), RangeError, "Invalid time value");
assertDuration(d2.add(d1, relativeToOptions), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false);
assertDuration(d1.add(d2, relativeToOptions), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false);
assertDuration(d1.add(d1, relativeToOptions), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
assertDuration(d2.add(d2, relativeToOptions), 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 1, false);
// Test large number
let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5);
assertThrows(() => d3.add(d3), RangeError, "Invalid time value");
assertDuration(d3.add(d3, relativeToOptions), 2e5, 4e5, 6e5, 8e5, 1e6, 12e5, 14e5, 16e5, 18e5, 2e6, 1, false);
// Test negative values
let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10);
assertThrows(() => d4.add(d0), RangeError, "Invalid time value");
assertThrows(() => d0.add(d4), RangeError, "Invalid time value");
assertThrows(() => d4.add(d4), RangeError, "Invalid time value");
assertThrows(() => d2.add(d4), RangeError, "Invalid time value");
assertThrows(() => d4.add(d2), RangeError, "Invalid time value");
assertDuration(d4.add(d0, relativeToOptions), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false);
assertDuration(d0.add(d4, relativeToOptions), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false);
assertDuration(d4.add(d4, relativeToOptions), -2, -4, -6, -8, -10, -12, -14, -16, -18, -20, -1, false);
assertDuration(d2.add(d4, relativeToOptions), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
assertDuration(d4.add(d2, relativeToOptions), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
// 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");
This diff is collapsed.
// 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.negated(), -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.negated(), -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.negated(), -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.negated(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false);
let d5 = new Temporal.Duration(-1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5);
assertDuration(d5.negated(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false);
// 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
assertEquals("PT0S", (new Temporal.Duration()).toJSON());
assertEquals("P1Y", (new Temporal.Duration(1)).toJSON());
assertEquals("-P1Y", (new Temporal.Duration(-1)).toJSON());
assertEquals("P1234567890Y", (new Temporal.Duration(1234567890)).toJSON());
assertEquals("-P1234567890Y", (new Temporal.Duration(-1234567890)).toJSON());
assertEquals("P1Y2M", (new Temporal.Duration(1, 2)).toJSON());
assertEquals("-P1Y2M", (new Temporal.Duration(-1, -2)).toJSON());
assertEquals("P2M", (new Temporal.Duration(0, 2)).toJSON());
assertEquals("-P2M", (new Temporal.Duration(0,-2)).toJSON());
assertEquals("P1234567890M", (new Temporal.Duration(0, 1234567890)).toJSON());
assertEquals("-P1234567890M", (new Temporal.Duration(0,-1234567890)).toJSON());
assertEquals("P1Y2M3W", (new Temporal.Duration(1, 2, 3)).toJSON());
assertEquals("-P1Y2M3W", (new Temporal.Duration(-1, -2, -3)).toJSON());
assertEquals("P3W", (new Temporal.Duration(0, 0, 3)).toJSON());
assertEquals("-P3W", (new Temporal.Duration(0, 0, -3)).toJSON());
assertEquals("P1Y3W", (new Temporal.Duration(1, 0, 3)).toJSON());
assertEquals("-P1Y3W", (new Temporal.Duration(-1, 0, -3)).toJSON());
assertEquals("P2M3W", (new Temporal.Duration(0, 2, 3)).toJSON());
assertEquals("-P2M3W", (new Temporal.Duration(0, -2, -3)).toJSON());
assertEquals("P1234567890W",
(new Temporal.Duration(0, 0, 1234567890)).toJSON());
assertEquals("-P1234567890W",
(new Temporal.Duration(0, 0, -1234567890)).toJSON());
assertEquals("P1Y2M3W4D", (new Temporal.Duration(1, 2, 3, 4)).toJSON());
assertEquals("-P1Y2M3W4D", (new Temporal.Duration(-1, -2, -3, -4)).toJSON());
assertEquals("P1234567890D",
(new Temporal.Duration(0, 0, 0, 1234567890)).toJSON());
assertEquals("-P1234567890D",
(new Temporal.Duration(0, 0, 0, -1234567890)).toJSON());
assertEquals("P4D", (new Temporal.Duration(0, 0, 0, 4)).toJSON());
assertEquals("-P4D", (new Temporal.Duration(0, 0, 0, -4)).toJSON());
assertEquals("P1Y4D", (new Temporal.Duration(1, 0, 0, 4)).toJSON());
assertEquals("-P1Y4D", (new Temporal.Duration(-1, 0, 0, -4)).toJSON());
assertEquals("P2M4D", (new Temporal.Duration(0, 2, 0, 4)).toJSON());
assertEquals("-P2M4D", (new Temporal.Duration(0, -2, 0, -4)).toJSON());
assertEquals("P3W4D", (new Temporal.Duration(0, 0, 3, 4)).toJSON());
assertEquals("-P3W4D", (new Temporal.Duration(0, 0, -3, -4)).toJSON());
assertEquals("PT5H", (new Temporal.Duration(0, 0, 0, 0, 5)).toJSON());
assertEquals("-PT5H", (new Temporal.Duration(0, 0, 0, 0, -5)).toJSON());
assertEquals("P1YT5H", (new Temporal.Duration(1, 0, 0, 0, 5)).toJSON());
assertEquals("-P1YT5H", (new Temporal.Duration(-1, 0, 0, 0, -5)).toJSON());
assertEquals("P2MT5H", (new Temporal.Duration(0, 2, 0, 0, 5)).toJSON());
assertEquals("-P2MT5H", (new Temporal.Duration(0, -2, 0, 0, -5)).toJSON());
assertEquals("PT6M", (new Temporal.Duration(0, 0, 0, 0, 0, 6)).toJSON());
assertEquals("-PT6M", (new Temporal.Duration(0, 0, 0, 0, 0, -6)).toJSON());
assertEquals("PT5H6M", (new Temporal.Duration(0, 0, 0, 0, 5, 6)).toJSON());
assertEquals("-PT5H6M", (new Temporal.Duration(0, 0, 0, 0, -5, -6)).toJSON());
assertEquals("P3WT6M", (new Temporal.Duration(0, 0, 3, 0, 0, 6)).toJSON());
assertEquals("-P3WT6M", (new Temporal.Duration(0, 0, -3, 0, 0, -6)).toJSON());
assertEquals("P4DT6M", (new Temporal.Duration(0, 0, 0, 4, 0, 6)).toJSON());
assertEquals("-P4DT6M", (new Temporal.Duration(0, 0, 0, -4, 0, -6)).toJSON());
assertEquals("PT7S", (new Temporal.Duration(0, 0, 0, 0, 0, 0, 7)).toJSON());
assertEquals("-PT7S", (new Temporal.Duration(0, 0, 0, 0, 0, 0, -7)).toJSON());
assertEquals("PT5H7S", (new Temporal.Duration(0, 0, 0, 0, 5, 0, 7)).toJSON());
assertEquals("-PT5H7S",
(new Temporal.Duration(0, 0, 0, 0, -5, 0, -7)).toJSON());
assertEquals("PT6M7S", (new Temporal.Duration(0, 0, 0, 0, 0, 6, 7)).toJSON());
assertEquals("-PT6M7S",
(new Temporal.Duration(0, 0, 0, 0, 0, -6, -7)).toJSON());
assertEquals("PT5H6M7S", (new Temporal.Duration(0, 0, 0, 0, 5, 6, 7)).toJSON());
assertEquals("-PT5H6M7S",
(new Temporal.Duration(0, 0, 0, 0, -5, -6, -7)).toJSON());
assertEquals("P1YT5H6M7S",
(new Temporal.Duration(1, 0, 0, 0, 5, 6, 7)).toJSON());
assertEquals("-P1YT5H6M7S",
(new Temporal.Duration(-1, 0, 0, 0, -5, -6, -7)).toJSON());
assertEquals("PT0.008S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 8)).toJSON());
assertEquals("-PT0.008S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -8)).toJSON());
assertEquals("PT0.08S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 80)).toJSON());
assertEquals("-PT0.08S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -80)).toJSON());
assertEquals("PT0.087S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 87)).toJSON());
assertEquals("-PT0.087S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -87)).toJSON());
assertEquals("PT0.876S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 876)).toJSON());
assertEquals("-PT0.876S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -876)).toJSON());
assertEquals("PT876.543S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 876543)).toJSON());
assertEquals("-PT876.543S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, -876543)).toJSON());
assertEquals("PT0.000009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 9)).toJSON());
assertEquals("-PT0.000009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -9)).toJSON());
assertEquals("PT0.00009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 90)).toJSON());
assertEquals("-PT0.00009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -90)).toJSON());
assertEquals("PT0.000098S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 98)).toJSON());
assertEquals("-PT0.000098S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -98)).toJSON());
assertEquals("PT0.0009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 900)).toJSON());
assertEquals("-PT0.0009S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -900)).toJSON());
assertEquals("PT0.000987S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 987)).toJSON());
assertEquals("-PT0.000987S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -987)).toJSON());
assertEquals("PT0.987654S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 987654)).toJSON());
assertEquals("-PT0.987654S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -987654)).toJSON());
assertEquals("PT987.654321S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 987654321)).toJSON());
assertEquals("-PT987.654321S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, -987654321)).toJSON());
assertEquals("PT0.000000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1)).toJSON());
assertEquals("-PT0.000000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1)).toJSON());
assertEquals("PT0.00000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 10)).toJSON());
assertEquals("-PT0.00000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -10)).toJSON());
assertEquals("PT0.000000012S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 12)).toJSON());
assertEquals("-PT0.000000012S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -12)).toJSON());
assertEquals("PT0.0000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 100)).toJSON());
assertEquals("-PT0.0000001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -100)).toJSON());
assertEquals("PT0.000000123S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 123)).toJSON());
assertEquals("-PT0.000000123S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -123)).toJSON());
assertEquals("PT0.000123456S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 123456)).toJSON());
assertEquals("-PT0.000123456S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -123456)).toJSON());
assertEquals("PT0.123456789S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 123456789)).toJSON());
assertEquals("-PT0.123456789S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -123456789)).toJSON());
assertEquals("PT1.234567891S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1234567891)).toJSON());
assertEquals("-PT1.234567891S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1234567891)).toJSON());
assertEquals("PT4.003002001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 4, 3, 2, 1)).toJSON());
assertEquals("-PT4.003002001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, -4, -3, -2, -1)).toJSON());
assertEquals("PT4.003092001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 4, 3, 2, 90001)).toJSON());
assertEquals("-PT4.003092001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, -4, -3, -2, -90001)).toJSON());
assertEquals("PT4.093082001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, 4, 3, 2, 90080001)).toJSON());
assertEquals("-PT4.093082001S",
(new Temporal.Duration(0, 0, 0, 0, 0, 0, -4, -3, -2, -90080001)).toJSON());
assertEquals("P1Y2M3W4DT5H6M7.008009001S",
(new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 1)).toJSON());
assertEquals("-P1Y2M3W4DT5H6M7.008009001S",
(new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -1)).toJSON());
assertEquals("P1234Y2345M3456W4567DT5678H6789M7890.890901123S",
(new Temporal.Duration(1234, 2345, 3456, 4567, 5678, 6789, 7890,
890, 901, 123)).toJSON());
assertEquals("-P1234Y2345M3456W4567DT5678H6789M7890.890901123S",
(new Temporal.Duration(-1234, -2345, -3456, -4567, -5678, -6789, -7890,
-890, -901, -123)).toJSON());
// 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
let d1 = new Temporal.Duration();
assertThrows(() => d1.valueOf(), TypeError,
"Method Temporal.Duration called on a non-object or on a wrong type of object.");
// 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 like1 = {years:9, months:8, weeks:7, days:6, hours: 5, minutes: 4,
seconds: 3, milliseconds: 2, microseconds: 1, nanoseconds: 10};
let like2 = {years: 9, hours:5};
let like3 = {months: 8, minutes:4};
let like4 = {weeks: 7, seconds:3};
let like5 = {days: 6, milliseconds:2};
let like6 = {microseconds: 987, nanoseconds: 123};
let like7 = {years:-9, months:-8, weeks:-7, days:-6, hours: -5, minutes: -4,
seconds: -3, milliseconds: -2, microseconds: -1, nanoseconds: -10};
let d1 = new Temporal.Duration();
assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true);
assertDuration(d1.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false);
assertDuration(d1.with(like2), 9, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, false);
assertDuration(d1.with(like3), 0, 8, 0, 0, 0, 4, 0, 0, 0, 0, 1, false);
assertDuration(d1.with(like4), 0, 0, 7, 0, 0, 0, 3, 0, 0, 0, 1, false);
assertDuration(d1.with(like5), 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 1, false);
assertDuration(d1.with(like6), 0, 0, 0, 0, 0, 0, 0, 0, 987, 123, 1, false);
assertDuration(d1.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1,
false);
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);
assertDuration(d2.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false);
assertDuration(d2.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1,
false);
// Different sign
assertThrows(() => d2.with({years: -1}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({months: -2}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({weeks: -3}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({days: -4}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({hours: -5}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({minutes: -6}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({seconds: -7}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({milliseconds: -8}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({microseconds: -9}), RangeError,
"Invalid time value");
assertThrows(() => d2.with({nanoseconds: -10}), RangeError,
"Invalid time value");
// 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);
assertDuration(d3.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false);
assertDuration(d3.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -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);
assertDuration(d4.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false);
// Throw when sign flip
assertThrows(() => d4.with({years: 1}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({months: 2}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({weeks: 3}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({days: 4}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({hours: 5}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({minutes: 6}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({seconds: 7}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({milliseconds: 8}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({microseconds: 9}), RangeError,
"Invalid time value");
assertThrows(() => d4.with({nanoseconds: 10}), RangeError,
"Invalid time value");
// singular throw
assertThrows(() => d1.with({year:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({month:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({week:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({day:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({hour:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({minute:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({second:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({millisecond:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({microsecond:1}), TypeError,
"invalid_argument");
assertThrows(() => d1.with({nanosecond:1}), TypeError,
"invalid_argument");
// 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.
// Helper function to test Tempral
function assertDuration(duration, years, months, weeks, days, hours,
minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) {
assertEquals(years, duration.years, duration);
assertEquals(months, duration.months, duration);
assertEquals(weeks, duration.weeks, duration);
assertEquals(days, duration.days, duration);
assertEquals(hours, duration.hours, duration);
assertEquals(minutes, duration.minutes, duration);
assertEquals(seconds, duration.seconds, duration);
assertEquals(milliseconds, duration.milliseconds, duration);
assertEquals(microseconds, duration.microseconds, duration);
assertEquals(nanoseconds, duration.nanoseconds, duration);
assertEquals(sign, duration.sign, duration);
assertEquals(blank, duration.blank, duration);
}
function assertPlainDate(time, year, month, day) {
let fields = time.getISOFields();
let keys = Object.keys(fields);
assertEquals(4, keys.length);
assertEquals("calendar", keys[0]);
assertEquals("isoDay", keys[1]);
assertEquals("isoMonth", keys[2]);
assertEquals("isoYear", keys[3]);
if (fields.calendar == "iso8601") {
assertEquals(year, fields.isoYear, "isoYear");
assertEquals(month, fields.isoMonth, "isoMonth");
assertEquals(day, fields.isoDay, "isoDay");
}
}
function assertPlainDateTime(datetime, year, month, day, hour, minute, second,
millisecond, microsecond, nanosecond) {
let fields = datetime.getISOFields();
let keys = Object.keys(fields);
assertEquals(10, keys.length);
assertEquals("calendar", keys[0]);
assertEquals("isoDay", keys[1]);
assertEquals("isoHour", keys[2]);
assertEquals("isoMicrosecond", keys[3]);
assertEquals("isoMillisecond", keys[4]);
assertEquals("isoMinute", keys[5]);
assertEquals("isoMonth", keys[6]);
assertEquals("isoNanosecond", keys[7]);
assertEquals("isoSecond", keys[8]);
assertEquals("isoYear", keys[9]);
if (fields.calendar == "iso8601") {
assertEquals(year, fields.isoYear, "isoYear");
assertEquals(month, fields.isoMonth, "isoMonth");
assertEquals(day, fields.isoDay, "isoDay");
assertEquals(hour, fields.isoHour, "isoHour");
assertEquals(minute, fields.isoMinute, "isoMinute");
assertEquals(second, fields.isoSecond, "isoSecond");
assertEquals(millisecond, fields.isoMillisecond, "isoMillisecond");
assertEquals(microsecond, fields.isoMicrosecond, "isoMicorsecond");
assertEquals(nanosecond, fields.isoNanosecond, "isoNanosecond");
assertEquals(datetime.calendar, fields.calendar, "calendar");
}
}
function assertPlainTime(time, hour, minute, second, millisecond, microsecond, nanosecond) {
assertEquals(hour, time.hour, "hour");
assertEquals(minute, time.minute, "minute");
assertEquals(second, time.second, "second");
assertEquals(millisecond, time.millisecond, "millisecond");
assertEquals(microsecond, time.microsecond, "microsecond");
assertEquals(nanosecond, time.nanosecond, "nanosecond");
}
function assertPlainMonthDay(md, monthCode, day) {
let fields = md.getISOFields();
let keys = Object.keys(fields);
assertEquals(4, keys.length);
assertEquals("calendar", keys[0]);
assertEquals("isoDay", keys[1]);
assertEquals("isoMonth", keys[2]);
assertEquals("isoYear", keys[3]);
assertEquals(monthCode, md.monthCode, "monthCode");
assertEquals(day, md.day, "day");
if (fields.calendar == "iso8601") {
assertEquals(monthCode, md.monthCode, "monthCode");
assertEquals(day, md.day, "day");
assertEquals(md.calendar, fields.calendar, "calendar");
}
}
function assertPlainYearMonth(ym, year, month) {
let fields = ym.getISOFields();
let keys = Object.keys(fields);
assertEquals(4, keys.length);
assertEquals("calendar", keys[0]);
assertEquals("isoDay", keys[1]);
assertEquals("isoMonth", keys[2]);
assertEquals("isoYear", keys[3]);
if (fields.calendar == "iso8601") {
assertEquals(year, fields.isoYear, "isoYear");
assertEquals(month, fields.isoMonth, "isoMonth");
assertEquals(ym.calendar, fields.calendar, "calendar");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment