#encoding: utf-8
require 'rspec'
require 'asciimath'
TEST_CASES = {
'x+b/(2a)<+-sqrt((b^2)/(4a^2)-c/a)' =>
{
:mathml => '',
:html => nil,
},
'a^2 + b^2 = c^2' =>
{
:mathml => '',
:html => 'a2舅+b2舅=c2舅',
},
'x = (-b+-sqrt(b^2-4ac))/(2a)' =>
{
:mathml => '',
:html => nil
},
'm = (y_2 - y_1)/(x_2 - x_1) = (Deltay)/(Deltax)' =>
{
:mathml => '',
:html => 'm=舅y舅2−y舅1x舅2−x舅1=舅ΔyΔx'
},
'f\'(x) = lim_(Deltax->0)(f(x+Deltax)-f(x))/(Deltax)' =>
{
:mathml => '',
:html => 'f\'(x)=舅舅limΔx→0舅f(x+Δx)−f(x)Δx'
},
'd/dx [x^n] = nx^(n - 1)' =>
{
:mathml => '',
:html => '舅ddx[xn舅]=nxn−1舅'
},
'int_a^b f(x) dx = [F(x)]_a^b = F(b) - F(a)' =>
{
:mathml => '',
:html => '∫baf(x)dx=[F(x)]ba=F(b)−F(a)'
},
'int_a^b f(x) dx = f(c)(b - a)' =>
{
:mathml => '',
:html => '∫baf(x)dx=f(c)(b−a)'
},
'ax^2 + bx + c = 0' =>
{
:mathml => '',
:html => 'ax2舅+bx+c=0'
},
'"average value"=1/(b-a) int_a^b f(x) dx' =>
{
:mathml => '',
:html => 'average value=舅1b−a∫baf(x)dx'
},
'd/dx[int_a^x f(t) dt] = f(x)' =>
{
:mathml => '',
:html => '舅ddx[∫xaf(t)dt]=f(x)'
},
'hat(ab) bar(xy) ul(A) vec(v)' =>
{
:mathml => '',
:html => '舅^(ab)舅舅¯(xy)舅舅舅(A)_舅→(v)舅'
},
'z_12^34' =>
{
:mathml => '',
:html => 'z3412'
},
'lim_(x->c)(f(x)-f(c))/(x-c)' =>
{
:mathml => '',
:html => '舅舅limx→c舅f(x)−f(c)x−c'
},
'int_0^(pi/2) g(x) dx' =>
{
:mathml => '',
:html => '∫舅π20g(x)dx'
},
'sum_(n=0)^oo a_n' =>
{
:mathml => '',
:html => '舅∞∑n=0a舅n'
},
'((1,2,3),(4,5,6),(7,8,9))' =>
{
:mathml => '',
:html => '(123456789)'
},
'|(a,b),(c,d)|=ad-bc' =>
{
:mathml => '',
:html => '|abcd|=ad−bc'
},
'((a_(11), cdots , a_(1n)),(vdots, ddots, vdots),(a_(m1), cdots , a_(mn)))' =>
{
:mathml => '',
:html => '(a舅11⋯a舅1n⋮⋱⋮a舅m1⋯a舅mn)'
},
'sum_(k=1)^n k = 1+2+ cdots +n=(n(n+1))/2' =>
{
:mathml => '',
:html => '舅n∑k=1k=1+2+⋯+n=舅n(n+1)2'
},
'"Скорость"=("Расстояние")/("Время")' =>
{
:mathml => '',
:html => 'Скорость=舅РасстояниеВремя'
},
'bb (a + b) + cc c = fr (d^n)' =>
{
:mathml => '',
:html => nil
},
'max()' =>
{
:mathml => '',
:html => 'max()'
},
'text("foo")' => {
:mathml => '',
:html => '"foo"'
},
'ubrace(1 + 2) obrace(3 + 4' => {
:mathml => ''
}
}
version = RUBY_VERSION.split('.').map { |s| s.to_i }
if version[0] > 1 || version[1] > 8
TEST_CASES['Скорость=(Расстояние)/(Время)'] =
{
:mathml => '',
:html => 'Скорость=舅РасстояниеВремя'
}
end
module AsciiMathHelper
def expect_mathml(asciimath, mathml)
expect(AsciiMath.parse(asciimath).to_mathml).to eq(mathml)
end
def expect_html(asciimath, html)
expect(AsciiMath.parse(asciimath).to_html).to eq(html)
end
end
RSpec.configure do |c|
c.include AsciiMathHelper
end
describe "AsciiMath::MathMLBuilder" do
TEST_CASES.each_pair do |asciimath, output|
it "should produce identical output to asciimathml.js for '#{asciimath}'" do
expect_mathml(asciimath, output[:mathml])
end
if output[:html]
it "should produce html that looks like the output from asciimathml.js for '#{asciimath}'" do
expect_html(asciimath, output[:html])
end
end
end
it 'should not generate mo elements for {: and :}' do
expect_mathml '{:(a,b),(c,d):}', ''
end
end