2024-08-24 08:07:31 +02:00
|
|
|
const createRegex = require("../../src/utils/createRegex");
|
|
|
|
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 08:07:31 +02:00
|
|
|
describe("createRegex", () => {
|
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
|
|
|
});
|
|
|
|
});
|