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

LibJS: Disallow mixed-sign durations in Intl.DurationFormat

This is a normative change in the Intl.DurationFormat spec.

See: 89ab1855
This commit is contained in:
Idan Horowitz 2022-07-02 15:55:14 +03:00
parent 8a671154c3
commit fb8c4a724e
5 changed files with 187 additions and 12 deletions

View file

@ -63,3 +63,58 @@ describe("correct behavior", () => {
).toBe("1 J, 2 M, 3 W, 3 T, 4 Std., 5 Min., 6 Sek., 7 ms und 8,009 μs");
});
});
describe("errors", () => {
test("non-object duration records", () => {
[-100, Infinity, NaN, "hello", 152n, Symbol("foo")].forEach(value => {
expect(() => {
new Intl.DurationFormat().format(value);
}).toThrowWithMessage(TypeError, "is not an object");
});
});
test("empty duration record", () => {
expect(() => {
new Intl.DurationFormat().format({});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
expect(() => {
new Intl.DurationFormat().format({ foo: 123 });
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
});
test("non-integral duration fields", () => {
[
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds",
].forEach(field => {
expect(() => {
new Intl.DurationFormat().format({ [field]: 1.5 });
}).toThrowWithMessage(
RangeError,
`Invalid value for duration property '${field}': must be an integer, got 1.5`
);
expect(() => {
new Intl.DurationFormat().format({ [field]: -Infinity });
}).toThrowWithMessage(
RangeError,
`Invalid value for duration property '${field}': must be an integer, got -Infinity`
);
});
});
test("inconsistent field signs", () => {
expect(() => {
new Intl.DurationFormat().format({ years: 1, months: -1 });
}).toThrowWithMessage(RangeError, "Invalid duration-like object");
});
});

View file

@ -263,3 +263,58 @@ describe("correct behavior", () => {
]);
});
});
describe("errors", () => {
test("non-object duration records", () => {
[-100, Infinity, NaN, "hello", 152n, Symbol("foo")].forEach(value => {
expect(() => {
new Intl.DurationFormat().formatToParts(value);
}).toThrowWithMessage(TypeError, "is not an object");
});
});
test("empty duration record", () => {
expect(() => {
new Intl.DurationFormat().formatToParts({});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
expect(() => {
new Intl.DurationFormat().formatToParts({ foo: 123 });
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
});
test("non-integral duration fields", () => {
[
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
"nanoseconds",
].forEach(field => {
expect(() => {
new Intl.DurationFormat().formatToParts({ [field]: 1.5 });
}).toThrowWithMessage(
RangeError,
`Invalid value for duration property '${field}': must be an integer, got 1.5`
);
expect(() => {
new Intl.DurationFormat().formatToParts({ [field]: -Infinity });
}).toThrowWithMessage(
RangeError,
`Invalid value for duration property '${field}': must be an integer, got -Infinity`
);
});
});
test("inconsistent field signs", () => {
expect(() => {
new Intl.DurationFormat().formatToParts({ years: 1, months: -1 });
}).toThrowWithMessage(RangeError, "Invalid duration-like object");
});
});