| 1 |
// GENERATED, DO NOT EDIT |
| 2 |
// file: asyncHelpers.js |
| 3 |
// Copyright (C) 2022 Igalia, S.L. All rights reserved. |
| 4 |
// This code is governed by the BSD license found in the LICENSE file. |
| 5 |
/*--- |
| 6 |
description: | |
| 7 |
A collection of assertion and wrapper functions for testing asynchronous built-ins. |
| 8 |
defines: [asyncTest, assert.throwsAsync] |
| 9 |
---*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Defines the **sole** asynchronous test of a file. |
| 13 |
* @see {@link ../docs/rfcs/async-helpers.md} for background. |
| 14 |
* |
| 15 |
* @param {Function} testFunc a callback whose returned promise indicates test results |
| 16 |
* (fulfillment for success, rejection for failure) |
| 17 |
* @returns {void} |
| 18 |
*/ |
| 19 |
function asyncTest(testFunc) { |
| 20 |
if (!Object.prototype.hasOwnProperty.call(globalThis, "$DONE")) { |
| 21 |
throw new Test262Error("asyncTest called without async flag"); |
| 22 |
} |
| 23 |
if (typeof testFunc !== "function") { |
| 24 |
$DONE(new Test262Error("asyncTest called with non-function argument")); |
| 25 |
return; |
| 26 |
} |
| 27 |
try { |
| 28 |
testFunc().then( |
| 29 |
function () { |
| 30 |
$DONE(); |
| 31 |
}, |
| 32 |
function (error) { |
| 33 |
$DONE(error); |
| 34 |
} |
| 35 |
); |
| 36 |
} catch (syncError) { |
| 37 |
$DONE(syncError); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Asserts that a callback asynchronously throws an instance of a particular |
| 43 |
* error (i.e., returns a promise whose rejection value is an object referencing |
| 44 |
* the constructor). |
| 45 |
* |
| 46 |
* @param {Function} expectedErrorConstructor the expected constructor of the |
| 47 |
* rejection value |
| 48 |
* @param {Function} func the callback |
| 49 |
* @param {string} [message] the prefix to use for failure messages |
| 50 |
* @returns {Promise<void>} fulfills if the expected error is thrown, |
| 51 |
* otherwise rejects |
| 52 |
*/ |
| 53 |
assert.throwsAsync = function (expectedErrorConstructor, func, message) { |
| 54 |
return new Promise(function (resolve) { |
| 55 |
var fail = function (detail) { |
| 56 |
if (message === undefined) { |
| 57 |
throw new Test262Error(detail); |
| 58 |
} |
| 59 |
throw new Test262Error(message + " " + detail); |
| 60 |
}; |
| 61 |
if (typeof expectedErrorConstructor !== "function") { |
| 62 |
fail("assert.throwsAsync called with an argument that is not an error constructor"); |
| 63 |
} |
| 64 |
if (typeof func !== "function") { |
| 65 |
fail("assert.throwsAsync called with an argument that is not a function"); |
| 66 |
} |
| 67 |
var expectedName = expectedErrorConstructor.name; |
| 68 |
var expectation = "Expected a " + expectedName + " to be thrown asynchronously"; |
| 69 |
var res; |
| 70 |
try { |
| 71 |
res = func(); |
| 72 |
} catch (thrown) { |
| 73 |
fail(expectation + " but the function threw synchronously"); |
| 74 |
} |
| 75 |
if (res === null || typeof res !== "object" || typeof res.then !== "function") { |
| 76 |
fail(expectation + " but result was not a thenable"); |
| 77 |
} |
| 78 |
var onResFulfilled, onResRejected; |
| 79 |
var resSettlementP = new Promise(function (onFulfilled, onRejected) { |
| 80 |
onResFulfilled = onFulfilled; |
| 81 |
onResRejected = onRejected; |
| 82 |
}); |
| 83 |
try { |
| 84 |
res.then(onResFulfilled, onResRejected) |
| 85 |
} catch (thrown) { |
| 86 |
fail(expectation + " but .then threw synchronously"); |
| 87 |
} |
| 88 |
resolve(resSettlementP.then( |
| 89 |
function () { |
| 90 |
fail(expectation + " but no exception was thrown at all"); |
| 91 |
}, |
| 92 |
function (thrown) { |
| 93 |
var actualName; |
| 94 |
if (thrown === null || typeof thrown !== "object") { |
| 95 |
fail(expectation + " but thrown value was not an object"); |
| 96 |
} else if (thrown.constructor !== expectedErrorConstructor) { |
| 97 |
actualName = thrown.constructor.name; |
| 98 |
if (expectedName === actualName) { |
| 99 |
fail(expectation + |
| 100 |
" but got a different error constructor with the same name"); |
| 101 |
} |
| 102 |
fail(expectation + " but got a " + actualName); |
| 103 |
} |
| 104 |
} |
| 105 |
)); |
| 106 |
}); |
| 107 |
}; |
| 108 |
|
| 109 |
// file: temporalHelpers.js |
| 110 |
// Copyright (C) 2021 Igalia, S.L. All rights reserved. |
| 111 |
// This code is governed by the BSD license found in the LICENSE file. |
| 112 |
/*--- |
| 113 |
description: | |
| 114 |
This defines helper objects and functions for testing Temporal. |
| 115 |
defines: [TemporalHelpers] |
| 116 |
features: [Symbol.species, Symbol.iterator, Temporal] |
| 117 |
---*/ |
| 118 |
|
| 119 |
const ASCII_IDENTIFIER = /^[$_a-zA-Z][$_a-zA-Z0-9]*$/u; |
| 120 |
|
| 121 |
function formatPropertyName(propertyKey, objectName = "") { |
| 122 |
switch (typeof propertyKey) { |
| 123 |
case "symbol": |
| 124 |
if (Symbol.keyFor(propertyKey) !== undefined) { |
| 125 |
return `${objectName}[Symbol.for('${Symbol.keyFor(propertyKey)}')]`; |
| 126 |
} else if (propertyKey.description.startsWith('Symbol.')) { |
| 127 |
return `${objectName}[${propertyKey.description}]`; |
| 128 |
} else { |
| 129 |
return `${objectName}[Symbol('${propertyKey.description}')]` |
| 130 |
} |
| 131 |
case "string": |
| 132 |
if (propertyKey !== String(Number(propertyKey))) { |
| 133 |
if (ASCII_IDENTIFIER.test(propertyKey)) { |
| 134 |
return objectName ? `${objectName}.${propertyKey}` : propertyKey; |
| 135 |
} |
| 136 |
return `${objectName}['${propertyKey.replace(/'/g, "\\'")}']` |
| 137 |
} |
| 138 |
// fall through |
| 139 |
default: |
| 140 |
// integer or string integer-index |
| 141 |
return `${objectName}[${propertyKey}]`; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
const SKIP_SYMBOL = Symbol("Skip"); |
| 146 |
|
| 147 |
var TemporalHelpers = { |
| 148 |
/* |
| 149 |
* Codes and maximum lengths of months in the ISO 8601 calendar. |
| 150 |
*/ |
| 151 |
ISOMonths: [ |
| 152 |
{ month: 1, monthCode: "M01", daysInMonth: 31 }, |
| 153 |
{ month: 2, monthCode: "M02", daysInMonth: 29 }, |
| 154 |
{ month: 3, monthCode: "M03", daysInMonth: 31 }, |
| 155 |
{ month: 4, monthCode: "M04", daysInMonth: 30 }, |
| 156 |
{ month: 5, monthCode: "M05", daysInMonth: 31 }, |
| 157 |
{ month: 6, monthCode: "M06", daysInMonth: 30 }, |
| 158 |
{ month: 7, monthCode: "M07", daysInMonth: 31 }, |
| 159 |
{ month: 8, monthCode: "M08", daysInMonth: 31 }, |
| 160 |
{ month: 9, monthCode: "M09", daysInMonth: 30 }, |
| 161 |
{ month: 10, monthCode: "M10", daysInMonth: 31 }, |
| 162 |
{ month: 11, monthCode: "M11", daysInMonth: 30 }, |
| 163 |
{ month: 12, monthCode: "M12", daysInMonth: 31 } |
| 164 |
], |
| 165 |
|
| 166 |
/* |
| 167 |
* List of known calendar eras and their possible aliases. |
| 168 |
* |
| 169 |
* https://tc39.es/proposal-intl-era-monthcode/#table-eras |
| 170 |
*/ |
| 171 |
CalendarEras: { |
| 172 |
buddhist: [ |
| 173 |
{ era: "buddhist", aliases: ["be"] }, |
| 174 |
], |
| 175 |
chinese: [ |
| 176 |
{ era: "chinese" }, |
| 177 |
], |
| 178 |
coptic: [ |
| 179 |
{ era: "coptic" }, |
| 180 |
{ era: "coptic-inverse" }, |
| 181 |
], |
| 182 |
dangi: [ |
| 183 |
{ era: "dangi" }, |
| 184 |
], |
| 185 |
ethiopic: [ |
| 186 |
{ era: "ethiopic", aliases: ["incar"] }, |
| 187 |
{ era: "ethioaa", aliases: ["ethiopic-amete-alem", "mundi"] }, |
| 188 |
], |
| 189 |
ethioaa: [ |
| 190 |
{ era: "ethioaa", aliases: ["ethiopic-amete-alem", "mundi"] }, |
| 191 |
], |
| 192 |
gregory: [ |
| 193 |
{ era: "gregory", aliases: ["ce", "ad"] }, |
| 194 |
{ era: "gregory-inverse", aliases: ["bc", "bce"] }, |
| 195 |
], |
| 196 |
hebrew: [ |
| 197 |
{ era: "hebrew", aliases: ["am"] }, |
| 198 |
], |
| 199 |
indian: [ |
| 200 |
{ era: "indian", aliases: ["saka"] }, |
| 201 |
], |
| 202 |
islamic: [ |
| 203 |
{ era: "islamic", aliases: ["ah"] }, |
| 204 |
], |
| 205 |
"islamic-civil": [ |
| 206 |
{ era: "islamic-civil", aliases: ["islamicc", "ah"] }, |
| 207 |
], |
| 208 |
"islamic-rgsa": [ |
| 209 |
{ era: "islamic-rgsa", aliases: ["ah"] }, |
| 210 |
], |
| 211 |
"islamic-tbla": [ |
| 212 |
{ era: "islamic-tbla", aliases: ["ah"] }, |
| 213 |
], |
| 214 |
"islamic-umalqura": [ |
| 215 |
{ era: "islamic-umalqura", aliases: ["ah"] }, |
| 216 |
], |
| 217 |
japanese: [ |
| 218 |
{ era: "heisei" }, |
| 219 |
{ era: "japanese", aliases: ["gregory", "ad", "ce"] }, |
| 220 |
{ era: "japanese-inverse", aliases: ["gregory-inverse", "bc", "bce"] }, |
| 221 |
{ era: "meiji" }, |
| 222 |
{ era: "reiwa" }, |
| 223 |
{ era: "showa" }, |
| 224 |
{ era: "taisho" }, |
| 225 |
], |
| 226 |
persian: [ |
| 227 |
{ era: "persian", aliases: ["ap"] }, |
| 228 |
], |
| 229 |
roc: [ |
| 230 |
{ era: "roc", aliases: ["minguo"] }, |
| 231 |
{ era: "roc-inverse", aliases: ["before-roc"] }, |
| 232 |
], |
| 233 |
}, |
| 234 |
|
| 235 |
/* |
| 236 |
* Return the canonical era code. |
| 237 |
*/ |
| 238 |
canonicalizeCalendarEra(calendarId, eraName) { |
| 239 |
assert.sameValue(typeof calendarId, "string", "calendar must be string in canonicalizeCalendarEra"); |
| 240 |
|
| 241 |
if (calendarId === "iso8601") { |
| 242 |
assert.sameValue(eraName, undefined); |
| 243 |
return undefined; |
| 244 |
} |
| 245 |
assert(Object.prototype.hasOwnProperty.call(TemporalHelpers.CalendarEras, calendarId)); |
| 246 |
|
| 247 |
if (eraName === undefined) { |
| 248 |
return undefined; |
| 249 |
} |
| 250 |
assert.sameValue(typeof eraName, "string", "eraName must be string or undefined in canonicalizeCalendarEra"); |
| 251 |
|
| 252 |
for (let {era, aliases = []} of TemporalHelpers.CalendarEras[calendarId]) { |
| 253 |
if (era === eraName || aliases.includes(eraName)) { |
| 254 |
return era; |
| 255 |
} |
| 256 |
} |
| 257 |
throw new Test262Error(`Unsupported era name: ${eraName}`); |
| 258 |
}, |
| 259 |
|
| 260 |
/* |
| 261 |
* assertDuration(duration, years, ..., nanoseconds[, description]): |
| 262 |
* |
| 263 |
* Shorthand for asserting that each field of a Temporal.Duration is equal to |
| 264 |
* an expected value. |
| 265 |
*/ |
| 266 |
assertDuration(duration, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, description = "") { |
| 267 |
const prefix = description ? `${description}: ` : ""; |
| 268 |
assert(duration instanceof Temporal.Duration, `${prefix}instanceof`); |
| 269 |
assert.sameValue(duration.years, years, `${prefix}years result:`); |
| 270 |
assert.sameValue(duration.months, months, `${prefix}months result:`); |
| 271 |
assert.sameValue(duration.weeks, weeks, `${prefix}weeks result:`); |
| 272 |
assert.sameValue(duration.days, days, `${prefix}days result:`); |
| 273 |
assert.sameValue(duration.hours, hours, `${prefix}hours result:`); |
| 274 |
assert.sameValue(duration.minutes, minutes, `${prefix}minutes result:`); |
| 275 |
assert.sameValue(duration.seconds, seconds, `${prefix}seconds result:`); |
| 276 |
assert.sameValue(duration.milliseconds, milliseconds, `${prefix}milliseconds result:`); |
| 277 |
assert.sameValue(duration.microseconds, microseconds, `${prefix}microseconds result:`); |
| 278 |
assert.sameValue(duration.nanoseconds, nanoseconds, `${prefix}nanoseconds result`); |
| 279 |
}, |
| 280 |
|
| 281 |
/* |
| 282 |
* assertDateDuration(duration, years, months, weeks, days, [, description]): |
| 283 |
* |
| 284 |
* Shorthand for asserting that each date field of a Temporal.Duration is |
| 285 |
* equal to an expected value. |
| 286 |
*/ |
| 287 |
assertDateDuration(duration, years, months, weeks, days, description = "") { |
| 288 |
const prefix = description ? `${description}: ` : ""; |
| 289 |
assert(duration instanceof Temporal.Duration, `${prefix}instanceof`); |
| 290 |
assert.sameValue(duration.years, years, `${prefix}years result:`); |
| 291 |
assert.sameValue(duration.months, months, `${prefix}months result:`); |
| 292 |
assert.sameValue(duration.weeks, weeks, `${prefix}weeks result:`); |
| 293 |
assert.sameValue(duration.days, days, `${prefix}days result:`); |
| 294 |
assert.sameValue(duration.hours, 0, `${prefix}hours result should be zero:`); |
| 295 |
assert.sameValue(duration.minutes, 0, `${prefix}minutes result should be zero:`); |
| 296 |
assert.sameValue(duration.seconds, 0, `${prefix}seconds result should be zero:`); |
| 297 |
assert.sameValue(duration.milliseconds, 0, `${prefix}milliseconds result should be zero:`); |
| 298 |
assert.sameValue(duration.microseconds, 0, `${prefix}microseconds result should be zero:`); |
| 299 |
assert.sameValue(duration.nanoseconds, 0, `${prefix}nanoseconds result should be zero:`); |
| 300 |
}, |
| 301 |
|
| 302 |
/* |
| 303 |
* assertDurationsEqual(actual, expected[, description]): |
| 304 |
* |
| 305 |
* Shorthand for asserting that each field of a Temporal.Duration is equal to |
| 306 |
* the corresponding field in another Temporal.Duration. |
| 307 |
*/ |
| 308 |
assertDurationsEqual(actual, expected, description = "") { |
| 309 |
const prefix = description ? `${description}: ` : ""; |
| 310 |
assert(expected instanceof Temporal.Duration, `${prefix}expected value should be a Temporal.Duration`); |
| 311 |
TemporalHelpers.assertDuration(actual, expected.years, expected.months, expected.weeks, expected.days, expected.hours, expected.minutes, expected.seconds, expected.milliseconds, expected.microseconds, expected.nanoseconds, description); |
| 312 |
}, |
| 313 |
|
| 314 |
/* |
| 315 |
* assertInstantsEqual(actual, expected[, description]): |
| 316 |
* |
| 317 |
* Shorthand for asserting that two Temporal.Instants are of the correct type |
| 318 |
* and equal according to their equals() methods. |
| 319 |
*/ |
| 320 |
assertInstantsEqual(actual, expected, description = "") { |
| 321 |
const prefix = description ? `${description}: ` : ""; |
| 322 |
assert(expected instanceof Temporal.Instant, `${prefix}expected value should be a Temporal.Instant`); |
| 323 |
assert(actual instanceof Temporal.Instant, `${prefix}instanceof`); |
| 324 |
assert(actual.equals(expected), `${prefix}equals method`); |
| 325 |
}, |
| 326 |
|
| 327 |
/* |
| 328 |
* assertPlainDate(date, year, ..., nanosecond[, description[, era, eraYear]]): |
| 329 |
* |
| 330 |
* Shorthand for asserting that each field of a Temporal.PlainDate is equal to |
| 331 |
* an expected value. (Except the `calendar` property, since callers may want |
| 332 |
* to assert either object equality with an object they put in there, or the |
| 333 |
* value of date.calendarId.) |
| 334 |
*/ |
| 335 |
assertPlainDate(date, year, month, monthCode, day, description = "", era = undefined, eraYear = undefined) { |
| 336 |
const prefix = description ? `${description}: ` : ""; |
| 337 |
assert(date instanceof Temporal.PlainDate, `${prefix}instanceof`); |
| 338 |
assert.sameValue( |
| 339 |
TemporalHelpers.canonicalizeCalendarEra(date.calendarId, date.era), |
| 340 |
TemporalHelpers.canonicalizeCalendarEra(date.calendarId, era), |
| 341 |
`${prefix}era result:` |
| 342 |
); |
| 343 |
assert.sameValue(date.eraYear, eraYear, `${prefix}eraYear result:`); |
| 344 |
assert.sameValue(date.year, year, `${prefix}year result:`); |
| 345 |
assert.sameValue(date.month, month, `${prefix}month result:`); |
| 346 |
assert.sameValue(date.monthCode, monthCode, `${prefix}monthCode result:`); |
| 347 |
assert.sameValue(date.day, day, `${prefix}day result:`); |
| 348 |
}, |
| 349 |
|
| 350 |
/* |
| 351 |
* assertPlainDateTime(datetime, year, ..., nanosecond[, description[, era, eraYear]]): |
| 352 |
* |
| 353 |
* Shorthand for asserting that each field of a Temporal.PlainDateTime is |
| 354 |
* equal to an expected value. (Except the `calendar` property, since callers |
| 355 |
* may want to assert either object equality with an object they put in there, |
| 356 |
* or the value of datetime.calendarId.) |
| 357 |
*/ |
| 358 |
assertPlainDateTime(datetime, year, month, monthCode, day, hour, minute, second, millisecond, microsecond, nanosecond, description = "", era = undefined, eraYear = undefined) { |
| 359 |
const prefix = description ? `${description}: ` : ""; |
| 360 |
assert(datetime instanceof Temporal.PlainDateTime, `${prefix}instanceof`); |
| 361 |
assert.sameValue( |
| 362 |
TemporalHelpers.canonicalizeCalendarEra(datetime.calendarId, datetime.era), |
| 363 |
TemporalHelpers.canonicalizeCalendarEra(datetime.calendarId, era), |
| 364 |
`${prefix}era result:` |
| 365 |
); |
| 366 |
assert.sameValue(datetime.eraYear, eraYear, `${prefix}eraYear result:`); |
| 367 |
assert.sameValue(datetime.year, year, `${prefix}year result:`); |
| 368 |
assert.sameValue(datetime.month, month, `${prefix}month result:`); |
| 369 |
assert.sameValue(datetime.monthCode, monthCode, `${prefix}monthCode result:`); |
| 370 |
assert.sameValue(datetime.day, day, `${prefix}day result:`); |
| 371 |
assert.sameValue(datetime.hour, hour, `${prefix}hour result:`); |
| 372 |
assert.sameValue(datetime.minute, minute, `${prefix}minute result:`); |
| 373 |
assert.sameValue(datetime.second, second, `${prefix}second result:`); |
| 374 |
assert.sameValue(datetime.millisecond, millisecond, `${prefix}millisecond result:`); |
| 375 |
assert.sameValue(datetime.microsecond, microsecond, `${prefix}microsecond result:`); |
| 376 |
assert.sameValue(datetime.nanosecond, nanosecond, `${prefix}nanosecond result:`); |
| 377 |
}, |
| 378 |
|
| 379 |
/* |
| 380 |
* assertPlainDateTimesEqual(actual, expected[, description]): |
| 381 |
* |
| 382 |
* Shorthand for asserting that two Temporal.PlainDateTimes are of the correct |
| 383 |
* type, equal according to their equals() methods, and additionally that |
| 384 |
* their calendar internal slots are the same value. |
| 385 |
*/ |
| 386 |
assertPlainDateTimesEqual(actual, expected, description = "") { |
| 387 |
const prefix = description ? `${description}: ` : ""; |
| 388 |
assert(expected instanceof Temporal.PlainDateTime, `${prefix}expected value should be a Temporal.PlainDateTime`); |
| 389 |
assert(actual instanceof Temporal.PlainDateTime, `${prefix}instanceof`); |
| 390 |
assert(actual.equals(expected), `${prefix}equals method`); |
| 391 |
assert.sameValue( |
| 392 |
actual.calendarId, |
| 393 |
expected.calendarId, |
| 394 |
`${prefix}calendar same value:` |
| 395 |
); |
| 396 |
}, |
| 397 |
|
| 398 |
/* |
| 399 |
* assertPlainMonthDay(monthDay, monthCode, day[, description [, referenceISOYear]]): |
| 400 |
* |
| 401 |
* Shorthand for asserting that each field of a Temporal.PlainMonthDay is |
| 402 |
* equal to an expected value. (Except the `calendar` property, since callers |
| 403 |
* may want to assert either object equality with an object they put in there, |
| 404 |
* or the value of monthDay.calendarId().) |
| 405 |
*/ |
| 406 |
assertPlainMonthDay(monthDay, monthCode, day, description = "", referenceISOYear = 1972) { |
| 407 |
const prefix = description ? `${description}: ` : ""; |
| 408 |
assert(monthDay instanceof Temporal.PlainMonthDay, `${prefix}instanceof`); |
| 409 |
assert.sameValue(monthDay.monthCode, monthCode, `${prefix}monthCode result:`); |
| 410 |
assert.sameValue(monthDay.day, day, `${prefix}day result:`); |
| 411 |
const isoYear = Number(monthDay.toString({ calendarName: "always" }).split("-")[0]); |
| 412 |
assert.sameValue(isoYear, referenceISOYear, `${prefix}referenceISOYear result:`); |
| 413 |
}, |
| 414 |
|
| 415 |
/* |
| 416 |
* assertPlainTime(time, hour, ..., nanosecond[, description]): |
| 417 |
* |
| 418 |
* Shorthand for asserting that each field of a Temporal.PlainTime is equal to |
| 419 |
* an expected value. |
| 420 |
*/ |
| 421 |
assertPlainTime(time, hour, minute, second, millisecond, microsecond, nanosecond, description = "") { |
| 422 |
const prefix = description ? `${description}: ` : ""; |
| 423 |
assert(time instanceof Temporal.PlainTime, `${prefix}instanceof`); |
| 424 |
assert.sameValue(time.hour, hour, `${prefix}hour result:`); |
| 425 |
assert.sameValue(time.minute, minute, `${prefix}minute result:`); |
| 426 |
assert.sameValue(time.second, second, `${prefix}second result:`); |
| 427 |
assert.sameValue(time.millisecond, millisecond, `${prefix}millisecond result:`); |
| 428 |
assert.sameValue(time.microsecond, microsecond, `${prefix}microsecond result:`); |
| 429 |
assert.sameValue(time.nanosecond, nanosecond, `${prefix}nanosecond result:`); |
| 430 |
}, |
| 431 |
|
| 432 |
/* |
| 433 |
* assertPlainTimesEqual(actual, expected[, description]): |
| 434 |
* |
| 435 |
* Shorthand for asserting that two Temporal.PlainTimes are of the correct |
| 436 |
* type and equal according to their equals() methods. |
| 437 |
*/ |
| 438 |
assertPlainTimesEqual(actual, expected, description = "") { |
| 439 |
const prefix = description ? `${description}: ` : ""; |
| 440 |
assert(expected instanceof Temporal.PlainTime, `${prefix}expected value should be a Temporal.PlainTime`); |
| 441 |
assert(actual instanceof Temporal.PlainTime, `${prefix}instanceof`); |
| 442 |
assert(actual.equals(expected), `${prefix}equals method`); |
| 443 |
}, |
| 444 |
|
| 445 |
/* |
| 446 |
* assertPlainYearMonth(yearMonth, year, month, monthCode[, description[, era, eraYear, referenceISODay]]): |
| 447 |
* |
| 448 |
* Shorthand for asserting that each field of a Temporal.PlainYearMonth is |
| 449 |
* equal to an expected value. (Except the `calendar` property, since callers |
| 450 |
* may want to assert either object equality with an object they put in there, |
| 451 |
* or the value of yearMonth.calendarId.) |
| 452 |
*/ |
| 453 |
assertPlainYearMonth(yearMonth, year, month, monthCode, description = "", era = undefined, eraYear = undefined, referenceISODay = 1) { |
| 454 |
const prefix = description ? `${description}: ` : ""; |
| 455 |
assert(yearMonth instanceof Temporal.PlainYearMonth, `${prefix}instanceof`); |
| 456 |
assert.sameValue( |
| 457 |
TemporalHelpers.canonicalizeCalendarEra(yearMonth.calendarId, yearMonth.era), |
| 458 |
TemporalHelpers.canonicalizeCalendarEra(yearMonth.calendarId, era), |
| 459 |
`${prefix}era result:` |
| 460 |
); |
| 461 |
assert.sameValue(yearMonth.eraYear, eraYear, `${prefix}eraYear result:`); |
| 462 |
assert.sameValue(yearMonth.year, year, `${prefix}year result:`); |
| 463 |
assert.sameValue(yearMonth.month, month, `${prefix}month result:`); |
| 464 |
assert.sameValue(yearMonth.monthCode, monthCode, `${prefix}monthCode result:`); |
| 465 |
const isoDay = Number(yearMonth.toString({ calendarName: "always" }).slice(1).split('-')[2].slice(0, 2)); |
| 466 |
assert.sameValue(isoDay, referenceISODay, `${prefix}referenceISODay result:`); |
| 467 |
}, |
| 468 |
|
| 469 |
/* |
| 470 |
* assertZonedDateTimesEqual(actual, expected[, description]): |
| 471 |
* |
| 472 |
* Shorthand for asserting that two Temporal.ZonedDateTimes are of the correct |
| 473 |
* type, equal according to their equals() methods, and additionally that |
| 474 |
* their time zones and calendar internal slots are the same value. |
| 475 |
*/ |
| 476 |
assertZonedDateTimesEqual(actual, expected, description = "") { |
| 477 |
const prefix = description ? `${description}: ` : ""; |
| 478 |
assert(expected instanceof Temporal.ZonedDateTime, `${prefix}expected value should be a Temporal.ZonedDateTime`); |
| 479 |
assert(actual instanceof Temporal.ZonedDateTime, `${prefix}instanceof`); |
| 480 |
assert(actual.equals(expected), `${prefix}equals method`); |
| 481 |
assert.sameValue(actual.timeZone, expected.timeZone, `${prefix}time zone same value:`); |
| 482 |
assert.sameValue( |
| 483 |
actual.calendarId, |
| 484 |
expected.calendarId, |
| 485 |
`${prefix}calendar same value:` |
| 486 |
); |
| 487 |
}, |
| 488 |
|
| 489 |
/* |
| 490 |
* assertUnreachable(description): |
| 491 |
* |
| 492 |
* Helper for asserting that code is not executed. |
| 493 |
*/ |
| 494 |
assertUnreachable(description) { |
| 495 |
let message = "This code should not be executed"; |
| 496 |
if (description) { |
| 497 |
message = `${message}: ${description}`; |
| 498 |
} |
| 499 |
throw new Test262Error(message); |
| 500 |
}, |
| 501 |
|
| 502 |
/* |
| 503 |
* checkPlainDateTimeConversionFastPath(func): |
| 504 |
* |
| 505 |
* ToTemporalDate and ToTemporalTime should both, if given a |
| 506 |
* Temporal.PlainDateTime instance, convert to the desired type by reading the |
| 507 |
* PlainDateTime's internal slots, rather than calling any getters. |
| 508 |
* |
| 509 |
* func(datetime) is the actual operation to test, that must |
| 510 |
* internally call the abstract operation ToTemporalDate or ToTemporalTime. |
| 511 |
* It is passed a Temporal.PlainDateTime instance. |
| 512 |
*/ |
| 513 |
checkPlainDateTimeConversionFastPath(func, message = "checkPlainDateTimeConversionFastPath") { |
| 514 |
const actual = []; |
| 515 |
const expected = []; |
| 516 |
|
| 517 |
const calendar = "iso8601"; |
| 518 |
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321, calendar); |
| 519 |
const prototypeDescrs = Object.getOwnPropertyDescriptors(Temporal.PlainDateTime.prototype); |
| 520 |
["year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => { |
| 521 |
Object.defineProperty(datetime, property, { |
| 522 |
get() { |
| 523 |
actual.push(`get ${formatPropertyName(property)}`); |
| 524 |
const value = prototypeDescrs[property].get.call(this); |
| 525 |
return { |
| 526 |
toString() { |
| 527 |
actual.push(`toString ${formatPropertyName(property)}`); |
| 528 |
return value.toString(); |
| 529 |
}, |
| 530 |
valueOf() { |
| 531 |
actual.push(`valueOf ${formatPropertyName(property)}`); |
| 532 |
return value; |
| 533 |
}, |
| 534 |
}; |
| 535 |
}, |
| 536 |
}); |
| 537 |
}); |
| 538 |
Object.defineProperty(datetime, "calendar", { |
| 539 |
get() { |
| 540 |
actual.push("get calendar"); |
| 541 |
return calendar; |
| 542 |
}, |
| 543 |
}); |
| 544 |
|
| 545 |
func(datetime); |
| 546 |
assert.compareArray(actual, expected, `${message}: property getters not called`); |
| 547 |
}, |
| 548 |
|
| 549 |
/* |
| 550 |
* Check that an options bag that accepts units written in the singular form, |
| 551 |
* also accepts the same units written in the plural form. |
| 552 |
* func(unit) should call the method with the appropriate options bag |
| 553 |
* containing unit as a value. This will be called twice for each element of |
| 554 |
* validSingularUnits, once with singular and once with plural, and the |
| 555 |
* results of each pair should be the same (whether a Temporal object or a |
| 556 |
* primitive value.) |
| 557 |
*/ |
| 558 |
checkPluralUnitsAccepted(func, validSingularUnits) { |
| 559 |
const plurals = { |
| 560 |
year: 'years', |
| 561 |
month: 'months', |
| 562 |
week: 'weeks', |
| 563 |
day: 'days', |
| 564 |
hour: 'hours', |
| 565 |
minute: 'minutes', |
| 566 |
second: 'seconds', |
| 567 |
millisecond: 'milliseconds', |
| 568 |
microsecond: 'microseconds', |
| 569 |
nanosecond: 'nanoseconds', |
| 570 |
}; |
| 571 |
|
| 572 |
validSingularUnits.forEach((unit) => { |
| 573 |
const singularValue = func(unit); |
| 574 |
const pluralValue = func(plurals[unit]); |
| 575 |
const desc = `Plural ${plurals[unit]} produces the same result as singular ${unit}`; |
| 576 |
if (singularValue instanceof Temporal.Duration) { |
| 577 |
TemporalHelpers.assertDurationsEqual(pluralValue, singularValue, desc); |
| 578 |
} else if (singularValue instanceof Temporal.Instant) { |
| 579 |
TemporalHelpers.assertInstantsEqual(pluralValue, singularValue, desc); |
| 580 |
} else if (singularValue instanceof Temporal.PlainDateTime) { |
| 581 |
TemporalHelpers.assertPlainDateTimesEqual(pluralValue, singularValue, desc); |
| 582 |
} else if (singularValue instanceof Temporal.PlainTime) { |
| 583 |
TemporalHelpers.assertPlainTimesEqual(pluralValue, singularValue, desc); |
| 584 |
} else if (singularValue instanceof Temporal.ZonedDateTime) { |
| 585 |
TemporalHelpers.assertZonedDateTimesEqual(pluralValue, singularValue, desc); |
| 586 |
} else { |
| 587 |
assert.sameValue(pluralValue, singularValue); |
| 588 |
} |
| 589 |
}); |
| 590 |
}, |
| 591 |
|
| 592 |
/* |
| 593 |
* checkRoundingIncrementOptionWrongType(checkFunc, assertTrueResultFunc, assertObjectResultFunc): |
| 594 |
* |
| 595 |
* Checks the type handling of the roundingIncrement option. |
| 596 |
* checkFunc(roundingIncrement) is a function which takes the value of |
| 597 |
* roundingIncrement to test, and calls the method under test with it, |
| 598 |
* returning the result. assertTrueResultFunc(result, description) should |
| 599 |
* assert that result is the expected result with roundingIncrement: true, and |
| 600 |
* assertObjectResultFunc(result, description) should assert that result is |
| 601 |
* the expected result with roundingIncrement being an object with a valueOf() |
| 602 |
* method. |
| 603 |
*/ |
| 604 |
checkRoundingIncrementOptionWrongType(checkFunc, assertTrueResultFunc, assertObjectResultFunc) { |
| 605 |
// null converts to 0, which is out of range |
| 606 |
assert.throws(RangeError, () => checkFunc(null), "null"); |
| 607 |
// Booleans convert to either 0 or 1, and 1 is allowed |
| 608 |
const trueResult = checkFunc(true); |
| 609 |
assertTrueResultFunc(trueResult, "true"); |
| 610 |
assert.throws(RangeError, () => checkFunc(false), "false"); |
| 611 |
// Symbols and BigInts cannot convert to numbers |
| 612 |
assert.throws(TypeError, () => checkFunc(Symbol()), "symbol"); |
| 613 |
assert.throws(TypeError, () => checkFunc(2n), "bigint"); |
| 614 |
|
| 615 |
// Objects prefer their valueOf() methods when converting to a number |
| 616 |
assert.throws(RangeError, () => checkFunc({}), "plain object"); |
| 617 |
|
| 618 |
const expected = [ |
| 619 |
"get roundingIncrement.valueOf", |
| 620 |
"call roundingIncrement.valueOf", |
| 621 |
]; |
| 622 |
const actual = []; |
| 623 |
const observer = TemporalHelpers.toPrimitiveObserver(actual, 2, "roundingIncrement"); |
| 624 |
const objectResult = checkFunc(observer); |
| 625 |
assertObjectResultFunc(objectResult, "object with valueOf"); |
| 626 |
assert.compareArray(actual, expected, "order of operations"); |
| 627 |
}, |
| 628 |
|
| 629 |
/* |
| 630 |
* checkStringOptionWrongType(propertyName, value, checkFunc, assertFunc): |
| 631 |
* |
| 632 |
* Checks the type handling of a string option, of which there are several in |
| 633 |
* Temporal. |
| 634 |
* propertyName is the name of the option, and value is the value that |
| 635 |
* assertFunc should expect it to have. |
| 636 |
* checkFunc(value) is a function which takes the value of the option to test, |
| 637 |
* and calls the method under test with it, returning the result. |
| 638 |
* assertFunc(result, description) should assert that result is the expected |
| 639 |
* result with the option value being an object with a toString() method |
| 640 |
* which returns the given value. |
| 641 |
*/ |
| 642 |
checkStringOptionWrongType(propertyName, value, checkFunc, assertFunc) { |
| 643 |
// null converts to the string "null", which is an invalid string value |
| 644 |
assert.throws(RangeError, () => checkFunc(null), "null"); |
| 645 |
// Booleans convert to the strings "true" or "false", which are invalid |
| 646 |
assert.throws(RangeError, () => checkFunc(true), "true"); |
| 647 |
assert.throws(RangeError, () => checkFunc(false), "false"); |
| 648 |
// Symbols cannot convert to strings |
| 649 |
assert.throws(TypeError, () => checkFunc(Symbol()), "symbol"); |
| 650 |
// Numbers convert to strings which are invalid |
| 651 |
assert.throws(RangeError, () => checkFunc(2), "number"); |
| 652 |
// BigInts convert to strings which are invalid |
| 653 |
assert.throws(RangeError, () => checkFunc(2n), "bigint"); |
| 654 |
|
| 655 |
// Objects prefer their toString() methods when converting to a string |
| 656 |
assert.throws(RangeError, () => checkFunc({}), "plain object"); |
| 657 |
|
| 658 |
const expected = [ |
| 659 |
`get ${propertyName}.toString`, |
| 660 |
`call ${propertyName}.toString`, |
| 661 |
]; |
| 662 |
const actual = []; |
| 663 |
const observer = TemporalHelpers.toPrimitiveObserver(actual, value, propertyName); |
| 664 |
const result = checkFunc(observer); |
| 665 |
assertFunc(result, "object with toString"); |
| 666 |
assert.compareArray(actual, expected, "order of operations"); |
| 667 |
}, |
| 668 |
|
| 669 |
/* |
| 670 |
* checkSubclassingIgnored(construct, constructArgs, method, methodArgs, |
| 671 |
* resultAssertions): |
| 672 |
* |
| 673 |
* Methods of Temporal classes that return a new instance of the same class, |
| 674 |
* must not take the constructor of a subclass into account, nor the @@species |
| 675 |
* property. This helper runs tests to ensure this. |
| 676 |
* |
| 677 |
* construct(...constructArgs) must yield a valid instance of the Temporal |
| 678 |
* class. instance[method](...methodArgs) is the method call under test, which |
| 679 |
* must also yield a valid instance of the same Temporal class, not a |
| 680 |
* subclass. See below for the individual tests that this runs. |
| 681 |
* resultAssertions() is a function that performs additional assertions on the |
| 682 |
* instance returned by the method under test. |
| 683 |
*/ |
| 684 |
checkSubclassingIgnored(...args) { |
| 685 |
this.checkSubclassConstructorNotObject(...args); |
| 686 |
this.checkSubclassConstructorUndefined(...args); |
| 687 |
this.checkSubclassConstructorThrows(...args); |
| 688 |
this.checkSubclassConstructorNotCalled(...args); |
| 689 |
this.checkSubclassSpeciesInvalidResult(...args); |
| 690 |
this.checkSubclassSpeciesNotAConstructor(...args); |
| 691 |
this.checkSubclassSpeciesNull(...args); |
| 692 |
this.checkSubclassSpeciesUndefined(...args); |
| 693 |
this.checkSubclassSpeciesThrows(...args); |
| 694 |
}, |
| 695 |
|
| 696 |
/* |
| 697 |
* Checks that replacing the 'constructor' property of the instance with |
| 698 |
* various primitive values does not affect the returned new instance. |
| 699 |
*/ |
| 700 |
checkSubclassConstructorNotObject(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 701 |
function check(value, description) { |
| 702 |
const instance = new construct(...constructArgs); |
| 703 |
instance.constructor = value; |
| 704 |
const result = instance[method](...methodArgs); |
| 705 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype, description); |
| 706 |
resultAssertions(result); |
| 707 |
} |
| 708 |
|
| 709 |
check(null, "null"); |
| 710 |
check(true, "true"); |
| 711 |
check("test", "string"); |
| 712 |
check(Symbol(), "Symbol"); |
| 713 |
check(7, "number"); |
| 714 |
check(7n, "bigint"); |
| 715 |
}, |
| 716 |
|
| 717 |
/* |
| 718 |
* Checks that replacing the 'constructor' property of the subclass with |
| 719 |
* undefined does not affect the returned new instance. |
| 720 |
*/ |
| 721 |
checkSubclassConstructorUndefined(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 722 |
let called = 0; |
| 723 |
|
| 724 |
class MySubclass extends construct { |
| 725 |
constructor() { |
| 726 |
++called; |
| 727 |
super(...constructArgs); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
const instance = new MySubclass(); |
| 732 |
assert.sameValue(called, 1); |
| 733 |
|
| 734 |
MySubclass.prototype.constructor = undefined; |
| 735 |
|
| 736 |
const result = instance[method](...methodArgs); |
| 737 |
assert.sameValue(called, 1); |
| 738 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 739 |
resultAssertions(result); |
| 740 |
}, |
| 741 |
|
| 742 |
/* |
| 743 |
* Checks that making the 'constructor' property of the instance throw when |
| 744 |
* called does not affect the returned new instance. |
| 745 |
*/ |
| 746 |
checkSubclassConstructorThrows(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 747 |
function CustomError() {} |
| 748 |
const instance = new construct(...constructArgs); |
| 749 |
Object.defineProperty(instance, "constructor", { |
| 750 |
get() { |
| 751 |
throw new CustomError(); |
| 752 |
} |
| 753 |
}); |
| 754 |
const result = instance[method](...methodArgs); |
| 755 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 756 |
resultAssertions(result); |
| 757 |
}, |
| 758 |
|
| 759 |
/* |
| 760 |
* Checks that when subclassing, the subclass constructor is not called by |
| 761 |
* the method under test. |
| 762 |
*/ |
| 763 |
checkSubclassConstructorNotCalled(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 764 |
let called = 0; |
| 765 |
|
| 766 |
class MySubclass extends construct { |
| 767 |
constructor() { |
| 768 |
++called; |
| 769 |
super(...constructArgs); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
const instance = new MySubclass(); |
| 774 |
assert.sameValue(called, 1); |
| 775 |
|
| 776 |
const result = instance[method](...methodArgs); |
| 777 |
assert.sameValue(called, 1); |
| 778 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 779 |
resultAssertions(result); |
| 780 |
}, |
| 781 |
|
| 782 |
/* |
| 783 |
* Check that the constructor's @@species property is ignored when it's a |
| 784 |
* constructor that returns a non-object value. |
| 785 |
*/ |
| 786 |
checkSubclassSpeciesInvalidResult(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 787 |
function check(value, description) { |
| 788 |
const instance = new construct(...constructArgs); |
| 789 |
instance.constructor = { |
| 790 |
[Symbol.species]: function() { |
| 791 |
return value; |
| 792 |
}, |
| 793 |
}; |
| 794 |
const result = instance[method](...methodArgs); |
| 795 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype, description); |
| 796 |
resultAssertions(result); |
| 797 |
} |
| 798 |
|
| 799 |
check(undefined, "undefined"); |
| 800 |
check(null, "null"); |
| 801 |
check(true, "true"); |
| 802 |
check("test", "string"); |
| 803 |
check(Symbol(), "Symbol"); |
| 804 |
check(7, "number"); |
| 805 |
check(7n, "bigint"); |
| 806 |
check({}, "plain object"); |
| 807 |
}, |
| 808 |
|
| 809 |
/* |
| 810 |
* Check that the constructor's @@species property is ignored when it's not a |
| 811 |
* constructor. |
| 812 |
*/ |
| 813 |
checkSubclassSpeciesNotAConstructor(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 814 |
function check(value, description) { |
| 815 |
const instance = new construct(...constructArgs); |
| 816 |
instance.constructor = { |
| 817 |
[Symbol.species]: value, |
| 818 |
}; |
| 819 |
const result = instance[method](...methodArgs); |
| 820 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype, description); |
| 821 |
resultAssertions(result); |
| 822 |
} |
| 823 |
|
| 824 |
check(true, "true"); |
| 825 |
check("test", "string"); |
| 826 |
check(Symbol(), "Symbol"); |
| 827 |
check(7, "number"); |
| 828 |
check(7n, "bigint"); |
| 829 |
check({}, "plain object"); |
| 830 |
}, |
| 831 |
|
| 832 |
/* |
| 833 |
* Check that the constructor's @@species property is ignored when it's null. |
| 834 |
*/ |
| 835 |
checkSubclassSpeciesNull(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 836 |
let called = 0; |
| 837 |
|
| 838 |
class MySubclass extends construct { |
| 839 |
constructor() { |
| 840 |
++called; |
| 841 |
super(...constructArgs); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
const instance = new MySubclass(); |
| 846 |
assert.sameValue(called, 1); |
| 847 |
|
| 848 |
MySubclass.prototype.constructor = { |
| 849 |
[Symbol.species]: null, |
| 850 |
}; |
| 851 |
|
| 852 |
const result = instance[method](...methodArgs); |
| 853 |
assert.sameValue(called, 1); |
| 854 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 855 |
resultAssertions(result); |
| 856 |
}, |
| 857 |
|
| 858 |
/* |
| 859 |
* Check that the constructor's @@species property is ignored when it's |
| 860 |
* undefined. |
| 861 |
*/ |
| 862 |
checkSubclassSpeciesUndefined(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 863 |
let called = 0; |
| 864 |
|
| 865 |
class MySubclass extends construct { |
| 866 |
constructor() { |
| 867 |
++called; |
| 868 |
super(...constructArgs); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
const instance = new MySubclass(); |
| 873 |
assert.sameValue(called, 1); |
| 874 |
|
| 875 |
MySubclass.prototype.constructor = { |
| 876 |
[Symbol.species]: undefined, |
| 877 |
}; |
| 878 |
|
| 879 |
const result = instance[method](...methodArgs); |
| 880 |
assert.sameValue(called, 1); |
| 881 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 882 |
resultAssertions(result); |
| 883 |
}, |
| 884 |
|
| 885 |
/* |
| 886 |
* Check that the constructor's @@species property is ignored when it throws, |
| 887 |
* i.e. it is not called at all. |
| 888 |
*/ |
| 889 |
checkSubclassSpeciesThrows(construct, constructArgs, method, methodArgs, resultAssertions) { |
| 890 |
function CustomError() {} |
| 891 |
|
| 892 |
const instance = new construct(...constructArgs); |
| 893 |
instance.constructor = { |
| 894 |
get [Symbol.species]() { |
| 895 |
throw new CustomError(); |
| 896 |
}, |
| 897 |
}; |
| 898 |
|
| 899 |
const result = instance[method](...methodArgs); |
| 900 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 901 |
}, |
| 902 |
|
| 903 |
/* |
| 904 |
* checkSubclassingIgnoredStatic(construct, method, methodArgs, resultAssertions): |
| 905 |
* |
| 906 |
* Static methods of Temporal classes that return a new instance of the class, |
| 907 |
* must not use the this-value as a constructor. This helper runs tests to |
| 908 |
* ensure this. |
| 909 |
* |
| 910 |
* construct[method](...methodArgs) is the static method call under test, and |
| 911 |
* must yield a valid instance of the Temporal class, not a subclass. See |
| 912 |
* below for the individual tests that this runs. |
| 913 |
* resultAssertions() is a function that performs additional assertions on the |
| 914 |
* instance returned by the method under test. |
| 915 |
*/ |
| 916 |
checkSubclassingIgnoredStatic(...args) { |
| 917 |
this.checkStaticInvalidReceiver(...args); |
| 918 |
this.checkStaticReceiverNotCalled(...args); |
| 919 |
this.checkThisValueNotCalled(...args); |
| 920 |
}, |
| 921 |
|
| 922 |
/* |
| 923 |
* Check that calling the static method with a receiver that's not callable, |
| 924 |
* still calls the intrinsic constructor. |
| 925 |
*/ |
| 926 |
checkStaticInvalidReceiver(construct, method, methodArgs, resultAssertions) { |
| 927 |
function check(value, description) { |
| 928 |
const result = construct[method].apply(value, methodArgs); |
| 929 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 930 |
resultAssertions(result); |
| 931 |
} |
| 932 |
|
| 933 |
check(undefined, "undefined"); |
| 934 |
check(null, "null"); |
| 935 |
check(true, "true"); |
| 936 |
check("test", "string"); |
| 937 |
check(Symbol(), "symbol"); |
| 938 |
check(7, "number"); |
| 939 |
check(7n, "bigint"); |
| 940 |
check({}, "Non-callable object"); |
| 941 |
}, |
| 942 |
|
| 943 |
/* |
| 944 |
* Check that calling the static method with a receiver that returns a value |
| 945 |
* that's not callable, still calls the intrinsic constructor. |
| 946 |
*/ |
| 947 |
checkStaticReceiverNotCalled(construct, method, methodArgs, resultAssertions) { |
| 948 |
function check(value, description) { |
| 949 |
const receiver = function () { |
| 950 |
return value; |
| 951 |
}; |
| 952 |
const result = construct[method].apply(receiver, methodArgs); |
| 953 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 954 |
resultAssertions(result); |
| 955 |
} |
| 956 |
|
| 957 |
check(undefined, "undefined"); |
| 958 |
check(null, "null"); |
| 959 |
check(true, "true"); |
| 960 |
check("test", "string"); |
| 961 |
check(Symbol(), "symbol"); |
| 962 |
check(7, "number"); |
| 963 |
check(7n, "bigint"); |
| 964 |
check({}, "Non-callable object"); |
| 965 |
}, |
| 966 |
|
| 967 |
/* |
| 968 |
* Check that the receiver isn't called. |
| 969 |
*/ |
| 970 |
checkThisValueNotCalled(construct, method, methodArgs, resultAssertions) { |
| 971 |
let called = false; |
| 972 |
|
| 973 |
class MySubclass extends construct { |
| 974 |
constructor(...args) { |
| 975 |
called = true; |
| 976 |
super(...args); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
const result = MySubclass[method](...methodArgs); |
| 981 |
assert.sameValue(called, false); |
| 982 |
assert.sameValue(Object.getPrototypeOf(result), construct.prototype); |
| 983 |
resultAssertions(result); |
| 984 |
}, |
| 985 |
|
| 986 |
/* |
| 987 |
* Check that any calendar-carrying Temporal object has its [[Calendar]] |
| 988 |
* internal slot read by ToTemporalCalendar, and does not fetch the calendar |
| 989 |
* by calling getters. |
| 990 |
*/ |
| 991 |
checkToTemporalCalendarFastPath(func) { |
| 992 |
const plainDate = new Temporal.PlainDate(2000, 5, 2, "iso8601"); |
| 993 |
const plainDateTime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321, "iso8601"); |
| 994 |
const plainMonthDay = new Temporal.PlainMonthDay(5, 2, "iso8601"); |
| 995 |
const plainYearMonth = new Temporal.PlainYearMonth(2000, 5, "iso8601"); |
| 996 |
const zonedDateTime = new Temporal.ZonedDateTime(1_000_000_000_000_000_000n, "UTC", "iso8601"); |
| 997 |
|
| 998 |
[plainDate, plainDateTime, plainMonthDay, plainYearMonth, zonedDateTime].forEach((temporalObject) => { |
| 999 |
const actual = []; |
| 1000 |
const expected = []; |
| 1001 |
|
| 1002 |
Object.defineProperty(temporalObject, "calendar", { |
| 1003 |
get() { |
| 1004 |
actual.push("get calendar"); |
| 1005 |
return calendar; |
| 1006 |
}, |
| 1007 |
}); |
| 1008 |
|
| 1009 |
func(temporalObject); |
| 1010 |
assert.compareArray(actual, expected, "calendar getter not called"); |
| 1011 |
}); |
| 1012 |
}, |
| 1013 |
|
| 1014 |
checkToTemporalInstantFastPath(func) { |
| 1015 |
const actual = []; |
| 1016 |
const expected = []; |
| 1017 |
|
| 1018 |
const datetime = new Temporal.ZonedDateTime(1_000_000_000_987_654_321n, "UTC"); |
| 1019 |
Object.defineProperty(datetime, 'toString', { |
| 1020 |
get() { |
| 1021 |
actual.push("get toString"); |
| 1022 |
return function (options) { |
| 1023 |
actual.push("call toString"); |
| 1024 |
return Temporal.ZonedDateTime.prototype.toString.call(this, options); |
| 1025 |
}; |
| 1026 |
}, |
| 1027 |
}); |
| 1028 |
|
| 1029 |
func(datetime); |
| 1030 |
assert.compareArray(actual, expected, "toString not called"); |
| 1031 |
}, |
| 1032 |
|
| 1033 |
checkToTemporalPlainDateTimeFastPath(func) { |
| 1034 |
const actual = []; |
| 1035 |
const expected = []; |
| 1036 |
|
| 1037 |
const date = new Temporal.PlainDate(2000, 5, 2, "iso8601"); |
| 1038 |
const prototypeDescrs = Object.getOwnPropertyDescriptors(Temporal.PlainDate.prototype); |
| 1039 |
["year", "month", "monthCode", "day"].forEach((property) => { |
| 1040 |
Object.defineProperty(date, property, { |
| 1041 |
get() { |
| 1042 |
actual.push(`get ${formatPropertyName(property)}`); |
| 1043 |
const value = prototypeDescrs[property].get.call(this); |
| 1044 |
return TemporalHelpers.toPrimitiveObserver(actual, value, property); |
| 1045 |
}, |
| 1046 |
}); |
| 1047 |
}); |
| 1048 |
["hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => { |
| 1049 |
Object.defineProperty(date, property, { |
| 1050 |
get() { |
| 1051 |
actual.push(`get ${formatPropertyName(property)}`); |
| 1052 |
return undefined; |
| 1053 |
}, |
| 1054 |
}); |
| 1055 |
}); |
| 1056 |
Object.defineProperty(date, "calendar", { |
| 1057 |
get() { |
| 1058 |
actual.push("get calendar"); |
| 1059 |
return "iso8601"; |
| 1060 |
}, |
| 1061 |
}); |
| 1062 |
|
| 1063 |
func(date); |
| 1064 |
assert.compareArray(actual, expected, "property getters not called"); |
| 1065 |
}, |
| 1066 |
|
| 1067 |
/* |
| 1068 |
* observeProperty(calls, object, propertyName, value): |
| 1069 |
* |
| 1070 |
* Defines an own property @object.@propertyName with value @value, that |
| 1071 |
* will log any calls to its accessors to the array @calls. |
| 1072 |
*/ |
| 1073 |
observeProperty(calls, object, propertyName, value, objectName = "") { |
| 1074 |
Object.defineProperty(object, propertyName, { |
| 1075 |
get() { |
| 1076 |
calls.push(`get ${formatPropertyName(propertyName, objectName)}`); |
| 1077 |
return value; |
| 1078 |
}, |
| 1079 |
set(v) { |
| 1080 |
calls.push(`set ${formatPropertyName(propertyName, objectName)}`); |
| 1081 |
} |
| 1082 |
}); |
| 1083 |
}, |
| 1084 |
|
| 1085 |
/* |
| 1086 |
* observeMethod(calls, object, propertyName, value): |
| 1087 |
* |
| 1088 |
* Defines an own property @object.@propertyName with value @value, that |
| 1089 |
* will log any calls of @value to the array @calls. |
| 1090 |
*/ |
| 1091 |
observeMethod(calls, object, propertyName, objectName = "") { |
| 1092 |
const method = object[propertyName]; |
| 1093 |
object[propertyName] = function () { |
| 1094 |
calls.push(`call ${formatPropertyName(propertyName, objectName)}`); |
| 1095 |
return method.apply(object, arguments); |
| 1096 |
}; |
| 1097 |
}, |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* Used for substituteMethod to indicate default behavior instead of a |
| 1101 |
* substituted value |
| 1102 |
*/ |
| 1103 |
SUBSTITUTE_SKIP: SKIP_SYMBOL, |
| 1104 |
|
| 1105 |
/* |
| 1106 |
* substituteMethod(object, propertyName, values): |
| 1107 |
* |
| 1108 |
* Defines an own property @object.@propertyName that will, for each |
| 1109 |
* subsequent call to the method previously defined as |
| 1110 |
* @object.@propertyName: |
| 1111 |
* - Call the method, if no more values remain |
| 1112 |
* - Call the method, if the value in @values for the corresponding call |
| 1113 |
* is SUBSTITUTE_SKIP |
| 1114 |
* - Otherwise, return the corresponding value in @value |
| 1115 |
*/ |
| 1116 |
substituteMethod(object, propertyName, values) { |
| 1117 |
let calls = 0; |
| 1118 |
const method = object[propertyName]; |
| 1119 |
object[propertyName] = function () { |
| 1120 |
if (calls >= values.length) { |
| 1121 |
return method.apply(object, arguments); |
| 1122 |
} else if (values[calls] === SKIP_SYMBOL) { |
| 1123 |
calls++; |
| 1124 |
return method.apply(object, arguments); |
| 1125 |
} else { |
| 1126 |
return values[calls++]; |
| 1127 |
} |
| 1128 |
}; |
| 1129 |
}, |
| 1130 |
|
| 1131 |
/* |
| 1132 |
* propertyBagObserver(): |
| 1133 |
* Returns an object that behaves like the given propertyBag but tracks Get |
| 1134 |
* and Has operations on any of its properties, by appending messages to an |
| 1135 |
* array. If the value of a property in propertyBag is a primitive, the value |
| 1136 |
* of the returned object's property will additionally be a |
| 1137 |
* TemporalHelpers.toPrimitiveObserver that will track calls to its toString |
| 1138 |
* and valueOf methods in the same array. This is for the purpose of testing |
| 1139 |
* order of operations that are observable from user code. objectName is used |
| 1140 |
* in the log. |
| 1141 |
* If skipToPrimitive is given, it must be an array of property keys. Those |
| 1142 |
* properties will not have a TemporalHelpers.toPrimitiveObserver returned, |
| 1143 |
* and instead just be returned directly. |
| 1144 |
*/ |
| 1145 |
propertyBagObserver(calls, propertyBag, objectName, skipToPrimitive) { |
| 1146 |
return new Proxy(propertyBag, { |
| 1147 |
ownKeys(target) { |
| 1148 |
calls.push(`ownKeys ${objectName}`); |
| 1149 |
return Reflect.ownKeys(target); |
| 1150 |
}, |
| 1151 |
getOwnPropertyDescriptor(target, key) { |
| 1152 |
calls.push(`getOwnPropertyDescriptor ${formatPropertyName(key, objectName)}`); |
| 1153 |
return Reflect.getOwnPropertyDescriptor(target, key); |
| 1154 |
}, |
| 1155 |
get(target, key, receiver) { |
| 1156 |
calls.push(`get ${formatPropertyName(key, objectName)}`); |
| 1157 |
const result = Reflect.get(target, key, receiver); |
| 1158 |
if (result === undefined) { |
| 1159 |
return undefined; |
| 1160 |
} |
| 1161 |
if ((result !== null && typeof result === "object") || typeof result === "function") { |
| 1162 |
return result; |
| 1163 |
} |
| 1164 |
if (skipToPrimitive && skipToPrimitive.indexOf(key) >= 0) { |
| 1165 |
return result; |
| 1166 |
} |
| 1167 |
return TemporalHelpers.toPrimitiveObserver(calls, result, `${formatPropertyName(key, objectName)}`); |
| 1168 |
}, |
| 1169 |
has(target, key) { |
| 1170 |
calls.push(`has ${formatPropertyName(key, objectName)}`); |
| 1171 |
return Reflect.has(target, key); |
| 1172 |
}, |
| 1173 |
}); |
| 1174 |
}, |
| 1175 |
|
| 1176 |
/* |
| 1177 |
* Returns an object that will append logs of any Gets or Calls of its valueOf |
| 1178 |
* or toString properties to the array calls. Both valueOf and toString will |
| 1179 |
* return the actual primitiveValue. propertyName is used in the log. |
| 1180 |
*/ |
| 1181 |
toPrimitiveObserver(calls, primitiveValue, propertyName) { |
| 1182 |
return { |
| 1183 |
get valueOf() { |
| 1184 |
calls.push(`get ${propertyName}.valueOf`); |
| 1185 |
return function () { |
| 1186 |
calls.push(`call ${propertyName}.valueOf`); |
| 1187 |
return primitiveValue; |
| 1188 |
}; |
| 1189 |
}, |
| 1190 |
get toString() { |
| 1191 |
calls.push(`get ${propertyName}.toString`); |
| 1192 |
return function () { |
| 1193 |
calls.push(`call ${propertyName}.toString`); |
| 1194 |
if (primitiveValue === undefined) return undefined; |
| 1195 |
return primitiveValue.toString(); |
| 1196 |
}; |
| 1197 |
}, |
| 1198 |
}; |
| 1199 |
}, |
| 1200 |
|
| 1201 |
/* |
| 1202 |
* An object containing further methods that return arrays of ISO strings, for |
| 1203 |
* testing parsers. |
| 1204 |
*/ |
| 1205 |
ISO: { |
| 1206 |
/* |
| 1207 |
* PlainMonthDay strings that are not valid. |
| 1208 |
*/ |
| 1209 |
plainMonthDayStringsInvalid() { |
| 1210 |
return [ |
| 1211 |
"11-18junk", |
| 1212 |
"11-18[u-ca=gregory]", |
| 1213 |
"11-18[u-ca=hebrew]", |
| 1214 |
"11-18[U-CA=iso8601]", |
| 1215 |
"11-18[u-CA=iso8601]", |
| 1216 |
"11-18[FOO=bar]", |
| 1217 |
"-999999-01-01[u-ca=gregory]", |
| 1218 |
"-999999-01-01[u-ca=chinese]", |
| 1219 |
"+999999-01-01[u-ca=gregory]", |
| 1220 |
"+999999-01-01[u-ca=chinese]", |
| 1221 |
]; |
| 1222 |
}, |
| 1223 |
|
| 1224 |
/* |
| 1225 |
* PlainMonthDay strings that are valid and that should produce October 1st. |
| 1226 |
*/ |
| 1227 |
plainMonthDayStringsValid() { |
| 1228 |
return [ |
| 1229 |
"10-01", |
| 1230 |
"1001", |
| 1231 |
"1965-10-01", |
| 1232 |
"1976-10-01T152330.1+00:00", |
| 1233 |
"19761001T15:23:30.1+00:00", |
| 1234 |
"1976-10-01T15:23:30.1+0000", |
| 1235 |
"1976-10-01T152330.1+0000", |
| 1236 |
"19761001T15:23:30.1+0000", |
| 1237 |
"19761001T152330.1+00:00", |
| 1238 |
"19761001T152330.1+0000", |
| 1239 |
"+001976-10-01T152330.1+00:00", |
| 1240 |
"+0019761001T15:23:30.1+00:00", |
| 1241 |
"+001976-10-01T15:23:30.1+0000", |
| 1242 |
"+001976-10-01T152330.1+0000", |
| 1243 |
"+0019761001T15:23:30.1+0000", |
| 1244 |
"+0019761001T152330.1+00:00", |
| 1245 |
"+0019761001T152330.1+0000", |
| 1246 |
"1976-10-01T15:23:00", |
| 1247 |
"1976-10-01T15:23", |
| 1248 |
"1976-10-01T15", |
| 1249 |
"1976-10-01", |
| 1250 |
"--10-01", |
| 1251 |
"--1001", |
| 1252 |
"-999999-10-01", |
| 1253 |
"-999999-10-01[u-ca=iso8601]", |
| 1254 |
"+999999-10-01", |
| 1255 |
"+999999-10-01[u-ca=iso8601]", |
| 1256 |
]; |
| 1257 |
}, |
| 1258 |
|
| 1259 |
/* |
| 1260 |
* PlainTime strings that may be mistaken for PlainMonthDay or |
| 1261 |
* PlainYearMonth strings, and so require a time designator. |
| 1262 |
*/ |
| 1263 |
plainTimeStringsAmbiguous() { |
| 1264 |
const ambiguousStrings = [ |
| 1265 |
"2021-12", // ambiguity between YYYY-MM and HHMM-UU |
| 1266 |
"2021-12[-12:00]", // ditto, TZ does not disambiguate |
| 1267 |
"1214", // ambiguity between MMDD and HHMM |
| 1268 |
"0229", // ditto, including MMDD that doesn't occur every year |
| 1269 |
"1130", // ditto, including DD that doesn't occur in every month |
| 1270 |
"12-14", // ambiguity between MM-DD and HH-UU |
| 1271 |
"12-14[-14:00]", // ditto, TZ does not disambiguate |
| 1272 |
"202112", // ambiguity between YYYYMM and HHMMSS |
| 1273 |
"202112[UTC]", // ditto, TZ does not disambiguate |
| 1274 |
]; |
| 1275 |
// Adding a calendar annotation to one of these strings must not cause |
| 1276 |
// disambiguation in favour of time. |
| 1277 |
const stringsWithCalendar = ambiguousStrings.map((s) => s + '[u-ca=iso8601]'); |
| 1278 |
return ambiguousStrings.concat(stringsWithCalendar); |
| 1279 |
}, |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* PlainTime strings that are of similar form to PlainMonthDay and |
| 1283 |
* PlainYearMonth strings, but are not ambiguous due to components that |
| 1284 |
* aren't valid as months or days. |
| 1285 |
*/ |
| 1286 |
plainTimeStringsUnambiguous() { |
| 1287 |
return [ |
| 1288 |
"2021-13", // 13 is not a month |
| 1289 |
"202113", // ditto |
| 1290 |
"2021-13[-13:00]", // ditto |
| 1291 |
"202113[-13:00]", // ditto |
| 1292 |
"0000-00", // 0 is not a month |
| 1293 |
"000000", // ditto |
| 1294 |
"0000-00[UTC]", // ditto |
| 1295 |
"000000[UTC]", // ditto |
| 1296 |
"1314", // 13 is not a month |
| 1297 |
"13-14", // ditto |
| 1298 |
"1232", // 32 is not a day |
| 1299 |
"0230", // 30 is not a day in February |
| 1300 |
"0631", // 31 is not a day in June |
| 1301 |
"0000", // 0 is neither a month nor a day |
| 1302 |
"00-00", // ditto |
| 1303 |
]; |
| 1304 |
}, |
| 1305 |
|
| 1306 |
/* |
| 1307 |
* PlainYearMonth-like strings that are not valid. |
| 1308 |
*/ |
| 1309 |
plainYearMonthStringsInvalid() { |
| 1310 |
return [ |
| 1311 |
"2020-13", |
| 1312 |
"1976-11[u-ca=gregory]", |
| 1313 |
"1976-11[u-ca=hebrew]", |
| 1314 |
"1976-11[U-CA=iso8601]", |
| 1315 |
"1976-11[u-CA=iso8601]", |
| 1316 |
"1976-11[FOO=bar]", |
| 1317 |
"+999999-01", |
| 1318 |
"-999999-01", |
| 1319 |
]; |
| 1320 |
}, |
| 1321 |
|
| 1322 |
/* |
| 1323 |
* PlainYearMonth-like strings that are valid and should produce November |
| 1324 |
* 1976 in the ISO 8601 calendar. |
| 1325 |
*/ |
| 1326 |
plainYearMonthStringsValid() { |
| 1327 |
return [ |
| 1328 |
"1976-11", |
| 1329 |
"1976-11-10", |
| 1330 |
"1976-11-01T09:00:00+00:00", |
| 1331 |
"1976-11-01T00:00:00+05:00", |
| 1332 |
"197611", |
| 1333 |
"+00197611", |
| 1334 |
"1976-11-18T15:23:30.1-02:00", |
| 1335 |
"1976-11-18T152330.1+00:00", |
| 1336 |
"19761118T15:23:30.1+00:00", |
| 1337 |
"1976-11-18T15:23:30.1+0000", |
| 1338 |
"1976-11-18T152330.1+0000", |
| 1339 |
"19761118T15:23:30.1+0000", |
| 1340 |
"19761118T152330.1+00:00", |
| 1341 |
"19761118T152330.1+0000", |
| 1342 |
"+001976-11-18T152330.1+00:00", |
| 1343 |
"+0019761118T15:23:30.1+00:00", |
| 1344 |
"+001976-11-18T15:23:30.1+0000", |
| 1345 |
"+001976-11-18T152330.1+0000", |
| 1346 |
"+0019761118T15:23:30.1+0000", |
| 1347 |
"+0019761118T152330.1+00:00", |
| 1348 |
"+0019761118T152330.1+0000", |
| 1349 |
"1976-11-18T15:23", |
| 1350 |
"1976-11-18T15", |
| 1351 |
"1976-11-18", |
| 1352 |
]; |
| 1353 |
}, |
| 1354 |
|
| 1355 |
/* |
| 1356 |
* PlainYearMonth-like strings that are valid and should produce November of |
| 1357 |
* the ISO year -9999. |
| 1358 |
*/ |
| 1359 |
plainYearMonthStringsValidNegativeYear() { |
| 1360 |
return [ |
| 1361 |
"-009999-11", |
| 1362 |
]; |
| 1363 |
}, |
| 1364 |
} |
| 1365 |
}; |