-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(pattern.ts): enable use of regex flags for pattern in ruleset fil…
…es (#251) * fix(pattern.ts): enable use of regex flags for pattern in ruleset files #242 * Update src/functions/pattern.ts Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com> * Update src/functions/pattern.ts Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com> * test(pattern.test.ts): testing exception on invalid flag in string regex #242 * test(pattern.ts): use a different assertion
- Loading branch information
1 parent
73b9022
commit e951347
Showing
2 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { pattern } from '../pattern'; | ||
|
||
function runPattern(targetVal: any, options?: any) { | ||
return pattern( | ||
targetVal, | ||
options, | ||
{ | ||
given: ['$'], | ||
}, | ||
{ | ||
given: null, | ||
original: null, | ||
}, | ||
); | ||
} | ||
|
||
describe('pattern', () => { | ||
describe('given a true regex', () => { | ||
test('should return empty array when given value matches the given match regex', () => { | ||
expect(runPattern('abc', { match: /[abc]+/ })).toEqual([]); | ||
}); | ||
|
||
test('should return a message when given value does not match the given notMatch regex', () => { | ||
expect(runPattern('def', { match: /[abc]+/ })).toEqual([{ message: "must match the pattern '/[abc]+/'" }]); | ||
}); | ||
|
||
test('should return empty array when given value does not match the given notMatch regex', () => { | ||
expect(runPattern('dEf', { notMatch: /[abc]+/i })).toEqual([]); | ||
}); | ||
|
||
test('should return a message when given value does match the given notMatch regex', () => { | ||
expect(runPattern('aBc', { notMatch: /[abc]+/i })).toEqual([ | ||
{ message: "must not match the pattern '/[abc]+/i'" }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('given a string regex', () => { | ||
test('should return empty array when given value matches the given match string regex without slashes', () => { | ||
expect(runPattern('abc', { match: '[abc]+' })).toEqual([]); | ||
}); | ||
|
||
test('should return empty array when given value matches the given match string regex with slashes', () => { | ||
expect(runPattern('abc', { match: '/[abc]+/' })).toEqual([]); | ||
}); | ||
|
||
test('should return empty array when given value matches the given match string regex with slashes and modifier', () => { | ||
expect(runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]); | ||
}); | ||
|
||
test('should throw an exception when given string regex contains invalid flags', () => { | ||
expect(() => runPattern('aBc', { match: '/[abc]+/invalid' })).toThrow( | ||
"Invalid flags supplied to RegExp constructor 'invalid'", | ||
); | ||
}); | ||
|
||
test('should return empty array when given value does not match the given notMatch string regex with slashes and modifier', () => { | ||
expect(runPattern('def', { notMatch: '/[abc]+/i' })).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('given match and notMatch regexes', () => { | ||
test('should return empty array when given value match the given match and does not match the given notMatch regexes', () => { | ||
expect(runPattern('def', { match: /[def]+/, notMatch: /[abc]+/ })).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters