Skip to content

Commit

Permalink
Temporal: Copy options object in Plain{Date,MonthDay,YearMonth}.{from…
Browse files Browse the repository at this point in the history
…,p.with}
  • Loading branch information
ptomato committed Aug 11, 2023
1 parent b37d2b6 commit bcd44ca
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@
/*---
esid: sec-temporal.plaindate.from
description: overflow property is extracted with ISO-invalid string argument.
info: |
1. Perform ? ToTemporalOverflow(_options_).
1. If ! IsValidISODate(year, month, day) is false, throw a RangeError exception.
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"get overflow",
"get overflow.toString",
"call overflow.toString",
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

let actual = [];
const object = {
get overflow() {
actual.push("get overflow");
return TemporalHelpers.toPrimitiveObserver(actual, "constrain", "overflow");
}
};
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

assert.throws(RangeError, () => Temporal.PlainDate.from("2020-13-34", object));
assert.throws(RangeError, () => Temporal.PlainDate.from("2020-13-34", options));
assert.compareArray(actual, expected);
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@
/*---
esid: sec-temporal.plaindate.from
description: overflow property is extracted with string argument.
info: |
1. Perform ? ToTemporalOverflow(_options_).
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"get overflow",
"get overflow.toString",
"call overflow.toString",
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];

let actual = [];
const object = {
get overflow() {
actual.push("get overflow");
return TemporalHelpers.toPrimitiveObserver(actual, "reject", "overflow");
}
};
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

const result = Temporal.PlainDate.from("2021-05-17", object);
const result = Temporal.PlainDate.from("2021-05-17", options);
assert.compareArray(actual, expected, "Successful call");
TemporalHelpers.assertPlainDate(result, 2021, 5, "M05", 17);

actual.splice(0); // empty it for the next check
assert.throws(TypeError, () => Temporal.PlainDate.from(7, object));
assert.compareArray(actual, expected, "Failing call");
const failureExpected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

assert.throws(TypeError, () => Temporal.PlainDate.from(7, options));
assert.compareArray(actual, failureExpected, "Failing call");
11 changes: 9 additions & 2 deletions test/built-ins/Temporal/PlainDate/from/order-of-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ features: [Temporal]
---*/

const expected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"getOwnPropertyDescriptor options.extra",
"get options.extra",
"get fields.calendar",
"has fields.calendar.dateAdd",
"has fields.calendar.dateFromFields",
Expand Down Expand Up @@ -48,7 +53,6 @@ const expected = [
"get fields.calendar.dateFromFields",
"call fields.calendar.dateFromFields",
// inside Calendar.p.dateFromFields
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];
Expand All @@ -63,7 +67,10 @@ const fields = TemporalHelpers.propertyBagObserver(actual, {
calendar,
}, "fields");

const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");
const options = TemporalHelpers.propertyBagObserver(actual, {
overflow: "constrain",
extra: "property",
}, "options");

const result = Temporal.PlainDate.from(fields, options);
assert.compareArray(actual, expected, "order of operations");
7 changes: 5 additions & 2 deletions test/built-ins/Temporal/PlainDate/prototype/with/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ features: [Temporal]
---*/

const result = new Temporal.PlainDate(1920, 5, 3);
const options = {};
const options = {
extra: "property",
};
let calls = 0;
class CustomCalendar extends Temporal.Calendar {
constructor() {
Expand All @@ -22,7 +24,8 @@ class CustomCalendar extends Temporal.Calendar {
assert.sameValue(args[0].month, 11, "First argument: month");
assert.sameValue(args[0].monthCode, "M11", "First argument: monthCode");
assert.sameValue(args[0].year, 43, "First argument: year");
assert.sameValue(args[1], options, "Second argument");
assert.notSameValue(args[1], options, "Second argument is a copy of options");
assert.sameValue(args[1].extra, "property", "All properties are copied");
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const expected = [
// RejectObjectWithCalendarOrTimeZone
"get fields.calendar",
"get fields.timeZone",
// CopyDataProperties
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"getOwnPropertyDescriptor options.extra",
"get options.extra",
// CalendarFields
"get this.calendar.fields",
"call this.calendar.fields",
Expand Down Expand Up @@ -44,7 +50,6 @@ const expected = [
"get this.calendar.dateFromFields",
"call this.calendar.dateFromFields",
// inside Calendar.p.dateFromFields
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];
Expand All @@ -64,6 +69,7 @@ const fields = TemporalHelpers.propertyBagObserver(actual, {

const options = TemporalHelpers.propertyBagObserver(actual, {
overflow: "constrain",
extra: "property",
}, "options");

instance.with(fields, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainmonthday.from
description: overflow property is extracted with ISO-invalid string argument.
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

let actual = [];
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

assert.throws(RangeError, () => Temporal.PlainMonthDay.from("13-34", options));
assert.compareArray(actual, expected);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainmonthday.from
description: overflow property is extracted with string argument.
info: |
1. Perform ? ToTemporalOverflow(_options_).
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];

let actual = [];
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

const result = Temporal.PlainMonthDay.from("05-17", options);
assert.compareArray(actual, expected, "Successful call");
TemporalHelpers.assertPlainMonthDay(result, "M05", 17);

actual.splice(0); // empty it for the next check
const failureExpected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

assert.throws(TypeError, () => Temporal.PlainMonthDay.from(7, options));
assert.compareArray(actual, failureExpected, "Failing call");
12 changes: 10 additions & 2 deletions test/built-ins/Temporal/PlainMonthDay/from/order-of-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ features: [Temporal]
---*/

const expected = [
// CopyDataProperties
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"getOwnPropertyDescriptor options.extra",
"get options.extra",
"get fields.calendar",
"has fields.calendar.dateAdd",
"has fields.calendar.dateFromFields",
Expand Down Expand Up @@ -51,7 +57,6 @@ const expected = [
"get fields.calendar.monthDayFromFields",
"call fields.calendar.monthDayFromFields",
// inside Calendar.p.monthDayFromFields
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];
Expand All @@ -65,7 +70,10 @@ const fields = TemporalHelpers.propertyBagObserver(actual, {
calendar: TemporalHelpers.calendarObserver(actual, "fields.calendar"),
}, "fields");

const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");
const options = TemporalHelpers.propertyBagObserver(actual, {
overflow: "constrain",
extra: "property",
}, "options");

Temporal.PlainMonthDay.from(fields, options);
assert.compareArray(actual, expected, "order of operations");
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ includes: [temporalHelpers.js]
features: [Temporal]
---*/

const options = {};
const options = {
extra: "property",
};
class CustomCalendar extends Temporal.Calendar {
constructor() {
super("iso8601");
}
monthDayFromFields(...args) {
assert.sameValue(args.length, 2, "args.length");
assert.sameValue(typeof args[0], "object", "args[0]");
assert.sameValue(args[1], options, "args[1]");
assert.notSameValue(args[1], options, "args[1] is a copy of options");
assert.sameValue(args[1].extra, "property", "All properties are copied");
return super.monthDayFromFields(...args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const expected = [
// RejectObjectWithCalendarOrTimeZone
"get fields.calendar",
"get fields.timeZone",
// CopyDataProperties
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"getOwnPropertyDescriptor options.extra",
"get options.extra",
// CalendarFields
"get this.calendar.fields",
"call this.calendar.fields",
Expand Down Expand Up @@ -40,7 +46,6 @@ const expected = [
"get this.calendar.monthDayFromFields",
"call this.calendar.monthDayFromFields",
// inside Calendar.p.monthDayFromFields
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];
Expand All @@ -58,7 +63,10 @@ const fields = TemporalHelpers.propertyBagObserver(actual, {
day: 1.7,
}, "fields");

const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");
const options = TemporalHelpers.propertyBagObserver(actual, {
overflow: "constrain",
extra: "property",
}, "options");

instance.with(fields, options);
assert.compareArray(actual, expected, "order of operations");
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainyearmonth.from
description: overflow property is extracted with ISO-invalid string argument.
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

let actual = [];
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

assert.throws(RangeError, () => Temporal.PlainYearMonth.from("2020-13", options));
assert.compareArray(actual, expected);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plainyearmonth.from
description: overflow property is extracted with string argument.
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/

const expected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];

let actual = [];
const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");

const result = Temporal.PlainYearMonth.from("2021-05", options);
assert.compareArray(actual, expected, "Successful call");
TemporalHelpers.assertPlainYearMonth(result, 2021, 5, "M05");

actual.splice(0); // empty it for the next check
const failureExpected = [
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
];

assert.throws(TypeError, () => Temporal.PlainYearMonth.from(7, options));
assert.compareArray(actual, failureExpected, "Failing call");
12 changes: 10 additions & 2 deletions test/built-ins/Temporal/PlainYearMonth/from/order-of-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ features: [Temporal]
---*/

const expected = [
// CopyDataProperties
"ownKeys options",
"getOwnPropertyDescriptor options.overflow",
"get options.overflow",
"getOwnPropertyDescriptor options.extra",
"get options.extra",
// GetTemporalCalendarSlotValueWithISODefault
"get fields.calendar",
"has fields.calendar.dateAdd",
Expand Down Expand Up @@ -49,7 +55,6 @@ const expected = [
"get fields.calendar.yearMonthFromFields",
"call fields.calendar.yearMonthFromFields",
// inside Calendar.p.yearMonthFromFields
"get options.overflow",
"get options.overflow.toString",
"call options.overflow.toString",
];
Expand All @@ -62,7 +67,10 @@ const fields = TemporalHelpers.propertyBagObserver(actual, {
calendar: TemporalHelpers.calendarObserver(actual, "fields.calendar"),
}, "fields");

const options = TemporalHelpers.propertyBagObserver(actual, { overflow: "constrain" }, "options");
const options = TemporalHelpers.propertyBagObserver(actual, {
overflow: "constrain",
extra: "property",
}, "options");

Temporal.PlainYearMonth.from(fields, options);
assert.compareArray(actual, expected, "order of operations");
Loading

0 comments on commit bcd44ca

Please sign in to comment.