1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:37:47 +00:00

LibJS: Implement Temporal.Duration.prototype.abs()

This commit is contained in:
Linus Groh 2021-07-16 19:53:14 +01:00
parent 7df47bf3fb
commit 86c6e68431
3 changed files with 69 additions and 0 deletions

View file

@ -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);
}
});
});
test("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Temporal.Duration.prototype.abs.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
});
});