diff --git a/test/built-ins/Temporal/Duration/prototype/abs/new-object.js b/test/built-ins/Temporal/Duration/prototype/abs/new-object.js new file mode 100644 index 0000000000..6f10880b7c --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/abs/new-object.js @@ -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.duration.prototype.abs +description: Temporal.Duration.prototype.abs returns a new object. +features: [Temporal] +---*/ + +let d1 = new Temporal.Duration(); +assert.notSameValue(d1.abs(), d1); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +assert.notSameValue(d2.abs(), d2); + +let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); +assert.notSameValue(d3.abs(), d3); + +let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); +assert.notSameValue(d4.abs(), d4); + +// Test with some zeros +let d5 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0); +assert.notSameValue(d5.abs(), d5); + +let d6 = new Temporal.Duration(0, 2, 0, 4, 0, 6, 0, 8, 0, 10); +assert.notSameValue(d6.abs(), d6); diff --git a/test/built-ins/Temporal/Duration/prototype/add/options-undefined.js b/test/built-ins/Temporal/Duration/prototype/add/options-undefined.js index ef9717d9ac..1b58ed054c 100644 --- a/test/built-ins/Temporal/Duration/prototype/add/options-undefined.js +++ b/test/built-ins/Temporal/Duration/prototype/add/options-undefined.js @@ -4,10 +4,29 @@ /*--- esid: sec-temporal.duration.prototype.add description: Verify that undefined options are handled correctly. +includes: [temporalHelpers.js] features: [Temporal] ---*/ const duration1 = new Temporal.Duration(1); const duration2 = new Temporal.Duration(0, 12); -assert.throws(RangeError, () => duration1.add(duration2), "default relativeTo is undefined"); -assert.throws(RangeError, () => duration1.add(duration2, undefined), "default relativeTo is undefined"); +const duration3 = new Temporal.Duration(0, 0, 0, 1); +const duration4 = new Temporal.Duration(0, 0, 0, 0, 24); + +assert.throws(RangeError, () => duration1.add(duration2), "no options with years"); +TemporalHelpers.assertDuration(duration3.add(duration4), + 0, 0, 0, /* days = */ 2, 0, 0, 0, 0, 0, 0, + "no options with days"); + +const optionValues = [ + [undefined, "undefined"], + [{}, "plain object"], + [() => {}, "lambda"], +]; +for (const [options, description] of optionValues) { + assert.throws(RangeError, () => duration1.add(duration2, options), + `options ${description} with years`); + TemporalHelpers.assertDuration(duration3.add(duration4, options), + 0, 0, 0, /* days = */ 2, 0, 0, 0, 0, 0, 0, + `options ${description} with days`); +} diff --git a/test/built-ins/Temporal/Duration/prototype/blank/basic.js b/test/built-ins/Temporal/Duration/prototype/blank/basic.js new file mode 100644 index 0000000000..10a594cb24 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/blank/basic.js @@ -0,0 +1,25 @@ +// Copyright (C) 2022 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-get-temporal.duration.prototype.blank +description: Basic tests for blank. +features: [Temporal] +---*/ + +assert.sameValue(Temporal.Duration.from("P3DT1H").blank, false); +assert.sameValue(Temporal.Duration.from("-PT2H20M30S").blank, false); +assert.sameValue(Temporal.Duration.from("PT0S").blank, true); +const zero = Temporal.Duration.from({ + years: 0, + months: 0, + weeks: 0, + days: 0, + hours: 0, + minutes: 0, + seconds: 0, + milliseconds: 0, + microseconds: 0, + nanoseconds: 0 +}); +assert.sameValue(zero.blank, true); diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/options-undefined.js b/test/built-ins/Temporal/Duration/prototype/subtract/options-undefined.js index 959fb77c61..0f6e4ac0e4 100644 --- a/test/built-ins/Temporal/Duration/prototype/subtract/options-undefined.js +++ b/test/built-ins/Temporal/Duration/prototype/subtract/options-undefined.js @@ -4,10 +4,29 @@ /*--- esid: sec-temporal.duration.prototype.subtract description: Verify that undefined options are handled correctly. +includes: [temporalHelpers.js] features: [Temporal] ---*/ const duration1 = new Temporal.Duration(1); -const duration2 = new Temporal.Duration(0, 12); -assert.throws(RangeError, () => duration1.subtract(duration2), "default relativeTo is undefined"); -assert.throws(RangeError, () => duration1.subtract(duration2, undefined), "default relativeTo is undefined"); +const duration2 = new Temporal.Duration(0, 24); +const duration3 = new Temporal.Duration(0, 0, 0, 1); +const duration4 = new Temporal.Duration(0, 0, 0, 0, 48); + +assert.throws(RangeError, () => duration1.subtract(duration2), "no options with years"); +TemporalHelpers.assertDuration(duration3.subtract(duration4), + 0, 0, 0, /* days = */ -1, 0, 0, 0, 0, 0, 0, + "no options with days"); + +const optionValues = [ + [undefined, "undefined"], + [{}, "plain object"], + [() => {}, "lambda"], +]; +for (const [options, description] of optionValues) { + assert.throws(RangeError, () => duration1.subtract(duration2, options), + `options ${description} with years`); + TemporalHelpers.assertDuration(duration3.subtract(duration4, options), + 0, 0, 0, /* days = */ -1, 0, 0, 0, 0, 0, 0, + `options ${description} with days`); +} diff --git a/test/built-ins/Temporal/Duration/prototype/toJSON/options.js b/test/built-ins/Temporal/Duration/prototype/toJSON/options.js new file mode 100644 index 0000000000..7728dcc2e2 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/toJSON/options.js @@ -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.duration.prototype.tojson +description: Temporal.Duration.prototype.toJSON does not support options, unlike toString. +features: [Temporal] +---*/ + +let called = 0; +const options = new Proxy({}, { + get() { + ++called; + } +}); +const d = new Temporal.Duration(1, 2); +assert.sameValue(d.toJSON(options), "P1Y2M"); +assert.sameValue(called, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/valueOf/basic.js b/test/built-ins/Temporal/Duration/prototype/valueOf/basic.js new file mode 100644 index 0000000000..95b2de9093 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/valueOf/basic.js @@ -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.duration.prototype.valueof +description: Basic tests for valueOf(). +features: [Temporal] +---*/ + +const d1 = Temporal.Duration.from("P3DT1H"); +const d2 = Temporal.Duration.from("P3DT1H"); + +assert.throws(TypeError, () => d1.valueOf(), "valueOf"); +assert.throws(TypeError, () => d1 < d1, "<"); +assert.throws(TypeError, () => d1 <= d1, "<="); +assert.throws(TypeError, () => d1 > d1, ">"); +assert.throws(TypeError, () => d1 >= d1, ">="); +assert.sameValue(d1 === d1, true, "==="); +assert.sameValue(d1 === d2, false, "==="); +assert.sameValue(d1 !== d1, false, "!=="); +assert.sameValue(d1 !== d2, true, "!=="); diff --git a/test/built-ins/Temporal/Duration/prototype/with/argument-invalid-prop.js b/test/built-ins/Temporal/Duration/prototype/with/argument-invalid-prop.js new file mode 100644 index 0000000000..f12c9f49dc --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/argument-invalid-prop.js @@ -0,0 +1,13 @@ +// 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.prototype.with +description: Singular properties are ignored. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ years: 5, days: 1 }); +const d2 = d.with({ year: 1, days: 0 }); +TemporalHelpers.assertDuration(d2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/with/argument-mixed-sign.js b/test/built-ins/Temporal/Duration/prototype/with/argument-mixed-sign.js new file mode 100644 index 0000000000..ca215ef33e --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/argument-mixed-sign.js @@ -0,0 +1,11 @@ +// 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.prototype.with +description: The durationLike argument must not contain different signs. +features: [Temporal] +---*/ + +const d = new Temporal.Duration(1, 2, 3, 4, 5); +assert.throws(RangeError, () => d.with({ hours: 1, minutes: -1 })); diff --git a/test/built-ins/Temporal/Duration/prototype/with/argument-sign-prop.js b/test/built-ins/Temporal/Duration/prototype/with/argument-sign-prop.js new file mode 100644 index 0000000000..728d2f92ee --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/argument-sign-prop.js @@ -0,0 +1,14 @@ +// 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.prototype.with +description: Passing a sign property is not supported. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ years: 5, days: 1 }); +assert.throws(TypeError, () => d.with({ sign: -1 })); +const d2 = d.with({ sign: -1, days: 0 }); +TemporalHelpers.assertDuration(d2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0); diff --git a/test/built-ins/Temporal/Duration/prototype/with/sign-replace.js b/test/built-ins/Temporal/Duration/prototype/with/sign-replace.js new file mode 100644 index 0000000000..a4b9c0ccd3 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/sign-replace.js @@ -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.duration.prototype.with +description: Replacing the sign is supported. +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const d = Temporal.Duration.from({ years: 5, days: 1 }); +assert.sameValue(d.sign, 1, "original sign"); +const d2 = d.with({ years: -1, days: 0, minutes: -1 }); +assert.sameValue(d2.sign, -1, "new sign"); +TemporalHelpers.assertDuration(d2, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0);