Permalink
Browse files

Merge branch 'cleanup'

  • Loading branch information...
2 parents 6b35fc1 + 0b92611 commit b6c1be924fccc2d60ff32d52e7a72f6990f0db09 @FredLoney FredLoney committed Dec 14, 2011
View
@@ -53,7 +53,7 @@ module UOM
MINUTE = Unit.new(:minute, :min, SECOND, 60)
HOUR = Unit.new(:hour, :hr, MINUTE, 60)
DAY = Unit.new(:day, HOUR, 24)
-
+
# information
BIT = Unit.new(:bit, BYTE, 1.0 / 8)
@@ -76,6 +76,11 @@ module UOM
JOULE = Unit.new(:joule, :J, (KILOGRAM * (METER * METER)) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
ERG = Unit.new(:erg, (GRAM * (CENTIMETER * CENTIMETER)) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
DYNE = Unit.new(:dyne, :dyn, (GRAM * CENTIMETER) / (SECOND * SECOND), *ELECTRONIC_FACTORS)
+ NEWTON = Unit.new(:newton, :N, (KILOGRAM * METER) / (SECOND * SECOND), *SIMPLE_FACTORS)
+ BAR = Unit.new(:bar, NEWTON, 0.00001, *SIMPLE_FACTORS)
+ PASCAL = Unit.new(:pascal, :Pa, NEWTON / (METER * METER), *SIMPLE_FACTORS)
+ ATMOSPHERE = Unit.new(:atmosphere, :atm, PASCAL, 0.101325, *SIMPLE_FACTORS)
+ TORR = Unit.new(:torr, ATMOSPHERE, 1.0 / 760, *SIMPLE_FACTORS)
BARYE = Unit.new(:barye, :Ba, GRAM / (CENTIMETER * (SECOND * SECOND)), *ELECTRONIC_FACTORS)
POISE = Unit.new(:poise, :P, GRAM / (CENTIMETER * SECOND), KILO, MILLI, MICRO, NANO, PICO)
COULOMB = Unit.new(:coulomb, AMPERE / SECOND, *ELECTRONIC_FACTORS) # C abbreviation is taken by CENTIGRADE
@@ -109,7 +114,7 @@ module UOM
DRY_GALLON = Unit.new(:dry_gallon, :dry_gal, DRY_QUART, 4)
PECK = Unit.new(:peck, :pk, DRY_GALLON, 2)
BUSHEL = Unit.new(:bushel, :bu, PECK, 4)
-
- # Pressure
+
+ # Pressure alias
PSI = Unit.for(:pounds_per_square_inch).add_abbreviation(:psi)
end
View
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'bundler'
+Bundler.require(:test, :development)
@@ -1,6 +1,4 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
require 'uom/units'
require 'uom/composite_unit_key_canonicalizer'
@@ -1,6 +1,4 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
require 'uom/units'
@@ -1,8 +1,6 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
-require 'measurement'
+require 'uom'
class FactorTest < Test::Unit::TestCase
def test_small_to_unit
@@ -1,6 +1,4 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
require 'uom'
@@ -53,8 +51,12 @@ def test_idempotent_quotient_conversion
roundtrip = Measurement.new(quotient, 4).as(other).as(quotient)
assert_equal(Measurement.new(other, 4.0), roundtrip, "Idempotent quotient conversion 4 grams_per_liter => milligrams_per_milliliter incorrect: #{roundtrip}")
end
+
+ def test_parse_unknown_factor
+ assert_raises(UOM::MeasurementError, "'1 unknown' didn't raise a measurement error") { "1 unknown".to_measurement }
+ end
- def test_parse
+ def test_parse_known_factors
assert_equal(Measurement.new(:gram, 1), "1g".to_measurement, "'1g' not parsed")
assert_equal(Measurement.new(:gram, 1), "1 g".to_measurement, "'1 g' not parsed")
assert_equal(Measurement.new(:gram, 1), "1 gm".to_measurement, "'1 gm' not parsed")
@@ -1,7 +1,9 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
+
+# JRuby SyncEnumerator moved from generator to REXML in JRuby 1.5
+require 'rexml/document'
+
require 'uom/units'
class UnitFactoryTest < Test::Unit::TestCase
@@ -73,32 +75,11 @@ def test_kilobyte
assert_equal(:kilobyte, @factory.create('kb').label, "KB not recognized")
end
- # test parsing all possible combinations of labels, abbrevs and factors
+ # Tests parsing all possible combinations of labels, abbrevs and factors.
def test_all
- File.open("test/results/uom/units.txt", "w") do |file|
- UOM::Unit.each do |unit|
- next if unit.scalar != UOM::UNIT or UOM::CompositeUnit === unit
- labels = []
- [UOM::UNIT].concat(unit.permissible_factors.to_a).each do |factor|
- labels << label = "#{factor}#{unit}"
- verify_create(label, unit, factor)
- unless label == label.pluralize then
- verify_create(label.pluralize, unit, factor)
- labels << label.pluralize
- end
- unit.abbreviations.each do |abbrev|
- labels << label = "#{factor.abbreviation}#{abbrev}"
- verify_create(label, unit, factor)
- abbrev_s = abbrev.to_s
- unless abbrev_s.length < 2 then
- plural = label + 's'
- verify_create(plural, unit, factor)
- labels << plural
- end
- end
- end
- file.puts(labels.join(', '))
- end
+ units = File.open(ALL_UNITS, 'r').map { |line| line.chomp }
+ REXML::SyncEnumerator.new(units, generate_all_units).each do |expected, actual|
+ assert_equal(expected, actual, "Generated units differs from documented units")
end
end
@@ -109,4 +90,45 @@ def verify_create(label, axis, scalar=nil)
assert_same(scalar, actual.scalar, "Unit '#{label}' scalar factor incorrect")
end
end
+
+ private
+
+ ALL_UNITS = File.expand_path('units.txt', File.dirname(__FILE__) + '/../../../doc')
+
+ def generate_all_units
+ basic = UOM::Unit.select do |unit|
+ unit.scalar == UOM::UNIT and not UOM::CompositeUnit === unit
+ end
+ all = basic.map do |unit|
+ labels = []
+ [UOM::UNIT].concat(unit.permissible_factors.to_a).each do |factor|
+ labels << label = "#{factor}#{unit}"
+ verify_create(label, unit, factor)
+ unless label == label.pluralize then
+ verify_create(label.pluralize, unit, factor)
+ labels << label.pluralize
+ end
+ unit.abbreviations.each do |abbrev|
+ labels << label = "#{factor.abbreviation}#{abbrev}"
+ verify_create(label, unit, factor)
+ abbrev_s = abbrev.to_s
+ unless abbrev_s.length < 2 then
+ plural = label + 's'
+ verify_create(plural, unit, factor)
+ labels << plural
+ end
+ end
+ end
+ labels.join(', ')
+ end
+ # combine byte measures
+ byte, std = all.partition { |line| line =~ /byte/ }
+ bh = {}
+ byte.each do |line|
+ key = /^(.*byte),.*/.match(line).captures.first.to_sym
+ bh[key] = line.chomp
+ end
+ std << [:byte, :kilobyte, :megabyte, :gigabyte, :terabyte, :petabyte].map { |k| bh[k] }.join(', ')
+ std.sort
+ end
end
View
@@ -1,20 +1,11 @@
-$:.unshift 'lib'
-$:.unshift '../extensional/lib'
-
+require File.dirname(__FILE__) + '/../helper'
require "test/unit"
require 'uom'
# Not to be confused with Test::Unit, UnitTest tests Unit.
class UnitTest < Test::Unit::TestCase
include UOM
- private
-
- # joule-pecks per erg-gauss
- JPEG = Unit.for((JOULE * PECK) / (ERG * GAUSS)).add_abbreviation(:jpeg)
-
- public
-
def test_scale_high_to_low
assert_equal(4000, METER.as(4, MILLIMETER).to_f, "Scale conversion incorrect")
end
@@ -42,4 +33,9 @@ def test_new_unit
expected = Measurement.new(:joule_peck, 4) / Measurement.new(:erg_gauss, 1)
assert_equal(expected, jpeg, "New unit incorrect")
end
+
+ private
+
+ # joule-pecks per erg-gauss
+ JPEG = Unit.for((JOULE * PECK) / (ERG * GAUSS)).add_abbreviation(:jpeg)
end
View
@@ -1,6 +1,7 @@
-require 'uom'
+require 'date'
+require File.expand_path('version', File.dirname(__FILE__) + '/lib/uom')
-SPEC = Gem::Specification.new do |s|
+Gem::Specification.new do |s|
s.name = "uom"
s.summary = "Ruby Unit of Measurement library."
s.description = <<-eof
@@ -9,14 +10,19 @@ SPEC = Gem::Specification.new do |s|
are supported out of the box.
eof
s.version = UOM::VERSION
- s.date = "2010-10-04"
+ s.date = Date.today
s.author = "OHSU"
s.email = "caruby.org@gmail.com"
s.homepage = "http://github.com/caruby/uom/"
s.platform = Gem::Platform::RUBY
- s.files = Dir.glob("{doc,lib}/**/*") + Dir.glob("test/{fixtures|lib}/**/*") + ['History.txt', 'LEGAL', 'LICENSE', 'README.md']
- s.require_paths = ['lib']
+ s.files = Dir.glob("{lib}/**/*") + Dir.glob("test/lib/**/*") + ['History.txt', 'LEGAL', 'LICENSE', 'README.md']
+ s.test_files = Dir['test/lib/**/*test.rb']
+ s.require_path = 'lib'
s.add_dependency('extensional')
- s.has_rdoc = 'uom'
+ s.add_development_dependency 'bundler'
+ s.add_development_dependency 'yard'
+ s.add_development_dependency 'rake'
+ s.has_rdoc = 'yard'
+ s.license = 'MIT'
s.rubyforge_project = 'caruby'
-end
+end

0 comments on commit b6c1be9

Please sign in to comment.