Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Temporal tests #3819

Merged
merged 4 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions harness/temporalHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defines: [TemporalHelpers]
features: [Symbol.species, Symbol.iterator, Temporal]
---*/

const IDENTIFIER = /^[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*$/u;

function formatPropertyName(propertyKey, objectName = "") {
switch (typeof propertyKey) {
case "symbol":
Expand All @@ -17,13 +19,20 @@ function formatPropertyName(propertyKey, objectName = "") {
} else {
return `${objectName}[Symbol('${propertyKey.description}')]`
}
case "number":
return `${objectName}[${propertyKey}]`;
case "string":
if (propertyKey !== String(Number(propertyKey))) {
if (IDENTIFIER.test(propertyKey)) {
return objectName ? `${objectName}.${propertyKey}` : propertyKey;
}
return `${objectName}['${propertyKey.replace(/'/g, "\\'")}']`
}
// fall through
default:
// TODO: check if propertyKey is an integer index.
return objectName ? `${objectName}.${propertyKey}` : propertyKey;
// integer or string integer-index
return `${objectName}[${propertyKey}]`;
}
}

const SKIP_SYMBOL = Symbol("Skip");

var TemporalHelpers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ asyncTest(async function () {
"get items.length",
"get items.length.valueOf",
"call items.length.valueOf",
"get items.0",
"get items.1",
"get items[0]",
"get items[1]",
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const expected = [
"get fields.extra",
// CopyDataProperties on additionalFields
"ownKeys additionalFields",
"getOwnPropertyDescriptor additionalFields.3",
"get additionalFields.3",
"getOwnPropertyDescriptor additionalFields[3]",
"get additionalFields[3]",
"getOwnPropertyDescriptor additionalFields.monthCode",
"get additionalFields.monthCode",
"getOwnPropertyDescriptor additionalFields[Symbol('extra')]",
Expand Down
98 changes: 79 additions & 19 deletions test/built-ins/Temporal/Duration/compare/order-of-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ Temporal.Duration.compare(
assert.compareArray(actual, expectedOpsForPlainRelativeTo, "order of operations with PlainDate relativeTo and no calendar units");
actual.splice(0); // clear

// code path through UnbalanceDurationRelative that balances higher units down
// to days:
const expectedOpsForPlainDayBalancing = expectedOpsForPlainRelativeTo.concat(
[
// UnbalanceDurationRelative
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
// UnbalanceDurationRelative again for the second argument:
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
]
);
Temporal.Duration.compare(
createDurationPropertyBagObserver("one", 1, 1, 1),
createDurationPropertyBagObserver("two", 1, 1, 1, 1),
createOptionsObserver(plainRelativeTo)
);
assert.compareArray(actual, expectedOpsForPlainDayBalancing, "order of operations with PlainDate relativeTo and calendar units");
actual.splice(0); // clear

const expectedOpsForZonedRelativeTo = expected.concat([
// ToRelativeTemporalObject
"get options.relativeTo.calendar",
Expand Down Expand Up @@ -240,6 +264,9 @@ const expectedOpsForZonedRelativeTo = expected.concat([
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]);

const expectedOpsForCalculateOffsetShift = [
// CalculateOffsetShift on first argument
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
Expand All @@ -264,7 +291,7 @@ const expectedOpsForZonedRelativeTo = expected.concat([
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]);
];

const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {
year: 2001,
Expand All @@ -282,33 +309,66 @@ const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {
timeZone: TemporalHelpers.timeZoneObserver(actual, "options.relativeTo.timeZone"),
}, "options.relativeTo");

// order of observable operations with zoned relativeTo and without calendar units
// order of observable operations with zoned relativeTo and without calendar units except days
Temporal.Duration.compare(
createDurationPropertyBagObserver("one", 0, 0, 0, 7),
createDurationPropertyBagObserver("two", 0, 0, 0, 6),
createOptionsObserver(zonedRelativeTo)
);
assert.compareArray(actual, expectedOpsForZonedRelativeTo, "order of operations with ZonedDateTime relativeTo and no calendar units");
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat(expectedOpsForCalculateOffsetShift),
"order of operations with ZonedDateTime relativeTo and no calendar units except days"
);
actual.splice(0); // clear

// order of observable operations with zoned relativeTo and with only time units
Temporal.Duration.compare(
createDurationPropertyBagObserver("one", 0, 0, 0, 0, 7),
createDurationPropertyBagObserver("two", 0, 0, 0, 0, 6),
createOptionsObserver(zonedRelativeTo)
);
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat([
// CalculateOffsetShift on first arg
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// CalculateOffsetShift on second arg
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]),
"order of operations with ZonedDateTime relativeTo and only time units"
);
actual.splice(0); // clear

// code path through UnbalanceDurationRelative that balances higher units down
// to days:
const expectedOpsForDayBalancing = expectedOpsForZonedRelativeTo.concat([
// UnbalanceDurationRelative
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7.a ToTemporalDate
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
// UnbalanceDurationRelative again for the second argument:
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7.a ToTemporalDate
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
]);
const expectedOpsForDayBalancing = expectedOpsForZonedRelativeTo.concat(
expectedOpsForCalculateOffsetShift,
[
// UnbalanceDurationRelative
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7.a ToTemporalDate
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
// UnbalanceDurationRelative again for the second argument:
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7.a ToTemporalDate
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
]
);
Temporal.Duration.compare(
createDurationPropertyBagObserver("one", 1, 1, 1),
createDurationPropertyBagObserver("two", 1, 1, 1, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,44 @@ const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {
// basic order of operations with ZonedDateTime relativeTo:
instance.round(createOptionsObserver({ relativeTo: zonedRelativeTo }));
assert.compareArray(actual, expectedOpsForZonedRelativeTo, "order of operations for ZonedDateTime relativeTo");
actual.splice(0); // clear

// code path through RoundDuration that rounds to the nearest year:
const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([
// NanosecondsToDays
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 11. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateUntil", // 12. DifferenceISODateTime
"call options.relativeTo.calendar.dateUntil",
// NanosecondsToDays → AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 8.
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor
"call options.relativeTo.timeZone.getPossibleInstantsFor",
// NanosecondsToDays → AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 8.
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.calendar.dateAdd", // 9.b
"call options.relativeTo.calendar.dateAdd", // 9.c
"call options.relativeTo.calendar.dateAdd", // 9.e
"call options.relativeTo.calendar.dateAdd", // 9.j
"get options.relativeTo.calendar.dateUntil", // 9.m
"call options.relativeTo.calendar.dateUntil", // 9.m
"call options.relativeTo.calendar.dateAdd", // 9.r
"call options.relativeTo.calendar.dateAdd", // 9.w MoveRelativeDate
]);
instance.round(createOptionsObserver({ smallestUnit: "years", relativeTo: zonedRelativeTo }));
assert.compareArray(
actual,
expectedOpsForYearRoundingZoned,
"order of operations with smallestUnit = years and ZonedDateTime relativeTo"
);
actual.splice(0); // clear
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ assert.throws(RangeError, () =>
oneNsDuration.round({
relativeTo: zdt,
smallestUnit: "days",
})
}),
"RangeError when days < 0 and sign = 1"
);

// NanosecondsToDays.20: days > 0 and sign = -1
Expand All @@ -74,7 +75,8 @@ assert.throws(RangeError, () =>
negOneNsDuration.round({
relativeTo: zdt,
smallestUnit: "days",
})
}),
"RangeError when days > 0 and sign = -1"
);

// NanosecondsToDays.22: nanoseconds > 0 and sign = -1
Expand All @@ -93,9 +95,10 @@ zdt = new Temporal.ZonedDateTime(
)
);
assert.throws(RangeError, () =>
// Using -1ns duration sets _nanoseocnds_ to -1 and _sign_ to -1
// Using -1ns duration sets _nanoseconds_ to -1 and _sign_ to -1
negOneNsDuration.round({
relativeTo: zdt,
smallestUnit: "days",
})
}),
"RangeError when nanoseconds > 0 and sign = -1"
);
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ const expectedOpsForZonedRelativeTo = [
"get options.unit",
"get options.unit.toString",
"call options.unit.toString",
// RoundDuration → ToTemporalDate
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
];

const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {
Expand All @@ -258,4 +255,63 @@ const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {

// basic order of observable operations, without rounding:
instance.total(createOptionsObserver({ unit: "nanoseconds", relativeTo: zonedRelativeTo }));
assert.compareArray(actual, expectedOpsForZonedRelativeTo, "order of operations for ZonedDateTime relativeTo");
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat([
// RoundDuration → ToTemporalDate
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]),
"order of operations for ZonedDateTime relativeTo");
actual.splice(0); // clear

const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([
// BalancePossiblyInfiniteDuration → NanosecondsToDays
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 7. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 11. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateUntil", // 12. DifferenceISODateTime
"call options.relativeTo.calendar.dateUntil",
// BalancePossiblyInfiniteDuration → NanosecondsToDays → AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 8.
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor
"call options.relativeTo.timeZone.getPossibleInstantsFor",
// BalancePossiblyInfiniteDuration → NanosecondsToDays → AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 8.
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor
"call options.relativeTo.timeZone.getPossibleInstantsFor",
], [
// ToTemporalDate
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// code path through RoundDuration that rounds to the nearest year:
// MoveRelativeZonedDateTime → AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.calendar.dateAdd", // 8.
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.calendar.dateAdd", // 9.b
"call options.relativeTo.calendar.dateAdd", // 9.c
"call options.relativeTo.calendar.dateAdd", // 9.e
"call options.relativeTo.calendar.dateAdd", // 9.j
"get options.relativeTo.calendar.dateUntil", // 9.m
"call options.relativeTo.calendar.dateUntil", // 9.m
"call options.relativeTo.calendar.dateAdd", // 9.r
"call options.relativeTo.calendar.dateAdd", // 9.w MoveRelativeDate
]);
instance.total(createOptionsObserver({ unit: "years", relativeTo: zonedRelativeTo }));
assert.compareArray(
actual,
expectedOpsForYearRoundingZoned,
"order of operations with unit = years and ZonedDateTime relativeTo"
);
actual.splice(0); // clear
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ assert.throws(RangeError, () =>
oneNsDuration.total({
relativeTo: zdt,
unit: "day",
})
}),
"RangeError when days < 0 and sign = 1"
);

// NanosecondsToDays.20: days > 0 and sign = -1
Expand All @@ -72,7 +73,8 @@ assert.throws(RangeError, () =>
negOneNsDuration.total({
relativeTo: zdt,
unit: "day",
})
}),
"RangeError when days > 0 and sign = -1"
);

// NanosecondsToDays.22: nanoseconds > 0 and sign = -1
Expand All @@ -94,5 +96,6 @@ assert.throws(RangeError, () =>
negOneNsDuration.total({
relativeTo: zdt,
unit: "day",
})
}),
"RangeError when nanoseconds > 0 and sign = -1"
);
Loading