1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/tests/utils/createRegex.test.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

const createRegex = require("../../src/utils/createRegex.js");
2024-08-24 08:07:31 +02:00
const os = require("os");
2024-08-24 08:02:11 +02:00
2024-08-24 08:07:31 +02:00
jest.mock("os", () => ({
2024-08-24 08:02:11 +02:00
platform: jest.fn(),
}));
2024-08-24 17:23:06 +02:00
describe("Regular expression creation function", () => {
2024-08-24 08:02:11 +02:00
beforeEach(() => {
os.platform.mockReset();
});
2024-08-24 08:07:31 +02:00
test("should throw an error for invalid regular expression", () => {
expect(() => createRegex("invalid/regex", false)).toThrow(
"Invalid regular expression: invalid/regex",
);
2024-08-24 08:02:11 +02:00
});
2024-08-24 08:07:31 +02:00
test("should create a regular expression without modifiers", () => {
const regex = createRegex("/test/", false);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("");
2024-08-24 08:02:11 +02:00
});
2024-08-24 08:07:31 +02:00
test("should create a regular expression with modifiers", () => {
const regex = createRegex("/test/gi", false);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("gi");
2024-08-24 08:02:11 +02:00
});
test('should add "i" modifier for paths on Windows', () => {
2024-08-24 08:07:31 +02:00
os.platform.mockReturnValue("win32");
const regex = createRegex("/test/", true);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("i");
2024-08-24 08:02:11 +02:00
});
test('should not add "i" modifier for paths on non-Windows platforms', () => {
2024-08-24 08:07:31 +02:00
os.platform.mockReturnValue("linux");
const regex = createRegex("/test/", true);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("");
2024-08-24 08:02:11 +02:00
});
test('should not add "i" modifier if already present', () => {
2024-08-24 08:07:31 +02:00
os.platform.mockReturnValue("win32");
const regex = createRegex("/test/i", true);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("i");
2024-08-24 08:02:11 +02:00
});
2024-08-24 08:07:31 +02:00
test("should handle escaped characters in the search string", () => {
const regex = createRegex("/test\\/path/", false);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test\\/path");
expect(regex.flags).toBe("");
2024-08-24 08:02:11 +02:00
});
2024-08-24 08:07:31 +02:00
test("should handle multiple modifiers", () => {
const regex = createRegex("/test/gim", false);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("test");
expect(regex.flags).toBe("gim");
2024-08-24 08:02:11 +02:00
});
2024-08-24 08:07:31 +02:00
test("should handle empty search string", () => {
const regex = createRegex("/^$/", false);
2024-08-24 08:02:11 +02:00
expect(regex).toBeInstanceOf(RegExp);
2024-08-24 08:07:31 +02:00
expect(regex.source).toBe("^$");
expect(regex.flags).toBe("");
2024-08-24 08:02:11 +02:00
});
});