Skip to content

Commit

Permalink
Separating test function into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinier Cruz committed Aug 19, 2024
1 parent 69ae50a commit 8367759
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
16 changes: 1 addition & 15 deletions src/panels/TcpDumpPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", {
Expand Down
28 changes: 28 additions & 0 deletions src/tests/suite/TcpDumpPanel.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 8367759

Please sign in to comment.