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(mm*s^-2)" INPUT 1 mm · s 2 mm*s^-2 mm·s −2 mm · s 2 milli m m m m OUTPUT end it "deals with non-Ascii units and prefixes" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(um)" INPUT 1 µm um um µm micro u μ $mu$ µ OUTPUT end it "does not insert space before non-alphabetic units" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(degK)" + 1 "unitsml(prime)" INPUT 1 °K kelvin K K thermodynamic temperature + 1 minute (minute of arc) plane angle angle OUTPUT end it "deals with sqrt units" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(sqrt(Hz))" INPUT 1 Hz Hz^0.5 √Hz Hz OUTPUT end it "deals with kg and g" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 1 "unitsml(kg)" + 1 "unitsml(g)" INPUT 1 kg kilogram kg kg kilo k k k k mass + 1 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 power 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 k k k × 9 kg · s 2 OUTPUT end 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 9 °K kelvin K K thermodynamic temperature + 10 K OUTPUT end it "deals with prefixed units" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(mbar)" INPUT 9 mbar millibar mbar mbar milli m m m m OUTPUT end it "deals with standalone prefixes" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) "unitsml(p-)" "unitsml(da-)" INPUT p pico p p p p da deka da da da da OUTPUT end it "deals with HTML entities in UnitsDB" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) "unitsml(u-)" + "unitsml(um)" INPUT µ micro u μ $mu$ µ mass + µm um um µm OUTPUT end it "deals with units division" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(A*C^3)" + 13 "unitsml(A/C^-3)" INPUT 9 A · C 3 A*C^3 A·C 3 A · C 3 + 13 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 k k 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 k k 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 k k 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 k k k OUTPUT end it "deals with dimension decomposition with like units" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(mW*cm^(-2))" INPUT 9 mW · cm 2 mW*cm^-2 mW·cm −2 mW · cm 2 milli m m m m centi c c c c OUTPUT end it "deals with quantity input" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(m, quantity: NISTq103)" INPUT 9 m meter m m position vector OUTPUT end it "deals with name input" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(cal_th/cm^2, name: langley)" INPUT 9 cal th / cm 2 langley cal th ·cm −2 cal th · cm 2 centi c c c c OUTPUT end it "deals with symbol input" do expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT) 9 "unitsml(m, symbol: La)" + 10 "unitsml(cm*s^-2, symbol: cm cdot s^-2)" INPUT 9 L a meter m m + 10 c m s 2 cm*s^-2 cm·s −2 cm · s 2 centi c c c c OUTPUT end end