Skip to content

Commit

Permalink
SQUASH - tests for previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent d4e73d1 commit 35fdc35
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 0 deletions.
24 changes: 24 additions & 0 deletions polyfill/test/Calendar/prototype/dateAdd/argument-plaindatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.dateadd
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.calendar.prototype.dateadd step 4:
4. Set _date_ to ? ToTemporalDate(_date_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const calendar = new Temporal.Calendar("iso8601");
const duration = new Temporal.Duration(0, 1);
const result = calendar.dateAdd(datetime, duration);
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 6, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, undefined, "instance of PlainDate returned, not PlainDateTime");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.dateuntil
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.calendar.prototype.dateuntil steps 4–5:
4. Set _one_ to ? ToTemporalDate(_one_).
5. Set _two_ to ? ToTemporalDate(_two_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

const date = new Temporal.PlainDate(2000, 5, 2);

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const calendar = new Temporal.Calendar("iso8601");
const result = calendar.dateUntil(datetime, date);
assert.sameValue(result.total({ unit: "nanoseconds" }), 0, "time part dropped");
});

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const calendar = new Temporal.Calendar("iso8601");
const result = calendar.dateUntil(date, datetime);
assert.sameValue(result.total({ unit: "nanoseconds" }), 0, "time part dropped");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindate.compare
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindate.compare steps 1–2:
1. Set _one_ to ? ToTemporalDate(_one_).
2. Set _two_ to ? ToTemporalDate(_two_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

const date = new Temporal.PlainDate(2000, 5, 2);

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const result = Temporal.PlainDate.compare(datetime, date);
assert.sameValue(result, 0, "comparison result");
});

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const result = Temporal.PlainDate.compare(date, datetime);
assert.sameValue(result, 0, "comparison result");
});
22 changes: 22 additions & 0 deletions polyfill/test/PlainDate/constructor/from/argument-plaindatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2021 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: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindate.from step 3:
3. Return ? ToTemporalDate(_item_, _options_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime, calendar) => {
const result = Temporal.PlainDate.from(datetime);
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.calendar, calendar, "calendar result");
});
19 changes: 19 additions & 0 deletions polyfill/test/PlainDate/prototype/equals/argument-plaindatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindate.prototype.equals
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindate.prototype.equals step 3:
3. Set _other_ to ? ToTemporalDate(_other_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const date = new Temporal.PlainDate(2000, 5, 2);
assert(date.equals(datetime));
});
20 changes: 20 additions & 0 deletions polyfill/test/PlainDate/prototype/since/argument-plaindatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindate.since
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindate.prototype.since step 3:
3. Set _other_ to ? ToTemporalDate(_other_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const date = new Temporal.PlainDate(2000, 5, 2);
const result = date.since(datetime);
assert.sameValue(result.total({ unit: "nanoseconds" }), 0, "time part dropped");
});
20 changes: 20 additions & 0 deletions polyfill/test/PlainDate/prototype/until/argument-plaindatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindate.until
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindate.prototype.until step 3:
3. Set _other_ to ? ToTemporalDate(_other_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const date = new Temporal.PlainDate(2000, 5, 2);
const result = date.until(datetime);
assert.sameValue(result.total({ unit: "nanoseconds" }), 0, "time part dropped");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaindatetime.withplaindate
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaindatetime.prototype.withplaindate step 3:
3. Let _plainDate_ be ? ToTemporalDate(_plainDateLike_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const receiver = new Temporal.PlainDateTime(2001, 9, 9, 6, 54, 32, 123, 456, 789);
const result = receiver.withPlainDate(datetime);
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, 6, "hour result");
assert.sameValue(result.minute, 54, "minute result");
assert.sameValue(result.second, 32, "second result");
assert.sameValue(result.millisecond, 123, "millisecond result");
assert.sameValue(result.microsecond, 456, "microsecond result");
assert.sameValue(result.nanosecond, 789, "nanosecond result");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaintime.toplaindatetime
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaintime.prototype.toplaindatetime step 3:
3. Set _temporalDate_ to ? ToTemporalDate(_temporalDate_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const time = new Temporal.PlainTime(6, 54, 32, 123, 456, 789);
const result = time.toPlainDateTime(datetime);
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, 6, "hour result");
assert.sameValue(result.minute, 54, "minute result");
assert.sameValue(result.second, 32, "second result");
assert.sameValue(result.millisecond, 123, "millisecond result");
assert.sameValue(result.microsecond, 456, "microsecond result");
assert.sameValue(result.nanosecond, 789, "nanosecond result");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaintime.tozoneddatetime
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.plaintime.prototype.tozoneddatetime step 5:
5. Let _temporalDate_ be ? ToTemporalDate(_temporalDateLike_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const time = new Temporal.PlainTime(6, 54, 32, 123, 456, 789);
const result = time.toZonedDateTime({ plainDate: datetime, timeZone: "UTC" });
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, 6, "hour result");
assert.sameValue(result.minute, 54, "minute result");
assert.sameValue(result.second, 32, "second result");
assert.sameValue(result.millisecond, 123, "millisecond result");
assert.sameValue(result.microsecond, 456, "microsecond result");
assert.sameValue(result.nanosecond, 789, "nanosecond result");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.zoneddatetime.withplaindate
description: Fast path for converting Temporal.PlainDateTime to Temporal.PlainDate by reading internal slots
info: |
sec-temporal.zoneddatetime.prototype.withplaindate step 3:
3. Let _plainDate_ be ? ToTemporalDate(_plainDateLike_).
sec-temporal-totemporaldate step 2.b:
b. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
i. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
includes: [compareArray.js, temporalHelpers.js]
---*/

TemporalHelpers.checkPlainDateTimeConversionFastPath((datetime) => {
const receiver = new Temporal.ZonedDateTime(1_000_000_000_123_456_789n, "UTC");
const result = receiver.withPlainDate(datetime);
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, 1, "hour result");
assert.sameValue(result.minute, 46, "minute result");
assert.sameValue(result.second, 40, "second result");
assert.sameValue(result.millisecond, 123, "millisecond result");
assert.sameValue(result.microsecond, 456, "microsecond result");
assert.sameValue(result.nanosecond, 789, "nanosecond result");
});
49 changes: 49 additions & 0 deletions polyfill/test/helpers/temporalHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,55 @@ features: [Symbol.species]
---*/

var TemporalHelpers = {
/*
* checkPlainDateTimeConversionFastPath(func):
*
* ToTemporalDate and ToTemporalTime should both, if given a
* Temporal.PlainDateTime instance, convert to the desired type by reading the
* PlainDateTime's internal slots, rather than calling any getters.
*
* func(datetime, calendar) is the actual operation to test, that must
* internally call the abstract operation ToTemporalDate or ToTemporalTime.
* It is passed a Temporal.PlainDateTime instance, as well as the instance's
* calendar object (so that it doesn't have to call the calendar getter itself
* if it wants to make any assertions about the calendar.)
*/
checkPlainDateTimeConversionFastPath(func) {
const actual = [];
const expected = [];

const calendar = new Temporal.Calendar("iso8601");
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321, calendar);
const prototypeDescrs = Object.getOwnPropertyDescriptors(Temporal.PlainDateTime.prototype);
["year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => {
Object.defineProperty(datetime, property, {
get() {
actual.push(`get ${property}`);
const value = prototypeDescrs[property].get.call(this);
return {
toString() {
actual.push(`toString ${property}`);
return value.toString();
},
valueOf() {
actual.push(`valueOf ${property}`);
return value;
},
};
},
});
});
Object.defineProperty(datetime, "calendar", {
get() {
actual.push("get calendar");
return calendar;
},
});

func(datetime, calendar);
assert.compareArray(actual, expected, "property getters not called");
},

/*
* checkSubclassingIgnored(construct, constructArgs, method, methodArgs,
* resultAssertions):
Expand Down

0 comments on commit 35fdc35

Please sign in to comment.