ladybird/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
Linus Groh d85b9fd5a0 LibJS: Bring back runtime validation of RegExp flags
This is a partial revert of commit 60064e2, which removed the validation
of RegExp flags during runtime and expected the parser to do that
exclusively - however this was not taking into account the RegExp()
constructor, which was subsequently crashing on invalid flags.

Also adds test for these constructor error cases, which were obviously
missing before.

Fixes #7042.
2021-05-11 22:47:14 +01:00

31 lines
985 B
JavaScript

describe("errors", () => {
test("invalid pattern", () => {
expect(() => {
RegExp("[");
}).toThrowWithMessage(
SyntaxError,
"RegExp compile error: Error during parsing of regular expression:"
);
});
test("invalid flag", () => {
expect(() => {
RegExp("", "x");
}).toThrowWithMessage(SyntaxError, "Invalid RegExp flag 'x'");
});
test("repeated flag", () => {
expect(() => {
RegExp("", "gg");
}).toThrowWithMessage(SyntaxError, "Repeated RegExp flag 'g'");
});
});
test("basic functionality", () => {
expect(RegExp().toString()).toBe("/(?:)/");
expect(RegExp(undefined).toString()).toBe("/(?:)/");
expect(RegExp("foo").toString()).toBe("/foo/");
expect(RegExp("foo", undefined).toString()).toBe("/foo/");
expect(RegExp("foo", "g").toString()).toBe("/foo/g");
expect(RegExp(undefined, "g").toString()).toBe("/(?:)/g");
});