mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-28 10:37:17 +02:00
LibJS: Add tests for regex literal parse-time error reporting
Cover the paths exercised by deferred regex compilation: - Invalid patterns in literals, eval(), and new Function() - Duplicate and invalid flags in literals - Valid literals with various flag combinations - Multiple literals in the same scope - Literals inside regular and arrow functions
This commit is contained in:
committed by
Andreas Kling
parent
b2b72a1884
commit
1f4700a0c8
Notes:
github-actions[bot]
2026-03-06 12:08:00 +00:00
Author: https://github.com/awesomekling Commit: https://github.com/LadybirdBrowser/ladybird/commit/1f4700a0c85 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/8211 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/Zaggy1024 Reviewed-by: https://github.com/alimpfard
62
Tests/LibJS/Runtime/syntax/regex-literal-errors.js
Normal file
62
Tests/LibJS/Runtime/syntax/regex-literal-errors.js
Normal file
@@ -0,0 +1,62 @@
|
||||
test("invalid regex literal pattern is a syntax error", () => {
|
||||
expect("/[/").not.toEval();
|
||||
expect("/(/").not.toEval();
|
||||
expect("/(?/").not.toEval();
|
||||
expect("/\\p{Invalid}/u").not.toEval();
|
||||
});
|
||||
|
||||
test("duplicate regex flags are a syntax error", () => {
|
||||
expect("/foo/gg").not.toEval();
|
||||
expect("/foo/ii").not.toEval();
|
||||
expect("/foo/gig").not.toEval();
|
||||
});
|
||||
|
||||
test("invalid regex flag is a syntax error", () => {
|
||||
expect("/foo/x").not.toEval();
|
||||
expect("/foo/Q").not.toEval();
|
||||
});
|
||||
|
||||
test("regex literal errors in eval", () => {
|
||||
expect(() => eval("/[/")).toThrow(SyntaxError);
|
||||
expect(() => eval("/foo/gg")).toThrow(SyntaxError);
|
||||
expect(() => eval("/foo/x")).toThrow(SyntaxError);
|
||||
});
|
||||
|
||||
test("regex literal errors in new Function()", () => {
|
||||
expect(() => new Function("/[/")).toThrow(SyntaxError);
|
||||
expect(() => new Function("/foo/gg")).toThrow(SyntaxError);
|
||||
expect(() => new Function("/foo/x")).toThrow(SyntaxError);
|
||||
});
|
||||
|
||||
test("valid regex literals parse and execute correctly", () => {
|
||||
expect("/foo/g").toEval();
|
||||
expect("/[a-z]+/gims").toEval();
|
||||
expect("/(?:abc)/u").toEval();
|
||||
expect("/hello world/").toEval();
|
||||
expect("/foo/dgimsuy").toEval();
|
||||
expect("/foo/v").toEval();
|
||||
});
|
||||
|
||||
test("regex literals work inside functions", () => {
|
||||
function f() {
|
||||
return /foo/g;
|
||||
}
|
||||
expect(f().test("foo")).toBeTrue();
|
||||
expect(f().test("bar")).toBeFalse();
|
||||
});
|
||||
|
||||
test("regex literals work inside arrow functions", () => {
|
||||
const f = () => /bar/i;
|
||||
expect(f().test("BAR")).toBeTrue();
|
||||
expect(f().test("baz")).toBeFalse();
|
||||
});
|
||||
|
||||
test("multiple regex literals in the same scope", () => {
|
||||
const a = /abc/;
|
||||
const b = /def/g;
|
||||
const c = /ghi/i;
|
||||
expect(a.test("abc")).toBeTrue();
|
||||
expect(b.test("def")).toBeTrue();
|
||||
expect(c.test("GHI")).toBeTrue();
|
||||
expect(a.test("def")).toBeFalse();
|
||||
});
|
||||
Reference in New Issue
Block a user