rails-units 1.4.1 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/RakeFile +1 -1
- data/lib/rails-units.rb +14 -0
- data/lib/{ruby_units → rails_units}/array.rb +0 -0
- data/lib/{ruby_units → rails_units}/cache.rb +0 -0
- data/lib/{ruby_units → rails_units}/date.rb +0 -0
- data/lib/{ruby_units → rails_units}/fixnum.rb +0 -0
- data/lib/{ruby_units → rails_units}/math.rb +0 -0
- data/lib/{ruby_units → rails_units}/numeric.rb +0 -0
- data/lib/{ruby_units → rails_units}/object.rb +0 -0
- data/lib/rails_units/string.rb +122 -0
- data/lib/{ruby_units → rails_units}/time.rb +0 -0
- data/lib/{ruby_units → rails_units}/unit.rb +0 -0
- data/lib/{ruby_units → rails_units}/unit_definitions.rb +0 -0
- data/lib/{ruby_units → rails_units}/version.rb +0 -0
- data/rails-units-1.3.1.gem +0 -0
- data/rails-units-1.4.1.gem +0 -0
- data/rails-units.gemspec +26 -24
- data/spec/{ruby-units → rails-units}/array_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/complex_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/date_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/math_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/numeric_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/object_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/string_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/time_spec.rb +0 -0
- data/spec/{ruby-units → rails-units}/unit_spec.rb +0 -0
- data/spec/spec_helper.rb +1 -1
- data/test/{test_ruby-units.rb → test_rails-units.rb} +189 -189
- metadata +34 -32
- data/lib/ruby-units.rb +0 -14
- data/lib/ruby_units.rb +0 -14
data/RakeFile
CHANGED
data/lib/rails-units.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
2
|
+
require "rails_units/version"
|
3
|
+
require "rails_units/cache"
|
4
|
+
require 'rails_units/array'
|
5
|
+
require 'rails_units/date'
|
6
|
+
require 'rails_units/time'
|
7
|
+
require 'rails_units/math'
|
8
|
+
require 'rails_units/numeric'
|
9
|
+
require 'rails_units/object'
|
10
|
+
require 'rails_units/string'
|
11
|
+
require 'rails_units/unit_definitions'
|
12
|
+
require 'rails_units/unit'
|
13
|
+
require 'rails_units/fixnum'
|
14
|
+
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'time'
|
2
|
+
# make a string into a unit
|
3
|
+
class String
|
4
|
+
def to_unit(other = nil)
|
5
|
+
other ? Unit.new(self).to(other) : Unit.new(self)
|
6
|
+
end
|
7
|
+
alias :unit :to_unit
|
8
|
+
alias :u :to_unit
|
9
|
+
alias :unit_format :%
|
10
|
+
|
11
|
+
# format unit output using formating codes '%0.2f' % '1 mm'.unit => '1.00 mm'
|
12
|
+
def %(*args)
|
13
|
+
return "" if self.empty?
|
14
|
+
case
|
15
|
+
when args.first.is_a?(Unit)
|
16
|
+
args.first.to_s(self)
|
17
|
+
when (!defined?(Uncertain).nil? && args.first.is_a?(Uncertain))
|
18
|
+
args.first.to_s(self)
|
19
|
+
when args.first.is_a?(Complex)
|
20
|
+
args.first.to_s
|
21
|
+
else
|
22
|
+
unit_format(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
#needed for compatibility with Rails, which defines a String.from method
|
27
|
+
if self.public_instance_methods.include? 'from'
|
28
|
+
alias :old_from :from
|
29
|
+
end
|
30
|
+
|
31
|
+
# "5 min".from("now")
|
32
|
+
def from(time_point = ::Time.now)
|
33
|
+
return old_from(time_point) if self.respond_to?(:old_from) && time_point.instance_of?(Integer)
|
34
|
+
self.unit.from(time_point)
|
35
|
+
end
|
36
|
+
|
37
|
+
alias :after :from
|
38
|
+
|
39
|
+
def from_now
|
40
|
+
self.from('now')
|
41
|
+
end
|
42
|
+
|
43
|
+
# "5 min".ago
|
44
|
+
def ago
|
45
|
+
self.unit.ago
|
46
|
+
end
|
47
|
+
|
48
|
+
def before(time_point = ::Time.now)
|
49
|
+
self.unit.before(time_point)
|
50
|
+
end
|
51
|
+
|
52
|
+
def before_now
|
53
|
+
self.before('now')
|
54
|
+
end
|
55
|
+
|
56
|
+
def since(time_point = ::Time.now)
|
57
|
+
self.unit.since(time_point)
|
58
|
+
end
|
59
|
+
|
60
|
+
def until(time_point = ::Time.now)
|
61
|
+
self.unit.until(time_point)
|
62
|
+
end
|
63
|
+
|
64
|
+
def time(options = {})
|
65
|
+
self.to_time(options) rescue self.to_datetime(options)
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_time(options = {})
|
69
|
+
begin
|
70
|
+
#raises exception when Chronic not defined or when it returns a nil (i.e., can't parse the input)
|
71
|
+
r = Chronic.parse(self,options)
|
72
|
+
raise(ArgumentError, 'Invalid Time String') unless r
|
73
|
+
return r
|
74
|
+
rescue
|
75
|
+
case
|
76
|
+
when self == "now"
|
77
|
+
Time.now
|
78
|
+
when Time.respond_to?(:parse)
|
79
|
+
Time.parse(self)
|
80
|
+
else
|
81
|
+
Time.local(*ParseDate.parsedate(self))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_datetime(options = {})
|
87
|
+
begin
|
88
|
+
# raises an exception if Chronic.parse = nil or if Chronic not defined
|
89
|
+
r = Chronic.parse(self,options).send(:to_datetime)
|
90
|
+
rescue Exception => e
|
91
|
+
r = case
|
92
|
+
when self.to_s == "now"
|
93
|
+
DateTime.now
|
94
|
+
else
|
95
|
+
DateTime.parse(self)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
raise RuntimeError, "Invalid Time String (#{self.to_s})" if r == DateTime.new
|
99
|
+
return r
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_date(options={})
|
103
|
+
begin
|
104
|
+
r = Chronic.parse(self,options).to_date
|
105
|
+
rescue
|
106
|
+
r = case
|
107
|
+
when self == "today"
|
108
|
+
Date.today
|
109
|
+
when RUBY_VERSION < "1.9"
|
110
|
+
Date.civil(*ParseDate.parsedate(self)[0..5].compact)
|
111
|
+
else
|
112
|
+
Date.parse(self)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
raise RuntimeError, 'Invalid Date String' if r == Date.new
|
116
|
+
return r
|
117
|
+
end
|
118
|
+
|
119
|
+
def datetime(options = {})
|
120
|
+
self.to_datetime(options) rescue self.to_time(options)
|
121
|
+
end
|
122
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
Binary file
|
Binary file
|
data/rails-units.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rails-units}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.5.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Kevin Olbrich, Ph.D.}]
|
@@ -26,33 +26,35 @@ Gem::Specification.new do |s|
|
|
26
26
|
"RakeFile",
|
27
27
|
"TODO",
|
28
28
|
"VERSION",
|
29
|
-
"lib/
|
30
|
-
"lib/
|
31
|
-
"lib/
|
32
|
-
"lib/
|
33
|
-
"lib/
|
34
|
-
"lib/
|
35
|
-
"lib/
|
36
|
-
"lib/
|
37
|
-
"lib/
|
29
|
+
"lib/rails-units.rb",
|
30
|
+
"lib/rails_units/array.rb",
|
31
|
+
"lib/rails_units/cache.rb",
|
32
|
+
"lib/rails_units/date.rb",
|
33
|
+
"lib/rails_units/fixnum.rb",
|
34
|
+
"lib/rails_units/math.rb",
|
35
|
+
"lib/rails_units/numeric.rb",
|
36
|
+
"lib/rails_units/object.rb",
|
37
|
+
"lib/rails_units/string.rb",
|
38
|
+
"lib/rails_units/time.rb",
|
39
|
+
"lib/rails_units/unit.rb",
|
40
|
+
"lib/rails_units/unit_definitions.rb",
|
41
|
+
"lib/rails_units/version.rb",
|
38
42
|
"lib/ruby_units/string.rb",
|
39
|
-
"
|
40
|
-
"
|
41
|
-
"lib/ruby_units/unit_definitions.rb",
|
42
|
-
"lib/ruby_units/version.rb",
|
43
|
+
"rails-units-1.3.1.gem",
|
44
|
+
"rails-units-1.4.1.gem",
|
43
45
|
"rails-units.gemspec",
|
44
|
-
"spec/
|
45
|
-
"spec/
|
46
|
-
"spec/
|
47
|
-
"spec/
|
48
|
-
"spec/
|
49
|
-
"spec/
|
50
|
-
"spec/
|
51
|
-
"spec/
|
52
|
-
"spec/
|
46
|
+
"spec/rails-units/array_spec.rb",
|
47
|
+
"spec/rails-units/complex_spec.rb",
|
48
|
+
"spec/rails-units/date_spec.rb",
|
49
|
+
"spec/rails-units/math_spec.rb",
|
50
|
+
"spec/rails-units/numeric_spec.rb",
|
51
|
+
"spec/rails-units/object_spec.rb",
|
52
|
+
"spec/rails-units/string_spec.rb",
|
53
|
+
"spec/rails-units/time_spec.rb",
|
54
|
+
"spec/rails-units/unit_spec.rb",
|
53
55
|
"spec/spec_helper.rb",
|
54
56
|
"test/test_cache.rb",
|
55
|
-
"test/
|
57
|
+
"test/test_rails-units.rb"
|
56
58
|
]
|
57
59
|
s.homepage = %q{https://github.com/scpiek/rails-units}
|
58
60
|
s.require_paths = [%q{lib}]
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
|
-
require '
|
3
|
+
require 'rails-units'
|
4
4
|
require 'yaml'
|
5
5
|
begin
|
6
|
-
|
6
|
+
require 'chronic'
|
7
7
|
rescue LoadError
|
8
|
-
|
8
|
+
warn "Can't test Chronic integration unless gem 'chronic' is installed"
|
9
9
|
end
|
10
|
-
begin
|
11
|
-
|
10
|
+
begin
|
11
|
+
require 'uncertain'
|
12
12
|
rescue LoadError
|
13
|
-
|
13
|
+
warn "Can't test Uncertain Units unless 'Uncertain' gem is installed"
|
14
14
|
end
|
15
15
|
|
16
16
|
|
@@ -29,12 +29,12 @@ class Time
|
|
29
29
|
@@forced_now.nil? ? unforced_now : @@forced_now
|
30
30
|
end
|
31
31
|
alias :now :forced_now
|
32
|
-
|
32
|
+
|
33
33
|
def forced_now=(now)
|
34
34
|
@@forced_now = now
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
alias :unforced_gmt :gmt_offset
|
39
39
|
def forced_gmt
|
40
40
|
@@forced_gmt.nil? ? unforced_gmt : @@forced_gmt
|
@@ -44,7 +44,7 @@ class Time
|
|
44
44
|
def forced_gmt=(gmt)
|
45
45
|
@@forced_gmt = now
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
end
|
49
49
|
|
50
50
|
class DateTime
|
@@ -55,11 +55,11 @@ class DateTime
|
|
55
55
|
return @@forced_now ? @@forced_now : unforced_now
|
56
56
|
end
|
57
57
|
alias :now :forced_now
|
58
|
-
|
58
|
+
|
59
59
|
def forced_now=(now)
|
60
60
|
@@forced_now = now
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -70,20 +70,20 @@ class Dummy
|
|
70
70
|
end
|
71
71
|
|
72
72
|
class TestRubyUnits < Test::Unit::TestCase
|
73
|
-
|
73
|
+
|
74
74
|
def setup
|
75
75
|
@april_fools = Time.at 1143910800
|
76
76
|
@april_fools_datetime = DateTime.parse('2006-04-01T12:00:00-05:00')
|
77
77
|
Time.forced_now = @april_fools
|
78
|
-
DateTime.forced_now = @april_fools_datetime
|
78
|
+
DateTime.forced_now = @april_fools_datetime
|
79
79
|
#Unit.clear_cache
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def teardown
|
83
83
|
Time.forced_now = nil
|
84
84
|
DateTime.forced_now = nil
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
def test_to_yaml
|
88
88
|
unit = "1 mm".u
|
89
89
|
if RUBY_PLATFORM == "java"
|
@@ -107,38 +107,38 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
107
107
|
assert_equal "01:00", "min".since(Time.now - 3600).to_s("%H:%M")
|
108
108
|
assert_in_delta Time.now, "now".time, 1
|
109
109
|
end
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
110
|
+
|
111
|
+
def test_from_now
|
112
|
+
assert_equal "1 day".from_now, @april_fools + 86400
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_from
|
116
|
+
assert_equal "1 day".from("now"), @april_fools + 86400
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_ago
|
120
|
+
assert_equal "1 day".ago, @april_fools - 86400
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_before_now
|
124
|
+
assert_equal "1 day".before_now, @april_fools - 86400
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_before
|
128
|
+
assert_equal '1 days'.before('now'), @april_fools - 86400
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_since
|
132
132
|
assert_equal 'days'.since(@april_fools - 86400), "1 day".unit
|
133
133
|
assert_equal 'days'.since('2006-3-31 12:00:00 -5:00'), "1 day".unit
|
134
|
-
assert_equal 'days'.since(DateTime.parse('2006-3-31 12:00 -5:00')), "1 day".unit
|
134
|
+
assert_equal 'days'.since(DateTime.parse('2006-3-31 12:00 -5:00')), "1 day".unit
|
135
135
|
assert_raises(ArgumentError) { 'days'.since(1) }
|
136
|
-
|
136
|
+
end
|
137
137
|
|
138
|
-
|
139
|
-
assert_equal 'days'.until('2006-04-2 12:00:00 -5:00'), '1 day'.unit
|
138
|
+
def test_until
|
139
|
+
assert_equal 'days'.until('2006-04-2 12:00:00 -5:00'), '1 day'.unit
|
140
140
|
assert_raises(ArgumentError) { 'days'.until(1) }
|
141
|
-
|
141
|
+
end
|
142
142
|
|
143
143
|
def test_time_helpers
|
144
144
|
assert_equal @april_fools, Time.now
|
@@ -149,33 +149,33 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
149
149
|
assert_equal @april_fools.unit.to_time, @april_fools
|
150
150
|
assert_equal Time.in('1 day'), @april_fools + 86400
|
151
151
|
assert_equal "2006-04-01T12:00:00-05:00", @april_fools_datetime.inspect
|
152
|
-
assert_equal "2006-04-01T00:00:00+00:00", '2453826.5 days'.unit.to_datetime.to_s
|
152
|
+
assert_equal "2006-04-01T00:00:00+00:00", '2453826.5 days'.unit.to_datetime.to_s
|
153
153
|
end
|
154
|
-
|
154
|
+
|
155
155
|
def test_string_helpers
|
156
156
|
assert_equal '1 mm'.to('in'), Unit('1 mm').to('in')
|
157
157
|
end
|
158
158
|
|
159
159
|
[:sin, :cos, :tan, :sinh, :cosh, :tanh].each do |trig|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
160
|
+
define_method("test_#{trig}") do
|
161
|
+
assert_equal Math.send(trig, Math::PI), Math.send(trig, "180 deg".unit)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
165
|
def test_clone
|
166
166
|
unit1= "1 mm".unit
|
167
167
|
unit2 = unit1.clone
|
168
168
|
assert_not_equal unit1.numerator.object_id, unit2.numerator.object_id
|
169
169
|
assert_not_equal unit1.denominator.object_id, unit2.denominator.object_id
|
170
170
|
assert unit1 === unit2
|
171
|
-
end
|
172
|
-
|
171
|
+
end
|
172
|
+
|
173
173
|
def test_unary_minus
|
174
174
|
unit1 = Unit.new("1 mm^2")
|
175
175
|
unit2 = Unit.new("-1 mm^2")
|
176
176
|
assert_equal unit1, -unit2
|
177
177
|
end
|
178
|
-
|
178
|
+
|
179
179
|
def test_unary_plus
|
180
180
|
unit1 = Unit.new("1 mm")
|
181
181
|
assert_equal unit1, +unit1
|
@@ -186,14 +186,14 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
186
186
|
assert_equal ['<meter>'], unit1.numerator
|
187
187
|
assert_equal 1.0, unit1.scalar
|
188
188
|
end
|
189
|
-
|
189
|
+
|
190
190
|
def test_to_base
|
191
191
|
unit1 = Unit.new("100 cm")
|
192
192
|
assert_in_delta 1, unit1.to_base.scalar, 0.001
|
193
193
|
unit2 = Unit("1 mm^2 ms^-2")
|
194
|
-
assert_in_delta 1, unit2.to_base.scalar, 0.001
|
194
|
+
assert_in_delta 1, unit2.to_base.scalar, 0.001
|
195
195
|
end
|
196
|
-
|
196
|
+
|
197
197
|
def test_to_unit
|
198
198
|
unit1 = "1 mm".to_unit
|
199
199
|
assert_equal unit1, unit1.to_unit
|
@@ -202,7 +202,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
202
202
|
assert Unit === unit1
|
203
203
|
assert unit1 == unit2
|
204
204
|
unit1 = "2.54 cm".to_unit("in")
|
205
|
-
assert_in_delta 1, unit1.scalar, 0.001
|
205
|
+
assert_in_delta 1, unit1.scalar, 0.001
|
206
206
|
assert_equal ['<inch>'], unit1.numerator
|
207
207
|
unit1 = "2.54 cm".unit("in")
|
208
208
|
assert_in_delta 1, unit1.scalar, 0.001
|
@@ -214,7 +214,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
214
214
|
assert_in_delta 1, unit1.scalar, 0.001
|
215
215
|
assert_equal ['<milli>','<meter>'], unit1.numerator
|
216
216
|
end
|
217
|
-
|
217
|
+
|
218
218
|
def test_create_unitless
|
219
219
|
unit1 = Unit("1")
|
220
220
|
assert_equal 1, unit1.to_f
|
@@ -225,28 +225,28 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
225
225
|
assert_equal ['<1>'],unit1.numerator
|
226
226
|
assert_equal ['<1>'],unit1.denominator
|
227
227
|
end
|
228
|
-
|
228
|
+
|
229
229
|
def test_create_simple
|
230
230
|
unit1 = Unit.new("1 m")
|
231
231
|
assert_equal 1,unit1.scalar
|
232
232
|
assert_equal ['<meter>'], unit1.numerator
|
233
233
|
assert_equal ['<1>'],unit1.denominator
|
234
234
|
end
|
235
|
-
|
235
|
+
|
236
236
|
def test_create_compound
|
237
237
|
unit1 = Unit.new("1 N*m")
|
238
|
-
assert_equal 1,unit1.scalar
|
238
|
+
assert_equal 1,unit1.scalar
|
239
239
|
assert_equal ['<newton>','<meter>'],unit1.numerator
|
240
240
|
assert_equal ['<1>'],unit1.denominator
|
241
241
|
end
|
242
|
-
|
242
|
+
|
243
243
|
def test_create_with_denominator
|
244
244
|
unit1 = Unit.new("1 m/s")
|
245
245
|
assert_equal 1, unit1.scalar
|
246
246
|
assert_equal ['<meter>'],unit1.numerator
|
247
247
|
assert_equal ['<second>'],unit1.denominator
|
248
248
|
end
|
249
|
-
|
249
|
+
|
250
250
|
def test_create_with_powers
|
251
251
|
unit1 = Unit.new("1 m^2/s^2")
|
252
252
|
assert_equal 1, unit1.scalar
|
@@ -256,7 +256,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
256
256
|
assert_equal 1, unit1.scalar
|
257
257
|
assert_equal ['<meter>','<meter>','<kilogram>','<kilogram>','<joule>','<joule>'],unit1.numerator
|
258
258
|
assert_equal ['<second>','<second>'],unit1.denominator
|
259
|
-
|
259
|
+
|
260
260
|
end
|
261
261
|
|
262
262
|
def test_create_with_zero_power
|
@@ -272,37 +272,37 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
272
272
|
assert_equal ['<meter>','<meter>'],unit1.numerator
|
273
273
|
assert_equal ['<second>','<second>'],unit1.denominator
|
274
274
|
end
|
275
|
-
|
275
|
+
|
276
276
|
def test_create_from_array
|
277
277
|
unit1 = Unit.new(1, "mm^2", "ul^2")
|
278
278
|
assert_equal 1, unit1.scalar
|
279
279
|
assert_equal ['<milli>','<meter>','<milli>','<meter>'], unit1.numerator
|
280
280
|
assert_equal ['<micro>','<liter>','<micro>','<liter>'], unit1.denominator
|
281
281
|
end
|
282
|
-
|
282
|
+
|
283
283
|
def test_bad_create
|
284
284
|
assert_raises(ArgumentError) { Unit.new(nil)}
|
285
285
|
assert_raises(ArgumentError) { Unit.new(true)}
|
286
286
|
assert_raises(ArgumentError) { Unit.new(false)}
|
287
287
|
assert_raises(ArgumentError) { Unit.new(/(.+)/)}
|
288
288
|
end
|
289
|
-
|
289
|
+
|
290
290
|
def test_convert
|
291
291
|
unit1 = Unit.new("1 attoparsec/microfortnight")
|
292
292
|
assert_nothing_raised {
|
293
293
|
unit2 = unit1 >> "in/s"
|
294
294
|
assert_equal ['<inch>'],unit2.numerator
|
295
295
|
assert_equal ['<second>'],unit2.denominator
|
296
|
-
assert_in_delta 1.0043269330917,unit2.scalar,0.00001
|
296
|
+
assert_in_delta 1.0043269330917,unit2.scalar,0.00001
|
297
297
|
}
|
298
298
|
end
|
299
|
-
|
299
|
+
|
300
300
|
def test_add_operator
|
301
301
|
a = '0 mm'.unit
|
302
302
|
b = '10 cm'.unit
|
303
303
|
c = '1 in'.unit
|
304
304
|
d = '1 ml'.unit
|
305
|
-
|
305
|
+
|
306
306
|
assert_equal((a+b), b)
|
307
307
|
assert_equal((a+b).units, b.units)
|
308
308
|
assert_equal((b+a), b)
|
@@ -313,13 +313,13 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
313
313
|
b + d
|
314
314
|
}
|
315
315
|
end
|
316
|
-
|
316
|
+
|
317
317
|
def test_subtract_operator
|
318
318
|
a = '0 mm'.unit
|
319
319
|
b = '10 cm'.unit
|
320
320
|
c = '1 in'.unit
|
321
321
|
d = '1 ml'.unit
|
322
|
-
|
322
|
+
|
323
323
|
assert_equal((a-b), -b)
|
324
324
|
assert_equal((a-b).units, b.units)
|
325
325
|
assert_equal((b-a), b)
|
@@ -330,7 +330,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
330
330
|
b - d
|
331
331
|
}
|
332
332
|
end
|
333
|
-
|
333
|
+
|
334
334
|
def test_convert_to
|
335
335
|
unit1 = Unit.new("1 mm")
|
336
336
|
unit2 = Unit.new("1 ft")
|
@@ -347,8 +347,8 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
347
347
|
assert_equal unit1, unit1.to(false)
|
348
348
|
assert_equal unit1, unit1.to(nil)
|
349
349
|
end
|
350
|
-
|
351
|
-
def test_compare
|
350
|
+
|
351
|
+
def test_compare
|
352
352
|
unit1 = "1 mm".unit
|
353
353
|
unit2 = "1 mm".unit
|
354
354
|
unit3 = unit2 >> "in"
|
@@ -356,24 +356,24 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
356
356
|
assert !(unit1 === unit3)
|
357
357
|
assert unit1 === "1 mm"
|
358
358
|
end
|
359
|
-
|
359
|
+
|
360
360
|
def test_matched_units
|
361
361
|
unit1 = Unit.new("1 m*kg/s")
|
362
362
|
unit2 = Unit.new("1 in*pound/min")
|
363
363
|
assert unit1 =~ unit2
|
364
364
|
end
|
365
|
-
|
365
|
+
|
366
366
|
def test_matched_units_using_string
|
367
367
|
unit1 = Unit.new("1 m*kg/s")
|
368
368
|
assert unit1 =~ "in*pound/min"
|
369
369
|
end
|
370
|
-
|
370
|
+
|
371
371
|
def test_unmatched_units
|
372
372
|
unit1 = Unit.new("1 m*kg/s")
|
373
373
|
unit2 = Unit.new("1 mm")
|
374
374
|
assert unit1 !~ unit2
|
375
375
|
end
|
376
|
-
|
376
|
+
|
377
377
|
def test_comparison_like_units
|
378
378
|
unit1 = Unit.new("1 in")
|
379
379
|
unit2 = Unit.new("1 mm")
|
@@ -381,21 +381,21 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
381
381
|
assert unit1 > unit2
|
382
382
|
}
|
383
383
|
end
|
384
|
-
|
384
|
+
|
385
385
|
def test_comparison_unlike_units
|
386
386
|
unit1 = Unit.new("1 kg")
|
387
387
|
unit2 = Unit.new("1 mm")
|
388
388
|
assert_raise(ArgumentError) { unit1 > unit2 }
|
389
389
|
end
|
390
|
-
|
390
|
+
|
391
391
|
def test_add_like_units
|
392
392
|
unit1 = Unit.new("1 mm")
|
393
393
|
unit2 = Unit.new("2 mm")
|
394
|
-
assert_nothing_raised {
|
394
|
+
assert_nothing_raised {
|
395
395
|
assert_equal 3.0, (unit1+unit2).scalar
|
396
396
|
}
|
397
397
|
end
|
398
|
-
|
398
|
+
|
399
399
|
def test_add_similar_units
|
400
400
|
unit1 = Unit.new("1 cm")
|
401
401
|
unit2 = Unit.new("1 in")
|
@@ -413,13 +413,13 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
413
413
|
assert_in_delta(-1.54, unit3.scalar, 0.01)
|
414
414
|
}
|
415
415
|
end
|
416
|
-
|
416
|
+
|
417
417
|
def test_add_unlike_units
|
418
418
|
unit1 = Unit.new("1 mm")
|
419
419
|
unit2 = Unit.new("2 ml")
|
420
420
|
assert_raise(ArgumentError) {(unit1+unit2).scalar}
|
421
421
|
end
|
422
|
-
|
422
|
+
|
423
423
|
def test_add_coerce
|
424
424
|
unit1 = "1 mm".unit
|
425
425
|
assert_nothing_raised {
|
@@ -460,12 +460,12 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
460
460
|
assert_equal "1".unit, unit2
|
461
461
|
}
|
462
462
|
end
|
463
|
-
|
463
|
+
|
464
464
|
def test_signature #"1 m s deg K kg A mol cd byte rad
|
465
465
|
unit1 = Unit.new("1 m*s*degK*kg*A*mol*cd*byte*rad*dollar")
|
466
466
|
assert_equal unit1.signature, (0..9).inject(0) {|product, n| product + 20**n}
|
467
467
|
end
|
468
|
-
|
468
|
+
|
469
469
|
def test_subtract_like_units
|
470
470
|
unit1 = Unit.new("1 mm")
|
471
471
|
unit2 = Unit.new("2 mm")
|
@@ -473,23 +473,23 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
473
473
|
assert_equal(-1, (unit1-unit2).scalar)
|
474
474
|
}
|
475
475
|
end
|
476
|
-
|
476
|
+
|
477
477
|
def test_subtract_unlike_units
|
478
478
|
unit1 = Unit.new("1 mm")
|
479
479
|
unit2 = Unit.new("2 ml")
|
480
480
|
assert_raise(ArgumentError) {(unit1-unit2).scalar}
|
481
481
|
end
|
482
|
-
|
482
|
+
|
483
483
|
def test_multiply
|
484
484
|
unit1 = Unit.new("1 m/ms")
|
485
485
|
unit2 = Unit.new("1 m/ms")
|
486
486
|
assert_nothing_raised {
|
487
487
|
unit3 = unit1 * unit2
|
488
|
-
assert_equal Unit.new("1 m^2/ms^2"), unit3
|
488
|
+
assert_equal Unit.new("1 m^2/ms^2"), unit3
|
489
489
|
}
|
490
490
|
assert_equal unit1 * 0, '0 m/ms'.unit
|
491
491
|
end
|
492
|
-
|
492
|
+
|
493
493
|
def test_divide
|
494
494
|
unit1 = Unit.new("200 M*g/mol")
|
495
495
|
unit2 = Unit.new("200 g/mole")
|
@@ -502,19 +502,19 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
502
502
|
assert_raises(ZeroDivisionError) {
|
503
503
|
unit1 / unit3
|
504
504
|
}
|
505
|
-
|
505
|
+
|
506
506
|
assert_raises(ZeroDivisionError) {
|
507
507
|
unit1 / 0
|
508
508
|
}
|
509
509
|
end
|
510
|
-
|
510
|
+
|
511
511
|
def test_inverse
|
512
512
|
unit1 = Unit.new("1 m")
|
513
513
|
unit2 = Unit.new("1 1/m")
|
514
514
|
assert_equal unit2, unit1.inverse
|
515
515
|
assert_raises((ZeroDivisionError)) { 0.unit.inverse }
|
516
516
|
end
|
517
|
-
|
517
|
+
|
518
518
|
def test_exponentiate_positive
|
519
519
|
unit1 = Unit.new("1 mm")
|
520
520
|
unit2 = Unit.new("1 mm^2")
|
@@ -522,12 +522,12 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
522
522
|
assert_equal unit2, unit1**2
|
523
523
|
}
|
524
524
|
end
|
525
|
-
|
525
|
+
|
526
526
|
def test_exponentiate_float
|
527
527
|
unit1 = Unit.new("1 mm")
|
528
528
|
assert_raise(ArgumentError) {unit1**2.5}
|
529
529
|
end
|
530
|
-
|
530
|
+
|
531
531
|
def test_exponentiate_negative
|
532
532
|
unit1 = Unit.new("1 m")
|
533
533
|
unit2 = Unit.new("1 m^-2")
|
@@ -547,84 +547,84 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
547
547
|
}
|
548
548
|
assert_equal 1, "0 mm".unit**0
|
549
549
|
end
|
550
|
-
|
550
|
+
|
551
551
|
def test_abs
|
552
552
|
unit1 = Unit.new("-1 mm")
|
553
553
|
assert_equal "1 mm".unit, unit1.abs
|
554
554
|
end
|
555
|
-
|
555
|
+
|
556
556
|
def test_ceil
|
557
557
|
unit1 = Unit.new("1.1 mm")
|
558
558
|
unit2 = Unit.new("2 mm")
|
559
559
|
assert_equal unit2, unit1.ceil
|
560
|
-
assert_equal(('1 mm'.unit / '1 mm'.unit).ceil, 1)
|
560
|
+
assert_equal(('1 mm'.unit / '1 mm'.unit).ceil, 1)
|
561
561
|
assert_equal("11 kg*m".unit, ("1003 kg*m".unit / 100).ceil)
|
562
562
|
end
|
563
|
-
|
563
|
+
|
564
564
|
def test_floor
|
565
565
|
unit1 = Unit.new("1.1 mm")
|
566
566
|
unit2 = Unit.new("1 mm")
|
567
567
|
assert_equal unit2, unit1.floor
|
568
568
|
assert_equal(('1 mm'.unit / '1 mm'.unit).floor, 1)
|
569
569
|
end
|
570
|
-
|
570
|
+
|
571
571
|
def test_to_int
|
572
572
|
assert_raises(RuntimeError) {Unit.new("1.1 mm").to_i}
|
573
573
|
assert_nothing_raised {Unit.new(10.5).to_i}
|
574
574
|
end
|
575
|
-
|
575
|
+
|
576
576
|
def test_truncate
|
577
577
|
unit1 = Unit.new("1.1 mm")
|
578
578
|
unit2 = Unit.new("1 mm")
|
579
579
|
assert_equal unit2, unit1.truncate
|
580
580
|
assert_equal((unit1/unit2).truncate, 1)
|
581
581
|
end
|
582
|
-
|
582
|
+
|
583
583
|
def test_round
|
584
584
|
unit1 = Unit.new("1.1 mm")
|
585
585
|
unit2 = Unit.new("1 mm")
|
586
586
|
assert_equal unit2, unit1.round
|
587
587
|
assert_equal((unit1/unit2).round, 1)
|
588
588
|
end
|
589
|
-
|
589
|
+
|
590
590
|
def test_zero?
|
591
591
|
unit1 = Unit.new("0")
|
592
592
|
assert unit1.zero?
|
593
593
|
end
|
594
|
-
|
594
|
+
|
595
595
|
def test_nonzero?
|
596
596
|
unit1 = Unit.new("0")
|
597
597
|
unit2 = Unit.new("1 mm")
|
598
598
|
assert_nil unit1.nonzero?
|
599
599
|
assert_equal unit2, unit2.nonzero?
|
600
600
|
end
|
601
|
-
|
601
|
+
|
602
602
|
def test_equality
|
603
603
|
unit1 = Unit.new("1 cm")
|
604
604
|
unit2 = Unit.new("10 mm")
|
605
605
|
assert unit1 == unit2
|
606
606
|
end
|
607
|
-
|
607
|
+
|
608
608
|
def test_temperature_conversions
|
609
609
|
assert_raises(ArgumentError) { '-1 tempK'.unit}
|
610
610
|
assert_raises(ArgumentError) { '-1 tempR'.unit}
|
611
611
|
assert_raises(ArgumentError) { '-1000 tempC'.unit}
|
612
612
|
assert_raises(ArgumentError) { '-1000 tempF'.unit}
|
613
|
-
|
613
|
+
|
614
614
|
assert_in_delta '32 tempF'.unit.base_scalar, '0 tempC'.unit.base_scalar, 0.01
|
615
615
|
assert_in_delta '0 tempC'.unit.base_scalar, '32 tempF'.unit.base_scalar, 0.01
|
616
616
|
assert_in_delta '0 tempC'.unit.base_scalar, '273.15 tempK'.unit.base_scalar, 0.01
|
617
617
|
assert_in_delta '0 tempC'.unit.base_scalar, '491.67 tempR'.unit.base_scalar, 0.01
|
618
|
-
|
618
|
+
|
619
619
|
a = '10 degC'.unit
|
620
620
|
assert_equal a >> 'tempC', '-263.15 tempC'.unit
|
621
621
|
assert_equal a >> 'tempK', '10 tempK'.unit
|
622
622
|
assert_equal a >> 'tempR', '18 tempR'.unit
|
623
623
|
assert_equal a >> 'tempF', '-441.67 tempF'.unit
|
624
|
-
|
624
|
+
|
625
625
|
unit1 = '37 tempC'.unit
|
626
626
|
assert_equal unit1 >> 'tempF' >> 'tempK' >> 'tempR' >> 'tempC', unit1
|
627
|
-
|
627
|
+
|
628
628
|
a = '100 tempF'.unit
|
629
629
|
b = '10 degC'.unit
|
630
630
|
c = '50 tempF'.unit
|
@@ -640,7 +640,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
640
640
|
assert_raises(ArgumentError) { c - '400 degK'.unit}
|
641
641
|
assert_equal a, a.to('tempF')
|
642
642
|
end
|
643
|
-
|
643
|
+
|
644
644
|
def test_feet
|
645
645
|
unit1 = Unit.new("6'6\"")
|
646
646
|
assert_in_delta 6.5, unit1.scalar, 0.01
|
@@ -650,13 +650,13 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
650
650
|
assert_equal unit3, '6 inch'.unit
|
651
651
|
|
652
652
|
end
|
653
|
-
|
653
|
+
|
654
654
|
def test_pounds
|
655
655
|
unit1 = Unit.new("8 pounds, 8 ounces")
|
656
656
|
assert_in_delta 8.5, unit1.scalar, 0.01
|
657
657
|
assert_equal '150#'.unit, '150 lbs'.unit
|
658
658
|
end
|
659
|
-
|
659
|
+
|
660
660
|
# these units are 'ambiguous' and could be mis-parsed
|
661
661
|
def test_parse_tricky_units
|
662
662
|
unit1 = Unit.new('1 mm') #sometimes parsed as 'm*m'
|
@@ -666,13 +666,13 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
666
666
|
unit3 = Unit.new('1 min') # could be a 'milli-inch' instead of a minute
|
667
667
|
assert_equal ['<minute>'], unit3.numerator
|
668
668
|
unit4 = Unit.new('1 ft') # could be a 'femto-ton' instead of a foot
|
669
|
-
assert_equal ['<foot>'], unit4.numerator
|
669
|
+
assert_equal ['<foot>'], unit4.numerator
|
670
670
|
unit5 = "1 atm".unit
|
671
671
|
assert_equal ['<atm>'], unit5.numerator
|
672
672
|
unit6 = "1 doz".unit
|
673
673
|
assert_equal ['<dozen>'], unit6.numerator
|
674
674
|
end
|
675
|
-
|
675
|
+
|
676
676
|
def test_to_s
|
677
677
|
unit1 = Unit.new("1")
|
678
678
|
assert_equal "1", unit1.to_s
|
@@ -694,7 +694,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
694
694
|
assert_equal("1.5 mm", Unit.new("1.5 mm").to_s)
|
695
695
|
assert_equal("1.5 mm", "#{Unit.new('1.5 mm')}")
|
696
696
|
end
|
697
|
-
|
697
|
+
|
698
698
|
def test_to_feet_inches
|
699
699
|
unit1 = Unit.new("6'5\"")
|
700
700
|
assert_equal "6'5\"", unit1.to_s(:ft)
|
@@ -703,7 +703,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
703
703
|
unit1.to_s(:ft)
|
704
704
|
}
|
705
705
|
end
|
706
|
-
|
706
|
+
|
707
707
|
def test_to_lbs_oz
|
708
708
|
unit1 = Unit.new("8 lbs 8 oz")
|
709
709
|
assert_equal "8 lbs, 8 oz", unit1.to_s(:lbs)
|
@@ -712,12 +712,12 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
712
712
|
unit1.to_s(:lbs)
|
713
713
|
}
|
714
714
|
end
|
715
|
-
|
715
|
+
|
716
716
|
def test_add_units
|
717
717
|
a = Unit.new("1 inchworm")
|
718
718
|
assert_equal "1 inworm", a.to_s
|
719
719
|
end
|
720
|
-
|
720
|
+
|
721
721
|
def test_ideal_gas_law
|
722
722
|
p = Unit "100 kPa"
|
723
723
|
v = Unit "1 m^3"
|
@@ -726,20 +726,20 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
726
726
|
t = ((p*v)/(n*r)).to('tempK')
|
727
727
|
assert_in_delta 12027.16,t.base_scalar, 0.1
|
728
728
|
end
|
729
|
-
|
730
|
-
|
729
|
+
|
730
|
+
|
731
731
|
def test_eliminate_terms
|
732
732
|
a = ['<meter>','<meter>','<kelvin>','<second>']
|
733
733
|
b = ['<meter>','<meter>','<second>']
|
734
734
|
h = Unit.eliminate_terms(1,a,b)
|
735
735
|
assert_equal ['<kelvin>'], h[:numerator]
|
736
736
|
end
|
737
|
-
|
737
|
+
|
738
738
|
def test_to_base_consistency
|
739
739
|
a = "1 W*m/J*s".unit
|
740
740
|
assert_equal a.signature, a.to_base.signature
|
741
741
|
end
|
742
|
-
|
742
|
+
|
743
743
|
def test_unit_roots
|
744
744
|
unit1 = Unit "2 m*J*kg"
|
745
745
|
unit2 = Unit "4 m^2*J^2*kg^2"
|
@@ -747,68 +747,68 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
747
747
|
assert_equal unit2**(1/2), unit1
|
748
748
|
assert_equal unit3**(1/3), unit1
|
749
749
|
end
|
750
|
-
|
750
|
+
|
751
751
|
def test_inspect
|
752
752
|
unit1 = Unit "1 mm"
|
753
753
|
assert_equal "1 mm", unit1.inspect
|
754
754
|
assert_not_equal "1.0 mm", unit1.inspect(:dump)
|
755
755
|
end
|
756
|
-
|
756
|
+
|
757
757
|
def test_to_f
|
758
758
|
assert_equal 1, 1.unit.to_f
|
759
759
|
assert_raises(RuntimeError) {
|
760
760
|
assert_equal 1, "1 mm".unit.to_f
|
761
761
|
}
|
762
762
|
end
|
763
|
-
|
763
|
+
|
764
764
|
def test_exponentiate_float2
|
765
|
-
assert_equal "2 m".unit, "4 m^2".unit**(0.5)
|
765
|
+
assert_equal "2 m".unit, "4 m^2".unit**(0.5)
|
766
766
|
assert_raises(ArgumentError) { "1 mm".unit**(2/3)}
|
767
767
|
assert_raises(ArgumentError) { "1 mm".unit**("A")}
|
768
|
-
|
768
|
+
|
769
769
|
end
|
770
|
-
|
770
|
+
|
771
771
|
def test_range
|
772
772
|
a = Unit "1 mm"
|
773
773
|
b = Unit "3 mm"
|
774
774
|
c = (a..b).to_a
|
775
775
|
assert_equal ["1 mm".unit, "2 mm".unit, "3 mm".unit], c
|
776
776
|
end
|
777
|
-
|
777
|
+
|
778
778
|
def test_scientific
|
779
779
|
a = Unit "1e6 cells"
|
780
780
|
assert_equal 1e6, a.scalar
|
781
781
|
assert_equal "cells", a.units
|
782
782
|
end
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
783
|
+
|
784
|
+
if defined?(Uncertain)
|
785
|
+
def test_uncertain
|
786
|
+
a = '1 +/- 1 mm'.unit
|
787
|
+
assert_equal a.to_s, '1 +/- 1 mm'
|
788
|
+
end
|
789
|
+
end
|
790
|
+
|
791
791
|
def test_format
|
792
|
-
assert_equal "%0.2f" % "1 mm".unit, "1.00 mm"
|
792
|
+
assert_equal "%0.2f" % "1 mm".unit, "1.00 mm"
|
793
793
|
end
|
794
|
-
|
794
|
+
|
795
795
|
def test_bad_units
|
796
796
|
assert_raises(ArgumentError) { '1 doohickey / thingamabob'.unit}
|
797
797
|
assert_raises(ArgumentError) { '1 minimeter'.unit}
|
798
798
|
end
|
799
|
-
|
799
|
+
|
800
800
|
def test_currency
|
801
801
|
assert_nothing_raised {"$1".unit}
|
802
802
|
end
|
803
|
-
|
803
|
+
|
804
804
|
def test_kind
|
805
805
|
a = "1 mm".unit
|
806
806
|
assert_equal a.kind, :length
|
807
807
|
end
|
808
|
-
|
808
|
+
|
809
809
|
def test_percent
|
810
810
|
assert_nothing_raised {
|
811
|
-
|
811
|
+
"1 percent".unit
|
812
812
|
"1%".unit
|
813
813
|
"0.01%".unit
|
814
814
|
}
|
@@ -818,7 +818,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
818
818
|
assert c =~ a
|
819
819
|
assert_in_delta '50 ml'.unit.scalar, c.scalar, 0.0001
|
820
820
|
end
|
821
|
-
|
821
|
+
|
822
822
|
def test_parse
|
823
823
|
assert_nothing_raised { "1 1/m".unit }
|
824
824
|
assert_raises(ArgumentError) { "3 s/s/ft".unit }
|
@@ -831,29 +831,29 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
831
831
|
assert_raises(ArgumentError) { "\t\t".unit }
|
832
832
|
assert_raises(ArgumentError) { "\n".unit }
|
833
833
|
end
|
834
|
-
|
834
|
+
|
835
835
|
def test_inline_conversions
|
836
836
|
assert_equal "60 s".unit, Unit.parse("1 min to seconds")
|
837
837
|
assert_equal "60 s".unit, Unit.parse("1 min as seconds")
|
838
838
|
assert_equal "60 s".unit, Unit.parse("1 min in seconds")
|
839
839
|
end
|
840
|
-
|
840
|
+
|
841
841
|
def test_time_conversions
|
842
842
|
today = 'now'.to_time
|
843
843
|
assert_equal today,@april_fools
|
844
844
|
last_century = today - '150 years'.unit
|
845
845
|
assert_equal last_century.to_date, '1856-04-01'.to_date
|
846
846
|
end
|
847
|
-
|
847
|
+
|
848
848
|
def test_coercion
|
849
849
|
assert_nothing_raised { 1.0 * '1 mm'.unit}
|
850
850
|
assert_nothing_raised { '1 mm'.unit * 1.0}
|
851
851
|
end
|
852
|
-
|
852
|
+
|
853
853
|
def test_zero
|
854
854
|
assert_nothing_raised { Unit.new(0) }
|
855
855
|
end
|
856
|
-
|
856
|
+
|
857
857
|
def test_divide_results_in_unitless
|
858
858
|
a = '10 mg/ml'.unit
|
859
859
|
b = '10 mg/ml'.unit
|
@@ -865,59 +865,59 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
865
865
|
b = '1 g/dl'.unit
|
866
866
|
assert_equal a,b
|
867
867
|
end
|
868
|
-
|
868
|
+
|
869
869
|
def test_parse_durations
|
870
870
|
assert_equal "1:00".unit, '1 hour'.unit
|
871
871
|
assert_equal "1:30".unit, "1.5 hour".unit
|
872
872
|
assert_equal "1:30:30".unit, "1.5 hour".unit + '30 sec'.unit
|
873
873
|
assert_equal "1:30:30,200".unit, "1.5 hour".unit + '30 sec'.unit + '200 usec'.unit
|
874
874
|
end
|
875
|
-
|
875
|
+
|
876
876
|
def test_coercion_2
|
877
877
|
a = Dummy.new
|
878
878
|
b = '1 mm'.unit
|
879
879
|
assert_equal '2 mm'.unit, b + a
|
880
880
|
end
|
881
|
-
|
881
|
+
|
882
882
|
def test_create_with_other_unit
|
883
883
|
a = '1 g'.unit
|
884
884
|
b = 0.unit(a)
|
885
885
|
assert_equal b, '0 g'.unit
|
886
886
|
end
|
887
|
-
|
887
|
+
|
888
888
|
def test_sqrt
|
889
889
|
a = '-9 mm^2'.unit
|
890
890
|
b = a**(0.5)
|
891
891
|
assert_in_delta Math.sqrt(a).to_unit.scalar.real, b.scalar.real, 0.00001
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
end
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
892
|
+
# ruby 1.8.x uses .image while 1.9.x uses .imaginary
|
893
|
+
if Complex.instance_methods.include?(:imaginary)
|
894
|
+
assert_in_delta Math.sqrt(a).to_unit.scalar.imaginary, b.scalar.imaginary, 0.00001
|
895
|
+
else
|
896
|
+
assert_in_delta Math.sqrt(a).to_unit.scalar.image, b.scalar.image, 0.00001
|
897
|
+
end
|
898
|
+
end
|
899
|
+
|
900
|
+
def test_div
|
901
|
+
assert_equal 11,"23 m".unit.div('2 m'.unit)
|
902
|
+
end
|
903
|
+
|
904
|
+
def test_divmod
|
905
|
+
assert_equal [277,1], 555.unit('mm').divmod(2.unit('mm'))
|
906
|
+
assert_equal [277, 0.0010000000000000373], '0.555 m'.unit.divmod('2 mm'.unit)
|
907
|
+
assert_raises(ArgumentError) { '1 m'.unit.divmod('1 kg'.unit) }
|
908
|
+
end
|
909
|
+
|
910
|
+
if Math.respond_to?(:cbrt)
|
911
|
+
def test_cbrt
|
912
|
+
assert_in_delta '2 mm'.to_unit, Math.cbrt('8 mm^3'.to_unit), 0.0001
|
913
|
+
end
|
914
|
+
end
|
915
|
+
|
916
916
|
def test_hypot
|
917
917
|
assert_equal Math.hypot('3 mm'.unit,'4 mm'.unit), '5 mm'.unit
|
918
918
|
assert_raises(ArgumentError) { Math.hypot('3 mm'.unit, '4 kg'.unit)}
|
919
919
|
end
|
920
|
-
|
920
|
+
|
921
921
|
def test_complex
|
922
922
|
assert_equal '1+1i mm'.unit.scalar, Complex(1,1)
|
923
923
|
assert_equal '1+1i'.unit.scalar, Complex(1,1)
|
@@ -935,16 +935,16 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
935
935
|
assert_equal '1/4 in/s'.unit, '0.25 in/s'.unit
|
936
936
|
assert_equal '1/4'.unit, 0.25
|
937
937
|
end
|
938
|
-
|
938
|
+
|
939
939
|
def test_to_date
|
940
940
|
a = Time.now
|
941
941
|
assert_equal a.send(:to_date), Date.today
|
942
942
|
end
|
943
|
-
|
943
|
+
|
944
944
|
def test_natural_language
|
945
945
|
assert_equal Unit.parse("10 mm in cm"), '10 mm'.unit.to('cm')
|
946
946
|
end
|
947
|
-
|
947
|
+
|
948
948
|
def test_round_pounds
|
949
949
|
assert_equal '1 lbs'.unit, '1.1 lbs'.unit.round
|
950
950
|
end
|
@@ -954,12 +954,12 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
954
954
|
assert_equal '1 lbs'.unit, '1 <pound>'.unit
|
955
955
|
assert_equal('1 kg*m'.unit, '1 <kilogram>*<meter>'.unit)
|
956
956
|
end
|
957
|
-
|
957
|
+
|
958
958
|
def test_format_nil_string
|
959
959
|
assert_nothing_raised {"" % nil}
|
960
960
|
assert_nothing_raised {"" % false}
|
961
961
|
end
|
962
|
-
|
962
|
+
|
963
963
|
def test_to_s_cache
|
964
964
|
Unit.clear_cache
|
965
965
|
a = Unit.new('1 mm')
|
@@ -969,16 +969,16 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
969
969
|
assert_equal('1/1000 m', a.to_s('m'))
|
970
970
|
assert_equal('1/1000 m', a.output['m'])
|
971
971
|
end
|
972
|
-
|
972
|
+
|
973
973
|
def test_version
|
974
974
|
assert_equal('1.3.0.a', Unit::VERSION)
|
975
975
|
end
|
976
|
-
|
976
|
+
|
977
977
|
def test_negation
|
978
978
|
a = 1.to_unit
|
979
979
|
assert_equal(a.class, (1-a).class)
|
980
980
|
end
|
981
|
-
|
981
|
+
|
982
982
|
def test_degree
|
983
983
|
assert "100 tempF".unit.degree?
|
984
984
|
assert "100 degC".unit.degree?
|
@@ -988,9 +988,9 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
988
988
|
def test_temperature
|
989
989
|
assert "100 tempF".unit.temperature?
|
990
990
|
assert !"100 degC".unit.temperature?
|
991
|
-
assert !"1 mm".unit.temperature?
|
991
|
+
assert !"1 mm".unit.temperature?
|
992
992
|
end
|
993
|
-
|
993
|
+
|
994
994
|
def test_parse_into_numbers_and_units
|
995
995
|
assert_equal([1,"m"], Unit.parse_into_numbers_and_units("1 m"))
|
996
996
|
assert_equal([1.0,"m"], Unit.parse_into_numbers_and_units("1.0 m"))
|