// Runs MathCAT's own Nemeth and UEB braille tests against a build of this module. // // git clone --branch v0.7.7 https://github.com/daisy/MathCAT // node wasm-src/mathcat/suite.mjs vendor/local/mathcat@1 // // Every test_braille("Nemeth" | "UEB", expr, "braille") call in // /tests/braille/{Nemeth,UEB}/*.rs is taken with the MathML its `expr` // holds at that point, and the module's braille() must return exactly the // expected string. Tests marked #[ignore] (known failures upstream) and the few // that set extra preferences (test_braille_prefs) are skipped. import { readFileSync, readdirSync } from 'node:fs'; import { join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; const [repo, moduleDir] = process.argv.slice(2); if (!repo || !moduleDir) { console.error('usage: node suite.mjs '); process.exit(2); } function unescapeRust(s) { return s .replace(/\\\r?\n\s*/g, '') .replace(/\\u\{([0-9a-fA-F]+)\}/g, (_, h) => String.fromCodePoint(parseInt(h, 16))) .replace(/\\(.)/g, (_, c) => ({ n: '\n', t: '\t', r: '\r', 0: '\0' })[c] ?? c); } /** The Rust string literal starting at s[i], as [value, end], or null. */ function literal(s, i) { const raw = /^r(#*)"/.exec(s.slice(i)); if (raw) { const close = '"' + raw[1]; const start = i + raw[0].length; const end = s.indexOf(close, start); return [s.slice(start, end), end + close.length]; } if (s[i] !== '"') return null; let j = i + 1; while (s[j] !== '"') j += s[j] === '\\' ? 2 : 1; return [unescapeRust(s.slice(i + 1, j)), j + 1]; } const cases = []; for (const code of ['Nemeth', 'UEB']) { const dir = join(repo, 'tests', 'braille', code); for (const file of readdirSync(dir).filter((f) => f.endsWith('.rs'))) { const src = readFileSync(join(dir, file), 'utf8').replace(/\r\n/g, '\n'); for (const block of src.split(/#\[test\]/).slice(1)) { if (/^\s*#\[ignore/.test(block)) continue; const body = block.replace(/^\s*\/\/.*$/gm, ''); const name = /fn\s+(\w+)/.exec(body)?.[1]; const vars = {}; const re = /let\s+(\w+)\s*=\s*|test_braille\(\s*"(\w+)"\s*,\s*(\w+)\s*,\s*/g; let m; while ((m = re.exec(body))) { const lit = literal(body, m.index + m[0].length); if (m[1]) { if (lit) vars[m[1]] = lit[0]; } else if (lit && m[3] in vars) { cases.push({ where: `${code}/${file} ${name}`, code: m[2], mathml: vars[m[3]], braille: lit[0] }); } } } } } const mod = await import(pathToFileURL(resolve(moduleDir, 'mathcat.js')).href); mod.initSync({ module: readFileSync(resolve(moduleDir, 'mathcat_bg.wasm')) }); let failed = 0; for (const c of cases) { let got; try { got = mod.braille(c.mathml, c.code); } catch (e) { got = `error: ${String(e).split('\n')[0]}`; } if (got !== c.braille) { failed++; console.log(`FAIL ${c.where}\n want ${c.braille}\n got ${got}`); } } console.log(`MathCAT ${mod.version()}: ${cases.length - failed} of ${cases.length} braille tests pass`); process.exit(failed ? 1 : 0);