From 8367759493bee73156e22092e4e9ae6430fd5019 Mon Sep 17 00:00:00 2001 From: Reinier Cruz Date: Mon, 19 Aug 2024 20:11:26 +0000 Subject: [PATCH] Separating test function into its own file --- src/panels/TcpDumpPanel.ts | 16 +--------------- src/tests/suite/TcpDumpPanel.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 src/tests/suite/TcpDumpPanel.test.ts diff --git a/src/panels/TcpDumpPanel.ts b/src/panels/TcpDumpPanel.ts index bf14b0ccc..c9a37930f 100644 --- a/src/panels/TcpDumpPanel.ts +++ b/src/panels/TcpDumpPanel.ts @@ -28,7 +28,7 @@ const captureFileBasePathEscaped = escapeRegExp(captureFileBasePath); const captureFilePathRegex = `${captureFileBasePathEscaped}(.*)\\.cap`; // Matches the part of the filename after the prefix // Escape all regex meta characters to ensure sanitation and '/' for later use in regex pattern -function escapeRegExp(input: string): string { +export function escapeRegExp(input: string): string { return input.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&"); //Escapes by adding '\' to every matched string $& } //Reference for regex syntax chars: https://262.ecma-international.org/13.0/index.html#prod-SyntaxCharacter @@ -60,20 +60,6 @@ function getCaptureFromFilePath(filePath: string): string | null { return fileMatch && fileMatch[1]; } -// Test function to verify escaping -function testEscapeRegExp() { - const testString = "/tmp/vscodenodecap_*+?^${}()|[]\\"; - const expectedEscapedString = "\\/tmp\\/vscodenodecap_\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\"; - - const actualEscapedString = escapeRegExp(testString); - console.assert( - actualEscapedString === expectedEscapedString, - `Expected ${expectedEscapedString}, but got ${actualEscapedString}`, - ); -} - -testEscapeRegExp(); - export class TcpDumpPanel extends BasePanel<"tcpDump"> { constructor(extensionUri: Uri) { super(extensionUri, "tcpDump", { diff --git a/src/tests/suite/TcpDumpPanel.test.ts b/src/tests/suite/TcpDumpPanel.test.ts new file mode 100644 index 000000000..5bbfc312b --- /dev/null +++ b/src/tests/suite/TcpDumpPanel.test.ts @@ -0,0 +1,28 @@ +import * as assert from "assert"; +import { escapeRegExp } from "../../panels/TcpDumpPanel"; + +describe("testEscapeRegExp", function () { + it("should escape special regex characters", function () { + const input = "a.b*c?d+e^f$g|h(i)j{k}l[m]n\\o"; + const expected = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o"; + assert.equal(escapeRegExp(input), expected); + }); + + it("should return the same string if no special characters", function () { + const input = "abcdefg"; + const expected = "abcdefg"; + assert.equal(escapeRegExp(input), expected); + }); + + it("should return an empty string if input is empty", function () { + const input = ""; + const expected = ""; + assert.equal(escapeRegExp(input), expected); + }); + + it("should not double escape already escaped characters", function () { + //const input = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o"; + const expected = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o"; + assert.equal("", expected); + }); +});