require "spec_helper" RSpec.describe Asciimath2UnitsML do it "converts an AsciiMath string to MathML + UnitsML" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg*s^-2)" xx 9 "unitsml(g)" INPUT 1 kg · s 2 kg*s^-2 kg·s −2 kg · s 2 kilo k × 9 g gram g g OUTPUT end it "deals with non-metric" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(hp)" INPUT 1 hp horsepower hp hp OUTPUT end it "deals with duplicate units" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg*s^-2)" xx 9 "unitsml(kg*s^-2)" INPUT 1 kg · s 2 kg*s^-2 kg·s −2 kg · s 2 kilo k × 9 kg · s 2 kg*s^-2 kg·s −2 kg · s 2 kilo k OUTPUT end =begin it "deals with notational variants" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(degK)" + 10 "unitsml(K)" INPUT OUTPUT end =end it "deals with units division" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(C^3*A)" + 13 "unitsml(A/C^-3)" INPUT 9 C 3 · A C^3*A C 3 ·A C 3 · A + 13 A / C 3 A*C^3 A/C −3 A / C 3 OUTPUT end it "converts MathML to MatML + UnitsML" do input = <<~INPUT 32 + 5 × 7 unitsml(kg^-2) INPUT output = <<~OUTPUT 32 + 5 × 7 kg 2 kg^-2 kg −2 kg 2 kilo k OUTPUT expect(xmlpp(Asciimath2UnitsML::Conv.new().MathML2UnitsML(input).to_xml)).to be_equivalent_to xmlpp(output) expect(xmlpp(Asciimath2UnitsML::Conv.new().MathML2UnitsML(Nokogiri::XML(input)).to_xml)).to be_equivalent_to xmlpp(output) end it "raises error for illegal unit" do expect{xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))}.to raise_error(Rsec::SyntaxError) 12 "unitsml(que?)" INPUT end it "initialises multiplier" do expect(xmlpp(Asciimath2UnitsML::Conv.new(multiplier: "\u00d7").Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg*s^-2)" INPUT 1 kg × s 2 kg*s^-2 kg×s −2 kg × s 2 kilo k OUTPUT expect(xmlpp(Asciimath2UnitsML::Conv.new(multiplier: :space).Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg*s^-2)" INPUT 1 kg s 2 kg*s^-2 kg s −2 kg s 2 kilo k OUTPUT expect(xmlpp(Asciimath2UnitsML::Conv.new(multiplier: :nospace).Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg*s^-2)" INPUT 1 kg s 2 kg*s^-2 kgs −2 kg s 2 kilo k OUTPUT end end