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

Follow-up on closed Temporal tests PRs #3438

Merged
merged 5 commits into from
Mar 29, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Temporal.Calendar.prototype.yearMonthFromFields return correctly with valid data.
info: |
1. Let calendar be the this value.
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
3. Assert: calendar.[[Identifier]] is "iso8601".
4. If Type(fields) is not Object, throw a TypeError exception.
5. Set options to ? GetOptionsObject(options).
6. Let result be ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601")

let result = cal.yearMonthFromFields({ year: 2021, month: 7 });
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "year 2021, month 7");
result = cal.yearMonthFromFields({ year: 2021, month: 12 });
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "year 2021, month 12");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" });
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "year 2021, monthCode M07");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" });
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "year 2021, monthCode M12");

["constrain", "reject"].forEach((overflow) => {
const opt = { overflow };
result = cal.yearMonthFromFields({ year: 2021, month: 7 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", `year 2021, month 7, overflow ${overflow}`);
result = cal.yearMonthFromFields({ year: 2021, month: 12 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", `year 2021, month 12, overflow ${overflow}`);
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", `year 2021, monthCode M07, overflow ${overflow}`);
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", `year 2021, monthCode M12, overflow ${overflow}`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const yearMonthFromFields = Temporal.Calendar.prototype.yearMonthFromFields;

assert.sameValue(typeof yearMonthFromFields, "function");

assert.throws(TypeError, () => yearMonthFromFields.call(undefined), "undefined");
assert.throws(TypeError, () => yearMonthFromFields.call(null), "null");
assert.throws(TypeError, () => yearMonthFromFields.call(true), "true");
assert.throws(TypeError, () => yearMonthFromFields.call(""), "empty string");
assert.throws(TypeError, () => yearMonthFromFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => yearMonthFromFields.call(1), "1");
assert.throws(TypeError, () => yearMonthFromFields.call({}), "plain object");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar), "Temporal.Calendar");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar.prototype), "Temporal.Calendar.prototype");
const arg = { year: 2021, month: 1 };

assert.throws(TypeError, () => yearMonthFromFields.call(undefined, arg), "undefined");
assert.throws(TypeError, () => yearMonthFromFields.call(null, arg), "null");
assert.throws(TypeError, () => yearMonthFromFields.call(true, arg), "true");
assert.throws(TypeError, () => yearMonthFromFields.call("", arg), "empty string");
assert.throws(TypeError, () => yearMonthFromFields.call(Symbol(), arg), "symbol");
assert.throws(TypeError, () => yearMonthFromFields.call(1, arg), "1");
assert.throws(TypeError, () => yearMonthFromFields.call({}, arg), "plain object");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar, arg), "Temporal.Calendar");
assert.throws(TypeError, () => yearMonthFromFields.call(Temporal.Calendar.prototype, arg), "Temporal.Calendar.prototype");
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Temporal.Calendar.prototype.yearMonthFromFields will throw TypeError with incorrect input data type.
info: |
1. Let calendar be the this value.
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
3. Assert: calendar.[[Identifier]] is "iso8601".
4. If Type(fields) is not Object, throw a TypeError exception.
5. Set options to ? GetOptionsObject(options).
6. Let result be ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601")

assert.throws(TypeError, () => cal.yearMonthFromFields({}), "at least one correctly spelled property is required");
assert.throws(TypeError, () => cal.yearMonthFromFields({ month: 1 }), "year is required");
assert.throws(TypeError, () => cal.yearMonthFromFields({ year: 2021 }), "month or monthCode is required");
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Throw a TypeError if the fields is not an object
features: [Symbol, Temporal]
---*/

const tests = [undefined, null, false, "string", Symbol("sym"), Math.PI, 42n];
const tests = [undefined, null, true, false, "string", Symbol("sym"), Math.PI, Infinity, NaN, 42n];
const iso = Temporal.Calendar.from("iso8601");
for (const fields of tests) {
assert.throws(TypeError, () => iso.yearMonthFromFields(fields, {}));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Throw RangeError for an out-of-range, conflicting, or ill-formed monthCode
info: |
1. Let calendar be the this value.
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
3. Assert: calendar.[[Identifier]] is "iso8601".
4. If Type(fields) is not Object, throw a TypeError exception.
5. Set options to ? GetOptionsObject(options).
6. Let result be ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601");

["m1", "M1", "m01"].forEach((monthCode) => {
assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, monthCode }),
`monthCode '${monthCode}' is not well-formed`);
});

assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, month: 12, monthCode: "M11" }),
"monthCode and month conflict");

["M00", "M19", "M99", "M13"].forEach((monthCode) => {
assert.throws(RangeError, () => cal.yearMonthFromFields({ year: 2021, monthCode }),
`monthCode '${monthCode}' is not valid for year 2021`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Throw a TypeError if options is not an object or undefined
info: |
5. Set options to ? GetOptionsObject(options).
features: [Symbol, Temporal]
---*/

const tests = [null, true, false, "string", Symbol("sym"), Math.PI, Infinity, NaN, 42n];
const iso = new Temporal.Calendar("iso8601");
for (const options of tests) {
assert.throws(TypeError, () => iso.yearMonthFromFields({ year: 2021, month: 7 }, options),
"options is not object");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Temporal.Calendar.prototype.yearMonthFromFields will return correctly with data and overflow set to 'constrain'.
info: |
1. Let calendar be the this value.
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
3. Assert: calendar.[[Identifier]] is "iso8601".
4. If Type(fields) is not Object, throw a TypeError exception.
5. Set options to ? GetOptionsObject(options).
6. Let result be ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601")
const opt = { overflow: "constrain" };

let result = cal.yearMonthFromFields({ year: 2021, month: 1 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 1, "M01", "month 1 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 2 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 2, "M02", "month 2 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 3 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 3, "M03", "month 3 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 4 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 4, "M04", "month 4 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 5 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 5, "M05", "month 5 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 6 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 6, "M06", "month 6 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 7 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "month 7 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 8 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 8, "M08", "month 8 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 9 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 9, "M09", "month 9 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 10 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 10, "M10", "month 10 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 11 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 11, "M11", "month 11 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, month: 12 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 12 with overflow constrain");

assert.throws(
RangeError,
() => cal.yearMonthFromFields({ year: 2021, month: -99999 }, opt),
"negative month -99999 is out of range even with overflow constrain"
)
assert.throws(
RangeError,
() => cal.yearMonthFromFields({ year: 2021, month: -1 }, opt),
"negative month -1 is out of range even with overflow constrain"
)
assert.throws(
RangeError,
() => cal.yearMonthFromFields({ year: 2021, month: 0 }, opt),
"month zero is out of range even with overflow constrain"
)

result = cal.yearMonthFromFields({ year: 2021, month: 13 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 13 is constrained to 12");
result = cal.yearMonthFromFields({ year: 2021, month: 99999 }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "month 99999 is constrained to 12");

result = cal.yearMonthFromFields({ year: 2021, monthCode: "M01" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 1, "M01", "monthCode M01 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M02" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 2, "M02", "monthCode M02 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M03" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 3, "M03", "monthCode M03 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M04" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 4, "M04", "monthCode M04 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M05" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 5, "M05", "monthCode M05 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M06" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 6, "M06", "monthCode M06 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M07" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 7, "M07", "monthCode M07 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M08" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 8, "M08", "monthCode M08 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M09" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 9, "M09", "monthCode M09 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M10" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 10, "M10", "monthCode M10 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M11" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 11, "M11", "monthCode M11 with overflow constrain");
result = cal.yearMonthFromFields({ year: 2021, monthCode: "M12" }, opt);
TemporalHelpers.assertPlainYearMonth(result, 2021, 12, "M12", "monthCode M12 with overflow constrain");
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.calendar.prototype.yearmonthfromfields
description: Throw RangeError for input data out of range with overflow reject
info: |
1. Let calendar be the this value.
2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
3. Assert: calendar.[[Identifier]] is "iso8601".
4. If Type(fields) is not Object, throw a TypeError exception.
5. Set options to ? GetOptionsObject(options).
6. Let result be ? ISOYearMonthFromFields(fields, options).
7. Return ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[ReferenceISODay]]).
features: [Temporal]
---*/

const cal = new Temporal.Calendar("iso8601");

[-1, 0, 13, 9995].forEach((month) => {
assert.throws(
RangeError,
() => cal.yearMonthFromFields({year: 2021, month, day: 5}, { overflow: "reject" }),
`Month ${month} is out of range for 2021 with overflow: reject`
);
});
39 changes: 39 additions & 0 deletions test/built-ins/Temporal/Duration/prototype/abs/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.abs
description: Temporal.Duration.prototype.abs will return absolute value of the input duration.
info: |
1. Let duration be the this value.
2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])).

features: [Temporal]
includes: [temporalHelpers.js]
---*/

let d1 = new Temporal.Duration();
TemporalHelpers.assertDuration(
d1.abs(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "empty");

let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
TemporalHelpers.assertDuration(
d2.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "positive");

let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5);
TemporalHelpers.assertDuration(
d3.abs(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, "large positive");

let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10);
TemporalHelpers.assertDuration(
d4.abs(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "negative");

// Test with some zeros
let d5 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0);
TemporalHelpers.assertDuration(
d5.abs(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, "some zeros");

let d6 = new Temporal.Duration(0, 2, 0, 4, 0, 6, 0, 8, 0, 10);
TemporalHelpers.assertDuration(
d6.abs(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, "other zeros");
51 changes: 51 additions & 0 deletions test/built-ins/Temporal/Duration/prototype/negated/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.negated
description: Temporal.Duration.prototype.negated will return negated value of the input duration.
info: |
3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])).
features: [Temporal]
includes: [temporalHelpers.js]
---*/

let d1 = new Temporal.Duration();
TemporalHelpers.assertDuration(
d1.negated(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"zeros");

let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
TemporalHelpers.assertDuration(
d2.negated(), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10,
"positive values");

let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5);
TemporalHelpers.assertDuration(
d3.negated(), -1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5,
"large positive values");

let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10);
TemporalHelpers.assertDuration(
d4.negated(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
"negative values");

let d5 = new Temporal.Duration(-1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5);
TemporalHelpers.assertDuration(
d5.negated(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5,
"large negative values");

let d6 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0);
TemporalHelpers.assertDuration(
d6.negated(), -1, 0, -3, 0, -5, 0, -7, 0, -9, 0,
"some zeros with positive values");

let d7 = new Temporal.Duration(-1, 0, -3, 0, -5, 0, -7, 0, -9, 0);
TemporalHelpers.assertDuration(
d7.negated(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0,
"some zeros with negative values");

let d8 = new Temporal.Duration(0, -2, 0, -4, 0, -6, 0, -8, 0, -10);
TemporalHelpers.assertDuration(
d8.negated(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10,
"other zeros with negative values");
Loading