Skip to content

Commit c35ae20

Browse files
authored
Temporal: Some more tests for PlainDateTime#with. (tc39#3481)
1 parent d9616ed commit c35ae20

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.plaindatetime.prototype.with
6+
description: Non-object arguments throw.
7+
features: [Temporal]
8+
---*/
9+
10+
const instance = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321);
11+
const args = [
12+
undefined,
13+
null,
14+
true,
15+
"2020-01-12T10:20:30",
16+
Symbol(),
17+
2020,
18+
2020n,
19+
];
20+
for (const argument of args) {
21+
assert.throws(TypeError, () => instance.with(argument), `Does not support ${typeof argument}`);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.plaindatetime.prototype.with
6+
description: The options argument is passed through to Calendar#dateFromFields as-is.
7+
includes: [temporalHelpers.js]
8+
features: [Temporal]
9+
---*/
10+
11+
const options = {};
12+
let calledDateFromFields = 0;
13+
class Calendar extends Temporal.Calendar {
14+
constructor() {
15+
super("iso8601");
16+
}
17+
dateFromFields(fields, optionsArg) {
18+
++calledDateFromFields;
19+
assert.sameValue(optionsArg, options, "should pass options object through");
20+
return super.dateFromFields(fields, optionsArg);
21+
}
22+
};
23+
const calendar = new Calendar();
24+
const plaindatetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321, calendar);
25+
const result = plaindatetime.with({ year: 2005 }, options);
26+
TemporalHelpers.assertPlainDateTime(result, 2005, 5, "M05", 2, 12, 34, 56, 987, 654, 321);
27+
assert.sameValue(calledDateFromFields, 1, "should have called overridden dateFromFields once");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
description: Throws if a Temporal object with a calendar is supplied
6+
esid: sec-temporal.plaindatetime.prototype.with
7+
features: [Temporal]
8+
---*/
9+
10+
const datetime = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
11+
12+
const values = [
13+
Temporal.PlainDate.from("2022-04-12"),
14+
Temporal.PlainDateTime.from("2022-04-12T15:19:45"),
15+
Temporal.PlainMonthDay.from("04-12"),
16+
Temporal.PlainTime.from("15:19:45"),
17+
Temporal.PlainYearMonth.from("2022-04"),
18+
Temporal.ZonedDateTime.from("2022-04-12T15:19:45[UTC]"),
19+
];
20+
21+
for (const value of values) {
22+
Object.defineProperty(value, "calendar", {
23+
get() { throw new Test262Error("should not get calendar property") }
24+
});
25+
Object.defineProperty(value, "timeZone", {
26+
get() { throw new Test262Error("should not get timeZone property") }
27+
});
28+
assert.throws(
29+
TypeError,
30+
() => datetime.with(value),
31+
"throws with temporal object"
32+
);
33+
}

test/built-ins/Temporal/PlainDateTime/prototype/with/order-of-operations.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const expected = [
4242
"get year",
4343
"get year.valueOf",
4444
"call year.valueOf",
45+
"get options.overflow",
46+
"get options.overflow.toString",
47+
"call options.overflow.toString",
48+
"get options.overflow",
49+
"get options.overflow.toString",
50+
"call options.overflow.toString",
4551
];
4652
const actual = [];
4753
const fields = {
@@ -70,7 +76,13 @@ const argument = new Proxy(fields, {
7076
return key in target;
7177
},
7278
});
73-
const result = instance.with(argument);
79+
const options = {
80+
get overflow() {
81+
actual.push("get options.overflow");
82+
return TemporalHelpers.toPrimitiveObserver(actual, "constrain", "options.overflow");
83+
}
84+
};
85+
const result = instance.with(argument, options);
7486
TemporalHelpers.assertPlainDateTime(result, 1, 1, "M01", 1, 1, 1, 1, 1, 1, 1);
7587
assert.sameValue(result.calendar.id, "iso8601", "calendar result");
7688
assert.compareArray(actual, expected, "order of operations");

0 commit comments

Comments
 (0)