[Temporal] Add some tests for PlainDate

Land some of the tests for Temporal.PlainDate
All marked as FAIL at this stage.

Bug: v8:11544
Change-Id: I004b7cb34effe1de1735b61c7ac749ae3c8e9bf7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3085624
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76551}
This commit is contained in:
Frank Tang 2021-08-27 22:00:41 -07:00 committed by V8 LUCI CQ
parent 7d729e85b6
commit d61809bb41
28 changed files with 533 additions and 0 deletions

View File

@ -0,0 +1,25 @@
// 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');
// Simple add
let d = new Temporal.PlainDate(2021, 7, 20);
assertPlainDate(d.add("P1D"), 2021, 7, 21);
assertPlainDate(d.subtract("-P1D"), 2021, 7, 21);
assertPlainDate(d.add("-P1D"), 2021, 7, 19);
assertPlainDate(d.subtract("P1D"), 2021, 7, 19);
assertPlainDate(d.add("P11D"), 2021, 7, 31);
assertPlainDate(d.subtract("-P11D"), 2021, 7, 31);
assertPlainDate(d.add("P12D"), 2021, 8, 1);
assertPlainDate(d.subtract("-P12D"), 2021, 8, 1);
let goodDate = new Temporal.PlainDate(2021, 7, 20);
let badDate = {add: goodDate.add};
assertThrows(() => badDateTime.add("P1D"), TypeError);
// Throw in ToLimitedTemporalDuration
assertThrows(() => (new Temporal.PlainDate(2021, 7, 20)).add("bad duration"),
RangeError);

View File

@ -0,0 +1,30 @@
// 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 t1 = new Temporal.PlainDate(2021, 3, 14);
let t2 = new Temporal.PlainDate(2021, 3, 14);
let t3 = t1;
let t4 = new Temporal.PlainDate(2021, 3, 15);
let t5 = new Temporal.PlainDate(2021, 4, 14);
let t6 = new Temporal.PlainDate(2022, 3, 14);
// years in 4 digits range
assertEquals(0, Temporal.PlainDate.compare(t1, t1));
assertEquals(0, Temporal.PlainDate.compare(t1, t2));
assertEquals(0, Temporal.PlainDate.compare(t1, t3));
assertEquals(0, Temporal.PlainDate.compare(t1, "2021-03-14"));
assertEquals(0, Temporal.PlainDate.compare(t1, "2021-03-14T23:59:59"));
assertEquals(1, Temporal.PlainDate.compare(t4, t1));
assertEquals(1, Temporal.PlainDate.compare(t5, t1));
assertEquals(1, Temporal.PlainDate.compare(t6, t1));
assertEquals(-1, Temporal.PlainDate.compare(t1, t4));
assertEquals(-1, Temporal.PlainDate.compare(t1, t5));
assertEquals(-1, Temporal.PlainDate.compare(t1, t6));
assertEquals(1, Temporal.PlainDate.compare("2021-07-21", t1));
// Test Throw
assertThrows(() => Temporal.PlainDate.compare(t1, "invalid iso8601 string"),
RangeError);
assertThrows(() => Temporal.PlainDate.compare("invalid iso8601 string", t1),
RangeError);

View File

@ -0,0 +1,82 @@
// 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.PlainDate(1911, 10, 10);
assertPlainDate(d1, 1911, 10, 10);
let d2 = new Temporal.PlainDate(2020, 3, 12);
assertPlainDate(d2, 2020, 3, 12);
let d3 = new Temporal.PlainDate(1, 12, 25);
assertPlainDate(d3, 1, 12, 25);
let d4 = new Temporal.PlainDate(1970, 1, 1);
assertPlainDate(d4, 1970, 1, 1);
let d5 = new Temporal.PlainDate(-10, 12, 1);
assertPlainDate(d5, -10, 12, 1);
let d6 = new Temporal.PlainDate(-25406, 1, 1);
assertPlainDate(d6, -25406, 1, 1);
let d7 = new Temporal.PlainDate(26890, 12, 31);
assertPlainDate(d7, 26890, 12, 31);
assertThrows(() => Temporal.PlainDate(2021, 7, 1), TypeError);
assertThrows(() => new Temporal.PlainDate(), RangeError);
assertThrows(() => new Temporal.PlainDate(2021), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 0), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 7), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 13), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 7, 0), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 7, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, -7, 1), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, -7, -1), RangeError);
// Wrong month
assertThrows(() => new Temporal.PlainDate(2021, 0, 1), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 13, 1), RangeError);
// Right day for month
assertPlainDate((new Temporal.PlainDate(2021, 1, 31)), 2021, 1, 31);
assertPlainDate((new Temporal.PlainDate(2021, 2, 28)), 2021, 2, 28);
assertPlainDate((new Temporal.PlainDate(2021, 3, 31)), 2021, 3, 31);
assertPlainDate((new Temporal.PlainDate(2021, 4, 30)), 2021, 4, 30);
assertPlainDate((new Temporal.PlainDate(2021, 5, 31)), 2021, 5, 31);
assertPlainDate((new Temporal.PlainDate(2021, 6, 30)), 2021, 6, 30);
assertPlainDate((new Temporal.PlainDate(2021, 7, 31)), 2021, 7, 31);
assertPlainDate((new Temporal.PlainDate(2021, 8, 31)), 2021, 8, 31);
assertPlainDate((new Temporal.PlainDate(2021, 9, 30)), 2021, 9, 30);
assertPlainDate((new Temporal.PlainDate(2021, 10, 31)), 2021, 10, 31);
assertPlainDate((new Temporal.PlainDate(2021, 11, 30)), 2021, 11, 30);
assertPlainDate((new Temporal.PlainDate(2021, 12, 31)), 2021, 12, 31);
// Check Feb 29 for Leap year
assertThrows(() => new Temporal.PlainDate(1900, 2, 29), RangeError);
assertPlainDate((new Temporal.PlainDate(2000, 2, 29)), 2000, 2, 29);
assertThrows(() => new Temporal.PlainDate(2001, 2, 29), RangeError);
assertThrows(() => new Temporal.PlainDate(2002, 2, 29), RangeError);
assertThrows(() => new Temporal.PlainDate(2003, 2, 29), RangeError);
assertPlainDate((new Temporal.PlainDate(2004, 2, 29)), 2004, 2, 29);
assertThrows(() => new Temporal.PlainDate(2100, 2, 29), RangeError);
// Wrong day for month
assertThrows(() => new Temporal.PlainDate(2021, 1, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 2, 29), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 3, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 4, 31), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 5, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 6, 31), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 7, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 8, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 9, 31), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 10, 32), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 11, 31), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 12, 32), RangeError);
// Infinty
assertThrows(() => new Temporal.PlainDate(Infinity, 12, 1), RangeError);
assertThrows(() => new Temporal.PlainDate(-Infinity, 12, 1), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 12, Infinity), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, 12, -Infinity), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, -Infinity, 1), RangeError);
assertThrows(() => new Temporal.PlainDate(2021, Infinity, 1), RangeError);
// TODO Test calendar
//

View File

@ -0,0 +1,15 @@
// 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.PlainDate(2021, 2, 28);
let d2 = Temporal.PlainDate.from("2021-02-28");
let d3 = Temporal.PlainDate.from("2021-01-28");
assertEquals(d1.equals(d2), true);
assertEquals(d1.equals(d3), false);
assertEquals(d2.equals(d3), false);
let badDate = {equals: d1.equals};
assertThrows(() => badDate.equals(d1), TypeError);

View File

@ -0,0 +1,51 @@
// 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 = Temporal.Now.plainDateISO();
// 1. Set options to ? GetOptionsObject(options).
[true, false, "string is invalid", Symbol(),
123, 456n, Infinity, NaN, null].forEach(function(invalidOptions) {
assertThrows(() => Temporal.PlainDate.from( d1, invalidOptions), TypeError);
});
// a. Perform ? ToTemporalOverflow(options).
assertThrows(() => Temporal.PlainDate.from(
d1, {overflow: "invalid overflow"}), RangeError);
[undefined, {}, {overflow: "constrain"}, {overflow: "reject"}].forEach(
function(validOptions) {
let d = new Temporal.PlainDate(1, 2, 3);
let d2 = Temporal.PlainDate.from(d, validOptions);
assertEquals(1, d2.year);
assertEquals(2, d2.month);
assertEquals(3, d2.day);
assertEquals("iso8601", d2.calendar.id);
});
[undefined, {}, {overflow: "constrain"}, {overflow: "reject"}].forEach(
function(validOptions) {
let d3 = Temporal.PlainDate.from( {year:9, month: 8, day:7}, validOptions);
assertEquals(9, d3.year);
assertEquals(8, d3.month);
assertEquals("M08", d3.monthCode);
assertEquals(7, d3.day);
assertEquals("iso8601", d3.calendar.id);
});
[undefined, {}, {overflow: "constrain"}].forEach(
function(validOptions) {
let d4 = Temporal.PlainDate.from( {year:9, month: 14, day:32}, validOptions);
assertEquals(9, d4.year);
assertEquals(12, d4.month);
assertEquals("M12", d4.monthCode);
assertEquals(31, d4.day);
assertEquals("iso8601", d4.calendar.id);
});
assertThrows(() => Temporal.PlainDate.from(
{year:9, month: 14, day:30}, {overflow: "reject"}), RangeError);
assertThrows(() => Temporal.PlainDate.from(
{year:9, month: 12, day:32}, {overflow: "reject"}), RangeError);

View File

@ -0,0 +1,8 @@
// 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.PlainDate(2021, 12, 11, {get id() {return "custom";}});
assertEquals("custom", d1.calendar.id);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{day: function(like) {return 2;}});
assertEquals(2, d1.day);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{dayOfWeek: function(like) {return 2;}});
assertEquals(2, d1.dayOfWeek);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{dayOfYear: function(like) {return 2;}});
assertEquals(2, d1.dayOfYear);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{daysInMonth: function(like) {return 2;}});
assertEquals(2, d1.daysInMonth);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{daysInWeek: function(like) {return 2;}});
assertEquals(2, d1.daysInWeek);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{daysInYear: function(like) {return 350;}});
assertEquals(350, d1.daysInYear);

View File

@ -0,0 +1,10 @@
// 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 ret = "foo bar";
let d1 = new Temporal.PlainDate(2021, 12, 11,
{era: function(like) {return ret;}});
assertEquals(ret, d1.era);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{eraYear: function(like) {return 3838;}});
assertEquals(3838, d1.eraYear);

View File

@ -0,0 +1,10 @@
// 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 ret = "foo bar";
let d1 = new Temporal.PlainDate(2021, 12, 11,
{inLeapYear: function(like) {return ret;}});
assertEquals(ret, d1.inLeapYear);

View File

@ -0,0 +1,21 @@
// 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.PlainDate(1911, 10, 10);
assertPlainDate(d1, 1911, 10, 10);
let d2 = new Temporal.PlainDate(2020, 3, 12);
assertPlainDate(d2, 2020, 3, 12);
let d3 = new Temporal.PlainDate(1, 12, 25);
assertPlainDate(d3, 1, 12, 25);
let d4 = new Temporal.PlainDate(1970, 1, 1);
assertPlainDate(d4, 1970, 1, 1);
let d5 = new Temporal.PlainDate(-10, 12, 1);
assertPlainDate(d5, -10, 12, 1);
let d6 = new Temporal.PlainDate(-25406, 1, 1);
assertPlainDate(d6, -25406, 1, 1);
let d7 = new Temporal.PlainDate(26890, 12, 31);
assertPlainDate(d7, 26890, 12, 31);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{month: function(like) {return 2;}});
assertEquals(2, d1.month);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{monthCode: function(like) {return "M02";}});
assertEquals("M02", d1.monthCode);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{monthsInYear: function(like) {return 14;}});
assertEquals(14, d1.monthsInYear);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{weekOfYear: function(like) {return 20;}});
assertEquals(20, d1.weekOfYear);

View File

@ -0,0 +1,9 @@
// 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.PlainDate(2021, 12, 11,
{year: function(like) {return 2;}});
assertEquals(2, d1.year);

View File

@ -0,0 +1,18 @@
// 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
// years in 4 digits range
assertEquals("2021-07-01", (new Temporal.PlainDate(2021, 7, 1)).toJSON());
assertEquals("9999-12-31", (new Temporal.PlainDate(9999, 12, 31)).toJSON());
assertEquals("1000-01-01", (new Temporal.PlainDate(1000, 1, 1)).toJSON());
// years out of 4 digits range
assertEquals("+010000-01-01", (new Temporal.PlainDate(10000, 1, 1)).toJSON());
assertEquals("+025021-07-01", (new Temporal.PlainDate(25021, 7, 1)).toJSON());
assertEquals("+000999-12-31", (new Temporal.PlainDate(999, 12, 31)).toJSON());
assertEquals("+000099-08-01", (new Temporal.PlainDate(99, 8, 1)).toJSON());
assertEquals("-000020-09-30", (new Temporal.PlainDate(-20, 9, 30)).toJSON());
assertEquals("-002021-07-01", (new Temporal.PlainDate(-2021, 7, 1)).toJSON());
assertEquals("-022021-07-01", (new Temporal.PlainDate(-22021, 7, 1)).toJSON());

View File

@ -0,0 +1,33 @@
// 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.PlainDate(2021, 12, 11);
let badDate = { toPlainDateTime: d1.toPlainDateTime }
assertThrows(() => badDate.toPlainDateTime(), TypeError);
assertThrows(() => d1.toPlainDateTime(null), RangeError);
assertThrows(() => d1.toPlainDateTime("string is invalid"), RangeError);
assertThrows(() => d1.toPlainDateTime(true), RangeError);
assertThrows(() => d1.toPlainDateTime(false), RangeError);
assertThrows(() => d1.toPlainDateTime(NaN), RangeError);
assertThrows(() => d1.toPlainDateTime(Infinity), RangeError);
// assertThrows(() => d1.toPlainDateTime(123), RangeError);
//assertThrows(() => d1.toPlainDateTime(456n), RangeError);
assertThrows(() => d1.toPlainDateTime(Symbol()), TypeError);
assertThrows(() => d1.toPlainDateTime({}), TypeError);
assertThrows(() => d1.toPlainDateTime({hour: 23}), TypeError);
assertThrows(() => d1.toPlainDateTime({minute: 23}), TypeError);
assertThrows(() => d1.toPlainDateTime({second: 23}), TypeError);
assertThrows(() => d1.toPlainDateTime({millisecond: 23}), TypeError);
assertThrows(() => d1.toPlainDateTime({microecond: 23}), TypeError);
assertThrows(() => d1.toPlainDateTime({nanosecond: 23}), TypeError);
assertPlainDateTime(d1.toPlainDateTime(),
2021, 12, 11, 0, 0, 0, 0, 0, 0);
assertPlainDateTime(d1.toPlainDateTime(
{hour: 9, minute: 8, second: 7, millisecond: 6, microsecond: 5, nanosecond: 4}),
2021, 12, 11, 9, 8, 7, 6, 5, 4);

View File

@ -0,0 +1,12 @@
// 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.PlainDate(2021, 12, 11);
let badDateTime = { toPlainMonthDay: d1.toPlainMonthDay }
assertThrows(() => badDateTime.toPlainMonthDay(), TypeError);
assertPlainMonthDay(d1.toPlainMonthDay(), "M12", 11);

View File

@ -0,0 +1,12 @@
// 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.PlainDate(2021, 12, 11);
let badDate = { toPlainYearMonth: d1.toPlainYearMonth }
assertThrows(() => badDate.toPlainYearMonth(), TypeError);
assertPlainYearMonth(d1.toPlainYearMonth(), 2021, 12);

View File

@ -0,0 +1,7 @@
// 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 = Temporal.Now.plainDateISO();
assertThrows(() => d1.valueOf(), TypeError);

View File

@ -0,0 +1,32 @@
// 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.PlainDate(1911, 10, 10);
let badDate = { withCalendar: d1.withCalendar }
assertThrows(() => badDate.withCalendar("iso8601"), TypeError);
// A simplified version of Republic of China calendar
let rocCal = {
iso8601: new Temporal.Calendar("iso8601"),
get id() {return "roc";},
dateFromFields: function(fields, options) {
fields.year -= 1911;
return this.iso8601.dateFromFields(fields, options);
},
year: function(date) { return this.iso8601.year(date) - 1911; },
month: function(date) { return this.iso8601.month(date); },
monthCode: function(date) { return this.iso8601.monthCode(date); },
day: function(date) { return this.iso8601.day(date); },
};
let d2 = d1.withCalendar(rocCal);
assertEquals(d2.calendar.id, "roc");
assertPlainDate(d2, 0, 10, 10);
let d3 = d2.withCalendar("iso8601");
assertEquals(d3.calendar.id, "iso8601");
assertPlainDate(d3, 1911, 10, 10);

View File

@ -0,0 +1,59 @@
// 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.PlainDate(1911, 10, 10);
assertPlainDate(d1.with({year:2021}), 2021, 10, 10);
assertPlainDate(d1.with({month:11}), 1911, 11, 10);
assertPlainDate(d1.with({monthCode:"M05"}), 1911, 5, 10);
assertPlainDate(d1.with({day:30}), 1911, 10, 30);
assertPlainDate(d1.with({year:2021, hour: 30}), 2021, 10, 10);
assertPlainDate(d1.with({month:11, minute: 71}), 1911, 11, 10);
assertPlainDate(d1.with({monthCode:"M05", second: 90}), 1911, 5, 10);
assertPlainDate(d1.with({day:30, era:"BC" }), 1911, 10, 30);
// A simplified version of Republic of China calendar
let rocCal = {
iso8601: new Temporal.Calendar("iso8601"),
get id() {return "roc";},
dateFromFields: function(fields, options) {
fields.year -= 1911;
return this.iso8601.dateFromFields(fields, options);
},
year: function(date) { return this.iso8601.year(date) - 1911; },
month: function(date) { return this.iso8601.month(date); },
monthCode: function(date) { return this.iso8601.monthCode(date); },
day: function(date) { return this.iso8601.day(date); },
};
let d2 = new Temporal.PlainDate(2021, 7, 20, rocCal);
assertPlainDate(d2, 110, 7, 20);
assertPlainDate(d2.with({year: 1912}), 1, 7, 20);
assertPlainDate(d2.with({year: 1987}), 76, 7, 20);
assertThrows(() => d1.with(new Temporal.PlainDate(2021, 7, 1)), TypeError);
assertThrows(() => d1.with(new Temporal.PlainDateTime(2021, 7, 1, 12, 13)), TypeError);
assertThrows(() => d1.with(new Temporal.PlainTime(1, 12, 13)), TypeError);
assertThrows(() => d1.with(new Temporal.PlainYearMonth(1991, 12)), TypeError);
assertThrows(() => d1.with(new Temporal.PlainMonthDay(5, 12)), TypeError);
assertThrows(() => d1.with("2012-05-13"), TypeError);
assertThrows(() => d1.with({calendar: "iso8601"}), TypeError);
assertThrows(() => d1.with({timeZone: "UTC"}), TypeError);
assertThrows(() => d1.with(true), TypeError);
assertThrows(() => d1.with(false), TypeError);
assertThrows(() => d1.with(NaN), TypeError);
assertThrows(() => d1.with(Infinity), TypeError);
assertThrows(() => d1.with(1234), TypeError);
assertThrows(() => d1.with(567n), TypeError);
assertThrows(() => d1.with(Symbol()), TypeError);
assertThrows(() => d1.with("string"), TypeError);
assertThrows(() => d1.with({}), TypeError);
assertThrows(() => d1.with([]), TypeError);
let badDate = { with: d1.with }
assertThrows(() => badDate.with({day: 3}), TypeError);