Files
Netcatty/domain/latexToMathML.test.ts
T
2026-08-22 19:32:34 +08:00

81 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from "node:test";
import assert from "node:assert/strict";
import { latexToMathML, tokenizeLatex } from "./latexToMathML.ts";
test("tokenizeLatex tokenizes basic commands and superscripts", () => {
const tokens = tokenizeLatex("E = mc^2");
assert.equal(tokens[0].type, "char");
assert.equal(tokens[0].value, "E");
assert.equal(tokens[1].type, "symbol");
assert.equal(tokens[1].value, "=");
assert.equal(tokens[2].type, "char");
assert.equal(tokens[2].value, "m");
assert.equal(tokens[3].type, "char");
assert.equal(tokens[3].value, "c");
assert.equal(tokens[4].type, "sup");
assert.equal(tokens[5].type, "number");
assert.equal(tokens[5].value, "2");
});
test("latexToMathML converts E = mc^2 correctly", () => {
const mathml = latexToMathML("E = mc^2");
assert.ok(mathml.includes("<math"));
assert.ok(mathml.includes("<msup>"));
assert.ok(mathml.includes("<mn>2</mn>"));
});
test("latexToMathML converts fractions and square roots", () => {
const mathml = latexToMathML("\\frac{a}{b} + \\sqrt{x}");
assert.ok(mathml.includes("<mfrac>"));
assert.ok(mathml.includes("<msqrt>"));
});
test("latexToMathML converts Greek letters and operators", () => {
const mathml = latexToMathML("\\sum_{i=1}^n \\alpha_i \\times \\beta_i");
assert.ok(mathml.includes("∑"));
assert.ok(mathml.includes("α"));
assert.ok(mathml.includes("β"));
assert.ok(mathml.includes("×"));
});
test("latexToMathML converts matrices", () => {
const mathml = latexToMathML("\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}");
assert.ok(mathml.includes("<mtable>"));
assert.ok(mathml.includes("<mtr>"));
assert.ok(mathml.includes("<mtd>"));
});
test("latexToMathML converts n-th root \\sqrt[3]{3} correctly using mroot", () => {
const mathml = latexToMathML("\\sqrt[3]{3}");
assert.ok(mathml.includes("<mroot>"));
assert.ok(mathml.includes("<mn>3</mn>"));
});
test("latexToMathML converts binomial and overline", () => {
const mathml = latexToMathML("\\binom{n}{k} + \\overline{z}");
assert.ok(mathml.includes("linethickness=\"0\""));
assert.ok(mathml.includes("<mover>"));
});
test("latexToMathML converts \\textstyle, \\displaystyle, and script styles", () => {
const textstyleMath = latexToMathML("\\textstyle \\sum_{i=1}^n x_i");
assert.ok(textstyleMath.includes("<mstyle displaystyle=\"false\" scriptlevel=\"0\">"));
assert.ok(textstyleMath.includes("∑"));
const groupedTextstyle = latexToMathML("{\\textstyle \\frac{1}{2}} + \\frac{3}{4}");
assert.ok(groupedTextstyle.includes("<mstyle displaystyle=\"false\" scriptlevel=\"0\"><mfrac>"));
const displaystyleMath = latexToMathML("\\displaystyle \\int_0^1 f(x) dx");
assert.ok(displaystyleMath.includes("<mstyle displaystyle=\"true\" scriptlevel=\"0\">"));
const scriptstyleMath = latexToMathML("\\scriptstyle a + b");
assert.ok(scriptstyleMath.includes("<mstyle displaystyle=\"false\" scriptlevel=\"1\">"));
});
test("latexToMathML converts text formatting commands", () => {
const mathml = latexToMathML("\\text{hello world} + \\textbf{bold} + \\textit{italic}");
assert.ok(mathml.includes("<mtext>hello world</mtext>"));
assert.ok(mathml.includes("<mtext mathvariant=\"bold\">bold</mtext>"));
assert.ok(mathml.includes("<mtext mathvariant=\"italic\">italic</mtext>"));
});