spec/quantity_spec.rb in quantify-3.1.4 vs spec/quantity_spec.rb in quantify-3.2.0

- old
+ new

@@ -2,10 +2,16 @@ require 'quantify' include Quantify describe Quantity do + it "should create a valid instance with nil values" do + quantity = Quantity.new nil, nil + quantity.value.should be_nil + quantity.unit.should eq(Unit.for('unity')) + end + it "should create a valid instance with standard create and unit name" do quantity = Quantity.new 10.0, 'metre' quantity.value.should == 10 quantity.unit.symbol.should == 'm' end @@ -114,10 +120,17 @@ quantities = Quantity.parse "100km" quantities.first.value.should == 100 quantities.first.unit.name.should == 'kilometre' end + + it "should create a valid instance with unity unit from an empty string" do + quantities = Quantity.parse " " + quantities.first.value.should == nil + quantities.first.unit.name.should == 'unity' + end + it "should create valid instances from complex string, no space and two-digit symbol" do quantities = Quantity.parse "100km driving cars" quantities.first.value.should == 100 quantities.first.unit.name.should == 'kilometre' end @@ -327,10 +340,14 @@ it "should throw error when adding quantities with different dimensions" do lambda{1.metre + 5.kg}.should raise_error end + it "should throw error when adding nil quantities" do + lambda{Quantity.new(nil,nil) + 5.kg}.should raise_error + end + it "should subtract quantities correctly with different units of same dimension" do result = (125.4.kelvin - -211.85.degree_celsius) result.value.should be_within(0.00000001).of(64.1) result.unit.symbol.should == "K" end @@ -345,10 +362,14 @@ it "should throw error when subtracting quantities with different dimensions" do lambda{1.metre - 5.kg}.should raise_error end + it "should throw error when subtracting nil quantities" do + lambda{Quantity.new(nil,nil) - 5.kg}.should raise_error + end + it "should successfully multiply a quantity by a scalar" do (20.metre * 3).to_s.should == "60.0 m" end it "should successfully multiply a quantity by a scalar" do @@ -357,11 +378,16 @@ it "should raise error if multiplying by string" do lambda{20.metre * '3'}.should raise_error end - it "should two quantities" do + it "should raise error when multiplying by nil quantity" do + lambda{Quantity.new(nil,nil) * 5.kg}.should raise_error + lambda{5.kg * Quantity.new(nil,nil)}.should raise_error + end + + it "should multiply two quantities" do quantity = (20.metre * 1.metre) quantity.value.should == 20 quantity.unit.measures.should == 'area' end @@ -383,10 +409,15 @@ it "should successfully divide a quantity by a scalar" do (2.kg / 0.5).round.to_s.should == "4 kg" end + it "should throw error when dividing nil quantities" do + lambda{Quantity.new(nil,nil) / 5.kg}.should raise_error + lambda{5.kg / Quantity.new(nil,nil)}.should raise_error + end + it "should calculate speed from distance and time quantities" do distance_in_km = 12.km time_in_min = 16.5.min distance_in_miles = distance_in_km.to_miles time_in_hours = time_in_min.to_hours @@ -427,10 +458,18 @@ # use #be_within to tolerate Ruby 1.8.7 - 1.9.2 differences quantity.value.should be_within(1.0e-08).of(83.1888383) quantity.unit.pluralized_name.should eql "kilograms" end + specify "converting a nil quantity to another unit yields nil" do + Quantity.new(nil,nil).to_kg.should be_nil + end + + it "should raise error when trying to round a nil quantity" do + lambda{Quantity.new(nil,nil).round(2)}.should raise_error + end + it "should convert compound units correctly" do speed = Quantity.new 100, (Unit.km/Unit.h) speed.to_mi.round(2).to_s.should == "62.14 mi/h" end @@ -466,10 +505,15 @@ (50.square_metres/10.m).to_s.should == "5.0 m" (1.kg*20.m*2.m/4.s/5.s).to_s(:name).should == '2.0 joules' (80.kg/2.m/4.s/5.s).to_s(:name).should == '2.0 pascals' end + it "should raise error when trying to raise a nil quantity to a power" do + lambda{Quantity.new(nil,nil) ** 2}.should raise_error + lambda{Quantity.new(nil,nil).pow(2)}.should raise_error + end + it "should raise a quantity to a power correctly" do unit = 50.ft ** 2 unit.to_s.should == "2500.0 ft²" unit = 50.ft ** 3 unit.to_s.should == "125000.0 ft³" @@ -514,10 +558,36 @@ quantity.rationalize_units! quantity.value.should be_within(0.0000001).of(144) quantity.unit.symbol.should eql "yd²" end + context "comparing nil quantities" do + specify "greater than" do + lambda{Quantity.new(nil,nil) > 1.m}.should raise_error + end + + specify "greater than or equals" do + lambda{Quantity.new(nil,nil) >= 1.m}.should raise_error + end + + specify "less than" do + lambda{Quantity.new(nil,nil) < 1.m}.should raise_error + end + + specify "less than or equals" do + lambda{Quantity.new(nil,nil) <= 1.m}.should raise_error + end + + specify "equals" do + lambda{Quantity.new(nil,nil) == 1.m}.should raise_error + end + + specify "between" do + lambda{Quantity.new(nil,nil).between? 1.ft,10.m}.should raise_error + end + end + it "should be greater than" do (20.ft > 1.m).should be_true end it "should be greater than" do @@ -576,9 +646,18 @@ lambda{20.ft === (1.ft..3.K)}.should raise_error end it "range comparison with non quantity should raise error" do lambda{20.ft === (1.ft..3)}.should raise_error + end + + specify "a range with nil quantities raises an error" do + lambda{Quantity.new(nil,nil)..Quantity.new(nil,nil)}.should raise_error + lambda{Quantity.new(nil,nil)..20.ft}.should raise_error + end + + specify "cover? with nil quantities raises an error" do + lambda{(2.ft..20.ft).cover?(Quantity.new(nil,nil))}.should raise_error end it "should return unit consolidation setting" do Quantity.auto_consolidate_units?.should be_false end