Temporal: Port `PlainDateTime`'s `from` tests

This commit is contained in:
Jesse Alama 2022-04-13 16:39:41 +02:00 committed by Philip Chimento
parent b68c751f5b
commit 089c74ce59
17 changed files with 551 additions and 0 deletions

View File

@ -0,0 +1,27 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: A number is converted to a string, which may be a valid ISO string
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(19761118),
1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0,
"Number may be an acceptable argument"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from(1),
"number, in decimal, has the wrong format (too few digits)"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from(1234567890),
"number, in decimal, has the wrong format (too many digits)"
);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: A plain object argument needs to specify a month
features: [Temporal]
---*/
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from({year: 1976, month: 11, monthCode: "M12", day: 18}),
"month and monthCode must agree"
);
assert.throws(
TypeError,
() => Temporal.PlainDateTime.from({year: 1976, month: undefined, monthCode: undefined, day: 18}),
"required prop undefined throws"
);
assert.throws(
TypeError,
() => Temporal.PlainDateTime.from({year: 1976, day: 18, hour: 15, minute: 23, second: 30, millisecond: 123}),
"required prop missing throws"
);
assert.throws(
TypeError,
() => Temporal.PlainDateTime.from({year: 1976, months: 11, day: 18}),
"plural \"months\" is not acceptable"
);

View File

@ -0,0 +1,40 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Plain objects are acceptable
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from({year: 1976, month: 11, monthCode: "M11", day: 18}),
1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0,
"plain object with month & month code"
);
assert.throws(
TypeError,
() => Temporal.PlainDateTime.from({}),
"empty object throws"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from({year: 1976, month: 11, day: 18, millisecond: 123}),
1976, 11, "M11", 18, 0, 0, 0, 123, 0, 0,
"plain object with month but not month code"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from({year: 1976, monthCode: "M09", day: 18, millisecond: 123}),
1976, 9, "M09", 18, 0, 0, 0, 123, 0, 0,
"plain object with month code but not month"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from({year: 1976, month: 11, day: 18, hours: 12}),
1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0,
"incorrectly-spelled properties (e.g., plural \"hours\") are ignored"
);

View File

@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: A Temporal PlainDateTime object is an acceptable argument
features: [Temporal]
includes: [temporalHelpers.js]
---*/
const orig = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 20, 123, 456, 789);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(orig),
1976, 11, "M11", 18, 15, 23, 20, 123, 456, 789,
"PlainDateTime is copied"
);
assert.notSameValue(
Temporal.PlainDateTime.from(orig),
orig,
"When a PlainDateTime is given, the returned value is not the original PlainDateTime"
);

View File

@ -0,0 +1,15 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Comma may be used as a decimal separator
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30,12"),
1976, 11, "M11", 18, 15, 23, 30, 120, 0, 0,
"comma decimal separator"
);

View File

@ -0,0 +1,59 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Reject string argument if it cannot be parsed
features: [Temporal]
---*/
const invalidStrings = [
// invalid ISO strings:
"",
"obviously invalid",
"2020-01-00",
"2020-01-32",
"2020-02-30",
"2021-02-29",
"2020-00-01",
"2020-13-01",
"2020-01-01T",
"2020-01-01T25:00:00",
"2020-01-01T01:60:00",
"2020-01-01T01:60:61",
"2020-01-01junk",
"2020-01-01T00:00:00junk",
"2020-01-01T00:00:00+00:00junk",
"2020-01-01T00:00:00+00:00[UTC]junk",
"2020-01-01T00:00:00+00:00[UTC][u-ca=iso8601]junk",
"02020-01-01",
"2020-001-01",
"2020-01-001",
"2020-01-01T001",
"2020-01-01T01:001",
"2020-01-01T01:01:001",
// valid, but forms not supported in Temporal:
"2020-W01-1",
"2020-001",
"+0002020-01-01",
// valid, but this calendar must not exist:
"2020-01-01[u-ca=notexist]",
// may be valid in other contexts, but insufficient information for PlainDateTime:
"2020-01",
"+002020-01",
"01-01",
"2020-W01",
"P1Y",
"-P12Y",
// valid, but outside the supported range:
"-999999-01-01",
"+999999-01-01",
];
invalidStrings.forEach((s) => {
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from(s),
`invalid date-time string (${s})`
);
});

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Non-ASCII minus sign is acceptable
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.12\u221202:00"),
1976, 11, "M11", 18, 15, 23, 30, 120, 0, 0,
"variant minus sign (offset)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("\u2212009999-11-18T15:23:30.12"),
-9999, 11, "M11", 18, 15, 23, 30, 120, 0, 0,
"variant minus sign (leading minus)"
);

View File

@ -0,0 +1,36 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Extended format may be used
features: [Temporal]
includes: [temporalHelpers.js]
---*/
const expected = [1976, 11, "M11", 18, 15, 23, 30, 100, 0, 0];
const strs = [
"1976-11-18T152330.1+00:00",
"19761118T15:23:30.1+00:00",
"1976-11-18T15:23:30.1+0000",
"1976-11-18T152330.1+0000",
"19761118T15:23:30.1+0000",
"19761118T152330.1+00:00",
"19761118T152330.1+0000",
"+001976-11-18T152330.1+00:00",
"+0019761118T15:23:30.1+00:00",
"+001976-11-18T15:23:30.1+0000",
"+001976-11-18T152330.1+0000",
"+0019761118T15:23:30.1+0000",
"+0019761118T152330.1+00:00",
"+0019761118T152330.1+0000"
];
strs.forEach((s) => {
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(s),
...expected,
`mixture of basic and extended format (${s})`
);
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Some parts of a string argument may be omitted
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30+00"),
1976, 11, "M11", 18, 15, 23, 30, 0, 0, 0,
"optional parts (no minute after offset)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15"),
1976, 11, "M11", 18, 15, 0, 0, 0, 0, 0,
"optional parts (no minute in time part)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18"),
1976, 11, "M11", 18, 0, 0, 0, 0, 0, 0,
"optional parts (no time part)"
);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Invalid ISO string not acceptable even with overflow = constrain
features: [Temporal]
---*/
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from("2020-13-34T24:60", {}),
"constrain has no effect on invalid ISO string (empty options argument)"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from("2020-13-34T24:60", () => {}),
"constrain has no effect on invalid ISO string (nullary empty object function argument)"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from("2020-13-34T24:60", {overflow: "constrain"}),
"overflow = constrain has no effect on invalid ISO string"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from("2020-13-34T24:60", {overflow: "reject"}),
"overflow = reject has no effect on invalid ISO string"
);

View File

@ -0,0 +1,69 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Up to nine digits of sub-second precision are acceptable
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.1"),
1976, 11, "M11", 18, 15, 23, 30, 100, 0, 0,
"various precisions are possible (one decimal digit)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.12"),
1976, 11, "M11", 18, 15, 23, 30, 120, 0, 0,
"various precisions are possible (two decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.123"),
1976, 11, "M11", 18, 15, 23, 30, 123, 0, 0,
"various precisions are possible (three decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.1234"),
1976, 11, "M11", 18, 15, 23, 30, 123, 400, 0,
"various precisions are possible (four decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.12345"),
1976, 11, "M11", 18, 15, 23, 30, 123, 450, 0,
"various precisions are possible (five decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.123456"),
1976, 11, "M11", 18, 15, 23, 30, 123, 456, 0,
"various precisions are possible (six decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.1234567"),
1976, 11, "M11", 18, 15, 23, 30, 123, 456, 700,
"various precisions are possible (seven decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.12345678"),
1976, 11, "M11", 18, 15, 23, 30, 123, 456, 780,
"various precisions are possible (eight decimal digits)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.123456789"),
1976, 11, "M11", 18, 15, 23, 30, 123, 456, 789,
"various precisions are possible (nine decimal digits)"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from("1976-11-18T15:23:30.1234567891"),
"ten decimal digits is too much"
);

View File

@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Time separator in string argument can vary
features: [Temporal]
includes: [temporalHelpers.js]
---*/
const expected = [1976, 11, "M11", 18, 15, 23, 0, 0, 0, 0];
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23"),
...expected,
"variant time separators (uppercase T)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18t15:23"),
...expected,
"variant time separators (lowercase T)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18 15:23"),
...expected,
"variant time separators (space between date and time)"
);

View File

@ -0,0 +1,15 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Timezone, if specified, is ignored
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("2020-01-01T01:23:45[Asia/Kolkata]"),
2020, 1, "M01", 1, 1, 23, 45, 0, 0, 0,
"ignores if a timezone is specified"
);

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: String arguments are acceptable
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30"),
1976, 11, "M11", 18, 15, 23, 30, 0, 0, 0,
"date and time (no subseconds)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.001"),
1976, 11, "M11", 18, 15, 23, 30, 1, 0, 0,
"date and time (millisecond)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.001123"),
1976, 11, "M11", 18, 15, 23, 30, 1, 123, 0,
"date and time (microsecond)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("1976-11-18T15:23:30.001123456"),
1976, 11, "M11", 18, 15, 23, 30, 1, 123, 456,
"date and time (nanosecond)"
);

View File

@ -0,0 +1,35 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Leap seconds may be accepted or rejected
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("2016-12-31T23:59:60"),
2016, 12, "M12", 31, 23, 59, 59, 0, 0, 0,
"ISO string with leap second is constrained"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from("2016-12-31T23:59:60", {overflow: "reject"}),
2016, 12, "M12", 31, 23, 59, 59, 0, 0, 0,
"ISO string with leap second is constrained (overflow = reject)"
);
const leap = {year: 2016, month: 12, day: 31, hour: 23, minute: 59, second: 60};
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(leap),
2016, 12, "M12", 31, 23, 59, 59, 0, 0, 0,
"constrain leap second"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from(leap, {overflow: "reject"}),
"reject leap second (plain object argument)"
);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: By default, overflow = constrain
features: [Temporal]
includes: [temporalHelpers.js]
---*/
const date = {year: 2019, month: 1, day: 32};
const data = [2019, 1, "M01", 31, 0, 0, 0, 0, 0, 0];
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(date),
...data,
"by default, overflow is constrain (overflow options argument absent)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(date, {}),
...data,
"by default, overflow is constrain (options argument is empty plain object)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(date, () => {}),
...data,
"by default, overflow is constrain (options argument is empty function)"
);
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from(date, {overflow: "constrain"}),
...data,
"by default, overflow is constrain (overflow options argument present)"
);

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.from
description: Possibly throw if overflow is reject
features: [Temporal]
includes: [temporalHelpers.js]
---*/
TemporalHelpers.assertPlainDateTime(
Temporal.PlainDateTime.from({year: 2019, month: 1, day: 31}, {overflow: "reject"}),
2019, 1, "M01", 31, 0, 0, 0, 0, 0, 0,
"overflow reject, acceptable argument"
);
assert.throws(
RangeError,
() => Temporal.PlainDateTime.from({year: 2019, month: 1, day: 32}, {overflow: "reject"}),
"overflow reject, unacceptable argument"
);