Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function test(REGEX_RE, filter, mutation, expected) {
- if(Array.isArray(mutation)) {
- return mutation.forEach(m => test(REGEX_RE, filter, m, expected));
- }
- const exec = REGEX_RE.exec(filter);
- try {
- const reg = new RegExp(exec[1], exec[2])
- const success = reg.test(mutation) === expected ? `\x1b[32msuccess\x1b[0m` : `\x1b[31mfailed \x1b[0m`;
- console.log('\t', success, filter, reg, mutation, expected);
- } catch(e) {
- console.log('\t', `\x1b[31mfailed \x1b[0m`, filter, 'Could not compile', exec.slice(1));
- }
- }
- function testfor(REGEX_RE) {
- console.log(REGEX_RE);
- test(REGEX_RE, '/^cou.*/', [
- 'coucou', 'couteau', 'couperet'
- ], true);
- test(REGEX_RE, '/^cou.*/', [
- 'toto', 'piscou'
- ], false);
- test(REGEX_RE, '/^cou.*/i', [
- 'CoucOu', 'cOuteau', 'cOuperet'
- ], true);
- test(REGEX_RE, '/^co\\/u.*/', [
- 'co/ucou', 'co/uteau', 'co/uperet'
- ], true);
- test(REGEX_RE, '/^co\\/u.*/', [
- 'coucou', 'couteau', 'couperet'
- ], false);
- test(REGEX_RE, '/^co\\/u.*/i', [
- 'cO/ucou', 'cO/uteau', 'cO/uperet'
- ], true);
- test(REGEX_RE, '/^co\\/u|toto.*/', [
- 'toto', 'totocoucou'
- ], true);
- test(REGEX_RE, '/^co/u|toto.*/', [
- 'toto', 'totocoucou'
- ], false);
- }
- testfor(/^\/(.*?)\/(\w*)/); // Initial regexp, should fail in several cases
- testfor(/^\/((?:(?:.*?)(?:\\\/)?)*?)\/(\w*)/); // works for all test cases
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement