Skip to content

Commit

Permalink
Fix failing test for Node < 23
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 29, 2024
1 parent f8c02aa commit 135f806
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions spec/helpers/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const duplicateCaptureNamesSupported = (() => {
try {
new RegExp('(?<n>)|(?<n>)');
} catch (e) {
return false;
}
return true;
})();
12 changes: 9 additions & 3 deletions spec/helpers/matchers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {toRegExp} from '../../dist/index.mjs';
import {EsVersion} from '../../src/utils.js';

function getArgs(actual, expected) {
const pattern = typeof expected === 'string' ? expected : expected.pattern;
const flags = expected?.flags ?? '';
const flags = expected.flags ?? '';
const strings = Array.isArray(actual) ? actual : [actual];
let targets = ['ES2018', 'ES2024', 'ESNext'];
if (expected.targetMax) {
targets = targets.filter(target => EsVersion[target] <= EsVersion[expected.targetMax]);
}
return {
pattern,
flags,
strings,
targets,
};
}

Expand All @@ -18,8 +24,8 @@ function matchedFullStr(match, str) {
// Expects `negate` to be set by `negativeCompare` and doesn't rely on Jasmine's automatic matcher
// negation because when negated we don't want to early return `true` when looping over the array
// of strings and one is found to not match; they all need to not match
function matchWithAllTargets({pattern, flags, strings}, {exact, negate}) {
for (const target of ['ES2018', 'ES2024', 'ESNext']) {
function matchWithAllTargets({pattern, flags, strings, targets}, {exact, negate}) {
for (const target of targets) {
const re = toRegExp(pattern, flags, {target});
for (const str of strings) {
// In case `flags` included `y`
Expand Down
10 changes: 7 additions & 3 deletions spec/match-backreference.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {compile} from '../dist/index.mjs';
import {r} from '../src/utils.js';
import {cp, r} from '../src/utils.js';
import {duplicateCaptureNamesSupported} from './helpers/features.js';
import {matchers} from './helpers/matchers.js';

beforeEach(() => {
Expand All @@ -21,7 +22,7 @@ describe('Backreference', () => {
});

it('should treat escaped number as octal if > 1 digit and not enough captures to the left', () => {
expect(`123456789${String.fromCodePoint(0o10)}`).toExactlyMatch(r`(1)(2)(3)(4)(5)(6)(7)(8)(9)\10`);
expect(`123456789${cp(0o10)}`).toExactlyMatch(r`(1)(2)(3)(4)(5)(6)(7)(8)(9)\10`);
expect('\u{1}8').toExactlyMatch(r`()\18`);
});

Expand Down Expand Up @@ -170,7 +171,10 @@ describe('Backreference', () => {

it('should reference the group to the left when there are duplicate names to the right', () => {
expect('aab').toExactlyMatch(r`(?<n>a)\k<n>(?<n>b)`);
expect('aa').toExactlyMatch(r`(?<n>a)\k<n>|(?<n>b)`);
expect('aa').toExactlyMatch({
pattern: r`(?<n>a)\k<n>|(?<n>b)`,
targetMax: duplicateCaptureNamesSupported ? null : 'ES2024',
});
});

it('should multiplex for duplicate names to the left', () => {
Expand Down
6 changes: 3 additions & 3 deletions spec/match-char-class.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {r} from '../src/utils.js';
import {cp, r} from '../src/utils.js';
import {matchers} from './helpers/matchers.js';

beforeEach(() => {
Expand Down Expand Up @@ -36,8 +36,8 @@ describe('CharacterClass', () => {
expect('\u{1}').toExactlyMatch(r`[\1]`);
expect('\u{1}').toExactlyMatch(r`[\01]`);
expect('\u{1}').toExactlyMatch(r`[\001]`);
expect(String.fromCodePoint(0o17)).toExactlyMatch(r`[\17]`);
expect(String.fromCodePoint(0o777)).toExactlyMatch(r`[\777]`);
expect(cp(0o17)).toExactlyMatch(r`[\17]`);
expect(cp(0o777)).toExactlyMatch(r`[\777]`);
});

it('should match octals followed by literal digits', () => {
Expand Down

0 comments on commit 135f806

Please sign in to comment.