Skip to content

Commit

Permalink
Add and expand tests for Duration constructor / from().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger authored and rwaldron committed Mar 10, 2022
1 parent d45476b commit c6c31c8
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 32 deletions.
16 changes: 16 additions & 0 deletions test/built-ins/Temporal/Duration/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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.duration
description: Basic constructor tests.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

TemporalHelpers.assertDuration(new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 0),
5, 5, 5, 5, 5, 5, 5, 5, 5, 0, "positive");
TemporalHelpers.assertDuration(new Temporal.Duration(-5, -5, -5, -5, -5, -5, -5, -5, -5, 0),
-5, -5, -5, -5, -5, -5, -5, -5, -5, 0, "negative");
TemporalHelpers.assertDuration(new Temporal.Duration(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "negative zero");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/days-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.days, 0, "days default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.days, 0, "days default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, "implicit");
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
/*---
esid: sec-temporal.duration.from
description: Property bag is converted to Duration; Duration is copied
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const d1 = Temporal.Duration.from({ milliseconds: 1000 });
assert.sameValue(d1.seconds, 0);
assert.sameValue(d1.milliseconds, 1000);
const d1 = Temporal.Duration.from({ milliseconds: 1000, month: 1 });
TemporalHelpers.assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0);

const d2 = Temporal.Duration.from(d1);
assert.notSameValue(d1, d2);
assert.sameValue(d1.seconds, 0);
assert.sameValue(d1.milliseconds, 1000);
assert.sameValue(d2.seconds, 0);
assert.sameValue(d2.milliseconds, 1000);
TemporalHelpers.assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0);
TemporalHelpers.assertDuration(d2, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0);
26 changes: 26 additions & 0 deletions test/built-ins/Temporal/Duration/from/argument-object-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.duration.from
description: Invalid object arguments.
features: [Temporal]
---*/

const tests = [
{ years: 0.5 },
{ months: 0.5 },
{ weeks: 0.5 },
{ days: 0.5 },
{ hours: 0.5, minutes: 20 },
{ hours: 0.5, seconds: 15 },
{ minutes: 10.7, nanoseconds: 400 },
{ hours: 1, minutes: -30 },
];

for (const input of tests) {
assert.throws(RangeError, () => Temporal.Duration.from(input));
}

assert.throws(TypeError, () => Temporal.Duration.from({}));
assert.throws(TypeError, () => Temporal.Duration.from({ month: 12 }));
32 changes: 32 additions & 0 deletions test/built-ins/Temporal/Duration/from/argument-string-invalid.js
Original file line number Diff line number Diff line change
@@ -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.duration.from
description: Invalid string arguments.
features: [Temporal]
---*/

const tests = [
"P1Y1M1W1DT1H1M1.123456789123S",
"P0.5Y",
"P1Y0,5M",
"P1Y1M0.5W",
"P1Y1M1W0,5D",
"P1Y1M1W1DT0.5H5S",
"P1Y1M1W1DT1.5H0,5M",
"P1Y1M1W1DT1H0.5M0.5S",
"P",
"PT",
"-P",
"-PT",
"+P",
"+PT",
"P1Y1M1W1DT1H1M1.01Sjunk",
"P-1Y1M",
"P1Y-1M"
];

for (const input of tests) {
assert.throws(RangeError, () => Temporal.Duration.from(input));
}
48 changes: 48 additions & 0 deletions test/built-ins/Temporal/Duration/from/argument-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.duration.from
description: Basic string arguments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

TemporalHelpers.assertDuration(Temporal.Duration.from("P1D"),
0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("p1y1m1dt1h1m1s"),
1, 1, 0, 1, 1, 1, 1, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.1S"),
1, 1, 1, 1, 1, 1, 1, 100, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.12S"),
1, 1, 1, 1, 1, 1, 1, 120, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.123S"),
1, 1, 1, 1, 1, 1, 1, 123, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.1234S"),
1, 1, 1, 1, 1, 1, 1, 123, 400, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.12345S"),
1, 1, 1, 1, 1, 1, 1, 123, 450, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.123456S"),
1, 1, 1, 1, 1, 1, 1, 123, 456, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.1234567S"),
1, 1, 1, 1, 1, 1, 1, 123, 456, 700);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.12345678S"),
1, 1, 1, 1, 1, 1, 1, 123, 456, 780);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1.123456789S"),
1, 1, 1, 1, 1, 1, 1, 123, 456, 789);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1Y1M1W1DT1H1M1,12S"),
1, 1, 1, 1, 1, 1, 1, 120, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1DT0.5M"),
0, 0, 0, 1, 0, 0, 30, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("P1DT0,5H"),
0, 0, 0, 1, 0, 30, 0, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("+P1D"),
0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("-P1D"),
0, 0, 0, -1, 0, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("\u2212P1D"),
0, 0, 0, -1, 0, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(Temporal.Duration.from("-P1Y1M1W1DT1H1M1.123456789S"),
-1, -1, -1, -1, -1, -1, -1, -123, -456, -789);
TemporalHelpers.assertDuration(Temporal.Duration.from("PT100M"),
0, 0, 0, 0, 0, 100, 0, 0, 0, 0);
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/hours-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.hours, 0, "hours default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.hours, 0, "hours default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/microseconds-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1, 1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.microseconds, 0, "microseconds default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.microseconds, 0, "microseconds default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/milliseconds-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.milliseconds, 0, "milliseconds default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.milliseconds, 0, "milliseconds default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/minutes-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.minutes, 0, "minutes default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.minutes, 0, "minutes default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, "implicit");
19 changes: 19 additions & 0 deletions test/built-ins/Temporal/Duration/mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.duration
description: Constructor with mixed signs.
features: [Temporal]
---*/

assert.throws(RangeError, () => new Temporal.Duration(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, -1, 1, 1, 1, 1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, -1, 1, 1, 1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, -1, 1, 1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, -1, 1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, 1, -1, 1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, 1, 1, -1, 1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, 1, 1, 1, -1, 1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, 1, 1, 1, 1, -1, 1));
assert.throws(RangeError, () => new Temporal.Duration(1, 1, 1, 1, 1, 1, 1, 1, 1, -1));
11 changes: 6 additions & 5 deletions test/built-ins/Temporal/Duration/months-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const years = 1;
const args = [1];

const explicit = new Temporal.Duration(years, undefined);
assert.sameValue(explicit.months, 0, "months default argument");
const explicit = new Temporal.Duration(...args, undefined);
TemporalHelpers.assertDuration(explicit, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(years);
assert.sameValue(implicit.months, 0, "months default argument");
const implicit = new Temporal.Duration(...args);
TemporalHelpers.assertDuration(implicit, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/nanoseconds-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1, 1, 1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.nanoseconds, 0, "nanoseconds default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.nanoseconds, 0, "nanoseconds default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/seconds-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1, 1, 1, 1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.seconds, 0, "seconds default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.seconds, 0, "seconds default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, "implicit");
5 changes: 3 additions & 2 deletions test/built-ins/Temporal/Duration/weeks-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const args = [1, 1];

const explicit = new Temporal.Duration(...args, undefined);
assert.sameValue(explicit.weeks, 0, "weeks default argument");
TemporalHelpers.assertDuration(explicit, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
assert.sameValue(implicit.weeks, 0, "weeks default argument");
TemporalHelpers.assertDuration(implicit, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "implicit");
11 changes: 7 additions & 4 deletions test/built-ins/Temporal/Duration/years-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
/*---
esid: sec-temporal.duration
description: Undefined arguments should be treated as zero.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const explicit = new Temporal.Duration(undefined);
assert.sameValue(explicit.years, 0, "years default argument");
const args = [];

const implicit = new Temporal.Duration();
assert.sameValue(implicit.years, 0, "years default argument");
const explicit = new Temporal.Duration(...args, undefined);
TemporalHelpers.assertDuration(explicit, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit");

const implicit = new Temporal.Duration(...args);
TemporalHelpers.assertDuration(implicit, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit");

0 comments on commit c6c31c8

Please sign in to comment.