mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 05:39:11 +00:00
LibJS: Implement Temporal.Instant.prototype.add/subtract/equals
This commit is contained in:
parent
615ad70030
commit
1d67f28e72
Notes:
github-actions[bot]
2024-11-25 12:34:16 +00:00
Author: https://github.com/trflynn89
Commit: 1d67f28e72
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2557
Reviewed-by: https://github.com/shannonbooth ✅
7 changed files with 226 additions and 0 deletions
|
@ -0,0 +1,56 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.add).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 1, 2, 3, 4, 5, 6);
|
||||
expect(instant.add(duration).epochNanoseconds).toBe(1625618644004005006n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.add.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, positive", () => {
|
||||
const instant = new Temporal.Instant(8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, negative", () => {
|
||||
const instant = new Temporal.Instant(-8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("disallowed fields", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
for (const [args, property] of [
|
||||
[[123, 0, 0, 0], "year"],
|
||||
[[0, 123, 0, 0], "month"],
|
||||
[[0, 0, 123, 0], "week"],
|
||||
[[0, 0, 0, 123], "day"],
|
||||
]) {
|
||||
const duration = new Temporal.Duration(...args);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(RangeError, `Largest unit must not be ${property}`);
|
||||
}
|
||||
});
|
||||
});
|
|
@ -0,0 +1,20 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.equals).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant1 = new Temporal.Instant(111n);
|
||||
expect(instant1.equals(instant1));
|
||||
const instant2 = new Temporal.Instant(999n);
|
||||
expect(!instant1.equals(instant2));
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.equals.call("foo", 1, 2);
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,56 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.subtract).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 1, 2, 3, 4, 5, 6);
|
||||
expect(instant.subtract(duration).epochNanoseconds).toBe(1625611197995994994n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.subtract.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, positive", () => {
|
||||
const instant = new Temporal.Instant(8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, negative", () => {
|
||||
const instant = new Temporal.Instant(-8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("disallowed fields", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
for (const [args, property] of [
|
||||
[[123, 0, 0, 0], "year"],
|
||||
[[0, 123, 0, 0], "month"],
|
||||
[[0, 0, 123, 0], "week"],
|
||||
[[0, 0, 0, 123], "day"],
|
||||
]) {
|
||||
const duration = new Temporal.Duration(...args);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(RangeError, `Largest unit must not be ${property}`);
|
||||
}
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue