Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ def self.parse_into_numbers_and_units(string)
rational = %r{[+-]?\d+\/\d+}
# complex numbers... -1.2+3i, +1.2-3.3i
complex = %r{#{sci}{2,2}i}
anynumber = %r{(?:(#{complex}|#{rational}|#{sci})\b)?\s?([\D].*)?}
anynumber = %r{(?:(#{complex}|#{rational}|#{sci})\b)?\s?([^\d\.].*)?}
num, unit = string.scan(anynumber).first

return [case num
Expand Down Expand Up @@ -1555,4 +1555,4 @@ def self.use_definition(definition)
end
end

end
end
3 changes: 2 additions & 1 deletion test/test_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'test_helper'
require 'rubygems'
require 'test/unit'
require 'ruby-units'
Expand All @@ -23,4 +24,4 @@ def test_get_cache
assert_equal("m".unit, Unit::Cache.get['m'])
end

end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
47 changes: 30 additions & 17 deletions test/test_ruby-units.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'test_helper'
require 'rubygems'
require 'test/unit'
require 'ruby-units'
Expand All @@ -14,10 +15,14 @@
end


class Unit < Numeric
@@USER_DEFINITIONS = {'<inchworm>' => [%w{inworm inchworm}, 0.0254, :length, %w{<meter>} ],
'<habenero>' => [%w{degH}, 100, :temperature, %w{<celsius>}]}
Unit.setup
Unit.define("inchworm") do |inchworm|
inchworm.definition = Unit("1 m") * 0.0254
inchworm.aliases = %w{inworm inchworm}
end

Unit.define("habanero") do |h|
h.definition = Unit("1 degC") * 100
h.aliases = %w{degH}
end

class Time
Expand Down Expand Up @@ -121,11 +126,12 @@ def test_string_helpers
assert_equal '1 mm'.convert_to('in'), Unit('1 mm').convert_to('in')
end

[:sin, :cos, :tan, :sinh, :cosh, :tanh].each do |trig|
define_method("test_#{trig}") do
assert_equal Math.send(trig, Math::PI), Math.send(trig, "180 deg".unit)
end
end
# these tests are covered in the spec/ dir
# [:sin, :cos, :tan, :sinh, :cosh, :tanh].each do |trig|
# define_method("test_#{trig}") do
# assert_equal Math.send(trig, Math::PI), Math.send(trig, "180 deg".unit)
# end
# end

def test_clone
unit1= "1 mm".unit
Expand Down Expand Up @@ -552,6 +558,13 @@ def test_round
assert_equal((unit1/unit2).round, 1)
end

def test_round_no_space
unit1 = Unit.new("1.1mm")
unit2 = Unit.new("1mm")
assert_equal unit2, unit1.round
assert_equal((unit1/unit2).round, 1)
end

def test_zero?
unit1 = Unit.new("0")
assert unit1.zero?
Expand Down Expand Up @@ -804,7 +817,7 @@ def test_inline_conversions
end

def test_time_conversions
today = Time.now
today = Time.now.getutc
assert_equal today,@april_fools
last_century = today - '150 years'.unit
assert_equal last_century.to_date, DateTime.parse('1856-04-01')
Expand All @@ -825,11 +838,11 @@ def test_divide_results_in_unitless
assert_equal a/b, 1
end

def test_wt_percent
a = '1 wt%'.unit
b = '1 g/dl'.unit
assert_equal a,b
end
# def test_wt_percent
# a = '1 wt%'.unit
# b = '1 g/dl'.unit
# assert_equal a,b
# end

def test_parse_durations
assert_equal "1:00".unit, '1 hour'.unit
Expand Down Expand Up @@ -902,7 +915,7 @@ def test_rational_units
end

def test_to_date
a = Time.now
a = Time.now.getutc
assert_equal a.send(:to_date), Date.today
end

Expand Down Expand Up @@ -936,7 +949,7 @@ def test_to_s_cache
end

def test_version
assert_equal('1.3.2', Unit::VERSION)
assert_equal('1.4.0', Unit::VERSION)
end

def test_negation
Expand Down