mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-02 15:46:33 +00:00
LibJS: Implement mathematical Temporal.Duration prototypes
Includes: Temporal.Duration.prototype.negated Temporal.Duration.prototype.abs Temporal.Duration.prototype.add Temporal.Duration.prototype.subtract
This commit is contained in:
parent
55c81482b0
commit
a80523be18
Notes:
github-actions[bot]
2024-11-21 00:06:00 +00:00
Author: https://github.com/trflynn89
Commit: a80523be18
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2431
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/shannonbooth ✅
11 changed files with 591 additions and 0 deletions
|
@ -0,0 +1,53 @@
|
|||
const DURATION_PROPERTIES = [
|
||||
"years",
|
||||
"months",
|
||||
"weeks",
|
||||
"days",
|
||||
"hours",
|
||||
"minutes",
|
||||
"seconds",
|
||||
"milliseconds",
|
||||
"microseconds",
|
||||
"nanoseconds",
|
||||
];
|
||||
|
||||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Duration.prototype.abs).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
let absoluteDuration;
|
||||
|
||||
absoluteDuration = new Temporal.Duration(123).abs();
|
||||
expect(absoluteDuration.years).toBe(123);
|
||||
|
||||
absoluteDuration = new Temporal.Duration(-123).abs();
|
||||
expect(absoluteDuration.years).toBe(123);
|
||||
});
|
||||
|
||||
test("each property is made absolute", () => {
|
||||
let values;
|
||||
let duration;
|
||||
|
||||
values = Array(DURATION_PROPERTIES.length).fill(-1);
|
||||
duration = new Temporal.Duration(...values).abs();
|
||||
for (const property of DURATION_PROPERTIES) {
|
||||
expect(duration[property]).toBe(1);
|
||||
}
|
||||
|
||||
values = Array(DURATION_PROPERTIES.length).fill(1);
|
||||
duration = new Temporal.Duration(...values).abs();
|
||||
for (const property of DURATION_PROPERTIES) {
|
||||
expect(duration[property]).toBe(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Duration object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.prototype.abs.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,77 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Duration.prototype.add).toHaveLength(1);
|
||||
});
|
||||
|
||||
function checkCommonResults(durationResult) {
|
||||
expect(durationResult.years).toBe(0);
|
||||
expect(durationResult.months).toBe(0);
|
||||
expect(durationResult.weeks).toBe(0);
|
||||
expect(durationResult.days).toBe(3);
|
||||
expect(durationResult.hours).toBe(3);
|
||||
expect(durationResult.minutes).toBe(3);
|
||||
expect(durationResult.seconds).toBe(3);
|
||||
expect(durationResult.milliseconds).toBe(3);
|
||||
expect(durationResult.microseconds).toBe(3);
|
||||
expect(durationResult.nanoseconds).toBe(3);
|
||||
}
|
||||
|
||||
test("basic functionality", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1);
|
||||
const durationResult = duration.add(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
|
||||
test("from duration-like", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = {
|
||||
days: 1,
|
||||
hours: 1,
|
||||
minutes: 1,
|
||||
seconds: 1,
|
||||
milliseconds: 1,
|
||||
microseconds: 1,
|
||||
nanoseconds: 1,
|
||||
};
|
||||
const durationResult = duration.add(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
|
||||
test("from string", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = "P1DT1H1M1.001001001S";
|
||||
const durationResult = duration.add(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Duration object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.prototype.add.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
|
||||
});
|
||||
|
||||
test("relativeTo is required when duration has calendar units", () => {
|
||||
const yearDuration = new Temporal.Duration(1);
|
||||
const monthDuration = new Temporal.Duration(0, 1);
|
||||
const weekDuration = new Temporal.Duration(0, 0, 1);
|
||||
const durationToAdd = { seconds: 1 };
|
||||
|
||||
expect(() => {
|
||||
yearDuration.add(durationToAdd);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
|
||||
expect(() => {
|
||||
monthDuration.add(durationToAdd);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
|
||||
expect(() => {
|
||||
weekDuration.add(durationToAdd);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,42 @@
|
|||
const DURATION_PROPERTIES = [
|
||||
"years",
|
||||
"months",
|
||||
"weeks",
|
||||
"days",
|
||||
"hours",
|
||||
"minutes",
|
||||
"seconds",
|
||||
"milliseconds",
|
||||
"microseconds",
|
||||
"nanoseconds",
|
||||
];
|
||||
|
||||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.Duration.prototype.negated).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const negativeDuration = new Temporal.Duration(123).negated();
|
||||
expect(negativeDuration.years).toBe(-123);
|
||||
|
||||
const positiveDuration = new Temporal.Duration(-123).negated();
|
||||
expect(positiveDuration.years).toBe(123);
|
||||
});
|
||||
|
||||
test("each property is negated", () => {
|
||||
const values = Array(DURATION_PROPERTIES.length).fill(1);
|
||||
const duration = new Temporal.Duration(...values).negated();
|
||||
for (const property of DURATION_PROPERTIES) {
|
||||
expect(duration[property]).toBe(-1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Duration object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.prototype.negated.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,77 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Duration.prototype.subtract).toHaveLength(1);
|
||||
});
|
||||
|
||||
function checkCommonResults(durationResult) {
|
||||
expect(durationResult.years).toBe(0);
|
||||
expect(durationResult.months).toBe(0);
|
||||
expect(durationResult.weeks).toBe(0);
|
||||
expect(durationResult.days).toBe(1);
|
||||
expect(durationResult.hours).toBe(1);
|
||||
expect(durationResult.minutes).toBe(1);
|
||||
expect(durationResult.seconds).toBe(1);
|
||||
expect(durationResult.milliseconds).toBe(1);
|
||||
expect(durationResult.microseconds).toBe(1);
|
||||
expect(durationResult.nanoseconds).toBe(1);
|
||||
}
|
||||
|
||||
test("basic functionality", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = new Temporal.Duration(0, 0, 0, 1, 1, 1, 1, 1, 1, 1);
|
||||
const durationResult = duration.subtract(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
|
||||
test("from duration-like", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = {
|
||||
days: 1,
|
||||
hours: 1,
|
||||
minutes: 1,
|
||||
seconds: 1,
|
||||
milliseconds: 1,
|
||||
microseconds: 1,
|
||||
nanoseconds: 1,
|
||||
};
|
||||
const durationResult = duration.subtract(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
|
||||
test("from string", () => {
|
||||
const duration = new Temporal.Duration(0, 0, 0, 2, 2, 2, 2, 2, 2, 2);
|
||||
const oneDuration = "P1DT1H1M1.001001001S";
|
||||
const durationResult = duration.subtract(oneDuration);
|
||||
|
||||
checkCommonResults(durationResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Duration object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.prototype.subtract.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
|
||||
});
|
||||
|
||||
test("relativeTo is required when duration has calendar units", () => {
|
||||
const yearDuration = new Temporal.Duration(1);
|
||||
const monthDuration = new Temporal.Duration(0, 1);
|
||||
const weekDuration = new Temporal.Duration(0, 0, 1);
|
||||
const durationToSubtract = { seconds: 1 };
|
||||
|
||||
expect(() => {
|
||||
yearDuration.subtract(durationToSubtract);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
|
||||
expect(() => {
|
||||
monthDuration.subtract(durationToSubtract);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
|
||||
expect(() => {
|
||||
weekDuration.subtract(durationToSubtract);
|
||||
}).toThrowWithMessage(RangeError, "Largest unit must not be a calendar unit");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue