Add and expand tests for Duration constructor / from().

This commit is contained in:
Ms2ger 2022-03-08 17:42:20 +01:00 committed by Rick Waldron
parent d45476b9fd
commit c6c31c8dac
16 changed files with 183 additions and 32 deletions

View File

@ -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");

View File

@ -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");

View File

@ -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);

View File

@ -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 }));

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.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));
}

View File

@ -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);

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -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));

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -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");

View File

@ -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");