require 'test/unit' require 'test/framework' require 'units' class TC_uses_2 < 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 s.unit :name => :mile, :abbrevs => :mi, :equals => 1760.yards end end Units.create :time do |m| m.system :base do |s| s.unit :name => :second, :abbrev => :sec s.unit :name => :minute, :abbrev => :min, :equals => 60.seconds s.unit :name => :hour, :abbrev => :hr, :equals => 60.minutes s.unit :name => :day, :abbrev => :dy, :equals => 24.hours end end end def test_use_2_convert load_units foot = 1.foot mile = 1.mile d = 140.miles assert(d.convert(mile).to_s == '140.0 miles') assert(d.convert(foot).to_s == '739200.0 feet') assert(d.convert(1.yard).to_s == '246400.0 yards') second = 1.second hour = 1.hour t = 2.hours + 35.minutes assert(t.convert(hour).to_s == '2.58333333333333 hours') assert(t.convert(second).to_s == '9300.0 seconds') r = d/t assert(r.to_s == '0.903225806451613 mi / min') assert(r.convert([mile, hour]).to_s == '54.1935483870968 mi / hr') assert(r.convert([foot, second]).to_s == '79.4838709677419 ft / sec') fps = [foot,second] mph = [mile,hour] assert(r.convert(fps).to_s == '79.4838709677419 ft / sec') assert(r.convert(mph).to_s == '54.1935483870968 mi / hr') assert((5.days * r).to_s == '6503.22580645161 miles') end def test_use_2_to load_units r = 55.feet / 1.second assert(r.to_s == '55 ft / sec') r.to_miles_and_hours assert(r.to_s == '37.5 mi / hr') end def test_use_2_in load_units r1 = 55.feet / 1.second assert(r1.to_s == '55 ft / sec') r2 = r1.in_miles_and_hours assert(r1.to_s == '55 ft / sec') assert(r2.to_s == '37.5 mi / hr') end def test_use_2_per load_units r1 = 55.ft/1.sec assert(r1.miles_per_hour.to_s == '37.5 mi / hr') assert(r1.minutes_per_mile.to_s == '1.6 min / mi') end def test_use_2_in_implicit load_units r1 = 55.feet / 1.second assert(r1.to_s == '55 ft / sec') r2 = r1.miles_and_hours assert(r1.to_s == '55 ft / sec') assert(r2.to_s == '37.5 mi / hr') assert(102.inches.feet.to_s == '8.5 feet') end def test_use_2_object_unite load_units r1 = 55.ft/sec assert(r1.miles_per_hour.to_s == '37.5 mi / hr') assert(r1.minutes_per_mile.to_s == '1.6 min / mi') end def test_use_2_numeric_unite_per load_units r1 = 55.feet_per_second assert(r1.miles_per_hour.to_s == '37.5 mi / hr') assert(r1.minutes_per_mile.to_s == '1.6 min / mi') end def test_use_2_object_per load_units r1 = feet_per_mile assert(r1.to_s == '5280.0') end end