Skip to content

Commit

Permalink
Port some basic Temporal.PlainDateTime tests from Demitasse to test262 (
Browse files Browse the repository at this point in the history
tc39#3430)

* Create a Temporal.PlainDateTime with all arguments supplied.

Migrates some tests that currently exist in the
proposal-temporal repo.

* Check all data in Temporal.PlainDateTimes, variously constructed

Enrich existing tests to check all basic data in the
instance of `Temporal.PlainDateTime`, not just a single
field.

These additional checks were motivated by the migration of
existing Demitasse tests in the proposal-temporal repo to
test262. The Demitasse tests check more than a single
field.
  • Loading branch information
jessealama authored Mar 21, 2022
1 parent f71d5e2 commit 7b78d4b
Show file tree
Hide file tree
Showing 68 changed files with 1,571 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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.compare
description: Plain object arguments may throw if they do not contain sufficient information
features: [Temporal]
---*/

const dt1 = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
const dt2 = new Temporal.PlainDateTime(2019, 10, 29, 10, 46, 38, 271, 986, 102);

assert.throws(
TypeError,
() => Temporal.PlainDateTime.compare({ year: 1976 }, dt2),
"object must contain at least the required properties (first arg)"
);

assert.throws(
TypeError,
() => Temporal.PlainDateTime.compare(dt1, { year: 2019 }),
"object must contain at least the required properties (second arg)"
);
29 changes: 29 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/compare/basic.js
Original file line number Diff line number Diff line change
@@ -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.compare
description: Checking a typical case (nothing undefined, no NaNs, does not throw, etc.)
features: [Temporal]
---*/

const dt1 = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
const dt2 = new Temporal.PlainDateTime(2019, 10, 29, 10, 46, 38, 271, 986, 102);

assert.sameValue(
Temporal.PlainDateTime.compare(dt1, dt1),
0,
"equal"
);

assert.sameValue(
Temporal.PlainDateTime.compare(dt1, dt2),
-1,
"smaller/larger"
);

assert.sameValue(
Temporal.PlainDateTime.compare(dt2, dt1),
1,
"larger/smaller"
);
35 changes: 35 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/compare/cast.js
Original file line number Diff line number Diff line change
@@ -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.compare
description: Arguments may be casted (string, plain object)
features: [Temporal]
---*/

const dt1 = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
const dt2 = new Temporal.PlainDateTime(2019, 10, 29, 10, 46, 38, 271, 986, 102);

assert.sameValue(
Temporal.PlainDateTime.compare({ year: 1976, month: 11, day: 18, hour: 15 }, dt2),
-1,
"casts first argument (plain object)"
);

assert.sameValue(
Temporal.PlainDateTime.compare("1976-11-18T15:23:30.123456789", dt2),
-1,
"casts first argument (string)"
);

assert.sameValue(
Temporal.PlainDateTime.compare(dt1, { year: 2019, month: 10, day: 29, hour: 10 }),
-1,
"casts second argument (plain object)"
);

assert.sameValue(
Temporal.PlainDateTime.compare(dt1, "2019-10-29T10:46:38.271986102"),
-1,
"casts second argument (string)"
);
23 changes: 23 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/constructor-full.js
Original file line number Diff line number Diff line change
@@ -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
description: Checking an explicitly constructed instance with all arguments
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const calendar = Temporal.Calendar.from("iso8601");
const datetime = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789, calendar);

TemporalHelpers.assertPlainDateTime(datetime,
1976, 11, "M11", 18, 15, 23, 30, 123, 456, 789,
"check instance (all arguments supplied)"
);

assert.sameValue(
datetime.calendar,
calendar,
"calendar supplied in constructor can be extracted and is unchanged"
);
47 changes: 47 additions & 0 deletions test/built-ins/Temporal/PlainDateTime/datetime-math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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
description: Testing combinations of since, until, add, subtract, and negated
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const earlier = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
const later = new Temporal.PlainDateTime(2019, 10, 29, 10, 46, 38, 271, 986, 102);
const units = ["years", "months", "weeks", "days", "hours", "minutes", "seconds"];

units.forEach((largestUnit) => {
const diff = later.since(earlier, { largestUnit });
TemporalHelpers.assertDurationsEqual(
earlier.since(later, { largestUnit }),
diff.negated(),
`(${earlier}).since(${later}) == (${later}).since(${earlier}).negated()`
);
TemporalHelpers.assertDurationsEqual(
earlier.until(later, { largestUnit }),
diff,
`(${earlier}).until(${later}) == (${later}).since(${earlier})`
);
assert.sameValue(
earlier.add(diff).equals(later),
true,
`(${earlier}).add(${diff}) == (${later})`
);
assert.sameValue(
later.subtract(diff).equals(earlier),
true,
`(${later}).subtract(${diff}) == (${earlier})`
);
assert.sameValue(
earlier.subtract(diff.negated()).equals(later),
true,
"symmetrical with regard to negative durations (1)"
);
assert.sameValue(
later.add(diff.negated()).equals(earlier),
true,
"symmetrical with regard to negative durations (2)"
);
});
15 changes: 11 additions & 4 deletions test/built-ins/Temporal/PlainDateTime/hour-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
esid: sec-temporal.plaindatetime
description: Hour argument defaults to 0 if not given
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const args = [2000, 5, 2];

const explicit = new Temporal.PlainDateTime(...args, undefined);
assert.sameValue(explicit.hour, 0, "hour default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args, undefined),
2000, 5, "M05", 2, 0, 0, 0, 0, 0, 0,
"hour default argument (argument present)"
);

const implicit = new Temporal.PlainDateTime(...args);
assert.sameValue(implicit.hour, 0, "hour default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args),
2000, 5, "M05", 2, 0, 0, 0, 0, 0, 0,
"hour default argument (argument missing)"
);
16 changes: 12 additions & 4 deletions test/built-ins/Temporal/PlainDateTime/microsecond-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
esid: sec-temporal.plaindatetime
description: Microsecond argument defaults to 0 if not given
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const args = [2000, 5, 2, 12, 34, 56, 123];

const explicit = new Temporal.PlainDateTime(...args, undefined);
assert.sameValue(explicit.microsecond, 0, "microsecond default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args, undefined),
2000, 5, "M05", 2, 12, 34, 56, 123, 0, 0,
"microsecond default argument (argument present)"
);

TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args),
2000, 5, "M05", 2, 12, 34, 56, 123, 0, 0,
"microsecond default argument (argument missing)"
);

const implicit = new Temporal.PlainDateTime(...args);
assert.sameValue(implicit.microsecond, 0, "microsecond default argument");
15 changes: 11 additions & 4 deletions test/built-ins/Temporal/PlainDateTime/millisecond-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
esid: sec-temporal.plaindatetime
description: Millisecond argument defaults to 0 if not given
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const args = [2000, 5, 2, 12, 34, 56];

const explicit = new Temporal.PlainDateTime(...args, undefined);
assert.sameValue(explicit.millisecond, 0, "millisecond default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args, undefined),
2000, 5, "M05", 2, 12, 34, 56, 0, 0, 0,
"millisecond default argument (argument present)"
);

const implicit = new Temporal.PlainDateTime(...args);
assert.sameValue(implicit.millisecond, 0, "millisecond default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args),
2000, 5, "M05", 2, 12, 34, 56, 0, 0, 0,
"millisecond default argument (argument missing)"
);
15 changes: 11 additions & 4 deletions test/built-ins/Temporal/PlainDateTime/minute-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
esid: sec-temporal.plaindatetime
description: Minute argument defaults to 0 if not given
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const args = [2000, 5, 2, 12];

const explicit = new Temporal.PlainDateTime(...args, undefined);
assert.sameValue(explicit.minute, 0, "minute default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args, undefined),
2000, 5, "M05", 2, 12, 0, 0, 0, 0, 0,
"minute default argument (argument present)"
);

const implicit = new Temporal.PlainDateTime(...args);
assert.sameValue(implicit.minute, 0, "minute default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args),
2000, 5, "M05", 2, 12, 0, 0, 0, 0, 0,
"minute default argument (argument missing)"
);
15 changes: 11 additions & 4 deletions test/built-ins/Temporal/PlainDateTime/nanosecond-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
esid: sec-temporal.plaindatetime
description: Nanosecond argument defaults to 0 if not given
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const args = [2000, 5, 2, 12, 34, 56, 123, 456];

const explicit = new Temporal.PlainDateTime(...args, undefined);
assert.sameValue(explicit.nanosecond, 0, "nanosecond default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args, undefined),
2000, 5, "M05", 2, 12, 34, 56, 123, 456, 0,
"nanosecond default argument (argument present)"
);

const implicit = new Temporal.PlainDateTime(...args);
assert.sameValue(implicit.nanosecond, 0, "nanosecond default argument");
TemporalHelpers.assertPlainDateTime(
new Temporal.PlainDateTime(...args),
2000, 5, "M05", 2, 12, 34, 56, 123, 456, 0,
"nanosecond default argument (argument missing)"
);
Original file line number Diff line number Diff line change
@@ -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.prototype.add
description: Ambiguous addition is handled according to the overflow option
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const jan31 = new Temporal.PlainDateTime(2020, 1, 31, 15, 0);

TemporalHelpers.assertPlainDateTime(
jan31.add({ months: 1 }),
2020, 2, "M02", 29, 15, 0, 0, 0, 0, 0,
"constrain when ambiguous result (overflow options not supplied)"
);

TemporalHelpers.assertPlainDateTime(
jan31.add({ months: 1 }, { overflow: "constrain" }),
2020, 2, "M02", 29, 15, 0, 0, 0, 0, 0,
"constrain when ambiguous result (overflow options supplied)"
);

assert.throws(
RangeError,
() => jan31.add({ months: 1 }, { overflow: "reject" }),
"throw when ambiguous result with reject"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.prototype.add
description: Duration object arguments are handled
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const jan31 = new Temporal.PlainDateTime(2020, 1, 31, 15, 0);

TemporalHelpers.assertPlainDateTime(
jan31.add(Temporal.Duration.from("P1MT1S")),
2020, 2, "M02", 29, 15, 0, 1, 0, 0, 0,
"Duration argument"
);
Original file line number Diff line number Diff line change
@@ -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.prototype.add
description: At least one recognized property has to be present in argument
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const jan31 = new Temporal.PlainDateTime(2020, 1, 31, 15, 0);

assert.throws(
TypeError,
() => jan31.add({}),
"empty object not acceptable"
);

assert.throws(
TypeError,
() => jan31.add({ month: 12 }), // should be "months"
"misspelled property in argument throws if no other properties are present"
);

assert.throws(
TypeError,
() => jan31.add({ nonsense: true }),
"unrecognized properties throw if no other recognized property is present"
);

TemporalHelpers.assertPlainDateTime(
jan31.add({ nonsense: 1, days: 1 }),
2020, 2, "M02", 1, 15, 0, 0, 0, 0, 0,
"unrecognized properties ignored provided at least one recognized property is present"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.prototype.add
description: Positive and negative values in the temporalDurationLike argument are not acceptable
features: [Temporal]
---*/

const jan31 = new Temporal.PlainDateTime(2020, 1, 31, 15, 0);

["constrain", "reject"].forEach((overflow) => {
assert.throws(
RangeError,
() => jan31.add({ hours: 1, minutes: -30 }, { overflow }),
`mixed positive and negative values always throw (overflow = "${overflow}")`
);
});
Loading

0 comments on commit 7b78d4b

Please sign in to comment.