require "spec_helper"
RSpec.describe Asciimath2UnitsML do
it "converts an AsciiMath string to MathML + UnitsML" do
input = <<~INPUT
1 "unitsml(mm*s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
OUTPUT
end
it "deals with non-Ascii units and prefixes" do
input = <<~INPUT
1 "unitsml(um)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1µmumumµmmicrouμ$mu$µ
OUTPUT
end
it "does not insert space before non-alphabetic units" do
input = <<~INPUT
1 "unitsml(degK)" + 1 "unitsml(prime)" + ii(theta) = s//r "unitsml(rad)" + 10^(12) "unitsml(Hz)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1°KkelvinKKthermodynamic temperature+1′minute (minute of arc)′′plane angleangle+θ=s/rradradianradrad+1012HzhertzHzHzfrequency
OUTPUT
end
it "does not insert space before operators" do
input = <<~INPUT
8 "unitsml(kg)" cdot "unitsml(m)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
8kgkilogramkgkgkilokkkkmass⋅mmetermm
OUTPUT
end
it "deals with sqrt units" do
input = <<~INPUT
1 "unitsml(sqrt(Hz))"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1HzHz^0.5√HzHz
OUTPUT
end
it "deals with kg and g" do
input = <<~INPUT
1 "unitsml(kg)" + 1 "unitsml(g)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1kgkilogramkgkgkilokkkkmass+1ggramgg
OUTPUT
end
it "deals with non-metric" do
input = <<~INPUT
1 "unitsml(hp)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1hphorsepowerhphppower
OUTPUT
end
it "deals with duplicate units" do
input = <<~INPUT
1 "unitsml(kg*s^-2)" xx 9 "unitsml(kg*s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
1kg⋅s−2kg*s^-2
kg⋅s
−2kg⋅s−2kilokkkk×9kg⋅s−2
OUTPUT
end
it "deals with parentheses" do
input = <<~INPUT
10 "unitsml(K/(kg*m))"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
10K/kg⋅mK*kg^-1*m^-1
K⋅kg
−1
⋅m
−1K⋅kg−1⋅m−1kilokkkk
OUTPUT
end
it "deals with notational variants" do
input = <<~INPUT
9 "unitsml(degK)" + 10 "unitsml(K)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9°KkelvinKKthermodynamic temperature+10K
OUTPUT
end
it "deals with prefixed units" do
input = <<~INPUT
9 "unitsml(mbar)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9mbarmillibarmbarmbarmillimmmm
OUTPUT
end
it "deals with standalone prefixes" do
input = <<~INPUT
"unitsml(p-)" "unitsml(da-)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
ppicoppppdadekadadadada
OUTPUT
end
it "deals with HTML entities in UnitsDB" do
input = <<~INPUT
"unitsml(u-)" + "unitsml(um)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
µmicrouμ$mu$µmass+µmumumµm
OUTPUT
end
it "deals with units division" do
input = <<~INPUT
9 "unitsml(A*C^3)" + 13 "unitsml(A/C^-3)" + 2 "unitsml(J/kg*K)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9A⋅C3A*C^3
A⋅C
3A⋅C3+13A/C−3+2J/kg⋅Kjoule per kilogram kelvin
J⋅kg
−1
⋅K
−1J⋅kg−1⋅K−1kilokkkk
OUTPUT
end
it "converts MathML to MatML + UnitsML" do
input = <<~INPUT
32+5×7unitsml(kg^-2)
INPUT
output = <<~OUTPUT
32+5×7kg−2kg^-2
kg
−2kg−2kilokkkk
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
input = <<~INPUT
12 "unitsml(que?)"
INPUT
expect { xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)) }
.to raise_error(Rsec::SyntaxError)
end
it "initialises multiplier" do
input = <<~INPUT
1 "unitsml(kg*s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv
.new(multiplier: "\u00d7")
.Asciimath2UnitsML(input))).to be_equivalent_to xmlpp(<<~OUTPUT)
1kg×s−2kg*s^-2
kg×s
−2kg×s−2kilokkkk
OUTPUT
input = <<~INPUT
1 "unitsml(kg*s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv
.new(multiplier: :space)
.Asciimath2UnitsML(input))).to be_equivalent_to xmlpp(<<~OUTPUT)
1kgs−2kg*s^-2
kg s
−2kgs−2kilokkkk
OUTPUT
input = <<~INPUT
1 "unitsml(kg*s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv
.new(multiplier: :nospace)
.Asciimath2UnitsML(input))).to be_equivalent_to xmlpp(<<~OUTPUT)
1kgs−2kg*s^-2
kgs
−2kgs−2kilokkkk
OUTPUT
end
it "deals with dimension decomposition with like units" do
input = <<~INPUT
9 "unitsml(mW*cm^(-2))"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9mW⋅cm−2mW*cm^-2
mW⋅cm
−2mW⋅cm−2millimmmmcenticccc
OUTPUT
end
it "deals with quantity input" do
input = <<~INPUT
9 "unitsml(m, quantity: NISTq103)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9mmetermmposition vector
OUTPUT
end
it "deals with name input" do
input = <<~INPUT
9 "unitsml(cal_th/cm^2, name: langley)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9calth/cm2langley
cal
th
⋅cm
−2calth⋅cm−2centicccc
OUTPUT
end
it "deals with symbol input" do
input = <<~INPUT
9 "unitsml(m, symbol: La)" + 10 "unitsml(cm*s^-2, symbol: cm cdot s^-2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
9Lametermm+10cm⋅s−2cm*s^-2
cm⋅s
−2cm⋅s−2centicccc
OUTPUT
end
it "deals with multiplier input" do
input = <<~INPUT
10 "unitsml(cm*s^-2, multiplier: xx)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
10cmxxs−2cm*s^-2
cm⋅s
−2cm⋅s−2centicccc
OUTPUT
end
it "deals with dimensions" do
input = <<~INPUT
"unitsml(dim_Theta*dim_L^2)"
INPUT
expect(xmlpp(Asciimath2UnitsML::Conv.new.Asciimath2UnitsML(input)))
.to be_equivalent_to xmlpp(<<~OUTPUT)
Θ⋅L2
OUTPUT
end
end