// ============================================================================= // Hilbert.js | MathML Tests // (c) Mathigon // ============================================================================= import tape from 'tape'; import {Expression} from '../src'; import {ExprString, MathMLArgument} from '../src/elements'; const expr = (src: string) => Expression.parse(src); const mathML = (src: string, replace = {}) => expr(src).toMathML(replace); tape('Basic', (test) => { test.equal(mathML('42'), '42'); test.equal(mathML('3.141592654'), '3.141592654'); test.equal(mathML('x'), 'x'); test.equal(mathML('xy'), 'xy'); test.equal(mathML('log'), 'log'); test.equal(mathML('x y'), 'xy'); test.equal(mathML('+'), '+'); test.equal(mathML('-'), ''); test.equal(mathML('1+1 = 2'), '1+1=2'); test.equal(mathML('3 - 2=1'), '32=1'); test.end(); }); tape('HTML Characters', (test) => { test.equal(mathML('a < b'), 'a<b'); test.equal(mathML('a > b'), 'a>b'); test.equal(mathML('a ≥ b'), 'ab'); test.end(); }); tape('SupSub', (test) => { test.equal(mathML('a^2+1'), 'a2+1'); test.equal(mathML('a^(2+1)'), 'a2+1'); test.equal(mathML('a_2+1'), 'a2+1'); test.equal(mathML('a_(2+1)'), 'a2+1'); test.equal(mathML('a^2_1'), 'a12'); test.equal(mathML('a_1^2'), 'a12'); test.equal(mathML('(a_1)^2'), 'a12'); test.equal(mathML('(a^2)_1'), 'a21'); test.end(); }); tape('Custom Functions', (test) => { const options = { a: (a: MathMLArgument) => `${a}`, b: (...b: MathMLArgument[]) => `${b.join(',')}`, c: (c: MathMLArgument) => `${c}` }; test.equal(mathML('a(1)', options), '1'); test.equal(mathML('b(1,2)', options), '1,2'); test.equal(mathML('c("text")', options), 'text'); test.end(); }); tape('Whitespace', (test) => { test.equal(mathML('a b'), 'ab'); test.equal(mathML('a b'), 'ab'); test.end(); }); tape('Functions', function(test) { test.equal(mathML('sin(a + b)'), 'sina+b'); test.equal(mathML('tan = sin/cos'), 'tan=sincos'); test.equal(mathML('sinh(x) = (e^x - e^(-x))/2'), 'sinhx=exex2'); test.equal(mathML('ln(x^2) = 2 ln(x)'), 'lnx2=2lnx'); test.equal(mathML('ln(x/y) = ln(x) - ln(y)'), 'lnxy=lnxlny'); test.equal(mathML('a^(p-1) == 1'), 'ap11'); // test.equal(mathML('log_b(x) = log_k(x)/log_k(b)'), 'xxx'); // TODO Support functions with subscripts test.end(); }); tape('Fractions', (test) => { test.equal(mathML('a/b'), 'ab'); test.equal(mathML('a//b'), 'a/b'); test.equal(mathML('(a+b)/(c+d)'), 'a+bc+d'); test.equal(mathML('phi = 1 + 1/(1 + 1/(1 + 1/(1 + 1/(1 + …))))'), 'φ=1+11+11+11+11+'); test.end(); }); tape('Super and Subscripts', (test) => { test.equal(mathML('a_i'), 'ai'); test.equal(mathML('a^2'), 'a2'); test.equal(mathML('a^2 + b^2 = c^2'), 'a2+b2=c2'); // test.equal(mathML('2^2^2^2'), '2222'); // TODO Right-to-left // test.equal(mathML('a_i^2'), 'ai2'); // TODO Support Super and Subscripts // test.equal(mathML('a^2_i'), 'ai2'); // TODO Support Super and Subscripts test.end(); }); tape('Roots', (test) => { test.equal(mathML('sqrt(x)'), 'x'); test.equal(mathML('root(x, 3)'), 'x3'); test.equal(mathML('sqrt(2) ~~ 1.414213562'), '21.414213562'); test.equal(mathML('x = (-b +- sqrt(b^2 - 4a c)) / (2a)'), 'x=b±b24ac2a'); test.equal(mathML('phi = (1 + sqrt(5))/2'), 'φ=1+52'); test.equal(mathML( 'sqrt(1 + sqrt(1 + sqrt(1 + sqrt(1 + sqrt(1 + sqrt(1 + sqrt(1 + …)))))))'), '1+1+1+1+1+1+1+'); test.end(); }); tape('Groupings', (test) => { test.equal(mathML('(a+b)'), 'a+b'); test.equal(mathML('{a+b}'), 'a+b'); test.equal(mathML('abs(a+b)'), 'a+b'); test.equal(mathML('a,b,c'), 'a,b,c'); test.equal(mathML('(a,b,c)'), 'a,b,c'); test.equal(mathML('(x+y)(x-y) = x^2-y^2'), 'x+yxy=x2y2'); test.equal(mathML('e^(-x)'), 'ex'); test.equal(mathML('e^(i tau) = 1'), 'eiτ=1'); test.end(); });