From 8418de341164cf239f905686e0d34a51bc5a4907 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 19 Jul 2021 14:50:02 -0700 Subject: [PATCH 1/2] Add tests for Temporal.Duration.p*.abs --- .../Temporal/Duration/prototype/abs/simple.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/abs/simple.js diff --git a/test/built-ins/Temporal/Duration/prototype/abs/simple.js b/test/built-ins/Temporal/Duration/prototype/abs/simple.js new file mode 100644 index 00000000000..16eaab1ba32 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/abs/simple.js @@ -0,0 +1,48 @@ +// Copyright (C) 2021 the V8 project authors. 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 will return absolute value of the input duration. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). + +features: [Temporal] +---*/ + +function assertDuration(duration, years, months, weeks, days, hours, + minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) { + assert.sameValue(years, duration.years, "years"); + assert.sameValue(months, duration.months, "months"); + assert.sameValue(weeks, duration.weeks, "weeks"); + assert.sameValue(days, duration.days, "days"); + assert.sameValue(hours, duration.hours, "hours"); + assert.sameValue(minutes, duration.minutes, "minutes"); + assert.sameValue(seconds, duration.seconds, "seconds"); + assert.sameValue(milliseconds, duration.milliseconds, "milliseconds"); + assert.sameValue(microseconds, duration.microseconds, "microseconds"); + assert.sameValue(nanoseconds, duration.nanoseconds, "nanoseconds"); + assert.sameValue(sign, duration.sign, "sign"); + assert.sameValue(blank, duration.blank, "blank"); +} +let d1 = new Temporal.Duration(); +assertDuration(d1.abs(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +assertDuration(d2.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); + +// Test large number +let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); +assertDuration(d3.abs(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false); + +// Test negative values +let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); +assertDuration(d4.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); + +// Test with some zeros +let d5 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0); +assertDuration(d5.abs(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 1, false); +let d6 = new Temporal.Duration(0, 2, 0, 4, 0, 6, 0, 8, 0, 10); +assertDuration(d6.abs(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 1, false); From 17d69e530e6a5787b742fc3f30bed272b5329e16 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Thu, 5 Aug 2021 17:58:48 -0700 Subject: [PATCH 2/2] update test --- .../Temporal/Duration/prototype/abs/simple.js | 35 +++++++------------ .../throw-type-error-RequireInternalSlot.js | 15 ++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/abs/throw-type-error-RequireInternalSlot.js diff --git a/test/built-ins/Temporal/Duration/prototype/abs/simple.js b/test/built-ins/Temporal/Duration/prototype/abs/simple.js index 16eaab1ba32..c6205d7d399 100644 --- a/test/built-ins/Temporal/Duration/prototype/abs/simple.js +++ b/test/built-ins/Temporal/Duration/prototype/abs/simple.js @@ -1,6 +1,5 @@ // Copyright (C) 2021 the V8 project authors. 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 will return absolute value of the input duration. @@ -10,39 +9,31 @@ info: | 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). features: [Temporal] +includes: [temporalHelpers.js] ---*/ -function assertDuration(duration, years, months, weeks, days, hours, - minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) { - assert.sameValue(years, duration.years, "years"); - assert.sameValue(months, duration.months, "months"); - assert.sameValue(weeks, duration.weeks, "weeks"); - assert.sameValue(days, duration.days, "days"); - assert.sameValue(hours, duration.hours, "hours"); - assert.sameValue(minutes, duration.minutes, "minutes"); - assert.sameValue(seconds, duration.seconds, "seconds"); - assert.sameValue(milliseconds, duration.milliseconds, "milliseconds"); - assert.sameValue(microseconds, duration.microseconds, "microseconds"); - assert.sameValue(nanoseconds, duration.nanoseconds, "nanoseconds"); - assert.sameValue(sign, duration.sign, "sign"); - assert.sameValue(blank, duration.blank, "blank"); -} let d1 = new Temporal.Duration(); -assertDuration(d1.abs(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); +TemporalHelpers.assertDuration( + d1.abs(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "empty"); let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); -assertDuration(d2.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); +TemporalHelpers.assertDuration( + d2.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "positive"); // Test large number let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); -assertDuration(d3.abs(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false); +TemporalHelpers.assertDuration( + d3.abs(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, "large positive"); // Test negative values let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); -assertDuration(d4.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); +TemporalHelpers.assertDuration( + d4.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "negative"); // Test with some zeros let d5 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0); -assertDuration(d5.abs(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 1, false); +TemporalHelpers.assertDuration( + d5.abs(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, "some zeros"); let d6 = new Temporal.Duration(0, 2, 0, 4, 0, 6, 0, 8, 0, 10); -assertDuration(d6.abs(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 1, false); +TemporalHelpers.assertDuration( + d6.abs(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, "other zeros"); diff --git a/test/built-ins/Temporal/Duration/prototype/abs/throw-type-error-RequireInternalSlot.js b/test/built-ins/Temporal/Duration/prototype/abs/throw-type-error-RequireInternalSlot.js new file mode 100644 index 00000000000..7f53cba90ae --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/abs/throw-type-error-RequireInternalSlot.js @@ -0,0 +1,15 @@ +// Copyright (C) 2021 the V8 project authors. 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 throws TypeError on + RequireInternalSlot if object has no internal slot. +info: | + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). +features: [Temporal] +---*/ +let dur = new Temporal.Duration(1,2,3,4,5); + +let badDur = { abs: dur.abs } +assert.throws(TypeError, () => badDur.abs(), + "Throw TypeError if there are no internal slot");