From f52218e4145a148246aa9fc46201deefac738d12 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 19 Jul 2021 15:32:37 -0700 Subject: [PATCH 1/3] Add tests for Temporal.Duration.p*.with --- .../Duration/prototype/with/simple.js | 108 ++++++++++++++++++ .../prototype/with/throw-range-error.js | 99 ++++++++++++++++ .../prototype/with/throw-type-error.js | 76 ++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/with/simple.js create mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js create mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js diff --git a/test/built-ins/Temporal/Duration/prototype/with/simple.js b/test/built-ins/Temporal/Duration/prototype/with/simple.js new file mode 100644 index 00000000000..f8a76c7d8a3 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/simple.js @@ -0,0 +1,108 @@ +// 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.with +description: Temporal.Duration.prototype.with will return correctly "merged" object. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). + 4. If temporalDurationLike.[[Years]] is not undefined, then + a. Let years be temporalDurationLike.[[Years]]. + 5. Else, + a. Let years be duration.[[Years]]. + 6. If temporalDurationLike.[[Months]] is not undefined, then + a. Let months be temporalDurationLike.[[Months]]. + 7. Else, + a. Let months be duration.[[Months]]. + 8. If temporalDurationLike.[[Weeks]] is not undefined, then + a. Let weeks be temporalDurationLike.[[Weeks]]. + 9. Else, + a. Let weeks be duration.[[Weeks]]. + 10. If temporalDurationLike.[[Days]] is not undefined, then + a. Let days be temporalDurationLike.[[Days]]. + 11. Else, + a. Let days be duration.[[Days]]. + 12. If temporalDurationLike.[[Hours]] is not undefined, then + a. Let hours be temporalDurationLike.[[Hours]]. + 13. Else, + a. Let hours be duration.[[Hours]]. + 14. If temporalDurationLike.[[Minutes]] is not undefined, then + a. Let minutes be temporalDurationLike.[[Minutes]]. + 15. Else, + a. Let minutes be duration.[[Minutes]]. + 16. If temporalDurationLike.[[Seconds]] is not undefined, then + a. Let seconds be temporalDurationLike.[[Seconds]]. + 17. Else, + a. Let seconds be duration.[[Seconds]]. + 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then + a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. + 19. Else, + a. Let milliseconds be duration.[[Milliseconds]]. + 20. If temporalDurationLike.[[Microseconds]] is not undefined, then + a. Let microseconds be temporalDurationLike.[[Microseconds]]. + 21. Else, + a. Let microseconds be duration.[[Microseconds]]. + 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then + a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. + 23. Else, + a. Let nanoseconds be duration.[[Nanoseconds]]. + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +---*/ +function assertDuration(duration, years, months, weeks, days, hours, + minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) { + assert.sameValue(years, duration.years, duration); + assert.sameValue(months, duration.months, duration); + assert.sameValue(weeks, duration.weeks, duration); + assert.sameValue(days, duration.days, duration); + assert.sameValue(hours, duration.hours, duration); + assert.sameValue(minutes, duration.minutes, duration); + assert.sameValue(seconds, duration.seconds, duration); + assert.sameValue(milliseconds, duration.milliseconds, duration); + assert.sameValue(microseconds, duration.microseconds, duration); + assert.sameValue(nanoseconds, duration.nanoseconds, duration); + assert.sameValue(sign, duration.sign, duration); + assert.sameValue(blank, duration.blank, duration); +} +let like1 = {years:9, months:8, weeks:7, days:6, hours: 5, minutes: 4, + seconds: 3, milliseconds: 2, microseconds: 1, nanoseconds: 10}; +let like2 = {years: 9, hours:5}; +let like3 = {months: 8, minutes:4}; +let like4 = {weeks: 7, seconds:3}; +let like5 = {days: 6, milliseconds:2}; +let like6 = {microseconds: 987, nanoseconds: 123}; +let like7 = {years:-9, months:-8, weeks:-7, days:-6, hours: -5, minutes: -4, + seconds: -3, milliseconds: -2, microseconds: -1, nanoseconds: -10}; + +let d1 = new Temporal.Duration(); +assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); +assertDuration(d1.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); +assertDuration(d1.with(like2), 9, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, false); +assertDuration(d1.with(like3), 0, 8, 0, 0, 0, 4, 0, 0, 0, 0, 1, false); +assertDuration(d1.with(like4), 0, 0, 7, 0, 0, 0, 3, 0, 0, 0, 1, false); +assertDuration(d1.with(like5), 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 1, false); +assertDuration(d1.with(like6), 0, 0, 0, 0, 0, 0, 0, 0, 987, 123, 1, false); +assertDuration(d1.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, + false); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +assertDuration(d2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); +assertDuration(d2.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); +assertDuration(d2.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, + false); + +// Test large number +let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, + 10e5); +assertDuration(d3, 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, + false); +assertDuration(d3.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); +assertDuration(d3.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, + false); + +// Test negative values +let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); +assertDuration(d4, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false); +assertDuration(d4.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js b/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js new file mode 100644 index 00000000000..206da1f2392 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js @@ -0,0 +1,99 @@ +// 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.with +description: Temporal.Duration.prototype.with will throw RangeError if the value is out of range. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). + 4. If temporalDurationLike.[[Years]] is not undefined, then + a. Let years be temporalDurationLike.[[Years]]. + 5. Else, + a. Let years be duration.[[Years]]. + 6. If temporalDurationLike.[[Months]] is not undefined, then + a. Let months be temporalDurationLike.[[Months]]. + 7. Else, + a. Let months be duration.[[Months]]. + 8. If temporalDurationLike.[[Weeks]] is not undefined, then + a. Let weeks be temporalDurationLike.[[Weeks]]. + 9. Else, + a. Let weeks be duration.[[Weeks]]. + 10. If temporalDurationLike.[[Days]] is not undefined, then + a. Let days be temporalDurationLike.[[Days]]. + 11. Else, + a. Let days be duration.[[Days]]. + 12. If temporalDurationLike.[[Hours]] is not undefined, then + a. Let hours be temporalDurationLike.[[Hours]]. + 13. Else, + a. Let hours be duration.[[Hours]]. + 14. If temporalDurationLike.[[Minutes]] is not undefined, then + a. Let minutes be temporalDurationLike.[[Minutes]]. + 15. Else, + a. Let minutes be duration.[[Minutes]]. + 16. If temporalDurationLike.[[Seconds]] is not undefined, then + a. Let seconds be temporalDurationLike.[[Seconds]]. + 17. Else, + a. Let seconds be duration.[[Seconds]]. + 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then + a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. + 19. Else, + a. Let milliseconds be duration.[[Milliseconds]]. + 20. If temporalDurationLike.[[Microseconds]] is not undefined, then + a. Let microseconds be temporalDurationLike.[[Microseconds]]. + 21. Else, + a. Let microseconds be duration.[[Microseconds]]. + 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then + a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. + 23. Else, + a. Let nanoseconds be duration.[[Nanoseconds]]. + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +---*/ +let dPos = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +// Different sign +assert.throws(RangeError, ()=> dPos.with({years: -1}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({months: -2}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({weeks: -3}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({days: -4}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({hours: -5}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({minutes: -6}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({seconds: -7}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({milliseconds: -8}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({microseconds: -9}), + "Invalid time value"); +assert.throws(RangeError, ()=> dPos.with({nanoseconds: -10}), + "Invalid time value"); + +// Test negative values +let dNeg = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); +// Throw when sign flip +assert.throws(RangeError, ()=> dNeg.with({years: 1}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({months: 2}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({weeks: 3}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({days: 4}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({hours: 5}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({minutes: 6}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({seconds: 7}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({milliseconds: 8}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({microseconds: 9}), + "Invalid time value"); +assert.throws(RangeError, ()=> dNeg.with({nanoseconds: 10}), + "Invalid time value"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js new file mode 100644 index 00000000000..d3c0c600a10 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js @@ -0,0 +1,76 @@ +// 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.with +description: Temporal.Duration.prototype.with will throw if the temporalDurationLike is object which contains values with key in singular form. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). + 4. If temporalDurationLike.[[Years]] is not undefined, then + a. Let years be temporalDurationLike.[[Years]]. + 5. Else, + a. Let years be duration.[[Years]]. + 6. If temporalDurationLike.[[Months]] is not undefined, then + a. Let months be temporalDurationLike.[[Months]]. + 7. Else, + a. Let months be duration.[[Months]]. + 8. If temporalDurationLike.[[Weeks]] is not undefined, then + a. Let weeks be temporalDurationLike.[[Weeks]]. + 9. Else, + a. Let weeks be duration.[[Weeks]]. + 10. If temporalDurationLike.[[Days]] is not undefined, then + a. Let days be temporalDurationLike.[[Days]]. + 11. Else, + a. Let days be duration.[[Days]]. + 12. If temporalDurationLike.[[Hours]] is not undefined, then + a. Let hours be temporalDurationLike.[[Hours]]. + 13. Else, + a. Let hours be duration.[[Hours]]. + 14. If temporalDurationLike.[[Minutes]] is not undefined, then + a. Let minutes be temporalDurationLike.[[Minutes]]. + 15. Else, + a. Let minutes be duration.[[Minutes]]. + 16. If temporalDurationLike.[[Seconds]] is not undefined, then + a. Let seconds be temporalDurationLike.[[Seconds]]. + 17. Else, + a. Let seconds be duration.[[Seconds]]. + 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then + a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. + 19. Else, + a. Let milliseconds be duration.[[Milliseconds]]. + 20. If temporalDurationLike.[[Microseconds]] is not undefined, then + a. Let microseconds be temporalDurationLike.[[Microseconds]]. + 21. Else, + a. Let microseconds be duration.[[Microseconds]]. + 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then + a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. + 23. Else, + a. Let nanoseconds be duration.[[Nanoseconds]]. + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +---*/ + +let d1 = new Temporal.Duration(); +// singular throw +assert.throws(TypeError, () => d1.with({year:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({month:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({week:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({day:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({hour:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({minute:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({second:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({millisecond:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({microsecond:1}), + "invalid_argument"); +assert.throws(TypeError, () => d1.with({nanosecond:1}), + "invalid_argument"); From 7906ed5028145c71a3e7efd6de09f20207d955ce Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Thu, 5 Aug 2021 21:47:42 -0700 Subject: [PATCH 2/3] update tests to use helper --- .../Duration/prototype/with/all-negative.js | 71 ++++++++++++++++ .../with/{simple.js => all-positive.js} | 54 +++--------- .../prototype/with/partial-positive.js | 84 +++++++++++++++++++ ...hrow-range-error-CreateTemporalDuration.js | 33 ++++++++ .../throw-type-error-RequireInternalSlot.js | 15 ++++ .../throw-type-error-ToPartialDuration.js | 23 +++++ 6 files changed, 237 insertions(+), 43 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/with/all-negative.js rename test/built-ins/Temporal/Duration/prototype/with/{simple.js => all-positive.js} (54%) create mode 100644 test/built-ins/Temporal/Duration/prototype/with/partial-positive.js create mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-range-error-CreateTemporalDuration.js create mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-type-error-RequireInternalSlot.js create mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-type-error-ToPartialDuration.js diff --git a/test/built-ins/Temporal/Duration/prototype/with/all-negative.js b/test/built-ins/Temporal/Duration/prototype/with/all-negative.js new file mode 100644 index 00000000000..073668c9ce0 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/all-negative.js @@ -0,0 +1,71 @@ +// 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.with +description: Temporal.Duration.prototype.with will return correctly "merged" object + with all negative values. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). + 4. If temporalDurationLike.[[Years]] is not undefined, then + a. Let years be temporalDurationLike.[[Years]]. + 5. Else, + a. Let years be duration.[[Years]]. + 6. If temporalDurationLike.[[Months]] is not undefined, then + a. Let months be temporalDurationLike.[[Months]]. + 7. Else, + a. Let months be duration.[[Months]]. + 8. If temporalDurationLike.[[Weeks]] is not undefined, then + a. Let weeks be temporalDurationLike.[[Weeks]]. + 9. Else, + a. Let weeks be duration.[[Weeks]]. + 10. If temporalDurationLike.[[Days]] is not undefined, then + a. Let days be temporalDurationLike.[[Days]]. + 11. Else, + a. Let days be duration.[[Days]]. + 12. If temporalDurationLike.[[Hours]] is not undefined, then + a. Let hours be temporalDurationLike.[[Hours]]. + 13. Else, + a. Let hours be duration.[[Hours]]. + 14. If temporalDurationLike.[[Minutes]] is not undefined, then + a. Let minutes be temporalDurationLike.[[Minutes]]. + 15. Else, + a. Let minutes be duration.[[Minutes]]. + 16. If temporalDurationLike.[[Seconds]] is not undefined, then + a. Let seconds be temporalDurationLike.[[Seconds]]. + 17. Else, + a. Let seconds be duration.[[Seconds]]. + 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then + a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. + 19. Else, + a. Let milliseconds be duration.[[Milliseconds]]. + 20. If temporalDurationLike.[[Microseconds]] is not undefined, then + a. Let microseconds be temporalDurationLike.[[Microseconds]]. + 21. Else, + a. Let microseconds be duration.[[Microseconds]]. + 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then + a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. + 23. Else, + a. Let nanoseconds be duration.[[Nanoseconds]]. + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +includes: [temporalHelpers.js] +---*/ +let like7 = {years:-9, months:-8, weeks:-7, days:-6, hours: -5, minutes: -4, + seconds: -3, milliseconds: -2, microseconds: -1, nanoseconds: -10}; + +let d1 = new Temporal.Duration(); +TemporalHelpers.assertDuration( + d1.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, "replace all neg"); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +TemporalHelpers.assertDuration( + d2.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, "replace all negative"); + +// Test large number +let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, + 10e5); +TemporalHelpers.assertDuration( + d3.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, "all negative"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/simple.js b/test/built-ins/Temporal/Duration/prototype/with/all-positive.js similarity index 54% rename from test/built-ins/Temporal/Duration/prototype/with/simple.js rename to test/built-ins/Temporal/Duration/prototype/with/all-positive.js index f8a76c7d8a3..25d046f9f19 100644 --- a/test/built-ins/Temporal/Duration/prototype/with/simple.js +++ b/test/built-ins/Temporal/Duration/prototype/with/all-positive.js @@ -3,7 +3,8 @@ /*--- esid: sec-temporal.duration.prototype.with -description: Temporal.Duration.prototype.with will return correctly "merged" object. +description: Temporal.Duration.prototype.with will return correctly "merged" object + with all positive duration. info: | 1. Let duration be the this value. 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). @@ -50,59 +51,26 @@ info: | a. Let nanoseconds be duration.[[Nanoseconds]]. 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, 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, duration); - assert.sameValue(months, duration.months, duration); - assert.sameValue(weeks, duration.weeks, duration); - assert.sameValue(days, duration.days, duration); - assert.sameValue(hours, duration.hours, duration); - assert.sameValue(minutes, duration.minutes, duration); - assert.sameValue(seconds, duration.seconds, duration); - assert.sameValue(milliseconds, duration.milliseconds, duration); - assert.sameValue(microseconds, duration.microseconds, duration); - assert.sameValue(nanoseconds, duration.nanoseconds, duration); - assert.sameValue(sign, duration.sign, duration); - assert.sameValue(blank, duration.blank, duration); -} let like1 = {years:9, months:8, weeks:7, days:6, hours: 5, minutes: 4, seconds: 3, milliseconds: 2, microseconds: 1, nanoseconds: 10}; -let like2 = {years: 9, hours:5}; -let like3 = {months: 8, minutes:4}; -let like4 = {weeks: 7, seconds:3}; -let like5 = {days: 6, milliseconds:2}; -let like6 = {microseconds: 987, nanoseconds: 123}; -let like7 = {years:-9, months:-8, weeks:-7, days:-6, hours: -5, minutes: -4, - seconds: -3, milliseconds: -2, microseconds: -1, nanoseconds: -10}; let d1 = new Temporal.Duration(); -assertDuration(d1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); -assertDuration(d1.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); -assertDuration(d1.with(like2), 9, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, false); -assertDuration(d1.with(like3), 0, 8, 0, 0, 0, 4, 0, 0, 0, 0, 1, false); -assertDuration(d1.with(like4), 0, 0, 7, 0, 0, 0, 3, 0, 0, 0, 1, false); -assertDuration(d1.with(like5), 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, 1, false); -assertDuration(d1.with(like6), 0, 0, 0, 0, 0, 0, 0, 0, 987, 123, 1, false); -assertDuration(d1.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, - false); +TemporalHelpers.assertDuration( + d1.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, "replace all positive"); let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); -assertDuration(d2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); -assertDuration(d2.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); -assertDuration(d2.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, - false); +TemporalHelpers.assertDuration( + d2.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, "replace all positive"); // Test large number let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); -assertDuration(d3, 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, - false); -assertDuration(d3.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); -assertDuration(d3.with(like7), -9, -8, -7, -6, -5, -4, -3, -2, -1, -10, -1, - false); +TemporalHelpers.assertDuration( + d3.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, "all positive"); // Test negative values let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); -assertDuration(d4, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false); -assertDuration(d4.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 1, false); +TemporalHelpers.assertDuration( + d4.with(like1), 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, "replace all neg to all positive"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/partial-positive.js b/test/built-ins/Temporal/Duration/prototype/with/partial-positive.js new file mode 100644 index 00000000000..b47ee6d2f5d --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/partial-positive.js @@ -0,0 +1,84 @@ +// 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.with +description: Temporal.Duration.prototype.with will return correctly "merged" object + with partial positive values. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). + 4. If temporalDurationLike.[[Years]] is not undefined, then + a. Let years be temporalDurationLike.[[Years]]. + 5. Else, + a. Let years be duration.[[Years]]. + 6. If temporalDurationLike.[[Months]] is not undefined, then + a. Let months be temporalDurationLike.[[Months]]. + 7. Else, + a. Let months be duration.[[Months]]. + 8. If temporalDurationLike.[[Weeks]] is not undefined, then + a. Let weeks be temporalDurationLike.[[Weeks]]. + 9. Else, + a. Let weeks be duration.[[Weeks]]. + 10. If temporalDurationLike.[[Days]] is not undefined, then + a. Let days be temporalDurationLike.[[Days]]. + 11. Else, + a. Let days be duration.[[Days]]. + 12. If temporalDurationLike.[[Hours]] is not undefined, then + a. Let hours be temporalDurationLike.[[Hours]]. + 13. Else, + a. Let hours be duration.[[Hours]]. + 14. If temporalDurationLike.[[Minutes]] is not undefined, then + a. Let minutes be temporalDurationLike.[[Minutes]]. + 15. Else, + a. Let minutes be duration.[[Minutes]]. + 16. If temporalDurationLike.[[Seconds]] is not undefined, then + a. Let seconds be temporalDurationLike.[[Seconds]]. + 17. Else, + a. Let seconds be duration.[[Seconds]]. + 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then + a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. + 19. Else, + a. Let milliseconds be duration.[[Milliseconds]]. + 20. If temporalDurationLike.[[Microseconds]] is not undefined, then + a. Let microseconds be temporalDurationLike.[[Microseconds]]. + 21. Else, + a. Let microseconds be duration.[[Microseconds]]. + 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then + a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. + 23. Else, + a. Let nanoseconds be duration.[[Nanoseconds]]. + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +includes: [temporalHelpers.js] +---*/ +let like2 = {years: 9, hours:5}; +let like3 = {months: 8, minutes:4}; +let like4 = {weeks: 7, seconds:3}; +let like5 = {days: 6, milliseconds:2}; +let like6 = {microseconds: 987, nanoseconds: 123}; + +let d1 = new Temporal.Duration(); +TemporalHelpers.assertDuration( + d1.with(like2), 9, 0, 0, 0, 5, 0, 0, 0, 0, 0, "repalce years and hours"); +TemporalHelpers.assertDuration( + d1.with(like3), 0, 8, 0, 0, 0, 4, 0, 0, 0, 0, "replace months and minutes"); +TemporalHelpers.assertDuration( + d1.with(like4), 0, 0, 7, 0, 0, 0, 3, 0, 0, 0, "replace weeks and seconds"); +TemporalHelpers.assertDuration( + d1.with(like5), 0, 0, 0, 6, 0, 0, 0, 2, 0, 0, "replace days and milliseconds"); +TemporalHelpers.assertDuration( + d1.with(like6), 0, 0, 0, 0, 0, 0, 0, 0, 987, 123, "replace mis and ns"); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +TemporalHelpers.assertDuration( + d2.with(like2), 9, 2, 3, 4, 5, 6, 7, 8, 9, 10, "replace years and hours"); +TemporalHelpers.assertDuration( + d2.with(like3), 1, 8, 3, 4, 5, 4, 7, 8, 9, 10, "replace months and minutes"); +TemporalHelpers.assertDuration( + d2.with(like4), 1, 2, 7, 4, 5, 6, 3, 8, 9, 10, "replace weeks and seconds"); +TemporalHelpers.assertDuration( + d2.with(like5), 1, 2, 3, 6, 5, 6, 7, 2, 9, 10, "replace days and ns"); +TemporalHelpers.assertDuration( + d2.with(like6), 1, 2, 3, 4, 5, 6, 7, 8, 987, 123, "replace mis and ns"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-range-error-CreateTemporalDuration.js b/test/built-ins/Temporal/Duration/prototype/with/throw-range-error-CreateTemporalDuration.js new file mode 100644 index 00000000000..78705b787f4 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/throw-range-error-CreateTemporalDuration.js @@ -0,0 +1,33 @@ +// 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.with +description: Temporal.Duration.prototype.with throws RangeError on + CreateTemporalDuration if resulting duration is invalid. +info: | + 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). +features: [Temporal] +---*/ +let dur = new Temporal.Duration(1,2,3,4,5,6,7,8,9,10); +[ + {years: -1}, {months: -1}, {weeks: -1}, {days: -1}, + {hours: -1}, {minutes: -1}, {seconds: -1}, {milliseconds: -1}, + {microseconds: -1}, {nanoseconds: -1}, +].forEach( + function(durationLike) { + assert.throws(RangeError, () => dur.with(durationLike), + "Throw RangeError if content in temporalDurationLike conflict with duration" + + JSON.stringify(durationLike)); + }); + +dur = new Temporal.Duration(-1,-2,-3,-4,-5,-6,-7,-8,-9,-10); +[ + {years: 1}, {months: 1}, {weeks: 1}, {days: 1}, + {hours: 1}, {minutes: 1}, {seconds: 1}, {milliseconds: 1}, + {microseconds: 1}, {nanoseconds: 1}, +].forEach( + function(durationLike) { + assert.throws(RangeError, () => dur.with(durationLike), + "Throw RangeError if content in temporalDurationLike conflict with duration" + + JSON.stringify(durationLike)); + }); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-type-error-RequireInternalSlot.js b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error-RequireInternalSlot.js new file mode 100644 index 00000000000..5f830707025 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/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.with +description: Temporal.Duration.prototype.with 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 = { with: dur.with } +assert.throws(TypeError, () => badDur.with({years: 3}), + "Throw TypeError if there are no internal slot"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-type-error-ToPartialDuration.js b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error-ToPartialDuration.js new file mode 100644 index 00000000000..e2a2a111e46 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error-ToPartialDuration.js @@ -0,0 +1,23 @@ +// 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.with +description: Temporal.Duration.prototype.with throws TypeError on + ToPartialDuration if temporalDurationLike is not valid object. +info: | + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). +features: [Temporal] +---*/ +let dur = new Temporal.Duration(1,2,3,4,5); + +["string", {}, true, false, NaN, Infinity, undefined, NaN, + 123, Symbol(), 456n, [], + // object with only singular key + {year: 1}, {month: 2}, {week: 3}, {day: 4}, + {hour: 5}, {minute: 6}, {second: 7}, {millisecond: 8}, + {microsecond: 9}, {nanosecond: 10} +].forEach( + function(durationLike) { + assert.throws(TypeError, () => dur.with(durationLike), + "Throw TypeError if temporalDurationLike is not valid"); + }); From 6cb1d6ca568f1e57911d612802ad0e4a7db91b02 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Thu, 5 Aug 2021 21:49:46 -0700 Subject: [PATCH 3/3] remove old tests --- .../prototype/with/throw-range-error.js | 99 ------------------- .../prototype/with/throw-type-error.js | 76 -------------- 2 files changed, 175 deletions(-) delete mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js delete mode 100644 test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js b/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js deleted file mode 100644 index 206da1f2392..00000000000 --- a/test/built-ins/Temporal/Duration/prototype/with/throw-range-error.js +++ /dev/null @@ -1,99 +0,0 @@ -// 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.with -description: Temporal.Duration.prototype.with will throw RangeError if the value is out of range. -info: | - 1. Let duration be the this value. - 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). - 4. If temporalDurationLike.[[Years]] is not undefined, then - a. Let years be temporalDurationLike.[[Years]]. - 5. Else, - a. Let years be duration.[[Years]]. - 6. If temporalDurationLike.[[Months]] is not undefined, then - a. Let months be temporalDurationLike.[[Months]]. - 7. Else, - a. Let months be duration.[[Months]]. - 8. If temporalDurationLike.[[Weeks]] is not undefined, then - a. Let weeks be temporalDurationLike.[[Weeks]]. - 9. Else, - a. Let weeks be duration.[[Weeks]]. - 10. If temporalDurationLike.[[Days]] is not undefined, then - a. Let days be temporalDurationLike.[[Days]]. - 11. Else, - a. Let days be duration.[[Days]]. - 12. If temporalDurationLike.[[Hours]] is not undefined, then - a. Let hours be temporalDurationLike.[[Hours]]. - 13. Else, - a. Let hours be duration.[[Hours]]. - 14. If temporalDurationLike.[[Minutes]] is not undefined, then - a. Let minutes be temporalDurationLike.[[Minutes]]. - 15. Else, - a. Let minutes be duration.[[Minutes]]. - 16. If temporalDurationLike.[[Seconds]] is not undefined, then - a. Let seconds be temporalDurationLike.[[Seconds]]. - 17. Else, - a. Let seconds be duration.[[Seconds]]. - 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then - a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. - 19. Else, - a. Let milliseconds be duration.[[Milliseconds]]. - 20. If temporalDurationLike.[[Microseconds]] is not undefined, then - a. Let microseconds be temporalDurationLike.[[Microseconds]]. - 21. Else, - a. Let microseconds be duration.[[Microseconds]]. - 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then - a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. - 23. Else, - a. Let nanoseconds be duration.[[Nanoseconds]]. - 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). -features: [Temporal] ----*/ -let dPos = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); -// Different sign -assert.throws(RangeError, ()=> dPos.with({years: -1}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({months: -2}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({weeks: -3}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({days: -4}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({hours: -5}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({minutes: -6}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({seconds: -7}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({milliseconds: -8}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({microseconds: -9}), - "Invalid time value"); -assert.throws(RangeError, ()=> dPos.with({nanoseconds: -10}), - "Invalid time value"); - -// Test negative values -let dNeg = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); -// Throw when sign flip -assert.throws(RangeError, ()=> dNeg.with({years: 1}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({months: 2}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({weeks: 3}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({days: 4}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({hours: 5}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({minutes: 6}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({seconds: 7}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({milliseconds: 8}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({microseconds: 9}), - "Invalid time value"); -assert.throws(RangeError, ()=> dNeg.with({nanoseconds: 10}), - "Invalid time value"); diff --git a/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js b/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js deleted file mode 100644 index d3c0c600a10..00000000000 --- a/test/built-ins/Temporal/Duration/prototype/with/throw-type-error.js +++ /dev/null @@ -1,76 +0,0 @@ -// 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.with -description: Temporal.Duration.prototype.with will throw if the temporalDurationLike is object which contains values with key in singular form. -info: | - 1. Let duration be the this value. - 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). - 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). - 4. If temporalDurationLike.[[Years]] is not undefined, then - a. Let years be temporalDurationLike.[[Years]]. - 5. Else, - a. Let years be duration.[[Years]]. - 6. If temporalDurationLike.[[Months]] is not undefined, then - a. Let months be temporalDurationLike.[[Months]]. - 7. Else, - a. Let months be duration.[[Months]]. - 8. If temporalDurationLike.[[Weeks]] is not undefined, then - a. Let weeks be temporalDurationLike.[[Weeks]]. - 9. Else, - a. Let weeks be duration.[[Weeks]]. - 10. If temporalDurationLike.[[Days]] is not undefined, then - a. Let days be temporalDurationLike.[[Days]]. - 11. Else, - a. Let days be duration.[[Days]]. - 12. If temporalDurationLike.[[Hours]] is not undefined, then - a. Let hours be temporalDurationLike.[[Hours]]. - 13. Else, - a. Let hours be duration.[[Hours]]. - 14. If temporalDurationLike.[[Minutes]] is not undefined, then - a. Let minutes be temporalDurationLike.[[Minutes]]. - 15. Else, - a. Let minutes be duration.[[Minutes]]. - 16. If temporalDurationLike.[[Seconds]] is not undefined, then - a. Let seconds be temporalDurationLike.[[Seconds]]. - 17. Else, - a. Let seconds be duration.[[Seconds]]. - 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then - a. Let milliseconds be temporalDurationLike.[[Milliseconds]]. - 19. Else, - a. Let milliseconds be duration.[[Milliseconds]]. - 20. If temporalDurationLike.[[Microseconds]] is not undefined, then - a. Let microseconds be temporalDurationLike.[[Microseconds]]. - 21. Else, - a. Let microseconds be duration.[[Microseconds]]. - 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then - a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]]. - 23. Else, - a. Let nanoseconds be duration.[[Nanoseconds]]. - 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). -features: [Temporal] ----*/ - -let d1 = new Temporal.Duration(); -// singular throw -assert.throws(TypeError, () => d1.with({year:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({month:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({week:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({day:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({hour:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({minute:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({second:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({millisecond:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({microsecond:1}), - "invalid_argument"); -assert.throws(TypeError, () => d1.with({nanosecond:1}), - "invalid_argument");