Skip to content

Commit

Permalink
Tests: toMatchTransipled matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 28, 2024
1 parent 031d367 commit e2f40ef
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h2>Try it</h2>
</p>
</details>
<pre id="output"></pre>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code> (returns an object with <code>pattern</code> and <code>flags</code> strings), <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept pattern and flags strings and an options object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a pattern and flags. You can also pass AST results to <code>printAst</code>.</small></p>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code>, <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept a <code>pattern</code> string, optional <code>flags</code> string, and optional <code>options</code> object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a <code>pattern</code> and optional <code>flags</code>. You can also pass AST results to <code>printAst</code>.</small></p>
</main>

<script src="../dist/index.min.js"></script>
Expand All @@ -78,7 +78,7 @@ <h2>Try it</h2>
nodeIds.set(this, ++counter);
}
if (key === 'type') {
return `${value} [${nodeIds.get(this)}]`;
return `${value} [ID:${nodeIds.get(this)}]`;
}
if (key === 'parent') {
let parentId = null;
Expand Down
23 changes: 23 additions & 0 deletions spec/character-set.spec.js
Original file line number Diff line number Diff line change
@@ -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'});
});
});
});
17 changes: 17 additions & 0 deletions spec/compile.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
25 changes: 25 additions & 0 deletions spec/helpers/matchers.js
Original file line number Diff line number Diff line change
@@ -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};
},
};
},
};
8 changes: 0 additions & 8 deletions spec/patterns.spec.js

This file was deleted.

0 comments on commit e2f40ef

Please sign in to comment.