#encoding: utf-8 require 'rspec' require 'asciimath' TEST_CASES = { 'x+b/(2a)<+-sqrt((b^2)/(4a^2)-c/a)' => { :mathml => 'x+b2a<±b24a2ca', :html => nil, }, 'a^2 + b^2 = c^2' => { :mathml => 'a2+b2=c2', :html => 'a2+b2=c2', }, 'x = (-b+-sqrt(b^2-4ac))/(2a)' => { :mathml => 'x=b±b2-4ac2a', :html => nil }, 'm = (y_2 - y_1)/(x_2 - x_1) = (Deltay)/(Deltax)' => { :mathml => 'm=y2y1x2x1=ΔyΔx', :html => 'm=y2y1x2x1=ΔyΔx' }, 'f\'(x) = lim_(Deltax->0)(f(x+Deltax)-f(x))/(Deltax)' => { :mathml => 'f\'(x)=limΔx0f(x+Δx)f(x)Δx', :html => 'f\'(x)=limΔx0f(x+Δx)f(x)Δx' }, 'd/dx [x^n] = nx^(n - 1)' => { :mathml => 'ddx[xn]=nxn1', :html => 'ddx[xn]=nxn1' }, 'int_a^b f(x) dx = [F(x)]_a^b = F(b) - F(a)' => { :mathml => 'abf(x)dx=[F(x)]ab=F(b)F(a)', :html => 'baf(x)dx=[F(x)]ba=F(b)F(a)' }, 'int_a^b f(x) dx = f(c)(b - a)' => { :mathml => 'abf(x)dx=f(c)(ba)', :html => 'baf(x)dx=f(c)(ba)' }, 'ax^2 + bx + c = 0' => { :mathml => 'ax2+bx+c=0', :html => 'ax2+bx+c=0' }, '"average value"=1/(b-a) int_a^b f(x) dx' => { :mathml => 'average value=1baabf(x)dx', :html => 'average value=1babaf(x)dx' }, 'd/dx[int_a^x f(t) dt] = f(x)' => { :mathml => 'ddx[axf(t)dt]=f(x)', :html => 'ddx[xaf(t)dt]=f(x)' }, 'hat(ab) bar(xy) ul(A) vec(v)' => { :mathml => 'ab^xy¯A_v', :html => '^(ab)¯(xy)(A)_(v)' }, 'z_12^34' => { :mathml => 'z1234', :html => 'z3412' }, 'lim_(x->c)(f(x)-f(c))/(x-c)' => { :mathml => 'limxcf(x)f(c)xc', :html => 'limxcf(x)f(c)xc' }, 'int_0^(pi/2) g(x) dx' => { :mathml => '0π2g(x)dx', :html => 'π20g(x)dx' }, 'sum_(n=0)^oo a_n' => { :mathml => 'n=0an', :html => 'n=0an' }, '((1,2,3),(4,5,6),(7,8,9))' => { :mathml => '(123456789)', :html => '(123456789)' }, '|(a,b),(c,d)|=ad-bc' => { :mathml => '|abcd|=adbc', :html => '|abcd|=adbc' }, '((a_(11), cdots , a_(1n)),(vdots, ddots, vdots),(a_(m1), cdots , a_(mn)))' => { :mathml => '(a11a1nam1amn)', :html => '(a11a1nam1amn)' }, 'sum_(k=1)^n k = 1+2+ cdots +n=(n(n+1))/2' => { :mathml => 'k=1nk=1+2++n=n(n+1)2', :html => 'nk=1k=1+2++n=n(n+1)2' }, '"Скорость"=("Расстояние")/("Время")' => { :mathml => 'Скорость=РасстояниеВремя', :html => 'Скорость=РасстояниеВремя' }, 'bb (a + b) + cc c = fr (d^n)' => { :mathml => 'a+b+c=dn', :html => nil }, 'max()' => { :mathml => 'max()', :html => 'max()' }, 'text("foo")' => { :mathml => '"foo"', :html => '"foo"' }, 'ubrace(1 + 2) obrace(3 + 4' => { :mathml => '1+23+4' } } 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):}', 'abcd' end end