From 15ed94c891cdfb3c4dea4993c2ec93f88da8e868 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 15 Apr 2022 11:27:44 -0700 Subject: [PATCH] Test conversion of object to string in Temporal.Instant context An object may be given in any context where a Temporal.Instant is expected (see ToTemporalInstant). There is no conversion from a property bag to an Instant, unlike with other Temporal types. Instead the object is converted to a string, and if its toString() method yields a valid ISO string, the conversion succeeds. (An object with the default Object.prototype.toString will not.) --- .../compare/argument-object-tostring.js | 22 +++++++++++++++++++ .../Instant/from/argument-object-tostring.js | 17 ++++++++++++++ .../equals/argument-object-tostring.js | 19 ++++++++++++++++ .../since/argument-object-tostring.js | 20 +++++++++++++++++ .../until/argument-object-tostring.js | 20 +++++++++++++++++ .../argument-object-tostring.js | 20 +++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 test/built-ins/Temporal/Instant/compare/argument-object-tostring.js create mode 100644 test/built-ins/Temporal/Instant/from/argument-object-tostring.js create mode 100644 test/built-ins/Temporal/Instant/prototype/equals/argument-object-tostring.js create mode 100644 test/built-ins/Temporal/Instant/prototype/since/argument-object-tostring.js create mode 100644 test/built-ins/Temporal/Instant/prototype/until/argument-object-tostring.js create mode 100644 test/built-ins/Temporal/TimeZone/prototype/getPlainDateTimeFor/argument-object-tostring.js diff --git a/test/built-ins/Temporal/Instant/compare/argument-object-tostring.js b/test/built-ins/Temporal/Instant/compare/argument-object-tostring.js new file mode 100644 index 0000000000..808cd21453 --- /dev/null +++ b/test/built-ins/Temporal/Instant/compare/argument-object-tostring.js @@ -0,0 +1,22 @@ +// 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.instant.compare +description: Object is converted to a string, then to Temporal.Instant +features: [Temporal] +---*/ + +const epoch = new Temporal.Instant(0n); + +const arg = {}; +assert.throws(RangeError, () => Temporal.Instant.compare(arg, epoch), "[object Object] is not a valid ISO string (first argument)"); +assert.throws(RangeError, () => Temporal.Instant.compare(epoch, arg), "[object Object] is not a valid ISO string (second argument)"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result1 = Temporal.Instant.compare(arg, epoch); +assert.sameValue(result1, 0, "result of toString is interpreted as ISO string (first argument)"); +const result2 = Temporal.Instant.compare(epoch, arg); +assert.sameValue(result2, 0, "result of toString is interpreted as ISO string (second argument)"); diff --git a/test/built-ins/Temporal/Instant/from/argument-object-tostring.js b/test/built-ins/Temporal/Instant/from/argument-object-tostring.js new file mode 100644 index 0000000000..a946791a10 --- /dev/null +++ b/test/built-ins/Temporal/Instant/from/argument-object-tostring.js @@ -0,0 +1,17 @@ +// 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.instant.from +description: Object is converted to a string, then to Temporal.Instant +features: [Temporal] +---*/ + +const arg = {}; +assert.throws(RangeError, () => Temporal.Instant.from(arg), "[object Object] is not a valid ISO string"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result = Temporal.Instant.from(arg); +assert.sameValue(result.epochNanoseconds, 0n, "result of toString is interpreted as ISO string"); diff --git a/test/built-ins/Temporal/Instant/prototype/equals/argument-object-tostring.js b/test/built-ins/Temporal/Instant/prototype/equals/argument-object-tostring.js new file mode 100644 index 0000000000..2a0e34955d --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/equals/argument-object-tostring.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.instant.prototype.equals +description: Object is converted to a string, then to Temporal.Instant +features: [Temporal] +---*/ + +const instance = new Temporal.Instant(0n); + +const arg = {}; +assert.throws(RangeError, () => instance.equals(arg), "[object Object] is not a valid ISO string"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result = instance.equals(arg); +assert.sameValue(result, true, "result of toString is interpreted as ISO string"); diff --git a/test/built-ins/Temporal/Instant/prototype/since/argument-object-tostring.js b/test/built-ins/Temporal/Instant/prototype/since/argument-object-tostring.js new file mode 100644 index 0000000000..437de1654b --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/since/argument-object-tostring.js @@ -0,0 +1,20 @@ +// 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.instant.prototype.since +description: Object is converted to a string, then to Temporal.Instant +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const instance = new Temporal.Instant(0n); + +const arg = {}; +assert.throws(RangeError, () => instance.since(arg), "[object Object] is not a valid ISO string"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result = instance.since(arg); +TemporalHelpers.assertDuration(result, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "result of toString is interpreted as ISO string"); diff --git a/test/built-ins/Temporal/Instant/prototype/until/argument-object-tostring.js b/test/built-ins/Temporal/Instant/prototype/until/argument-object-tostring.js new file mode 100644 index 0000000000..b91a46a442 --- /dev/null +++ b/test/built-ins/Temporal/Instant/prototype/until/argument-object-tostring.js @@ -0,0 +1,20 @@ +// 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.instant.prototype.until +description: Object is converted to a string, then to Temporal.Instant +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const instance = new Temporal.Instant(0n); + +const arg = {}; +assert.throws(RangeError, () => instance.until(arg), "[object Object] is not a valid ISO string"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result = instance.until(arg); +TemporalHelpers.assertDuration(result, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "result of toString is interpreted as ISO string"); diff --git a/test/built-ins/Temporal/TimeZone/prototype/getPlainDateTimeFor/argument-object-tostring.js b/test/built-ins/Temporal/TimeZone/prototype/getPlainDateTimeFor/argument-object-tostring.js new file mode 100644 index 0000000000..f2cdde1596 --- /dev/null +++ b/test/built-ins/Temporal/TimeZone/prototype/getPlainDateTimeFor/argument-object-tostring.js @@ -0,0 +1,20 @@ +// 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.timezone.prototype.getplaindatetimefor +description: Object is converted to a string, then to Temporal.Instant +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const instance = new Temporal.TimeZone("UTC"); + +const arg = {}; +assert.throws(RangeError, () => instance.getPlainDateTimeFor(arg), "[object Object] is not a valid ISO string"); + +arg.toString = function() { + return "1970-01-01T00:00Z"; +}; +const result = instance.getPlainDateTimeFor(arg); +TemporalHelpers.assertPlainDateTime(result, 1970, 1, "M01", 1, 0, 0, 0, 0, 0, 0, "result of toString is interpreted as ISO string");