mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:17:35 +00:00
LibJS: Implement Temporal.Duration.prototype.abs()
This commit is contained in:
parent
7df47bf3fb
commit
86c6e68431
3 changed files with 69 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("errors", () => {
|
||||
test("this value must be a Temporal.Duration object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.prototype.abs.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue