Skip to content

Commit

Permalink
onig:compare
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Nov 2, 2024
1 parent 3252eb3 commit b133a78
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
80 changes: 57 additions & 23 deletions scripts/onig-compare.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
import {r} from '../src/utils.js';
import {ansi, areMatchDetailsEqual, err, ok, onigurumaResult, transpiledRegExpResult} from './utils.js';
import {areMatchDetailsEqual, color, err, ok, onigurumaResult, transpiledRegExpResult} from './utils.js';

// Help with improving this script or moving it into Jasmine specs would be very welcome
// Help with improving this script or comparing with Oniguruma automaticlly in Jasmine specs would
// be very welcome

compare([
[r`\x7F`, '\x7F'],
[r`\x80`, '\x80'],
[r`\x`, '\\x'],
[r`\p{`, '\\p{'],
[r`\O`, '\n'],
[r`\u{A0}`, '\u{A0}\\u{A0}']
[r`\0`, `\0`],
[r`\00`, `\0`],
[r`\000`, `\0`],
[r`\0000`, `\u{0}0`],
[r`\c`, r`\c`],
[r`\O`, `\n`], // Ucase o
[r`\p`, r`\p`],
[r`\p{`, r`\p{`],
[r`\u`, r`\u`],
[r`\u0`, r`\u0`],
[r`\u00`, r`\u00`],
[r`\u000`, r`\u000`],
[r`\u0000`, `\0`],
[r`\u{A0}`, `\u{A0}`],
[r`\x`, r`\x`],
[r`\x1`, `\x01`],
[r`\x7F`, `\x7F`],
[r`\x80`, `\x80`],
]);

async function compare(tests) {
let numSame = 0;
let numDiff = 0;
for (let i = 0; i < tests.length; i++) {
const [pattern, str] = tests[i];
const lib = transpiledRegExpResult(pattern, str);
const onig = await onigurumaResult(pattern, str);
const searched = `/${pattern}/ with str "${esc(str)}" (len ${str.length})`;
if (areMatchDetailsEqual(lib, onig)) {
const libMatch = transpiledRegExpResult(pattern, str);
const onigMatch = await onigurumaResult(pattern, str);
const searched = `${color('yellow', `/${pattern}/`)} with ${value(str)} ${color('gray', `(len ${str.length})`)}`;
if (areMatchDetailsEqual(libMatch, onigMatch)) {
numSame++;
ok(i, `Results matched for ${searched}${lib.error ? ` ${ansi.yellow}(both errored)${ansi.reset}` : ''}`);
let detail = '';
if (libMatch.error) {
detail = 'error';
} else if (libMatch.result === null) {
detail = 'no match';
}
ok(i, `Results same for ${searched}${
detail ? ` (${color('yellow', detail)})` : ''
}`);
continue;
}
numDiff++;
if (lib.error) {
err(i, `Only lib errored for ${searched}`);
} else if (onig.error) {
err(i, `Only onig errored for ${searched}`);
} else if (lib.result !== onig.result) {
err(i, `Results differed for ${searched}: lib: ${lib.result && `"${esc(lib.result)}"`}, onig: ${onig.result && `"${esc(onig.result)}"`}`);
} else if (lib.index !== onig.index) {
err(i, `Match positions differed for ${searched}: lib: ${lib.index}, onig: ${onig.index}`);
if (libMatch.error) {
err(i, `Only the library errored for ${searched}`);
} else if (onigMatch.error) {
err(i, `Only Oniguruma errored for ${searched}`);
} else if (libMatch.result !== onigMatch.result) {
err(i, `Results differed for ${searched}: lib: ${value(libMatch.result)}, onig: ${value(onigMatch.result)}`);
} else if (libMatch.index !== onigMatch.index) {
err(i, `Match positions differed for ${searched}: lib: ${value(libMatch.index)}, onig: ${value(onigMatch.index)}`);
}
}
numSame &&= `${ansi.green}${numSame}${ansi.reset}`;
numDiff &&= `${ansi.red}${numDiff}${ansi.reset}`;
numSame &&= `${color('green', numSame)}`;
numDiff &&= `${color('red', numDiff)}`;
console.log(`\nFinished: ${numSame} same, ${numDiff} different`);
}

function value(value) {
if (value === null) {
return color('gray', value);
}
if (typeof value === 'number') {
return color('blue', value);
}
if (typeof value === 'string') {
return color('cyan', `"${esc(value)}"`);
}
return String(value);
}

function esc(str) {
return str.
replace(/\n/g, '\\n').
Expand Down
4 changes: 2 additions & 2 deletions scripts/onig-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {areMatchDetailsEqual, err, ok, onigurumaResult, transpiledRegExpResult}

exec(process.argv.slice(2));

// Basic Oniguruma console-based tester that also does a comparison with Oniguruma-to-ES results
// Basic Oniguruma console-based tester that also does a comparison with Oniguruma-to-ES
async function exec([pattern, str]) {
if (!(typeof pattern === 'string' && typeof str === 'string')) {
err(null, 'pattern and str args expected');
Expand Down Expand Up @@ -45,7 +45,7 @@ async function exec([pattern, str]) {
err(null, 'Oniguruma and library results differed');
console.log('Library results:', libMatches);
} else {
ok(null, 'Oniguruma and library results matched');
ok(null, 'Results same for Oniguruma and library');
}
}
}
20 changes: 16 additions & 4 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ import {readFileSync} from 'node:fs';
import oniguruma from 'vscode-oniguruma';

const ansi = {
blue: '\x1b[34m',
cyan: '\x1b[36m',
gray: '\x1b[38;2;100;100;100m',
green: '\x1b[32m',
red: '\x1b[31m',
reset: '\x1b[0m',
yellow: '\x1b[33m',
yellow: '\x1b[38;2;253;182;0m',
};

/**
@param {keyof ansi} color
@param {string} str
@returns {string}
*/
function color(color, str) {
return `${ansi[color]}${str}${ansi.reset}`;
}

function ok(i, msg) {
console.log(`${i ? ` ${i}. ` : ''}${ansi.green}${ansi.reset} ${msg}`);
console.log(`${typeof i === 'number' ? ` ${i}. ` : ''}${color('green', '✅')} ${msg}`);
}

function err(i, msg) {
console.log(`${i ? ` ${i}. ` : ''}${ansi.red} ${msg}${ansi.reset}`);
console.log(`${typeof i === 'number' ? ` ${i}. ` : ''}${color('red', '❌')} ${msg}`);
}

/**
Expand Down Expand Up @@ -118,8 +130,8 @@ function areMatchDetailsEqual(a, b) {
}

export {
ansi,
areMatchDetailsEqual,
color,
err,
ok,
onigurumaResult,
Expand Down

0 comments on commit b133a78

Please sign in to comment.