From c6c31c8dac173f323546c05e5db048296ef51804 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 8 Mar 2022 17:42:20 +0100 Subject: [PATCH] Add and expand tests for Duration constructor / from(). --- test/built-ins/Temporal/Duration/basic.js | 16 +++++++ .../Temporal/Duration/days-undefined.js | 5 +- .../Duration/from/argument-existing-object.js | 12 ++--- .../Duration/from/argument-object-invalid.js | 26 ++++++++++ .../Duration/from/argument-string-invalid.js | 32 +++++++++++++ .../Temporal/Duration/from/argument-string.js | 48 +++++++++++++++++++ .../Temporal/Duration/hours-undefined.js | 5 +- .../Duration/microseconds-undefined.js | 5 +- .../Duration/milliseconds-undefined.js | 5 +- .../Temporal/Duration/minutes-undefined.js | 5 +- test/built-ins/Temporal/Duration/mixed.js | 19 ++++++++ .../Temporal/Duration/months-undefined.js | 11 +++-- .../Duration/nanoseconds-undefined.js | 5 +- .../Temporal/Duration/seconds-undefined.js | 5 +- .../Temporal/Duration/weeks-undefined.js | 5 +- .../Temporal/Duration/years-undefined.js | 11 +++-- 16 files changed, 183 insertions(+), 32 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/basic.js create mode 100644 test/built-ins/Temporal/Duration/from/argument-object-invalid.js create mode 100644 test/built-ins/Temporal/Duration/from/argument-string-invalid.js create mode 100644 test/built-ins/Temporal/Duration/from/argument-string.js create mode 100644 test/built-ins/Temporal/Duration/mixed.js diff --git a/test/built-ins/Temporal/Duration/basic.js b/test/built-ins/Temporal/Duration/basic.js new file mode 100644 index 0000000000..69cea7447c --- /dev/null +++ b/test/built-ins/Temporal/Duration/basic.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/days-undefined.js b/test/built-ins/Temporal/Duration/days-undefined.js index 716ad626f5..f4072e0470 100644 --- a/test/built-ins/Temporal/Duration/days-undefined.js +++ b/test/built-ins/Temporal/Duration/days-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/from/argument-existing-object.js b/test/built-ins/Temporal/Duration/from/argument-existing-object.js index 8173f7c96c..7d0cb17502 100644 --- a/test/built-ins/Temporal/Duration/from/argument-existing-object.js +++ b/test/built-ins/Temporal/Duration/from/argument-existing-object.js @@ -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); diff --git a/test/built-ins/Temporal/Duration/from/argument-object-invalid.js b/test/built-ins/Temporal/Duration/from/argument-object-invalid.js new file mode 100644 index 0000000000..31818010fc --- /dev/null +++ b/test/built-ins/Temporal/Duration/from/argument-object-invalid.js @@ -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 })); diff --git a/test/built-ins/Temporal/Duration/from/argument-string-invalid.js b/test/built-ins/Temporal/Duration/from/argument-string-invalid.js new file mode 100644 index 0000000000..62708e2939 --- /dev/null +++ b/test/built-ins/Temporal/Duration/from/argument-string-invalid.js @@ -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)); +} diff --git a/test/built-ins/Temporal/Duration/from/argument-string.js b/test/built-ins/Temporal/Duration/from/argument-string.js new file mode 100644 index 0000000000..e41100b3d6 --- /dev/null +++ b/test/built-ins/Temporal/Duration/from/argument-string.js @@ -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); diff --git a/test/built-ins/Temporal/Duration/hours-undefined.js b/test/built-ins/Temporal/Duration/hours-undefined.js index 2669656938..e068dee12c 100644 --- a/test/built-ins/Temporal/Duration/hours-undefined.js +++ b/test/built-ins/Temporal/Duration/hours-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/microseconds-undefined.js b/test/built-ins/Temporal/Duration/microseconds-undefined.js index e0f92cb967..1537ff7545 100644 --- a/test/built-ins/Temporal/Duration/microseconds-undefined.js +++ b/test/built-ins/Temporal/Duration/microseconds-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/milliseconds-undefined.js b/test/built-ins/Temporal/Duration/milliseconds-undefined.js index c460c6a06e..cee2888568 100644 --- a/test/built-ins/Temporal/Duration/milliseconds-undefined.js +++ b/test/built-ins/Temporal/Duration/milliseconds-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/minutes-undefined.js b/test/built-ins/Temporal/Duration/minutes-undefined.js index 265bd4fbc6..562622ef7a 100644 --- a/test/built-ins/Temporal/Duration/minutes-undefined.js +++ b/test/built-ins/Temporal/Duration/minutes-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/mixed.js b/test/built-ins/Temporal/Duration/mixed.js new file mode 100644 index 0000000000..5ba8517cf6 --- /dev/null +++ b/test/built-ins/Temporal/Duration/mixed.js @@ -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)); diff --git a/test/built-ins/Temporal/Duration/months-undefined.js b/test/built-ins/Temporal/Duration/months-undefined.js index f9cad3f216..8ad0734831 100644 --- a/test/built-ins/Temporal/Duration/months-undefined.js +++ b/test/built-ins/Temporal/Duration/months-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/nanoseconds-undefined.js b/test/built-ins/Temporal/Duration/nanoseconds-undefined.js index 2d4de39199..ec1d1518bb 100644 --- a/test/built-ins/Temporal/Duration/nanoseconds-undefined.js +++ b/test/built-ins/Temporal/Duration/nanoseconds-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/seconds-undefined.js b/test/built-ins/Temporal/Duration/seconds-undefined.js index a4cc4add4a..e3cd9b15b2 100644 --- a/test/built-ins/Temporal/Duration/seconds-undefined.js +++ b/test/built-ins/Temporal/Duration/seconds-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/weeks-undefined.js b/test/built-ins/Temporal/Duration/weeks-undefined.js index d01172db2d..12c03332d0 100644 --- a/test/built-ins/Temporal/Duration/weeks-undefined.js +++ b/test/built-ins/Temporal/Duration/weeks-undefined.js @@ -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"); diff --git a/test/built-ins/Temporal/Duration/years-undefined.js b/test/built-ins/Temporal/Duration/years-undefined.js index 5523584091..c0fb9cea21 100644 --- a/test/built-ins/Temporal/Duration/years-undefined.js +++ b/test/built-ins/Temporal/Duration/years-undefined.js @@ -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");