From ed5ada3e13b36ddf0d83828c4c6abeb0e3aa53d7 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 25 May 2022 12:46:06 +0200 Subject: [PATCH] Temporal: Improve coverage for PlainDate constructor and from(). --- .../Temporal/PlainDate/argument-convert.js | 53 +++++++++++++++++++ .../Temporal/PlainDate/argument-invalid.js | 24 +++++++++ .../PlainDate/from/argument-plaindate.js | 9 +++- .../from/argument-propertybag-calendar.js | 14 +++++ .../from/argument-zoneddatetime-convert.js | 19 +++++++ .../PlainDate/from/argument-zoneddatetime.js | 25 +++++++++ 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 test/built-ins/Temporal/PlainDate/argument-convert.js create mode 100644 test/built-ins/Temporal/PlainDate/argument-invalid.js create mode 100644 test/built-ins/Temporal/PlainDate/from/argument-propertybag-calendar.js create mode 100644 test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime-convert.js create mode 100644 test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime.js diff --git a/test/built-ins/Temporal/PlainDate/argument-convert.js b/test/built-ins/Temporal/PlainDate/argument-convert.js new file mode 100644 index 0000000000..0e00a1278e --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/argument-convert.js @@ -0,0 +1,53 @@ +// 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.plaindate +description: PlainDate constructor with non-integer arguments. +includes: [compareArray.js, temporalHelpers.js] +features: [Temporal] +---*/ + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(2020.6, 11.7, 24.1), + 2020, 11, "M11", 24, "positive fractional"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(-2020.6, 11.7, 24.1), + -2020, 11, "M11", 24, "negative fractional"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(undefined, 11, 24), + 0, 11, "M11", 24, "undefined"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(null, 11, 24), + 0, 11, "M11", 24, "null"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(true, 11, 24), + 1, 11, "M11", 24, "boolean"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate("2020.6", "11.7", "24.1"), + 2020, 11, "M11", 24, "fractional strings"); + +TemporalHelpers.assertPlainDate(new Temporal.PlainDate("invalid", 11, 24), + 0, 11, "M11", 24, "invalid string"); + +for (const invalid of [Symbol(), 1n]) { + assert.throws(TypeError, () => new Temporal.PlainDate(invalid, 11, 24), `year ${typeof invalid}`); + assert.throws(TypeError, () => new Temporal.PlainDate(2020, invalid, 24), `month ${typeof invalid}`); + assert.throws(TypeError, () => new Temporal.PlainDate(2020, 11, invalid), `day ${typeof invalid}`); +} + +const actual = []; +const args = [ + TemporalHelpers.toPrimitiveObserver(actual, 2020, "year"), + TemporalHelpers.toPrimitiveObserver(actual, 11, "month"), + TemporalHelpers.toPrimitiveObserver(actual, 24, "day"), +]; +TemporalHelpers.assertPlainDate(new Temporal.PlainDate(...args), + 2020, 11, "M11", 24, "invalid string"); +assert.compareArray(actual, [ + "get year.valueOf", + "call year.valueOf", + "get month.valueOf", + "call month.valueOf", + "get day.valueOf", + "call day.valueOf", +]); diff --git a/test/built-ins/Temporal/PlainDate/argument-invalid.js b/test/built-ins/Temporal/PlainDate/argument-invalid.js new file mode 100644 index 0000000000..2b9ea5d156 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/argument-invalid.js @@ -0,0 +1,24 @@ +// 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.plaindate +description: PlainDate constructor with invalid iso dates +features: [Temporal] +---*/ + +const tests = [ + [2020, 0, 24], + [2020, 13, 24], + [2020, -3, 24], + [2020, 12, 32], + [2020, 2, 30], + [2019, 2, 29], + [2019, 2, 0], + [2019, 2, -20], +]; + +for (const [year, month, day] of tests) { + assert.throws(RangeError, () => new Temporal.PlainDate(year, month, day), + `year=${year}, month=${month}, day=${day}`); +} diff --git a/test/built-ins/Temporal/PlainDate/from/argument-plaindate.js b/test/built-ins/Temporal/PlainDate/from/argument-plaindate.js index 6eb685f042..5dbdceb478 100644 --- a/test/built-ins/Temporal/PlainDate/from/argument-plaindate.js +++ b/test/built-ins/Temporal/PlainDate/from/argument-plaindate.js @@ -8,7 +8,8 @@ includes: [temporalHelpers.js] features: [Temporal] ---*/ -const orig = new Temporal.PlainDate(2000, 5, 2); +const calendar = new Temporal.Calendar("iso8601"); +const orig = new Temporal.PlainDate(2000, 5, 2, calendar); const result = Temporal.PlainDate.from(orig); TemporalHelpers.assertPlainDate( @@ -17,6 +18,12 @@ TemporalHelpers.assertPlainDate( "PlainDate is copied" ); +assert.sameValue( + result.calendar, + calendar, + "Calendar is copied" +); + assert.notSameValue( result, orig, diff --git a/test/built-ins/Temporal/PlainDate/from/argument-propertybag-calendar.js b/test/built-ins/Temporal/PlainDate/from/argument-propertybag-calendar.js new file mode 100644 index 0000000000..8747c7ecc7 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/from/argument-propertybag-calendar.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.plaindate.from +description: Property bag is correctly converted into PlainDate +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const calendar = new Temporal.Calendar("iso8601"); +const plainDate = Temporal.PlainDate.from({ year: 1976, month: 11, day: 18, calendar }); +TemporalHelpers.assertPlainDate(plainDate, 1976, 11, "M11", 18); +assert.sameValue(plainDate.calendar, calendar); diff --git a/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime-convert.js b/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime-convert.js new file mode 100644 index 0000000000..658e86c720 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime-convert.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.plaindate.from +description: An exception from TimeZone#getOffsetNanosecondsFor() is propagated. +features: [Temporal] +---*/ + +class TZ extends Temporal.TimeZone { + constructor() { super("UTC") } + getOffsetNanosecondsFor() { throw new Test262Error() } +} + +const tz = new TZ(); +const arg = new Temporal.ZonedDateTime(0n, tz); +const instance = new Temporal.PlainDate(1976, 11, 18); + +assert.throws(Test262Error, () => Temporal.PlainDate.from(arg)); diff --git a/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime.js b/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime.js new file mode 100644 index 0000000000..133ea14f89 --- /dev/null +++ b/test/built-ins/Temporal/PlainDate/from/argument-zoneddatetime.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-temporal.plaindate.from +description: A ZonedDateTime object is handled separately +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const calendar = new Temporal.Calendar("iso8601"); +const zdt = new Temporal.ZonedDateTime(1_000_000_000_987_654_321n, "UTC", calendar); +const result = Temporal.PlainDate.from(zdt); + +TemporalHelpers.assertPlainDate( + result, + 2001, 9, "M09", 9, + "ZonedDateTime is converted" +); + +assert.sameValue( + result.calendar, + calendar, + "Calendar is copied" +);