Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
SYBIOTE committed Jul 26, 2024
1 parent 8946c7f commit 007d1e6
Showing 1 changed file with 24 additions and 40 deletions.
64 changes: 24 additions & 40 deletions packages/common/tests/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,46 +89,30 @@ describe('regex.test', () => {

describe('HEIRARCHY_SEARCH_REPLACE_REGEX', () => {
it('should replace special characters in search', () => {
const inValidHeirarchySearchs = [
'hello_world',
'file<name',
'email@example.com:80',
'path/to/file',
'back\\slash',
'pipe|symbol',
'question?mark',
'asterisk*char',
'control\0char',
'another\ncontrol'
]
inValidHeirarchySearchs.forEach((filename) => {
assert.ok(INVALID_FILENAME_REGEX.test(filename), `Expected '${filename}' to be invalid`)
})
})

const escapeSpecialChars = (input) => {
return input.replace(VALID_HEIRACHY_SEARCH_REGEX, '\\$&')
}

const testCases = [
{ input: 'a.b', expected: 'a\\.b' },
{ input: 'a*b', expected: 'a\\*b' },
{ input: 'a+b', expected: 'a\\+b' },
{ input: 'a?b', expected: 'a\\?b' },
{ input: '^a', expected: '\\^a' },
{ input: 'a$b', expected: 'a\\$b' },
{ input: '(a)', expected: '\\(a\\)' },
{ input: 'a|b', expected: 'a\\|b' },
{ input: '[a]', expected: '\\[a\\]' },
{ input: 'a\\b', expected: 'a\\\\b' },
{ input: 'a.b*c+?^${}()|[]\\', expected: 'a\\.b\\*c\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\' },
{ input: 'abc', expected: 'abc' }
]

testCases.forEach(({ input, expected }) => {
it(`should escape special characters in "${input}" correctly`, function () {
const escaped = escapeSpecialChars(input)
assert.equal(escaped, expected, `Expected escaped ${input} string to match expected ${expected} value`)
const escapeSpecialChars = (input) => {
return input.replace(VALID_HEIRACHY_SEARCH_REGEX, '\\$&')
}

const testCases = [
{ input: 'a.b', expected: 'a\\.b' },
{ input: 'a*b', expected: 'a\\*b' },
{ input: 'a+b', expected: 'a\\+b' },
{ input: 'a?b', expected: 'a\\?b' },
{ input: '^a', expected: '\\^a' },
{ input: 'a$b', expected: 'a\\$b' },
{ input: '(a)', expected: '\\(a\\)' },
{ input: 'a|b', expected: 'a\\|b' },
{ input: '[a]', expected: '\\[a\\]' },
{ input: 'a\\b', expected: 'a\\\\b' },
{ input: 'a.b*c+?^${}()|[]\\', expected: 'a\\.b\\*c\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\' },
{ input: 'abc', expected: 'abc' }
]

testCases.forEach(({ input, expected }) => {
it(`should escape special characters in "${input}" correctly`, function () {
const escaped = escapeSpecialChars(input)
assert.equal(escaped, expected, `Expected escaped ${input} string to match expected ${expected} value`)
})
})
})
})
Expand Down

0 comments on commit 007d1e6

Please sign in to comment.