require 'test/unit' require 'test/framework' require 'units' class TC_unes < Test::Unit::TestCase understands UnitsTest def load_units Units.create :length do |m| m.system :english do |s| s.unit :name => :inch, :plural => :inches, :abbrev => :in s.unit :name => :foot, :plural => :feet, :abbrev => :ft, :equals => 12.inches s.unit :name => :yard, :abbrevs => :yd, :equals => 3.feet end end end def test_use_1_identities load_units assert(72.inches.to_s == '72 inches') assert(6.feet.to_s == '6 feet') assert(2.yards.to_s == '2 yards') end def test_use_1_test_use_1_multiplication load_units assert((72.inches/10).to_s == '7 inches') assert((6.feet*5).to_s == '30 feet') assert((2.yards**2).to_s == '4 yd^2') assert((72.0.inches/10).to_s == '7.2 inches') assert((12.inches*12.inches).to_s == '144.0 in^2') assert((12.inches*1.foot).to_s == '1.0 ft^2') assert((1.foot*12.inches).to_s == '144.0 in^2') assert((1.foot*1.foot).to_s == '1.0 ft^2') assert(12.0.in == 1.0.ft) assert(144.0.in**2 != 1.0.ft**2) assert(NumericWithUnits.new(144.0,Units.length.english.inch,2).to_s == '144.0 in^2') assert(144.0.in^2 == 1.0.ft^2) assert(72.inches/2.feet == 3.0) assert(6.feet/2.feet == 3.0) assert(2.yards/2.feet == 3.0) assert((144 * 1.in^2).to_s == '144 in^2') assert((2.0 / 3.ft^3).to_s == '0.666666666666667 1/ft^3') end def test_use_1_addition load_units assert(((2.0.ft^2) + (3.0.in^2)).to_s == '291.0 in^2') assert(((1.0.yd^2) - (1.0.ft^2)).to_s == '8.0 ft^2') assert((5.0.inches + 2).to_s == '7.0 inches') assert((2 + 5.0.inches).to_s == '7.0 inches') assert((5.0.inches - 2).to_s == '3.0 inches') assert((2 - 5.0.inches).to_s == '-3.0 inches') end def test_use_1_comparison load_units assert((13.inches <=> 1.foot) == 1) assert((12.inches <=> 1.foot) == 0) assert((11.inches <=> 1.foot) == -1) assert((13.inches > 1.foot) == true) assert((12.inches > 1.foot) == false) assert((11.inches > 1.foot) == false) assert((13.inches < 1.foot) == false) assert((12.inches < 1.foot) == false) assert((11.inches < 1.foot) == true) assert((13.inches >= 1.foot) == true) assert((12.inches >= 1.foot) == true) assert((11.inches >= 1.foot) == false) assert((13.inches <= 1.foot) == false) assert((12.inches <= 1.foot) == true) assert((11.inches <= 1.foot) == true) assert((13.inches == 1.foot) == false) assert((12.inches == 1.foot) == true) assert((11.inches == 1.foot) == false) assert((13.inches <=> 12) == 1) assert((12.inches <=> 12) == 0) assert((11.inches <=> 12) == -1) assert((13.inches > 12) == true) assert((12.inches > 12) == false) assert((11.inches > 12) == false) assert((13.inches < 12) == false) assert((12.inches < 12) == false) assert((11.inches < 12) == true) assert((13.inches >= 12) == true) assert((12.inches >= 12) == true) assert((11.inches >= 12) == false) assert((13.inches <= 12) == false) assert((12.inches <= 12) == true) assert((11.inches <= 12) == true) assert((13.inches == 12) == false) assert((12.inches == 12) == true) assert((11.inches == 12) == false) assert((13 <=> 12.inches) == 1) assert((12 <=> 12.inches) == 0) assert((11 <=> 12.inches) == -1) assert((13 > 12.inches) == true) assert((12 > 12.inches) == false) assert((11 > 12.inches) == false) assert((13 < 12.inches) == false) assert((12 < 12.inches) == false) assert((11 < 12.inches) == true) assert((13 >= 12.inches) == true) assert((12 >= 12.inches) == true) assert((11 >= 12.inches) == false) assert((13 <= 12.inches) == false) assert((12 <= 12.inches) == true) assert((11 <= 12.inches) == true) assert((13 == 12.inches) == false) assert((12 == 12.inches) == true) assert((11 == 12.inches) == false) end def test_use_1_modulo load_units assert((5.feet % 7.inches).to_s == '4.0 inches') assert((27.inches % 2.feet).to_s == '0.25 feet') assert((12.inches % 5).to_s == '2 inches') assert((12 % 9.inches).to_s == '3 inches') end def test_use_1_niceties load_units assert(10.4.inches.ceil.to_s == '11 inches') assert(-10.4.inches.ceil.to_s == '-10 inches') assert(10.4.inches.floor.to_s == '10 inches') assert(-10.4.inches.floor.to_s == '-11 inches') assert(10.4.inches.abs.to_s == '10.4 inches') assert(-10.4.inches.abs.to_s == '10.4 inches') assert(10.4.inches.round.to_s == '10 inches') assert(-10.4.inches.round.to_s == '-10 inches') end def test_use_1_inheritence load_units assert(10.inches.kind_of?(Numeric) == true) assert(10.inches.kind_of?(Fixnum) == true) assert(10.0.inches.kind_of?(Float) == true) assert(10.0.inches.kind_of?(String) == false) end def test_use_1_promotion load_units inch = Units.length.english.inch n = 10 assert((n.unite inch).to_s == '10 inches') assert((n.unite "inch").to_s == '10 inches') assert((n.unite :inch).to_s == '10 inches') assert((n.unite 1.inch).to_s == '10 inches') assert((n.unite 1.inch.unit).to_s == '10 inches') assert((n.unite nil).to_s == '10') n = 10.feet assert((n.unite inch).to_s == '10 inches') assert((n.unite "inch").to_s == '10 inches') assert((n.unite :inch).to_s == '10 inches') assert((n.unite 1.inch).to_s == '10 inches') assert((n.unite 1.inch.unit).to_s == '10 inches') assert((n.unite nil).to_s == '10') end end