From e2f40ef5a55acccf43fb394977473d48a5693968 Mon Sep 17 00:00:00 2001 From: Steven Levithan Date: Mon, 28 Oct 2024 01:21:28 +0100 Subject: [PATCH] Tests: toMatchTransipled matcher --- demo/index.html | 4 ++-- spec/character-set.spec.js | 23 +++++++++++++++++++++++ spec/compile.spec.js | 17 +++++++++++++++++ spec/helpers/matchers.js | 25 +++++++++++++++++++++++++ spec/patterns.spec.js | 8 -------- 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 spec/character-set.spec.js create mode 100644 spec/compile.spec.js create mode 100644 spec/helpers/matchers.js delete mode 100644 spec/patterns.spec.js diff --git a/demo/index.html b/demo/index.html index 01b6e31..b11da89 100644 --- a/demo/index.html +++ b/demo/index.html @@ -59,7 +59,7 @@

Try it


-    

The output shows the result of calling toRegExp. Oniguruma-To-ES includes functions to generate additional formats: compile (returns an object with pattern and flags strings), toOnigurumaAst, and toRegexAst (for an AST based on regex). You can run all of these from the console on this page. compile and toRegExp accept pattern and flags strings and an options object. toOnigurumaAst and toRegexAst accept a pattern and flags. You can also pass AST results to printAst.

+

The output shows the result of calling toRegExp. Oniguruma-To-ES includes functions to generate additional formats: compile, toOnigurumaAst, and toRegexAst (for an AST based on regex). You can run all of these from the console on this page. compile and toRegExp accept a pattern string, optional flags string, and optional options object. toOnigurumaAst and toRegexAst accept a pattern and optional flags. You can also pass AST results to printAst.

@@ -78,7 +78,7 @@

Try it

nodeIds.set(this, ++counter); } if (key === 'type') { - return `${value} [${nodeIds.get(this)}]`; + return `${value} [ID:${nodeIds.get(this)}]`; } if (key === 'parent') { let parentId = null; diff --git a/spec/character-set.spec.js b/spec/character-set.spec.js new file mode 100644 index 0000000..e792282 --- /dev/null +++ b/spec/character-set.spec.js @@ -0,0 +1,23 @@ +import {matchers} from './helpers/matchers.js'; + +beforeEach(() => { + jasmine.addMatchers(matchers); +}); + +describe('CharacterSet', () => { + describe('any', () => { + it('should match any character except line feed', () => { + expect('a').toMatchTranspiled('.'); + expect('\0').toMatchTranspiled('.'); + expect('\r').toMatchTranspiled('.'); + }); + + it('should not match line feed with flag m disabled', () => { + expect('\n').not.toMatchTranspiled('.'); + }); + + it('should match line feed with flag m enabled', () => { + expect('\n').toMatchTranspiled({pattern: '.', flags: 'm'}); + }); + }); +}); diff --git a/spec/compile.spec.js b/spec/compile.spec.js new file mode 100644 index 0000000..6abb27d --- /dev/null +++ b/spec/compile.spec.js @@ -0,0 +1,17 @@ +import {compile} from '../src/index.js'; + +describe('compile', () => { + it('should return an object with pattern and flags properties', () => { + expect(compile('')).toEqual({pattern: '', flags: 'v'}); + }); + + it('should accept supported targets', () => { + expect(compile('', '', {target: 'ES2018'})).toEqual({pattern: '', flags: 'u'}); + expect(compile('', '', {target: 'ES2024'})).toEqual({pattern: '', flags: 'v'}); + expect(compile('', '', {target: 'ESNext'})).toEqual({pattern: '', flags: 'v'}); + }); + + it('should not accept unsupported targets', () => { + expect(() => compile('', '', {target: 'ES2019'})).toThrow(); + }); +}); diff --git a/spec/helpers/matchers.js b/spec/helpers/matchers.js new file mode 100644 index 0000000..ae8ff36 --- /dev/null +++ b/spec/helpers/matchers.js @@ -0,0 +1,25 @@ +import {toRegExp} from '../../src/index.js'; +import {Target} from '../../src/utils.js'; + +const targets = Object.keys(Target); + +export const matchers = { + toMatchTranspiled() { + return { + compare(actual, expected) { + const pattern = typeof expected === 'string' ? expected : expected.pattern; + const flags = expected?.flags ?? ''; + for (const target of targets) { + const re = toRegExp(pattern, flags, {target}); + if (!re.test(actual)) { + return { + pass: false, + message: `Expected "${actual}" to match "${pattern}" with ${flags ? `flags "${flags}" and ` : ''}target ${target}`, + }; + } + } + return {pass: true}; + }, + }; + }, +}; diff --git a/spec/patterns.spec.js b/spec/patterns.spec.js deleted file mode 100644 index 30f561d..0000000 --- a/spec/patterns.spec.js +++ /dev/null @@ -1,8 +0,0 @@ -import {compile} from '../src/index.js'; -import {r} from '../src/utils.js'; - -describe('compile', () => { - it('should compile a list of patterns', () => { - expect(compile('.')).toEqual({pattern: r`[^\n]`, flags: 'v'}); - }); -});