From f8d1f6106e7131acc206fbbad32c4318ca4eba77 Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 14:31:49 -0700 Subject: [PATCH 1/9] Fixed inconsistency between Ruby 1.8.7 and Ruby 1.9+ --- spec/quantity.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/quantity.spec b/spec/quantity.spec index 44ac3d2..dd8fd40 100644 --- a/spec/quantity.spec +++ b/spec/quantity.spec @@ -28,7 +28,7 @@ describe Quantity do it "should have a string representation" do 2.meters.to_s.should == "2 meter" - (2.meters * 2.meters).to_s.should == (defined?(Rational) ? "4 meter^2" : "4.0 meter^2") + (2.meters * 2.meters).to_s.should == (defined?(Rational) ? "#{4.to_r.to_s} meter^2" : "4.0 meter^2") end end From f99e2dee10e72b3efb2d670dad92cd7a5a48d13a Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 14:34:31 -0700 Subject: [PATCH 2/9] Fixed specs: ArgumentError is now used instead of TypeError --- spec/quantity.spec | 4 ++-- spec/unit.spec | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/quantity.spec b/spec/quantity.spec index dd8fd40..0cc7210 100644 --- a/spec/quantity.spec +++ b/spec/quantity.spec @@ -169,7 +169,7 @@ describe Quantity do end it "does not add items of different types" do - lambda { 12.meters + 24.picograms }.should raise_error TypeError + lambda { 12.meters + 24.picograms }.should raise_error ArgumentError end it "adds negative quantities" do @@ -185,7 +185,7 @@ describe Quantity do end it "does not add items of different types" do - lambda { (12.meters - 3650.picograms)}.should raise_error TypeError + lambda { (12.meters - 3650.picograms)}.should raise_error ArgumentError end it "subtracts numerics from quantities" do diff --git a/spec/unit.spec b/spec/unit.spec index 4df6cbd..a2cc7a5 100644 --- a/spec/unit.spec +++ b/spec/unit.spec @@ -262,15 +262,15 @@ describe Quantity::Unit do end it "won't convert a simple unit to another dimension" do - lambda { @foot.convert(:second) }.should raise_error TypeError + lambda { @foot.convert(:second) }.should raise_error ArgumentError end it "won't convert a complex unit to a dimension it doesn't contain" do - lambda { @mps.convert(:gram) }.should raise_error TypeError + lambda { @mps.convert(:gram) }.should raise_error ArgumentError end it "won't convert to a higher-order unit unless it has an exact matching dimension" do - lambda { @liter.convert(:'mm^2') }.should raise_error TypeError + lambda { @liter.convert(:'mm^2') }.should raise_error ArgumentError end it "breaks down named complex units" do From c9e69c1815fcf6913548ada85203c812c1eecefb Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 15:01:41 -0700 Subject: [PATCH 3/9] Fixed so unit name is always a symbol --- lib/quantity/unit.rb | 2 +- spec/quantity.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/quantity/unit.rb b/lib/quantity/unit.rb index d7d2555..df29035 100644 --- a/lib/quantity/unit.rb +++ b/lib/quantity/unit.rb @@ -296,7 +296,7 @@ def initialize(opts) unless opts[:name] || !@dimension.is_base? raise ArgumentError, "Single-order units must be uniquely named (#{name} - #{dimension})" end - @name = opts[:name] || string_form + @name = (opts[:name] || string_form).to_sym self.class.add_alias(self,@name.to_sym) raise ArgumentError, "Creating new unit with no value" unless @value end diff --git a/spec/quantity.spec b/spec/quantity.spec index 0cc7210..409bbd4 100644 --- a/spec/quantity.spec +++ b/spec/quantity.spec @@ -55,7 +55,7 @@ describe Quantity do it "converts derived units to named units" do (1.centimeter * 1.centimeter * 1.centimeter).should == 0.1.centiliter (1000.mm * 1.mm * 1.mm).should == 1.ml - (1.mm**3).unit.name.should == 'millimeter^3' + (1.mm**3).unit.name.should == :'millimeter^3' (1.mm**3).measures.name.should == :volume (1.centimeter * 1.centimeter).measures.name.should == :area (30.meters / 1.second).measures.name.should == :speed From 1f65f7253731f4c94dfa6a75cc3d752ee4e0c898 Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 16:45:14 -0700 Subject: [PATCH 4/9] Fixes inconsistent unit string form (i.e 'length^2' should be 'foot^2') --- lib/quantity/unit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/quantity/unit.rb b/lib/quantity/unit.rb index df29035..1d29ddb 100644 --- a/lib/quantity/unit.rb +++ b/lib/quantity/unit.rb @@ -331,7 +331,7 @@ def string_form def self.string_form(dimension,units) string = dimension.string_form units.each do | dimension, unit | - string = string.gsub(dimension.name.to_s, unit.name.to_s) + string = string.gsub(dimension.string_form, unit.name.to_s) end string end From 445a523696a37d539559491246248dcfd582bc70 Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 16:49:56 -0700 Subject: [PATCH 5/9] Dimension#reduce is not implemented --- spec/dimension.spec | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/dimension.spec b/spec/dimension.spec index 7f7bef2..226c34c 100644 --- a/spec/dimension.spec +++ b/spec/dimension.spec @@ -114,6 +114,7 @@ describe Quantity::Dimension do end it "provides a reduced form" do + pending @area.name.should == :area @area.reduce.name.should == :'length^2' end From 84367848ef7e4d7b851b50999ce8dcb2ae9823eb Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 16:51:43 -0700 Subject: [PATCH 6/9] Dimenions#to_hash is not implemented --- spec/dimension.spec | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/dimension.spec b/spec/dimension.spec index 226c34c..acf5571 100644 --- a/spec/dimension.spec +++ b/spec/dimension.spec @@ -92,24 +92,29 @@ describe Quantity::Dimension do end it "provides a hash form for simple dimensions" do - @length.hash.should == { :length => 1 } + pending + @length.to_hash.should == { :length => 1 } end it "provides a hash form for unnamed dimensions" do + pending lm = @length * @mass - lm.hash.should == { :length => 1, :mass => 1 } + lm.to_hash.should == { :length => 1, :mass => 1 } end it "provides a hash form for named dimensions" do + pending @area.to_hash.should == { :area => 1 } end it "provides a reduced hash form" do + pending @length.to_hash.should == @length.reduced_hash @area.reduced.to_hash.should == { :length => 2 } end it "provides a hash form with negative components" do + pending @force.to_hash.should = { :length => 1, :mass => -1 , :time => -2 } end From b85a6545016ef66510031b0d243bb61f4b3ce69c Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 17:13:05 -0700 Subject: [PATCH 7/9] Fixed: the #name of a dimension should be the name passed in, not the first alias --- lib/quantity/dimension.rb | 2 +- spec/dimension.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/quantity/dimension.rb b/lib/quantity/dimension.rb index 325c20b..08b42f0 100644 --- a/lib/quantity/dimension.rb +++ b/lib/quantity/dimension.rb @@ -55,7 +55,7 @@ def self.add_dimension(name, *aliases) dim = name name.name = aliases.first if aliases.first else - dim = self.for(name) || self.new({ :name => aliases.first , :description => name}) + dim = self.for(name) || self.new({ :name => name , :description => name}) self.add_alias(dim,name) end unless (dim.class == Dimension) diff --git a/spec/dimension.spec b/spec/dimension.spec index acf5571..b49473b 100644 --- a/spec/dimension.spec +++ b/spec/dimension.spec @@ -23,7 +23,7 @@ describe Quantity::Dimension do end it "creates complex dimensions" do - Quantity::Dimension.add_dimension :'length^2', :area + Quantity::Dimension.add_dimension :area, :'length^2' area = Quantity::Dimension.for(:area) area.to_s.should == "area" area.name.should == :area From 627358a11eab4389e7c1349eb930c07f14144dc5 Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 17:16:01 -0700 Subject: [PATCH 8/9] Fixed spec (inconsistency between Rational and Fixnum division) --- spec/unit.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit.spec b/spec/unit.spec index a2cc7a5..94ca420 100644 --- a/spec/unit.spec +++ b/spec/unit.spec @@ -222,7 +222,7 @@ describe Quantity::Unit do it "supports mixed unit divisors" do result = @meter / (@gram * @second) result.name.should == :'meter/gram*second' - result.value.should == @meter.value / (@gram.value*@second.value) + result.value.should == @meter.value.to_r / (@gram.value*@second.value).to_r end it "simplifies results" do From 9f6476e7aa452ee55eec5c2ab1c2875035d5e043 Mon Sep 17 00:00:00 2001 From: David Butler Date: Fri, 26 Jul 2013 17:31:36 -0700 Subject: [PATCH 9/9] Fixed incorrect spec --- spec/unit.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit.spec b/spec/unit.spec index 94ca420..4045bfc 100644 --- a/spec/unit.spec +++ b/spec/unit.spec @@ -190,7 +190,7 @@ describe Quantity::Unit do it "supports units of different dimensions" do s_f3 = @second * (@foot * @foot * @foot) s_f3.name.should == :'foot^3*second' - s_f3.value.should == @foot.value**3 / @second.value + s_f3.value.should == @foot.value**3 * @second.value end it "supports different units of the same dimension" do