-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing of special unit formats (#277)
* 5' 3/4" doesn't parse correctly Fixes #276 - update the regex used for feet-inch notation to handle signed ints, and rational parts - handle negative lbs/oz and stone-lbs forms with fractional secondary units - Handle a bunch of parsing corner cases. WARNING: the time format HH::mm:ss,nsec is gone now in favor of HH::mm:ss.SSS which better lines up with how times are parsed by Time class. * tweak special formats so we generate improper fractions for inches and ounces, etc.. This is more in line with how these formats are used in practice. WARNING: If you want precision, don't use these formats.
- Loading branch information
Showing
5 changed files
with
258 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe 'Number parsing' do | ||
context 'with Integers' do | ||
it { expect(RubyUnits::Unit.new('1')).to have_attributes(scalar: 1) } | ||
it { expect(RubyUnits::Unit.new('-1')).to have_attributes(scalar: -1) } | ||
it { expect(RubyUnits::Unit.new('+1')).to have_attributes(scalar: 1) } | ||
it { expect(RubyUnits::Unit.new('01')).to have_attributes(scalar: 1) } | ||
it { expect(RubyUnits::Unit.new('1,000')).to have_attributes(scalar: 1000) } | ||
it { expect(RubyUnits::Unit.new('1_000')).to have_attributes(scalar: 1000) } | ||
end | ||
|
||
context 'with Decimals' do | ||
# NOTE: that since this float is the same as an integer, the integer is returned | ||
it { expect(RubyUnits::Unit.new('1.0').scalar).to be(1) } | ||
it { expect(RubyUnits::Unit.new('-1.0').scalar).to be(-1) } | ||
|
||
it { expect(RubyUnits::Unit.new('1.1').scalar).to be(1.1) } | ||
it { expect(RubyUnits::Unit.new('-1.1').scalar).to be(-1.1) } | ||
it { expect(RubyUnits::Unit.new('+1.1').scalar).to be(1.1) } | ||
it { expect(RubyUnits::Unit.new('0.1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('-0.1').scalar).to be(-0.1) } | ||
it { expect(RubyUnits::Unit.new('+0.1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('.1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('-.1').scalar).to be(-0.1) } | ||
it { expect(RubyUnits::Unit.new('+.1').scalar).to be(0.1) } | ||
|
||
it { expect { RubyUnits::Unit.new('0.1.') }.to raise_error(ArgumentError) } | ||
it { expect { RubyUnits::Unit.new('-0.1.') }.to raise_error(ArgumentError) } | ||
it { expect { RubyUnits::Unit.new('+0.1.') }.to raise_error(ArgumentError) } | ||
end | ||
|
||
context 'with Fractions' do | ||
it { expect(RubyUnits::Unit.new('1/1').scalar).to be(1) } | ||
it { expect(RubyUnits::Unit.new('-1/1').scalar).to be(-1) } | ||
it { expect(RubyUnits::Unit.new('+1/1').scalar).to be(1) } | ||
|
||
# NOTE: eql? is used here because two equivalent Rational objects are not the same object, unlike Integers | ||
it { expect(RubyUnits::Unit.new('1/2').scalar).to eql(1/2r) } | ||
it { expect(RubyUnits::Unit.new('-1/2').scalar).to eql(-1/2r) } | ||
it { expect(RubyUnits::Unit.new('+1/2').scalar).to eql(1/2r) } | ||
it { expect(RubyUnits::Unit.new('(1/2)').scalar).to eql(1/2r) } | ||
it { expect(RubyUnits::Unit.new('(-1/2)').scalar).to eql(-1/2r) } | ||
it { expect(RubyUnits::Unit.new('(+1/2)').scalar).to eql(1/2r) } | ||
|
||
# improper fractions | ||
it { expect(RubyUnits::Unit.new('1 1/2').scalar).to eql(3/2r) } | ||
it { expect(RubyUnits::Unit.new('-1 1/2').scalar).to eql(-3/2r) } | ||
it { expect(RubyUnits::Unit.new('+1 1/2').scalar).to eql(3/2r) } | ||
it { expect(RubyUnits::Unit.new('1-1/2').scalar).to eql(3/2r) } | ||
it { expect(RubyUnits::Unit.new('-1-1/2').scalar).to eql(-3/2r) } | ||
it { expect(RubyUnits::Unit.new('+1-1/2').scalar).to eql(3/2r) } | ||
it { expect(RubyUnits::Unit.new('1 2/2').scalar).to be(2) } # weird, but not wrong | ||
it { expect(RubyUnits::Unit.new('1 3/2').scalar).to eql(5/2r) } # weird, but not wrong | ||
it { expect { RubyUnits::Unit.new('1.5 1/2') }.to raise_error(ArgumentError, 'Improper fractions must have a whole number part') } | ||
it { expect { RubyUnits::Unit.new('1.5/2') }.to raise_error(ArgumentError, 'invalid value for Integer(): "1.5"') } | ||
it { expect { RubyUnits::Unit.new('1/2.5') }.to raise_error(ArgumentError, 'invalid value for Integer(): "2.5"') } | ||
end | ||
|
||
context 'with Scientific Notation' do | ||
it { expect(RubyUnits::Unit.new('1e0').scalar).to be(1) } | ||
it { expect(RubyUnits::Unit.new('-1e0').scalar).to be(-1) } | ||
it { expect(RubyUnits::Unit.new('+1e0').scalar).to be(1) } | ||
it { expect(RubyUnits::Unit.new('1e1').scalar).to be(10) } | ||
it { expect(RubyUnits::Unit.new('-1e1').scalar).to be(-10) } | ||
it { expect(RubyUnits::Unit.new('+1e1').scalar).to be(10) } | ||
it { expect(RubyUnits::Unit.new('1e-1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('-1e-1').scalar).to be(-0.1) } | ||
it { expect(RubyUnits::Unit.new('+1e-1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('1E+1').scalar).to be(10) } | ||
it { expect(RubyUnits::Unit.new('-1E+1').scalar).to be(-10) } | ||
it { expect(RubyUnits::Unit.new('+1E+1').scalar).to be(10) } | ||
it { expect(RubyUnits::Unit.new('1E-1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('-1E-1').scalar).to be(-0.1) } | ||
it { expect(RubyUnits::Unit.new('+1E-1').scalar).to be(0.1) } | ||
it { expect(RubyUnits::Unit.new('1.0e2').scalar).to be(100) } | ||
it { expect(RubyUnits::Unit.new('.1e2').scalar).to be(10) } | ||
it { expect(RubyUnits::Unit.new('0.1e2').scalar).to be(10) } | ||
it { expect { RubyUnits::Unit.new('0.1e2.5') }.to raise_error(ArgumentError) } | ||
end | ||
|
||
context 'with Complex numbers' do | ||
it { expect(RubyUnits::Unit.new('1+1i').scalar).to eql(Complex(1, 1)) } | ||
it { expect(RubyUnits::Unit.new('1i').scalar).to eql(Complex(0, 1)) } | ||
it { expect(RubyUnits::Unit.new('-1i').scalar).to eql(Complex(0, -1)) } | ||
it { expect(RubyUnits::Unit.new('-1+1i').scalar).to eql(Complex(-1, 1)) } | ||
it { expect(RubyUnits::Unit.new('+1+1i').scalar).to eql(Complex(1, 1)) } | ||
it { expect(RubyUnits::Unit.new('1-1i').scalar).to eql(Complex(1, -1)) } | ||
it { expect(RubyUnits::Unit.new('-1.23-4.5i').scalar).to eql(Complex(-1.23, -4.5)) } | ||
it { expect(RubyUnits::Unit.new('1+0i').scalar).to be(1) } | ||
end | ||
end |
Oops, something went wrong.