ruby-units 1.2.0 → 1.3.0.a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec/core'
4
+ require File.dirname(__FILE__) + "/../lib/ruby-units"
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ruby-units'
4
+
5
+ class TestCache < Test::Unit::TestCase
6
+ def setup
7
+ Unit::Cache.clear
8
+ end
9
+
10
+ def test_clear
11
+ Unit::Cache.set("m", "m".unit)
12
+ Unit::Cache.clear
13
+ assert_nil(Unit::Cache.get('m'))
14
+ end
15
+
16
+ def test_set_cache
17
+ assert_nothing_raised { Unit::Cache.set("m", "m".unit) }
18
+ assert Unit::Cache.get.keys.include?('m')
19
+ end
20
+
21
+ def test_get_cache
22
+ Unit::Cache.set("m", "m".unit)
23
+ assert_equal("m".unit, Unit::Cache.get['m'])
24
+ end
25
+
26
+ end
@@ -86,7 +86,12 @@ class TestRubyUnits < Test::Unit::TestCase
86
86
 
87
87
  def test_to_yaml
88
88
  unit = "1 mm".u
89
- assert_equal "--- !ruby/object:Unit \nscalar: 1.0\nnumerator: \n- <milli>\n- <meter>\ndenominator: \n- <1>\nsignature: 1\nbase_scalar: 0.001\n", unit.to_yaml
89
+ if RUBY_PLATFORM == "java"
90
+ # apparently jruby's implementation of yaml has some white space differences
91
+ assert_equal "--- !ruby/object:Unit \nscalar: 1\nnumerator: \n - <milli>\n - <meter>\ndenominator: \n - <1>\nsignature: 1\nbase_scalar: !ruby/object:Rational \n denominator: 1000\n numerator: 1\n", unit.to_yaml
92
+ else
93
+ assert_equal "--- !ruby/object:Unit \nscalar: 1\nnumerator: \n- <milli>\n- <meter>\ndenominator: \n- <1>\nsignature: 1\nbase_scalar: !ruby/object:Rational \n denominator: 1000\n numerator: 1\n", unit.to_yaml
94
+ end
90
95
  end
91
96
 
92
97
  def test_time
@@ -96,9 +101,8 @@ class TestRubyUnits < Test::Unit::TestCase
96
101
  assert_equal a - 3600, a - "1 h".unit
97
102
  assert_in_delta Time.now - "1 h".unit, "1 h".ago, 1
98
103
  assert_in_delta Time.now + 3600, "1 h".from_now, 1
99
- assert_in_delta "1 h".unit + Time.now, "1 h".from_now, 1
104
+ assert_in_delta Time.now + "1 h".unit, "1 h".from_now, 1
100
105
  assert_in_delta Time.now - 3600, "1 h".before_now, 1
101
- assert_in_delta((Time.now.unit - Time.now).unit.scalar, 0, 1)
102
106
  assert_equal "60 min", "min".until(Time.now + 3600).to_s
103
107
  assert_equal "01:00", "min".since(Time.now - 3600).to_s("%H:%M")
104
108
  assert_in_delta Time.now, "now".time, 1
@@ -546,7 +550,7 @@ class TestRubyUnits < Test::Unit::TestCase
546
550
 
547
551
  def test_abs
548
552
  unit1 = Unit.new("-1 mm")
549
- assert_equal 1, unit1.abs
553
+ assert_equal "1 mm".unit, unit1.abs
550
554
  end
551
555
 
552
556
  def test_ceil
@@ -629,8 +633,7 @@ class TestRubyUnits < Test::Unit::TestCase
629
633
  assert_equal b+a, '118 tempF'.unit
630
634
  assert_equal a-b, '82 tempF'.unit
631
635
  assert_in_delta((a-c).scalar, '50 degF'.unit.scalar, 0.01)
632
- assert_equal b+d, '20 degC'.unit
633
-
636
+ assert_in_delta '20 degC'.unit.scalar, (b+d).scalar, 0.01
634
637
  assert_raises(ArgumentError) { a * b }
635
638
  assert_raises(ArgumentError) { a / b }
636
639
  assert_raises(ArgumentError) { a ** 2 }
@@ -676,7 +679,7 @@ class TestRubyUnits < Test::Unit::TestCase
676
679
  unit2 = Unit.new("mm")
677
680
  assert_equal "1 mm", unit2.to_s
678
681
  assert_equal "0.04 in", unit2.to_s("%0.2f in")
679
- assert_equal "0.1 cm", unit2.to_s("cm")
682
+ assert_equal "1/10 cm", unit2.to_s("cm")
680
683
  unit3 = Unit.new("1 mm")
681
684
  assert_equal "1 mm", unit3.to_s
682
685
  assert_equal "0.04 in", unit3.to_s("%0.2f in")
@@ -823,6 +826,16 @@ class TestRubyUnits < Test::Unit::TestCase
823
826
  assert_raises(ArgumentError) { "3 s**2 4s s**2".unit }
824
827
  assert_raises(ArgumentError) { "3 s 5^6".unit }
825
828
  assert_raises(ArgumentError) { "".unit }
829
+ assert_raises(ArgumentError) { " ".unit }
830
+ assert_raises(ArgumentError) { "\t".unit }
831
+ assert_raises(ArgumentError) { "\t\t".unit }
832
+ assert_raises(ArgumentError) { "\n".unit }
833
+ end
834
+
835
+ def test_inline_conversions
836
+ assert_equal "60 s".unit, Unit.parse("1 min to seconds")
837
+ assert_equal "60 s".unit, Unit.parse("1 min as seconds")
838
+ assert_equal "60 s".unit, Unit.parse("1 min in seconds")
826
839
  end
827
840
 
828
841
  def test_time_conversions
@@ -914,7 +927,7 @@ class TestRubyUnits < Test::Unit::TestCase
914
927
  def test_atan2
915
928
  assert_equal Math.atan2('1 mm'.unit,'1 mm'.unit), Math.atan2(1,1)
916
929
  assert_raises(ArgumentError) {Math.atan2('1 mm'.unit, '1 lb'.unit)}
917
- assert_raises(ArgumentError) {Math.atan2('1 mm'.unit, 1)}
930
+ assert_raises(RuntimeError) {Math.atan2('1 mm'.unit, 1)}
918
931
  end
919
932
 
920
933
  def test_rational_units
@@ -953,12 +966,12 @@ class TestRubyUnits < Test::Unit::TestCase
953
966
  a.to_s # cache the conversion to itself
954
967
  b = Unit.new('2 mm')
955
968
  assert_equal('2 mm', b.to_s)
956
- assert_equal('0.001 m', a.to_s('m'))
957
- assert_equal('0.001 m', a.output['m'])
969
+ assert_equal('1/1000 m', a.to_s('m'))
970
+ assert_equal('1/1000 m', a.output['m'])
958
971
  end
959
972
 
960
973
  def test_version
961
- assert_equal('1.2.0.b', Unit::VERSION)
974
+ assert_equal('1.3.0.a', Unit::VERSION)
962
975
  end
963
976
 
964
977
  def test_negation
@@ -978,5 +991,21 @@ class TestRubyUnits < Test::Unit::TestCase
978
991
  assert !"1 mm".unit.temperature?
979
992
  end
980
993
 
994
+ def test_parse_into_numbers_and_units
995
+ assert_equal([1,"m"], Unit.parse_into_numbers_and_units("1 m"))
996
+ assert_equal([1.0,"m"], Unit.parse_into_numbers_and_units("1.0 m"))
997
+ assert_equal([0.1,"m"], Unit.parse_into_numbers_and_units("0.1 m"))
998
+ assert_equal([0.1,"m"], Unit.parse_into_numbers_and_units(".1 m"))
999
+ assert_equal([-1.23E-3,"m"], Unit.parse_into_numbers_and_units("-1.23E-3 m"))
1000
+ assert_equal([1/4,"m"], Unit.parse_into_numbers_and_units("1/4 m"))
1001
+ assert_equal([-1/4,"m"], Unit.parse_into_numbers_and_units("-1/4 m"))
1002
+ assert_equal([1,"m"], Unit.parse_into_numbers_and_units("1 m"))
1003
+ assert_equal([1,"m"], Unit.parse_into_numbers_and_units("m"))
1004
+ assert_equal([10,""], Unit.parse_into_numbers_and_units("10"))
1005
+ assert_equal([10.0,""], Unit.parse_into_numbers_and_units("10.0"))
1006
+ assert_equal([(1/4),""], Unit.parse_into_numbers_and_units("1/4"))
1007
+ assert_equal([Complex(1,1),""], Unit.parse_into_numbers_and_units("1+1i"))
1008
+ end
1009
+
981
1010
  end
982
1011
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-units
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 2
9
- - 0
10
- version: 1.2.0
4
+ prerelease: 6
5
+ version: 1.3.0.a
11
6
  platform: ruby
12
7
  authors:
13
8
  - Kevin Olbrich, Ph.D.
@@ -15,52 +10,52 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-04-01 00:00:00 -04:00
19
- default_executable:
13
+ date: 2011-04-30 00:00:00 Z
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
- type: :development
23
- prerelease: false
24
16
  name: bundler
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &id001 !ruby/object:Gem::Requirement
26
18
  none: false
27
19
  requirements:
28
20
  - - ~>
29
21
  - !ruby/object:Gem::Version
30
- hash: 15
31
- segments:
32
- - 1
33
- - 0
34
22
  version: "1.0"
35
- requirement: *id001
36
- - !ruby/object:Gem::Dependency
37
23
  type: :development
38
24
  prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
39
27
  name: rcov
40
- version_requirements: &id002 !ruby/object:Gem::Requirement
28
+ requirement: &id002 !ruby/object:Gem::Requirement
41
29
  none: false
42
30
  requirements:
43
31
  - - ">="
44
32
  - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
33
  version: "0"
49
- requirement: *id002
50
- - !ruby/object:Gem::Dependency
51
34
  type: :development
52
35
  prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
53
38
  name: jeweler
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
39
+ requirement: &id003 !ruby/object:Gem::Requirement
55
40
  none: false
56
41
  requirements:
57
42
  - - ">="
58
43
  - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
44
  version: "0"
63
- requirement: *id003
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: "2.5"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
64
59
  description: Provides classes and methods to perform unit math and conversions
65
60
  email:
66
61
  - kevin.olbrich+ruby_units@gmail.com
@@ -73,31 +68,21 @@ extra_rdoc_files:
73
68
  - README.md
74
69
  - TODO
75
70
  files:
76
- - CHANGELOG.txt
77
- - Gemfile
78
71
  - LICENSE.txt
79
- - Manifest.txt
80
72
  - README.md
81
- - RakeFile
82
73
  - TODO
83
- - VERSION
84
- - lib/ruby-units.rb
85
- - lib/ruby_units.rb
86
- - lib/ruby_units/array.rb
87
- - lib/ruby_units/cmath.rb
88
- - lib/ruby_units/complex.rb
89
- - lib/ruby_units/date.rb
90
- - lib/ruby_units/math.rb
91
- - lib/ruby_units/numeric.rb
92
- - lib/ruby_units/object.rb
93
- - lib/ruby_units/ruby-units.rb
94
- - lib/ruby_units/string.rb
95
- - lib/ruby_units/time.rb
96
- - lib/ruby_units/units.rb
97
- - lib/ruby_units/version.rb
98
- - ruby-units.gemspec
74
+ - spec/ruby-units/array_spec.rb
75
+ - spec/ruby-units/complex_spec.rb
76
+ - spec/ruby-units/date_spec.rb
77
+ - spec/ruby-units/math_spec.rb
78
+ - spec/ruby-units/numeric_spec.rb
79
+ - spec/ruby-units/object_spec.rb
80
+ - spec/ruby-units/string_spec.rb
81
+ - spec/ruby-units/time_spec.rb
82
+ - spec/ruby-units/unit_spec.rb
83
+ - spec/spec_helper.rb
84
+ - test/test_cache.rb
99
85
  - test/test_ruby-units.rb
100
- has_rdoc: true
101
86
  homepage: https://github.com/olbrich/ruby-units
102
87
  licenses: []
103
88
 
@@ -111,25 +96,30 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
96
  requirements:
112
97
  - - ">="
113
98
  - !ruby/object:Gem::Version
114
- hash: 3
115
- segments:
116
- - 0
117
99
  version: "0"
118
100
  required_rubygems_version: !ruby/object:Gem::Requirement
119
101
  none: false
120
102
  requirements:
121
- - - ">="
103
+ - - ">"
122
104
  - !ruby/object:Gem::Version
123
- hash: 3
124
- segments:
125
- - 0
126
- version: "0"
105
+ version: 1.3.1
127
106
  requirements: []
128
107
 
129
108
  rubyforge_project:
130
- rubygems_version: 1.3.7
109
+ rubygems_version: 1.7.2
131
110
  signing_key:
132
111
  specification_version: 3
133
112
  summary: A class that performs unit conversions and unit math
134
113
  test_files:
114
+ - spec/ruby-units/array_spec.rb
115
+ - spec/ruby-units/complex_spec.rb
116
+ - spec/ruby-units/date_spec.rb
117
+ - spec/ruby-units/math_spec.rb
118
+ - spec/ruby-units/numeric_spec.rb
119
+ - spec/ruby-units/object_spec.rb
120
+ - spec/ruby-units/string_spec.rb
121
+ - spec/ruby-units/time_spec.rb
122
+ - spec/ruby-units/unit_spec.rb
123
+ - spec/spec_helper.rb
124
+ - test/test_cache.rb
135
125
  - test/test_ruby-units.rb
data/CHANGELOG.txt DELETED
@@ -1,206 +0,0 @@
1
- Change Log for Ruby-units
2
- =========================
3
- 2010-11-07 1.2.0.a * a bunch of fixes to make ruby-units ruby 1.9 compatible
4
- (ruby 1.9.3dev (2010-11-07 trunk 29711) [i386-darwin9.8.0])
5
- 2010-03-16 1.1.5 * another bugfix, and update url to point to github
6
- 2010-03-15 1.1.4 * fixed a couple of outstanding bugs
7
- 2007-12-13 1.1.3 * fixed a minor bug with string %
8
- 2007-12-12 1.1.2 * fixed a bug with format strings
9
- * detect if ruby 1.8.6 is installed and use its' to_date function
10
-
11
- 2007-07-14 1.1.1 * fixed bug that would prevent creating '<pound-mass>' units, which
12
- prevented rounding from working
13
- * tests do not fail if Uncertain gem is not installed, you just get an
14
- annoying warning message
15
-
16
- 2007-01-28 1.1.0 * completely revamped the temperature handling system (see README)
17
- * fixed some spelling errors in some units
18
- * fixed to_datetime and to_date to convert durations to datetimes and dates'
19
-
20
- 2007-01-24 1.0.2 * Minor changes in the way powers are calculated to support Uncertain
21
- numbers better.
22
- * Fixed parsing bug with Uncertain Numbers
23
- * added resolution / typography units (pixels, points, pica)
24
- Note that 'pt' means 'pints' and not 'points'
25
- * added some pressure units ('inHg' & 'inH2O')
26
- * changed default abbreviation of 'knots' to 'kt'
27
- * Changed directory layout
28
- * fixed a minor bug with Time.to_date so comparisons work properly
29
-
30
- 2007-01-17 1.0.1 * Force units are now defined correctly.
31
-
32
- 2007-01-12 1.0.0 * Improved handling of complex numbers. Now you can specify
33
- '1+1i mm'.unit to get a complex unit.
34
- * Taking the root of a negative unit will give you a complex unit
35
- * fixed unary minus to work again
36
- * Math.hypot now takes units. Both parameters must be the compatible
37
- units or it will assert. Units will be converted to a common base
38
- before use.
39
- * Can now specify units in rational numbers, i.e., '1/4 cup'.unit
40
- * Seems like a good time to move to 1.0 status
41
-
42
- 2006-12-15 0.3.9 * forgot to increment the version in the gem file..ooops.
43
-
44
- 2006-12-15 0.3.8 * Any object that supports a 'to_unit' method will now be
45
- automatically coerced to a unit during math operations.
46
-
47
- 2006-12-14 0.3.7 * improved handling of percents and added a 'wt%' unit
48
- equivalent to 1 g/dl.
49
- * Improved handling for units with non-alphanumeric names
50
- (like ' for feet, # for pound)
51
- * Now you can enter durations as "HH:MM:SS, usec" or
52
- "HH:MM:SS:usec"
53
-
54
- 2006-12-05 0.3.6 * Fixed bug where (unit/unit).ceil would fail
55
-
56
- 2006-11-20 0.3.5 * Minor bug fixes
57
- * to_int now coerces the result to an actual Integer,
58
- but only works properly for unitless Units.
59
-
60
- 2006-10-27 0.3.4 * Fixed a few more parsing bugs so that it will properly
61
- complain about malformed units.
62
- * Fixed a bug that prevents proper use of percents
63
- * several minor tweaks
64
- * some improved Date and DateTime handling
65
- * can convert between Date, DateTime, and Time objects
66
- * Time math will now return a DateTime if it goes out of
67
- range.
68
-
69
- 2006-10-03 0.3.3 * Apparently I can't do math late at night.
70
- Fixed a bug that would cause problems when adding
71
- or subtracting units to a unit with a zero scalar.
72
- * Date and DateTime objects can be converted to 'units'
73
-
74
- 2006-10-03 0.3.2 * More minor bug fixes
75
- (now fixes a minor name collision with rails)
76
-
77
- 2006-10-02 0.3.1 * minor bug fixes
78
-
79
- 2006-10-02 0.3.0 * Performance enhanced by caching results of many
80
- functions (Thanks to Kurt Stephens for pushing this.)
81
- * Throws an exception if the unit is not recognized
82
- * units can now identify what 'kind' they are
83
- (:length, :mass, etc..)
84
- * New constructors:
85
- Unit(1,"mm")
86
- Unit(1,"mm/s")
87
- Unit(1,"mm","s")
88
-
89
- 2006-09-22 0.2.3 * added support for date/time parsing with the Chronic gem
90
- parsing will use Chronic if it is loaded
91
- * allows Date / Time / DateTime conversions
92
- * better test coverage
93
- * The 'string'.to_time returns a Time object
94
- * 'string'.to_datetime returns a DateTime object
95
- * 'string'.time returns a Time object or a DateTime if the
96
- Time object fails
97
- * 'string'.datetime returns a DateTime or a Time if the
98
- DateTime fails
99
-
100
- 2006-09-19 0.2.2 * tweaked temperature handling a bit. Now enter
101
- temperatures like this:
102
- '0 tempC'.unit #=> 273.15 degK
103
- They will always be converted to kelvin to avoid
104
- problems when temperatures are used in equations.
105
- * added Time.in("5 min")
106
- * added Unit.to_unit to simplify some calls
107
-
108
- 2006-09-18 0.2.1 * Trig math functions (sin, cos, tan, sinh, cosh, tanh)
109
- accept units that can be converted to radians
110
- Math.sin("90 deg".unit) => 1.0
111
- * Date and DateTime can be offset by a time unit
112
- (Date.today + "1 day".unit) => 2006-09-19
113
- Does not work with months since they aren't a consistent
114
- size
115
- * Tweaked time usage a bit
116
- Time.now + "1 hr".unit => Mon Sep 18 11:51:29 EDT 2006
117
- * can output time in 'hh:mm:ss' format by using
118
- 'unit.to_s(:time)'
119
- * added time helper methods
120
- ago,
121
- since(Time/DateTime),
122
- until(Time/DateTime),
123
- from(Time/DateTime),
124
- before(Time/DateTime), and
125
- after(Time/DateTime)
126
- * Time helpers also work on strings. In this case they
127
- are first converted to units
128
- '5 min'.from_now
129
- '1 week'.ago
130
- 'min'.since(time)
131
- 'min'.until(time)
132
- '1 day'.from()
133
- * Can pass Strings to time helpers and they will be parsed
134
- with ParseDate
135
- * Fixed most parsing bugs (I think)
136
- * Can pass a strftime format string to to_s to format time
137
- output
138
- * can use U'1 mm' or '1 mm'.u to specify units now
139
-
140
- 2006-09-17 * can now use the '%' format specifier like
141
- '%0.2f' % '1 mm'.unit #=> '1.00 mm'
142
- * works nicely with time now.
143
- '1 week'.unit + Time.now => 1.159e+09 s
144
- Time.at('1.159e+09 s'.unit)
145
- => Sat Sep 23 04:26:40 EDT 2006
146
- "1.159e9 s".unit.time
147
- => Sat Sep 23 04:26:40 EDT 2006
148
- * Time.now.unit => 1.159e9 s
149
- * works well with 'Uncertain' numerics
150
- (www.rubyforge.org/projects/uncertain)
151
- * Improved parsing
152
-
153
- 2006-08-28 0.2.0 * Added 'ruby_unit.rb' file so that requires will still
154
- work if the wrong name is used
155
- * Added 'to' as an alias to '>>' so conversions can be
156
- done as '1 m'.unit.to('1 cm')
157
- * Added ability to convert temperatures to absolute values
158
- using the following syntax:
159
- '37 degC'.unit.to('tempF') #=> '98.6 degF'.unit
160
- * Tweaked abbreviations a bit. 'ton' is now 'tn' instead
161
- of 't'. It was causing parse collisions with 'atm'.
162
- * fixed a bug in term elimination routine
163
- * fixed a bug in parsing of powers, and added support for
164
- 'm**2' format
165
- * Added support for taking roots of units. Just
166
- exponentiate with a fraction (0.5, 1.0/3, 0.25)
167
- * renamed 'quantity' to 'scalar'
168
- * any type of Numeric can be used to initialize a Unit,
169
- although this can't really be done with a string
170
- * Units can not be forced to a float using to_f unless
171
- they are unitless. This prevents some math functions
172
- from forcing the conversion. To get the scalar, just
173
- use 'unit.scalar'
174
- * 'inspect' returns string representation
175
- * better edge-case detection with math functions.
176
- "0 mm".unit**-1 now throws a ZeroDivisionError exception
177
- * Ranges can make a series of units, so long as the end
178
- points have integer scalars.
179
- * Fixed a parsing bug with feet/pounds and scientific
180
- numbers
181
-
182
- 2006-08-22 0.1.1 * Added new format option "1 mm".to_unit("in") now
183
- converts the result to the indicated units
184
- * Fixed some naming issues so that the gem name matches
185
- the require name.
186
- * Added CHANGELOG
187
- * Improved test coverage (100% code coverage via RCov)
188
- * fixed a bug that prevented units with a prefix in the
189
- denominator from converting properly
190
- * can use .unit method on a string to create a new unit
191
- object
192
- * can now coerce or define units from arrays, strings,
193
- numerics.
194
- "1 mm".unit + [1, 'mm'] === "2 mm".unit
195
- [1,'mm','s'].unit === "1 mm/s".unit
196
- 2.5.unit === "2.5".unit
197
- * Added instructions on how to add custom units
198
-
199
- 2006-08-22 0.1.0 * Initial Release
200
-
201
-
202
-
203
-
204
-
205
-
206
-