ruby-units 1.3.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +25 -7
- data/LICENSE.txt +1 -1
- data/README.md +68 -55
- data/RakeFile +27 -18
- data/TODO +2 -1
- data/VERSION +1 -1
- data/lib/ruby-units.rb +2 -2
- data/lib/ruby_units.rb +2 -2
- data/lib/ruby_units/array.rb +4 -2
- data/lib/ruby_units/date.rb +17 -4
- data/lib/ruby_units/definition.rb +100 -0
- data/lib/ruby_units/fixnum.rb +6 -4
- data/lib/ruby_units/math.rb +32 -2
- data/lib/ruby_units/numeric.rb +2 -1
- data/lib/ruby_units/object.rb +8 -1
- data/lib/ruby_units/string.rb +10 -109
- data/lib/ruby_units/string/extra.rb +45 -11
- data/lib/ruby_units/time.rb +11 -2
- data/lib/ruby_units/unit.rb +722 -434
- data/lib/ruby_units/unit_definitions.rb +3 -252
- data/lib/ruby_units/unit_definitions/base.rb +103 -0
- data/lib/ruby_units/unit_definitions/prefix.rb +40 -0
- data/lib/ruby_units/unit_definitions/standard.rb +705 -0
- data/lib/ruby_units/version.rb +1 -0
- data/ruby-units.gemspec +15 -20
- metadata +46 -35
- data/Gemfile +0 -12
- data/Manifest.txt +0 -19
- data/autotest/discover.rb +0 -1
- data/spec/ruby-units/array_spec.rb +0 -14
- data/spec/ruby-units/complex_spec.rb +0 -37
- data/spec/ruby-units/date_spec.rb +0 -38
- data/spec/ruby-units/math_spec.rb +0 -63
- data/spec/ruby-units/numeric_spec.rb +0 -12
- data/spec/ruby-units/object_spec.rb +0 -7
- data/spec/ruby-units/string/extra_spec.rb +0 -45
- data/spec/ruby-units/string_spec.rb +0 -20
- data/spec/ruby-units/time_spec.rb +0 -28
- data/spec/ruby-units/unit_spec.rb +0 -965
- data/spec/spec_helper.rb +0 -5
- data/test/test_cache.rb +0 -26
- data/test/test_ruby-units.rb +0 -976
@@ -1,12 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
# some rubies return an array of strings for .instance_methods and others return an array of symbols
|
4
|
-
# so let's stringify them before we compare
|
5
|
-
describe Numeric do
|
6
|
-
specify { Float.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
7
|
-
specify { Integer.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
8
|
-
specify { Fixnum.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
9
|
-
specify { Complex.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
10
|
-
specify { Bignum.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
11
|
-
specify { Rational.instance_methods.map {|m| m.to_s}.should include("to_unit") }
|
12
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/../../../lib/ruby_units/string/extra'
|
3
|
-
describe String do
|
4
|
-
context "Time syntax sugar" do
|
5
|
-
before(:each) do
|
6
|
-
Time.stub(:now).and_return(Time.at(1303656390))
|
7
|
-
Date.stub(:today).and_return(Date.new(2011,4,1))
|
8
|
-
DateTime.stub(:now).and_return(DateTime.new(2011,4,1,0,0,0))
|
9
|
-
end
|
10
|
-
|
11
|
-
specify { "5 min".ago.should be_instance_of Time }
|
12
|
-
|
13
|
-
specify { "5 min".from(Time.now).should be_instance_of Time }
|
14
|
-
specify { "5 min".from_now.should be_instance_of Time }
|
15
|
-
|
16
|
-
specify { "5 min".after(Time.parse('12:00')).should be_instance_of Time }
|
17
|
-
|
18
|
-
specify { "5 min".before.should be_instance_of Time}
|
19
|
-
specify { "5 min".before(Time.now).should be_instance_of Time}
|
20
|
-
specify { "5 min".before_now.should be_instance_of Time}
|
21
|
-
specify { "5 min".before(Time.parse('12:00')).should be_instance_of Time}
|
22
|
-
|
23
|
-
specify { "min".since.should be_instance_of Unit}
|
24
|
-
specify { "min".since(Time.parse("12:00")).should be_instance_of Unit}
|
25
|
-
specify { "min".since(Time.now - 60).should == Unit("1 min")}
|
26
|
-
specify { "days".since(Date.today - 3).should == Unit("3 d")}
|
27
|
-
specify { expect {"days".since(1000) }.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
28
|
-
|
29
|
-
specify { "min".until.should be_instance_of Unit}
|
30
|
-
specify { "min".until(Time.parse("12:00")).should be_instance_of Unit}
|
31
|
-
specify { "min".until(Time.now + 60).should == Unit("1 min")}
|
32
|
-
specify { "days".until(Date.today + 3).should == Unit("3 d")}
|
33
|
-
specify { expect {"days".until(1000) }.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
34
|
-
|
35
|
-
specify { "today".to_date.should be_instance_of Date }
|
36
|
-
specify { "2011-4-1".to_date.should be_instance_of Date }
|
37
|
-
|
38
|
-
specify { "now".to_datetime.should be_instance_of DateTime }
|
39
|
-
specify { "now".to_time.should be_instance_of Time }
|
40
|
-
|
41
|
-
specify { "10001-01-01 12:00".to_time.should be_instance_of Time }
|
42
|
-
specify { "2001-01-01 12:00".to_time.should be_instance_of Time }
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe String do
|
4
|
-
context "Unit creation from strings" do
|
5
|
-
specify { "1 mm".to_unit.should be_instance_of Unit }
|
6
|
-
specify { "1 mm".unit.should be_instance_of Unit }
|
7
|
-
specify { "1 mm".u.should be_instance_of Unit }
|
8
|
-
specify { "1 m".convert_to("ft").should be_within(Unit("0.01 ft")).of Unit("3.28084 ft") }
|
9
|
-
end
|
10
|
-
|
11
|
-
context "output format" do
|
12
|
-
subject { Unit("1.23456 m/s^2") }
|
13
|
-
specify { ("" % subject).should == ""}
|
14
|
-
specify { ("%0.2f" % subject).should == "1.23 m/s^2"}
|
15
|
-
specify { ("%0.2f km/h^2" % subject).should == "15999.90 km/h^2"}
|
16
|
-
specify { ("km/h^2" % subject).should == "15999.9 km/h^2"}
|
17
|
-
specify { ("%H:%M:%S" % Unit("1.5 h")).should == "01:30:00"}
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe Time do
|
4
|
-
before(:each) do
|
5
|
-
Time.stub(:now).and_return(Time.at(1303656390))
|
6
|
-
end
|
7
|
-
|
8
|
-
context ".at" do
|
9
|
-
subject { Date.new(2011,4,1).to_unit }
|
10
|
-
specify { Time.at(subject - Date.new(1970,1,1)).strftime("%D %T").should == "03/31/11 20:00:00"}
|
11
|
-
end
|
12
|
-
|
13
|
-
context ".in" do
|
14
|
-
specify { Time.in("5 min").should be_a Time}
|
15
|
-
specify { Time.in("5 min").should > Time.now}
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'addition (+)' do
|
19
|
-
specify { (Time.now + 1).should == Time.at(1303656390 + 1)}
|
20
|
-
specify { (Time.now + Unit("10 min")).should == Time.at(1303656390 + 600)}
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'subtraction (-)' do
|
24
|
-
specify { (Time.now - 1).should == Time.at(1303656390 - 1)}
|
25
|
-
specify { (Time.now - Unit("10 min")).should == Time.at(1303656390 - 600)}
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
@@ -1,965 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe Unit.base_units do
|
4
|
-
it {should be_a Array}
|
5
|
-
it {should have(14).elements}
|
6
|
-
%w{kilogram meter second Ampere degK tempK mole candela each dollar steradian radian decibel byte}.each do |u|
|
7
|
-
it {should include(Unit(u))}
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
describe "Create some simple units" do
|
13
|
-
|
14
|
-
describe Unit("0") do
|
15
|
-
it {should be_a Numeric}
|
16
|
-
it {should be_an_instance_of Unit}
|
17
|
-
its(:scalar) {should === 0}
|
18
|
-
its(:scalar) {should be_an Integer}
|
19
|
-
its(:units) {should be_empty}
|
20
|
-
its(:kind) {should == :unitless}
|
21
|
-
it {should_not be_temperature}
|
22
|
-
it {should_not be_degree}
|
23
|
-
it {should be_base}
|
24
|
-
it {should be_unitless}
|
25
|
-
it {should be_zero}
|
26
|
-
its(:base) {should == subject}
|
27
|
-
end
|
28
|
-
|
29
|
-
describe Unit("1") do
|
30
|
-
it {should be_a Numeric}
|
31
|
-
it {should be_an_instance_of Unit}
|
32
|
-
its(:scalar) {should === 1}
|
33
|
-
its(:scalar) {should be_an Integer}
|
34
|
-
its(:units) {should be_empty}
|
35
|
-
its(:kind) {should == :unitless}
|
36
|
-
it {should_not be_temperature}
|
37
|
-
it {should_not be_degree}
|
38
|
-
it {should be_base}
|
39
|
-
it {should be_unitless}
|
40
|
-
it {should_not be_zero}
|
41
|
-
its(:base) {should == subject}
|
42
|
-
end
|
43
|
-
|
44
|
-
describe Unit(1) do
|
45
|
-
it {should be_a Numeric}
|
46
|
-
it {should be_an_instance_of Unit}
|
47
|
-
its(:scalar) {should === 1}
|
48
|
-
its(:scalar) {should be_an Integer}
|
49
|
-
its(:units) {should be_empty}
|
50
|
-
its(:kind) {should == :unitless}
|
51
|
-
it {should_not be_temperature}
|
52
|
-
it {should_not be_degree}
|
53
|
-
it {should be_base}
|
54
|
-
it {should be_unitless}
|
55
|
-
it {should_not be_zero}
|
56
|
-
its(:base) {should == subject}
|
57
|
-
end
|
58
|
-
|
59
|
-
describe Unit(Rational(1,2)) do
|
60
|
-
it {should be_a Numeric}
|
61
|
-
it {should be_an_instance_of Unit}
|
62
|
-
its(:scalar) {should === Rational(1,2)}
|
63
|
-
its(:scalar) {should be_a Rational}
|
64
|
-
its(:units) {should be_empty}
|
65
|
-
its(:kind) {should == :unitless}
|
66
|
-
it {should_not be_temperature}
|
67
|
-
it {should_not be_degree}
|
68
|
-
it {should be_base}
|
69
|
-
it {should be_unitless}
|
70
|
-
it {should_not be_zero}
|
71
|
-
its(:base) {should == subject}
|
72
|
-
end
|
73
|
-
|
74
|
-
describe Unit(0.5) do
|
75
|
-
it {should be_a Numeric}
|
76
|
-
it {should be_an_instance_of Unit}
|
77
|
-
its(:scalar) {should === 0.5}
|
78
|
-
its(:scalar) {should be_a Float}
|
79
|
-
its(:units) {should be_empty}
|
80
|
-
its(:kind) {should == :unitless}
|
81
|
-
it {should_not be_temperature}
|
82
|
-
it {should_not be_degree}
|
83
|
-
it {should be_base}
|
84
|
-
it {should be_unitless}
|
85
|
-
it {should_not be_zero}
|
86
|
-
its(:base) {should == subject}
|
87
|
-
end
|
88
|
-
|
89
|
-
describe Unit(Complex(1,1)) do
|
90
|
-
it {should be_a Numeric}
|
91
|
-
it {should be_an_instance_of Unit}
|
92
|
-
its(:scalar) {should === Complex(1,1)}
|
93
|
-
its(:scalar) {should be_a Complex}
|
94
|
-
its(:units) {should be_empty}
|
95
|
-
its(:kind) {should == :unitless}
|
96
|
-
it {should_not be_temperature}
|
97
|
-
it {should_not be_degree}
|
98
|
-
it {should be_base}
|
99
|
-
it {should be_unitless}
|
100
|
-
it {should_not be_zero}
|
101
|
-
its(:base) {should == subject}
|
102
|
-
end
|
103
|
-
|
104
|
-
describe Unit("1 mm") do
|
105
|
-
it {should be_a Numeric}
|
106
|
-
it {should be_an_instance_of Unit}
|
107
|
-
its(:scalar) {should == 1}
|
108
|
-
its(:scalar) {should be_an Integer}
|
109
|
-
its(:units) {should == "mm"}
|
110
|
-
its(:kind) {should == :length}
|
111
|
-
it {should_not be_temperature}
|
112
|
-
it {should_not be_degree}
|
113
|
-
it {should_not be_base}
|
114
|
-
it {should_not be_unitless}
|
115
|
-
it {should_not be_zero}
|
116
|
-
its(:base) {should == Unit("0.001 m")}
|
117
|
-
end
|
118
|
-
|
119
|
-
describe Unit("10 m/s^2") do
|
120
|
-
it {should be_an_instance_of Unit}
|
121
|
-
its(:scalar) {should == 10}
|
122
|
-
its(:scalar) {should be_an Integer}
|
123
|
-
its(:units) {should == "m/s^2"}
|
124
|
-
its(:kind) {should == :acceleration}
|
125
|
-
it {should_not be_temperature}
|
126
|
-
it {should_not be_degree}
|
127
|
-
it {should be_base}
|
128
|
-
it {should_not be_unitless}
|
129
|
-
it {should_not be_zero}
|
130
|
-
its(:base) {should == Unit("10 m/s^2")}
|
131
|
-
end
|
132
|
-
|
133
|
-
describe Unit("5ft 6in") do
|
134
|
-
it {should be_an_instance_of Unit}
|
135
|
-
its(:scalar) {should == 5.5}
|
136
|
-
its(:units) {should == "ft"}
|
137
|
-
its(:kind) {should == :length}
|
138
|
-
it {should_not be_temperature}
|
139
|
-
it {should_not be_degree}
|
140
|
-
it {should_not be_base}
|
141
|
-
it {should_not be_unitless}
|
142
|
-
it {should_not be_zero}
|
143
|
-
its(:base) {should be_within(Unit("0.01 m")).of Unit("1.6764 m")}
|
144
|
-
specify { subject.to_s(:ft).should == %{5'6"} }
|
145
|
-
end
|
146
|
-
|
147
|
-
describe Unit("6lbs 5oz") do
|
148
|
-
it {should be_an_instance_of Unit}
|
149
|
-
its(:scalar) {should be_within(0.001).of 6.312}
|
150
|
-
its(:units) {should == "lbs"}
|
151
|
-
its(:kind) {should == :mass}
|
152
|
-
it {should_not be_temperature}
|
153
|
-
it {should_not be_degree}
|
154
|
-
it {should_not be_base}
|
155
|
-
it {should_not be_unitless}
|
156
|
-
it {should_not be_zero}
|
157
|
-
its(:base) {should be_within(Unit("0.01 kg")).of Unit("2.8633 kg")}
|
158
|
-
specify { subject.to_s(:lbs).should == "6 lbs, 5 oz" }
|
159
|
-
end
|
160
|
-
|
161
|
-
describe Unit("100 tempC") do
|
162
|
-
it {should be_an_instance_of Unit}
|
163
|
-
its(:scalar) {should be_within(0.001).of 100}
|
164
|
-
its(:units) {should == "tempC"}
|
165
|
-
its(:kind) {should == :temperature}
|
166
|
-
it {should be_temperature}
|
167
|
-
it {should be_degree}
|
168
|
-
it {should_not be_base}
|
169
|
-
it {should_not be_unitless}
|
170
|
-
it {should_not be_zero}
|
171
|
-
its(:base) {should be_within(Unit("0.01 degK")).of Unit("373.15 tempK")}
|
172
|
-
its(:temperature_scale) {should == "degC"}
|
173
|
-
end
|
174
|
-
|
175
|
-
describe Unit(Time.now) do
|
176
|
-
it {should be_an_instance_of Unit}
|
177
|
-
its(:scalar) {should be_a(Numeric)}
|
178
|
-
its(:units) {should == "s"}
|
179
|
-
its(:kind) {should == :time}
|
180
|
-
it {should_not be_temperature}
|
181
|
-
it {should_not be_degree}
|
182
|
-
it {should be_base}
|
183
|
-
it {should_not be_unitless}
|
184
|
-
it {should_not be_zero}
|
185
|
-
its(:base) {should be_a(Numeric)}
|
186
|
-
its(:temperature_scale) {should be_nil}
|
187
|
-
end
|
188
|
-
|
189
|
-
describe Unit("100 degC") do
|
190
|
-
it {should be_an_instance_of Unit}
|
191
|
-
its(:scalar) {should be_within(0.001).of 100}
|
192
|
-
its(:units) {should == "degC"}
|
193
|
-
its(:kind) {should == :temperature}
|
194
|
-
it {should_not be_temperature}
|
195
|
-
it {should be_degree}
|
196
|
-
it {should_not be_base}
|
197
|
-
it {should_not be_unitless}
|
198
|
-
its(:base) {should be_within(Unit("0.01 degK")).of Unit("100 degK")}
|
199
|
-
end
|
200
|
-
|
201
|
-
describe Unit("75%") do
|
202
|
-
it {should be_an_instance_of Unit}
|
203
|
-
its(:scalar) {should be_an Integer}
|
204
|
-
its(:units) {should == "%"}
|
205
|
-
its(:kind) {should == :unitless}
|
206
|
-
it {should_not be_temperature}
|
207
|
-
it {should_not be_degree}
|
208
|
-
it {should_not be_base}
|
209
|
-
it {should_not be_unitless}
|
210
|
-
it {should_not be_zero}
|
211
|
-
its(:base) {should be_a(Numeric)}
|
212
|
-
its(:temperature_scale) {should be_nil}
|
213
|
-
end
|
214
|
-
|
215
|
-
describe Unit("180 deg") do
|
216
|
-
it {should be_an_instance_of Unit}
|
217
|
-
its(:scalar) {should be_a Numeric}
|
218
|
-
its(:units) {should == "deg"}
|
219
|
-
its(:kind) {should == :angle}
|
220
|
-
it {should_not be_temperature}
|
221
|
-
it {should_not be_degree}
|
222
|
-
it {should_not be_base}
|
223
|
-
it {should_not be_unitless}
|
224
|
-
it {should_not be_zero}
|
225
|
-
its(:base) {should be_a(Numeric)}
|
226
|
-
its(:temperature_scale) {should be_nil}
|
227
|
-
end
|
228
|
-
|
229
|
-
describe Unit("1 radian") do
|
230
|
-
it {should be_an_instance_of Unit}
|
231
|
-
its(:scalar) {should be_a Numeric}
|
232
|
-
its(:units) {should == "rad"}
|
233
|
-
its(:kind) {should == :angle}
|
234
|
-
it {should_not be_temperature}
|
235
|
-
it {should_not be_degree}
|
236
|
-
it {should be_base}
|
237
|
-
it {should_not be_unitless}
|
238
|
-
it {should_not be_zero}
|
239
|
-
its(:base) {should be_a Numeric}
|
240
|
-
its(:temperature_scale) {should be_nil}
|
241
|
-
end
|
242
|
-
|
243
|
-
describe Unit("12 dozen") do
|
244
|
-
it {should be_an_instance_of Unit}
|
245
|
-
its(:scalar) {should be_an Integer}
|
246
|
-
its(:units) {should == "doz"}
|
247
|
-
its(:kind) {should == :unitless}
|
248
|
-
it {should_not be_temperature}
|
249
|
-
it {should_not be_degree}
|
250
|
-
it {should_not be_base}
|
251
|
-
it {should_not be_unitless}
|
252
|
-
it {should_not be_zero}
|
253
|
-
its(:base) {should be_a Numeric}
|
254
|
-
its(:temperature_scale) {should be_nil}
|
255
|
-
end
|
256
|
-
|
257
|
-
describe Unit("1/2 kg") do
|
258
|
-
it {should be_an_instance_of Unit}
|
259
|
-
its(:scalar) {should be_an Rational}
|
260
|
-
its(:units) {should == "kg"}
|
261
|
-
its(:kind) {should == :mass}
|
262
|
-
it {should_not be_temperature}
|
263
|
-
it {should_not be_degree}
|
264
|
-
it {should be_base}
|
265
|
-
it {should_not be_unitless}
|
266
|
-
it {should_not be_zero}
|
267
|
-
its(:base) {should be_a Numeric}
|
268
|
-
its(:temperature_scale) {should be_nil}
|
269
|
-
end
|
270
|
-
|
271
|
-
describe Unit("1/2 kg/m") do
|
272
|
-
it {should be_an_instance_of Unit}
|
273
|
-
its(:scalar) {should be_an Rational}
|
274
|
-
its(:units) {should == "kg/m"}
|
275
|
-
its(:kind) {should be_nil}
|
276
|
-
it {should_not be_temperature}
|
277
|
-
it {should_not be_degree}
|
278
|
-
it {should be_base}
|
279
|
-
it {should_not be_unitless}
|
280
|
-
it {should_not be_zero}
|
281
|
-
its(:base) {should be_a Numeric}
|
282
|
-
its(:temperature_scale) {should be_nil}
|
283
|
-
end
|
284
|
-
|
285
|
-
describe Unit("1:23:45") do
|
286
|
-
it {should be_an_instance_of Unit}
|
287
|
-
its(:scalar) {should be_an Rational}
|
288
|
-
its(:units) {should == "h"}
|
289
|
-
its(:kind) {should == :time}
|
290
|
-
it {should_not be_temperature}
|
291
|
-
it {should_not be_degree}
|
292
|
-
it {should_not be_base}
|
293
|
-
it {should_not be_unitless}
|
294
|
-
it {should_not be_zero}
|
295
|
-
its(:base) {should be_a Numeric}
|
296
|
-
its(:temperature_scale) {should be_nil}
|
297
|
-
end
|
298
|
-
|
299
|
-
# also '1 hours as minutes'
|
300
|
-
# '1 hour to minutes'
|
301
|
-
describe Unit.parse("1 hour in minutes") do
|
302
|
-
it {should be_an_instance_of Unit}
|
303
|
-
its(:scalar) {should be_an Integer}
|
304
|
-
its(:units) {should == "min"}
|
305
|
-
its(:kind) {should == :time}
|
306
|
-
it {should_not be_temperature}
|
307
|
-
it {should_not be_degree}
|
308
|
-
it {should_not be_base}
|
309
|
-
it {should_not be_unitless}
|
310
|
-
it {should_not be_zero}
|
311
|
-
its(:base) {should be_a Numeric}
|
312
|
-
its(:temperature_scale) {should be_nil}
|
313
|
-
end
|
314
|
-
|
315
|
-
describe Unit.new("1 attoparsec/microfortnight") do
|
316
|
-
it {should be_an_instance_of Unit}
|
317
|
-
its(:scalar) {should be_an Integer}
|
318
|
-
its(:units) {should == "apc/ufortnight"}
|
319
|
-
its(:kind) {should == :speed}
|
320
|
-
it {should_not be_temperature}
|
321
|
-
it {should_not be_degree}
|
322
|
-
it {should_not be_base}
|
323
|
-
it {should_not be_unitless}
|
324
|
-
it {should_not be_zero}
|
325
|
-
its(:base) {should be_a Numeric}
|
326
|
-
its(:temperature_scale) {should be_nil}
|
327
|
-
it { subject.convert_to("in/s").should be_within(Unit("0.0001 in/s")).of(Unit("1.0043269330917 in/s"))}
|
328
|
-
end
|
329
|
-
|
330
|
-
describe Unit.new("1 F") do
|
331
|
-
it {should be_an_instance_of Unit}
|
332
|
-
its(:scalar) {should be_an Integer}
|
333
|
-
its(:units) {should == "F"}
|
334
|
-
its(:kind) {should == :capacitance}
|
335
|
-
it {should_not be_temperature}
|
336
|
-
it {should_not be_degree}
|
337
|
-
it {should_not be_base}
|
338
|
-
it {should_not be_unitless}
|
339
|
-
it {should_not be_zero}
|
340
|
-
its(:base) {should be_a Numeric}
|
341
|
-
its(:temperature_scale) {should be_nil}
|
342
|
-
end
|
343
|
-
|
344
|
-
|
345
|
-
end
|
346
|
-
|
347
|
-
describe "Unit handles attempts to create bad units" do
|
348
|
-
specify "no empty strings" do
|
349
|
-
expect {Unit("")}.to raise_error(ArgumentError,"No Unit Specified")
|
350
|
-
end
|
351
|
-
|
352
|
-
specify "no blank strings" do
|
353
|
-
expect {Unit(" ")}.to raise_error(ArgumentError,"No Unit Specified")
|
354
|
-
end
|
355
|
-
|
356
|
-
specify "no strings with tabs" do
|
357
|
-
expect {Unit("\t")}.to raise_error(ArgumentError,"No Unit Specified")
|
358
|
-
end
|
359
|
-
|
360
|
-
specify "no strings with newlines" do
|
361
|
-
expect {Unit("\n")}.to raise_error(ArgumentError,"No Unit Specified")
|
362
|
-
end
|
363
|
-
|
364
|
-
specify "no strings that don't specify a valid unit" do
|
365
|
-
expect {Unit("random string")}.to raise_error(ArgumentError,"'random string' Unit not recognized")
|
366
|
-
end
|
367
|
-
|
368
|
-
specify "no unhandled classes" do
|
369
|
-
expect {Unit(STDIN)}.to raise_error(ArgumentError,"Invalid Unit Format")
|
370
|
-
end
|
371
|
-
|
372
|
-
specify "no undefined units" do
|
373
|
-
expect {Unit("1 mFoo")}.to raise_error(ArgumentError,"'1 mFoo' Unit not recognized")
|
374
|
-
end
|
375
|
-
|
376
|
-
specify "no units with powers greater than 19" do
|
377
|
-
expect {Unit("1 m^20")}.to raise_error(ArgumentError, "Power out of range (-20 < net power of a unit < 20)")
|
378
|
-
end
|
379
|
-
|
380
|
-
specify "no units with powers less than 19" do
|
381
|
-
expect {Unit("1 m^-20")}.to raise_error(ArgumentError, "Power out of range (-20 < net power of a unit < 20)")
|
382
|
-
end
|
383
|
-
|
384
|
-
specify "no temperatures less than absolute zero" do
|
385
|
-
expect {Unit("-100 tempK")}.to raise_error(ArgumentError,"Temperatures must not be less than absolute zero")
|
386
|
-
expect {Unit("-100 tempR")}.to raise_error(ArgumentError,"Temperatures must not be less than absolute zero")
|
387
|
-
expect {Unit("-500/9 tempR")}.to raise_error(ArgumentError,"Temperatures must not be less than absolute zero")
|
388
|
-
end
|
389
|
-
|
390
|
-
end
|
391
|
-
|
392
|
-
describe Unit do
|
393
|
-
it "is a subclass of Numeric" do
|
394
|
-
described_class.should < Numeric
|
395
|
-
end
|
396
|
-
it "is Comparable" do
|
397
|
-
described_class.should < Comparable
|
398
|
-
end
|
399
|
-
end
|
400
|
-
|
401
|
-
describe "Unit Comparisons" do
|
402
|
-
context "Unit should detect if two units are 'compatible' (i.e., can be converted into each other)" do
|
403
|
-
specify { Unit("1 ft").should =~ Unit('1 m')}
|
404
|
-
specify { Unit("1 ft").should =~ "m"}
|
405
|
-
specify { Unit("1 ft").should be_compatible_with Unit('1 m')}
|
406
|
-
specify { Unit("1 ft").should be_compatible_with "m"}
|
407
|
-
specify { Unit("1 m").should be_compatible_with Unit('1 kg*m/kg')}
|
408
|
-
specify { Unit("1 ft").should_not =~ Unit('1 kg')}
|
409
|
-
specify { Unit("1 ft").should_not be_compatible_with Unit('1 kg')}
|
410
|
-
end
|
411
|
-
|
412
|
-
context "Equality" do
|
413
|
-
context "units of same kind" do
|
414
|
-
specify { Unit("1000 m").should == Unit('1 km')}
|
415
|
-
specify { Unit("100 m").should_not == Unit('1 km')}
|
416
|
-
specify { Unit("1 m").should == Unit('100 cm')}
|
417
|
-
end
|
418
|
-
|
419
|
-
context "units of incompatible types" do
|
420
|
-
specify { Unit("1 m").should_not == Unit("1 kg")}
|
421
|
-
end
|
422
|
-
|
423
|
-
context "units with a zero scalar are equal" do
|
424
|
-
specify {Unit("0 m").should == Unit("0 s")}
|
425
|
-
specify {Unit("0 m").should == Unit("0 kg")}
|
426
|
-
|
427
|
-
context "except for temperature units" do
|
428
|
-
specify {Unit("0 tempK").should == Unit("0 m")}
|
429
|
-
specify {Unit("0 tempR").should == Unit("0 m")}
|
430
|
-
specify {Unit("0 tempC").should_not == Unit("0 m")}
|
431
|
-
specify {Unit("0 tempF").should_not == Unit("0 m")}
|
432
|
-
end
|
433
|
-
end
|
434
|
-
end
|
435
|
-
|
436
|
-
context "Equivalence" do
|
437
|
-
context "units and scalars are the exactly the same" do
|
438
|
-
specify { Unit("1 m").should === Unit("1 m")}
|
439
|
-
specify { Unit("1 m").should be_same Unit("1 m")}
|
440
|
-
specify { Unit("1 m").should be_same_as Unit("1 m")}
|
441
|
-
end
|
442
|
-
|
443
|
-
context "units are compatible but not identical" do
|
444
|
-
specify { Unit("1000 m").should_not === Unit("1 km")}
|
445
|
-
specify { Unit("1000 m").should_not be_same Unit("1 km")}
|
446
|
-
specify { Unit("1000 m").should_not be_same_as Unit("1 km")}
|
447
|
-
end
|
448
|
-
|
449
|
-
context "units are not compatible" do
|
450
|
-
specify { Unit("1000 m").should_not === Unit("1 hour")}
|
451
|
-
specify { Unit("1000 m").should_not be_same Unit("1 hour")}
|
452
|
-
specify { Unit("1000 m").should_not be_same_as Unit("1 hour")}
|
453
|
-
end
|
454
|
-
|
455
|
-
context "scalars are different" do
|
456
|
-
specify { Unit("1 m").should_not === Unit("2 m")}
|
457
|
-
specify { Unit("1 m").should_not be_same Unit("2 m")}
|
458
|
-
specify { Unit("1 m").should_not be_same_as Unit("2 m")}
|
459
|
-
end
|
460
|
-
end
|
461
|
-
|
462
|
-
context "Comparisons" do
|
463
|
-
context "compatible units can be compared" do
|
464
|
-
specify { Unit("1 m").should < Unit("2 m")}
|
465
|
-
specify { Unit("2 m").should > Unit("1 m")}
|
466
|
-
specify { Unit("1 m").should < Unit("1 mi")}
|
467
|
-
specify { Unit("2 m").should > Unit("1 ft")}
|
468
|
-
specify { Unit("70 tempF").should > Unit("10 degC")}
|
469
|
-
specify { Unit("1 m").should > 0 }
|
470
|
-
end
|
471
|
-
|
472
|
-
context "incompatible units cannot be compared" do
|
473
|
-
specify { expect { Unit("1 m") < Unit("1 liter")}.to raise_error(ArgumentError,"Incompatible Units (m !~ l)")}
|
474
|
-
specify { expect { Unit("1 kg") > Unit("60 mph")}.to raise_error(ArgumentError,"Incompatible Units (kg !~ mph)")}
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
end
|
479
|
-
|
480
|
-
describe "Unit Conversions" do
|
481
|
-
|
482
|
-
context "between compatible units" do
|
483
|
-
specify { Unit("1 s").convert_to("ns").should == Unit("1e9 ns")}
|
484
|
-
specify { Unit("1 s").convert_to("ns").should == Unit("1e9 ns")}
|
485
|
-
specify { (Unit("1 s") >> "ns").should == Unit("1e9 ns")}
|
486
|
-
|
487
|
-
specify { Unit("1 m").convert_to(Unit("ft")).should be_within(Unit("0.001 ft")).of(Unit("3.28084 ft"))}
|
488
|
-
end
|
489
|
-
|
490
|
-
context "between incompatible units" do
|
491
|
-
specify { expect { Unit("1 s").convert_to("m")}.to raise_error(ArgumentError,"Incompatible Units")}
|
492
|
-
end
|
493
|
-
|
494
|
-
context "given bad input" do
|
495
|
-
specify { expect { Unit("1 m").convert_to("random string")}.to raise_error(ArgumentError,"'random string' Unit not recognized")}
|
496
|
-
specify { expect { Unit("1 m").convert_to(STDOUT)}.to raise_error(ArgumentError,"Unknown target units")}
|
497
|
-
end
|
498
|
-
|
499
|
-
context "between temperature scales" do
|
500
|
-
# note that 'temp' units are for temperature readings on a scale, while 'deg' units are used to represent
|
501
|
-
# differences between temperatures, offsets, or other differential temperatures.
|
502
|
-
|
503
|
-
specify { Unit("100 tempC").should be_within(Unit("0.001 degK")).of(Unit("373.15 tempK")) }
|
504
|
-
specify { Unit("0 tempC").should be_within(Unit("0.001 degK")).of(Unit("273.15 tempK")) }
|
505
|
-
specify { Unit("37 tempC").should be_within(Unit("0.01 degK")).of(Unit("310.15 tempK"))}
|
506
|
-
specify { Unit("-273.15 tempC").should == Unit("0 tempK") }
|
507
|
-
|
508
|
-
specify { Unit("212 tempF").should be_within(Unit("0.001 degK")).of(Unit("373.15 tempK")) }
|
509
|
-
specify { Unit("32 tempF").should be_within(Unit("0.001 degK")).of(Unit("273.15 tempK")) }
|
510
|
-
specify { Unit("98.6 tempF").should be_within(Unit("0.01 degK")).of(Unit("310.15 tempK"))}
|
511
|
-
specify { Unit("-459.67 tempF").should == Unit("0 tempK") }
|
512
|
-
|
513
|
-
specify { Unit("671.67 tempR").should be_within(Unit("0.001 degK")).of(Unit("373.15 tempK")) }
|
514
|
-
specify { Unit("491.67 tempR").should be_within(Unit("0.001 degK")).of(Unit("273.15 tempK")) }
|
515
|
-
specify { Unit("558.27 tempR").should be_within(Unit("0.01 degK")).of(Unit("310.15 tempK"))}
|
516
|
-
specify { Unit("0 tempR").should == Unit("0 tempK") }
|
517
|
-
|
518
|
-
specify { Unit("100 tempK").convert_to("tempC").should be_within(U"0.01 degC").of(Unit("-173.15 tempC"))}
|
519
|
-
specify { Unit("100 tempK").convert_to("tempF").should be_within(U"0.01 degF").of(Unit("-279.67 tempF"))}
|
520
|
-
specify { Unit("100 tempK").convert_to("tempR").should be_within(U"0.01 degR").of(Unit("180 tempR"))}
|
521
|
-
|
522
|
-
specify { Unit("1 degC").should == Unit("1 degK")}
|
523
|
-
specify { Unit("1 degF").should == Unit("1 degR")}
|
524
|
-
specify { Unit("1 degC").should == Unit("1.8 degR")}
|
525
|
-
specify { Unit("1 degF").should be_within(Unit("0.001 degK")).of(Unit("0.5555 degK"))}
|
526
|
-
end
|
527
|
-
|
528
|
-
context "reported bugs" do
|
529
|
-
specify { (Unit("189 Mtonne") * Unit("1189 g/tonne")).should == Unit("224721 tonne") }
|
530
|
-
specify { (Unit("189 Mtonne") * Unit("1189 g/tonne")).convert_to("tonne").should == Unit("224721 tonne") }
|
531
|
-
end
|
532
|
-
end
|
533
|
-
|
534
|
-
describe "Unit Math" do
|
535
|
-
context "operators:" do
|
536
|
-
context "addition (+)" do
|
537
|
-
context "between compatible units" do
|
538
|
-
specify { (Unit("0 m") + Unit("10 m")).should == Unit("10 m")}
|
539
|
-
specify { (Unit("5 kg") + Unit("10 kg")).should == Unit("15 kg")}
|
540
|
-
end
|
541
|
-
|
542
|
-
context "between a zero unit and another unit" do
|
543
|
-
specify { (Unit("0 kg") + Unit("10 m")).should == Unit("10 m")}
|
544
|
-
specify { (Unit("0 m") + Unit("10 kg")).should == Unit("10 kg")}
|
545
|
-
end
|
546
|
-
|
547
|
-
context "between incompatible units" do
|
548
|
-
specify { expect {Unit("10 kg") + Unit("10 m")}.to raise_error(ArgumentError)}
|
549
|
-
specify { expect {Unit("10 m") + Unit("10 kg")}.to raise_error(ArgumentError)}
|
550
|
-
end
|
551
|
-
|
552
|
-
context "a number from a unit" do
|
553
|
-
specify { expect { Unit("10 kg") + 1 }.to raise_error(ArgumentError)}
|
554
|
-
specify { expect { 10 + Unit("10 kg") }.to raise_error(ArgumentError)}
|
555
|
-
end
|
556
|
-
|
557
|
-
context "between two temperatures" do
|
558
|
-
specify { expect {(Unit("100 tempK") + Unit("100 tempK"))}.to raise_error(ArgumentError,"Cannot add two temperatures") }
|
559
|
-
end
|
560
|
-
|
561
|
-
context "between a temperature and a degree" do
|
562
|
-
specify { (Unit("100 tempK") + Unit("100 degK")).should == Unit("200 tempK") }
|
563
|
-
end
|
564
|
-
|
565
|
-
context "between a degree and a temperature" do
|
566
|
-
specify { (Unit("100 degK") + Unit("100 tempK")).should == Unit("200 tempK") }
|
567
|
-
end
|
568
|
-
|
569
|
-
end
|
570
|
-
|
571
|
-
context "subtracting (-)" do
|
572
|
-
context "compatible units" do
|
573
|
-
specify { (Unit("0 m") - Unit("10 m")).should == Unit("-10 m")}
|
574
|
-
specify { (Unit("5 kg") - Unit("10 kg")).should == Unit("-5 kg")}
|
575
|
-
end
|
576
|
-
|
577
|
-
context "a unit from a zero unit" do
|
578
|
-
specify { (Unit("0 kg") - Unit("10 m")).should == Unit("-10 m")}
|
579
|
-
specify { (Unit("0 m") - Unit("10 kg")).should == Unit("-10 kg")}
|
580
|
-
end
|
581
|
-
|
582
|
-
context "incompatible units" do
|
583
|
-
specify { expect {Unit("10 kg") - Unit("10 m")}.to raise_error(ArgumentError)}
|
584
|
-
specify { expect {Unit("10 m") - Unit("10 kg")}.to raise_error(ArgumentError)}
|
585
|
-
end
|
586
|
-
|
587
|
-
context "a number from a unit" do
|
588
|
-
specify { expect { Unit("10 kg") - 1 }.to raise_error(ArgumentError)}
|
589
|
-
specify { expect { 10 - Unit("10 kg") }.to raise_error(ArgumentError)}
|
590
|
-
end
|
591
|
-
|
592
|
-
context "between two temperatures" do
|
593
|
-
specify { (Unit("100 tempK") - Unit("100 tempK")).should == Unit("0 degK") }
|
594
|
-
end
|
595
|
-
|
596
|
-
context "between a temperature and a degree" do
|
597
|
-
specify { (Unit("100 tempK") - Unit("100 degK")).should == Unit("0 tempK") }
|
598
|
-
end
|
599
|
-
|
600
|
-
context "between a degree and a temperature" do
|
601
|
-
specify { expect {(Unit("100 degK") - Unit("100 tempK"))}.to raise_error(ArgumentError,"Cannot subtract a temperature from a differential degree unit")}
|
602
|
-
end
|
603
|
-
|
604
|
-
end
|
605
|
-
|
606
|
-
context "multiplying (*)" do
|
607
|
-
context "between compatible units" do
|
608
|
-
specify { (Unit("0 m") * Unit("10 m")).should == Unit("0 m^2")}
|
609
|
-
specify { (Unit("5 kg") * Unit("10 kg")).should == Unit("50 kg^2")}
|
610
|
-
end
|
611
|
-
|
612
|
-
context "between incompatible units" do
|
613
|
-
specify { (Unit("0 m") * Unit("10 kg")).should == Unit("0 kg*m")}
|
614
|
-
specify { (Unit("5 m") * Unit("10 kg")).should == Unit("50 kg*m")}
|
615
|
-
end
|
616
|
-
|
617
|
-
context "by a temperature" do
|
618
|
-
specify { expect { Unit("5 kg") * Unit("100 tempF")}.to raise_exception(ArgumentError) }
|
619
|
-
end
|
620
|
-
|
621
|
-
context "by a number" do
|
622
|
-
specify { (10 * Unit("5 kg")).should == Unit("50 kg")}
|
623
|
-
end
|
624
|
-
|
625
|
-
end
|
626
|
-
|
627
|
-
context "dividing (/)" do
|
628
|
-
context "compatible units" do
|
629
|
-
specify { (Unit("0 m") / Unit("10 m")).should == Unit(0)}
|
630
|
-
specify { (Unit("5 kg") / Unit("10 kg")).should == Rational(1,2)}
|
631
|
-
end
|
632
|
-
|
633
|
-
context "incompatible units" do
|
634
|
-
specify { (Unit("0 m") / Unit("10 kg")).should == Unit("0 m/kg")}
|
635
|
-
specify { (Unit("5 m") / Unit("10 kg")).should == Unit("1/2 m/kg")}
|
636
|
-
end
|
637
|
-
|
638
|
-
context "by a temperature" do
|
639
|
-
specify { expect { Unit("5 kg") / Unit("100 tempF")}.to raise_exception(ArgumentError) }
|
640
|
-
end
|
641
|
-
|
642
|
-
context "a number by a unit" do
|
643
|
-
specify { (10 / Unit("5 kg")).should == Unit("2 1/kg")}
|
644
|
-
end
|
645
|
-
|
646
|
-
context "a unit by a number" do
|
647
|
-
specify { (Unit("5 kg") / 2).should == Unit("2.5 kg")}
|
648
|
-
end
|
649
|
-
|
650
|
-
context "by zero" do
|
651
|
-
specify { expect { Unit("10 m") / 0}.to raise_error(ZeroDivisionError)}
|
652
|
-
specify { expect { Unit("10 m") / Unit("0 m")}.to raise_error(ZeroDivisionError)}
|
653
|
-
specify { expect { Unit("10 m") / Unit("0 kg")}.to raise_error(ZeroDivisionError)}
|
654
|
-
end
|
655
|
-
end
|
656
|
-
|
657
|
-
context "exponentiating (**)" do
|
658
|
-
|
659
|
-
specify "a temperature raises an execption" do
|
660
|
-
expect { Unit("100 tempK")**2 }.to raise_error(ArgumentError,"Cannot raise a temperature to a power")
|
661
|
-
end
|
662
|
-
|
663
|
-
context Unit("0 m") do
|
664
|
-
it { (subject**1).should == subject }
|
665
|
-
it { (subject**2).should == subject }
|
666
|
-
end
|
667
|
-
|
668
|
-
context Unit("1 m") do
|
669
|
-
it { (subject**0).should == 1 }
|
670
|
-
it { (subject**1).should == subject }
|
671
|
-
it { (subject**(-1)).should == 1/subject }
|
672
|
-
it { (subject**(2)).should == Unit("1 m^2")}
|
673
|
-
it { (subject**(-2)).should == Unit("1 1/m^2")}
|
674
|
-
specify { expect { subject**(1/2)}.to raise_error(ArgumentError, "Illegal root")}
|
675
|
-
# because 1 m^(1/2) doesn't make any sense
|
676
|
-
specify { expect { subject**(Complex(1,1))}.to raise_error(ArgumentError, "exponentiation of complex numbers is not yet supported.")}
|
677
|
-
specify { expect { subject**(Unit("1 m"))}.to raise_error(ArgumentError, "Invalid Exponent")}
|
678
|
-
end
|
679
|
-
|
680
|
-
context Unit("1 m^2") do
|
681
|
-
it { (subject**(Rational(1,2))).should == Unit("1 m")}
|
682
|
-
it { (subject**(0.5)).should == Unit("1 m")}
|
683
|
-
|
684
|
-
specify { expect { subject**(0.12345) }.to raise_error(ArgumentError,"Not a n-th root (1..9), use 1/n")}
|
685
|
-
specify { expect { subject**("abcdefg") }.to raise_error(ArgumentError,"Invalid Exponent")}
|
686
|
-
end
|
687
|
-
|
688
|
-
end
|
689
|
-
|
690
|
-
context "modulo (%)" do
|
691
|
-
context "compatible units" do
|
692
|
-
specify { (Unit("2 m") % Unit("1 m")).should == 0 }
|
693
|
-
specify { (Unit("5 m") % Unit("2 m")).should == 1 }
|
694
|
-
end
|
695
|
-
|
696
|
-
specify "incompatible units raises an exception" do
|
697
|
-
expect { Unit("1 m") % Unit("1 kg")}.to raise_error(ArgumentError,"Incompatible Units")
|
698
|
-
end
|
699
|
-
end
|
700
|
-
end
|
701
|
-
|
702
|
-
context "#power" do
|
703
|
-
subject { Unit("1 m") }
|
704
|
-
it "raises an exception when passed a Float argument" do
|
705
|
-
expect {subject.power(1.5)}.to raise_error(ArgumentError,"Exponent must an Integer")
|
706
|
-
end
|
707
|
-
it "raises an exception when passed a Rational argument" do
|
708
|
-
expect {subject.power(Rational(1,2))}.to raise_error(ArgumentError,"Exponent must an Integer")
|
709
|
-
end
|
710
|
-
it "raises an exception when passed a Complex argument" do
|
711
|
-
expect {subject.power(Complex(1,2))}.to raise_error(ArgumentError,"Exponent must an Integer")
|
712
|
-
end
|
713
|
-
it "raises an exception when called on a temperature unit" do
|
714
|
-
expect { Unit("100 tempC").power(2)}.to raise_error(ArgumentError,"Cannot raise a temperature to a power")
|
715
|
-
end
|
716
|
-
|
717
|
-
specify { (subject.power(-1)).should == Unit("1 1/m") }
|
718
|
-
specify { (subject.power(0)).should == 1 }
|
719
|
-
specify { (subject.power(1)).should == subject }
|
720
|
-
specify { (subject.power(2)).should == Unit("1 m^2") }
|
721
|
-
|
722
|
-
end
|
723
|
-
|
724
|
-
context "#root" do
|
725
|
-
subject { Unit("1 m") }
|
726
|
-
it "raises an exception when passed a Float argument" do
|
727
|
-
expect {subject.root(1.5)}.to raise_error(ArgumentError,"Exponent must an Integer")
|
728
|
-
end
|
729
|
-
it "raises an exception when passed a Rational argument" do
|
730
|
-
expect {subject.root(Rational(1,2))}.to raise_error(ArgumentError,"Exponent must an Integer")
|
731
|
-
end
|
732
|
-
it "raises an exception when passed a Complex argument" do
|
733
|
-
expect {subject.root(Complex(1,2))}.to raise_error(ArgumentError,"Exponent must an Integer")
|
734
|
-
end
|
735
|
-
it "raises an exception when called on a temperature unit" do
|
736
|
-
expect { Unit("100 tempC").root(2)}.to raise_error(ArgumentError,"Cannot take the root of a temperature")
|
737
|
-
end
|
738
|
-
|
739
|
-
specify { (Unit("1 m^2").root(-2)).should == Unit("1 1/m") }
|
740
|
-
specify { (subject.root(-1)).should == Unit("1 1/m") }
|
741
|
-
specify { expect {(subject.root(0))}.to raise_error(ArgumentError, "0th root undefined")}
|
742
|
-
specify { (subject.root(1)).should == subject }
|
743
|
-
specify { (Unit("1 m^2").root(2)).should == Unit("1 m") }
|
744
|
-
|
745
|
-
end
|
746
|
-
|
747
|
-
context "#inverse" do
|
748
|
-
specify { Unit("1 m").inverse.should == Unit("1 1/m") }
|
749
|
-
specify { expect {Unit("100 tempK").inverse}.to raise_error(ArgumentError,"Cannot divide with temperatures") }
|
750
|
-
end
|
751
|
-
|
752
|
-
context "convert to scalars" do
|
753
|
-
specify {Unit("10").to_i.should be_kind_of(Integer)}
|
754
|
-
specify { expect { Unit("10 m").to_i }.to raise_error(RuntimeError,"Cannot convert '10 m' to Integer unless unitless. Use Unit#scalar") }
|
755
|
-
|
756
|
-
specify {Unit("10.0").to_f.should be_kind_of(Float)}
|
757
|
-
specify { expect { Unit("10.0 m").to_f }.to raise_error(RuntimeError,"Cannot convert '10 m' to Float unless unitless. Use Unit#scalar") }
|
758
|
-
|
759
|
-
specify {Unit("1+1i").to_c.should be_kind_of(Complex)}
|
760
|
-
specify { expect { Unit("1+1i m").to_c }.to raise_error(RuntimeError,"Cannot convert '1.0+1.0i m' to Complex unless unitless. Use Unit#scalar") }
|
761
|
-
|
762
|
-
specify {Unit("3/7").to_r.should be_kind_of(Rational)}
|
763
|
-
specify { expect { Unit("3/7 m").to_r }.to raise_error(RuntimeError,"Cannot convert '3/7 m' to Rational unless unitless. Use Unit#scalar") }
|
764
|
-
|
765
|
-
end
|
766
|
-
|
767
|
-
context "absolute value (#abs)" do
|
768
|
-
context "of a unitless unit" do
|
769
|
-
specify "returns the absolute value of the scalar" do
|
770
|
-
Unit("-10").abs.should == 10
|
771
|
-
end
|
772
|
-
end
|
773
|
-
|
774
|
-
context "of a unit" do
|
775
|
-
specify "returns a unit with the absolute value of the scalar" do
|
776
|
-
Unit("-10 m").abs.should == Unit("10 m")
|
777
|
-
end
|
778
|
-
end
|
779
|
-
end
|
780
|
-
|
781
|
-
context "#ceil" do
|
782
|
-
context "of a unitless unit" do
|
783
|
-
specify "returns the ceil of the scalar" do
|
784
|
-
Unit("10.1").ceil.should == 11
|
785
|
-
end
|
786
|
-
end
|
787
|
-
|
788
|
-
context "of a unit" do
|
789
|
-
specify "returns a unit with the ceil of the scalar" do
|
790
|
-
Unit("10.1 m").ceil.should == Unit("11 m")
|
791
|
-
end
|
792
|
-
end
|
793
|
-
end
|
794
|
-
|
795
|
-
context "#floor" do
|
796
|
-
context "of a unitless unit" do
|
797
|
-
specify "returns the floor of the scalar" do
|
798
|
-
Unit("10.1").floor.should == 10
|
799
|
-
end
|
800
|
-
end
|
801
|
-
|
802
|
-
context "of a unit" do
|
803
|
-
specify "returns a unit with the floor of the scalar" do
|
804
|
-
Unit("10.1 m").floor.should == Unit("10 m")
|
805
|
-
end
|
806
|
-
end
|
807
|
-
end
|
808
|
-
|
809
|
-
context "#round" do
|
810
|
-
context "of a unitless unit" do
|
811
|
-
specify "returns the round of the scalar" do
|
812
|
-
Unit("10.5").round.should == 11
|
813
|
-
end
|
814
|
-
end
|
815
|
-
|
816
|
-
context "of a unit" do
|
817
|
-
specify "returns a unit with the round of the scalar" do
|
818
|
-
Unit("10.5 m").round.should == Unit("11 m")
|
819
|
-
end
|
820
|
-
end
|
821
|
-
end
|
822
|
-
|
823
|
-
context "#truncate" do
|
824
|
-
context "of a unitless unit" do
|
825
|
-
specify "returns the truncate of the scalar" do
|
826
|
-
Unit("10.5").truncate.should == 10
|
827
|
-
end
|
828
|
-
end
|
829
|
-
|
830
|
-
context "of a unit" do
|
831
|
-
specify "returns a unit with the truncate of the scalar" do
|
832
|
-
Unit("10.5 m").truncate.should == Unit("10 m")
|
833
|
-
end
|
834
|
-
end
|
835
|
-
end
|
836
|
-
|
837
|
-
context '#zero?' do
|
838
|
-
it "is true when the scalar is zero on the base scale" do
|
839
|
-
Unit("0").should be_zero
|
840
|
-
Unit("0 mm").should be_zero
|
841
|
-
Unit("-273.15 tempC").should be_zero
|
842
|
-
end
|
843
|
-
|
844
|
-
it "is false when the scalar is not zero" do
|
845
|
-
Unit("1").should_not be_zero
|
846
|
-
Unit("1 mm").should_not be_zero
|
847
|
-
Unit("0 tempC").should_not be_zero
|
848
|
-
end
|
849
|
-
end
|
850
|
-
|
851
|
-
context '#succ' do
|
852
|
-
specify { Unit("1").succ.should == Unit("2")}
|
853
|
-
specify { Unit("1 mm").succ.should == Unit("2 mm")}
|
854
|
-
specify { Unit("1 mm").next.should == Unit("2 mm")}
|
855
|
-
specify { Unit("-1 mm").succ.should == Unit("0 mm")}
|
856
|
-
specify { expect {Unit("1.5 mm").succ}.to raise_error(ArgumentError,"Non Integer Scalar")}
|
857
|
-
end
|
858
|
-
|
859
|
-
context '#pred' do
|
860
|
-
specify { Unit("1").pred.should == Unit("0")}
|
861
|
-
specify { Unit("1 mm").pred.should == Unit("0 mm")}
|
862
|
-
specify { Unit("-1 mm").pred.should == Unit("-2 mm")}
|
863
|
-
specify { expect {Unit("1.5 mm").pred}.to raise_error(ArgumentError,"Non Integer Scalar")}
|
864
|
-
end
|
865
|
-
|
866
|
-
context '#divmod' do
|
867
|
-
specify { Unit("5 mm").divmod(Unit("2 mm")).should == [2,1] }
|
868
|
-
specify { Unit("1 km").divmod(Unit("2 m")).should == [500,0] }
|
869
|
-
end
|
870
|
-
|
871
|
-
context "Time helper functions" do
|
872
|
-
before do
|
873
|
-
Time.stub!(:now).and_return(Time.utc(2011,10,16))
|
874
|
-
DateTime.stub!(:now).and_return(DateTime.civil(2011,10,16))
|
875
|
-
Date.stub!(:today).and_return(Date.civil(2011,10,16))
|
876
|
-
end
|
877
|
-
|
878
|
-
context '#since' do
|
879
|
-
specify { Unit("min").since(Time.utc(2001,4,1,0,0,0)).should == Unit("5544000 min")}
|
880
|
-
specify { Unit("min").since(DateTime.civil(2001,4,1,0,0,0)).should == Unit("5544000 min")}
|
881
|
-
specify { Unit("min").since(Date.civil(2001,4,1)).should == Unit("5544000 min")}
|
882
|
-
specify { expect {Unit("min").since("4-1-2001")}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime") }
|
883
|
-
specify { expect {Unit("min").since(nil)}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime") }
|
884
|
-
end
|
885
|
-
|
886
|
-
context '#before' do
|
887
|
-
specify { Unit("5 min").before(Time.now).should == Time.utc(2011,10,15,23,55)}
|
888
|
-
specify { Unit("5 min").before(DateTime.now).should == DateTime.civil(2011,10,15,23,55)}
|
889
|
-
specify { Unit("5 min").before(Date.today).should == DateTime.civil(2011,10,15,23,55)}
|
890
|
-
specify { expect {Unit('5 min').before(nil)}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
891
|
-
specify { expect {Unit('5 min').before("12:00")}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
892
|
-
end
|
893
|
-
|
894
|
-
context '#ago' do
|
895
|
-
specify { Unit("5 min").ago.should be_kind_of Time}
|
896
|
-
specify { Unit("10000 y").ago.should be_kind_of Time}
|
897
|
-
specify { Unit("1 year").ago.should == Time.utc(2010,10,16)}
|
898
|
-
end
|
899
|
-
|
900
|
-
context '#until' do
|
901
|
-
specify { Unit("min").until(Date.civil(2011,10,17)).should == Unit("1440 min")}
|
902
|
-
specify { Unit("min").until(DateTime.civil(2011,10,21)).should == Unit("7200 min")}
|
903
|
-
specify { Unit("min").until(Time.utc(2011,10,21)).should == Unit("7200 min")}
|
904
|
-
specify { expect {Unit('5 min').until(nil)}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
905
|
-
specify { expect {Unit('5 min').until("12:00")}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
906
|
-
end
|
907
|
-
|
908
|
-
context '#from' do
|
909
|
-
specify { Unit("1 day").from(Date.civil(2011,10,17)).should == Date.civil(2011,10,18)}
|
910
|
-
specify { Unit("5 min").from(DateTime.civil(2011,10,21)).should == DateTime.civil(2011,10,21,00,05)}
|
911
|
-
specify { Unit("5 min").from(Time.utc(2011,10,21)).should == Time.utc(2011,10,21,00,05)}
|
912
|
-
specify { expect {Unit('5 min').from(nil)}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
913
|
-
specify { expect {Unit('5 min').from("12:00")}.to raise_error(ArgumentError, "Must specify a Time, Date, or DateTime")}
|
914
|
-
end
|
915
|
-
|
916
|
-
end
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
end
|
921
|
-
|
922
|
-
describe "Unit Output formatting" do
|
923
|
-
context Unit("10.5 m/s^2") do
|
924
|
-
specify { subject.to_s.should == "10.5 m/s^2" }
|
925
|
-
specify { subject.to_s("%0.2f").should == "10.50 m/s^2"}
|
926
|
-
specify { subject.to_s("%0.2e km/s^2").should == "1.05e-02 km/s^2"}
|
927
|
-
specify { subject.to_s("km/s^2").should == "0.0105 km/s^2"}
|
928
|
-
specify { subject.to_s(STDOUT).should == "10.5 m/s^2" }
|
929
|
-
specify { expect {subject.to_s("random string")}.to raise_error(ArgumentError,"'random' Unit not recognized")}
|
930
|
-
end
|
931
|
-
|
932
|
-
end
|
933
|
-
|
934
|
-
describe "Foot-inch conversions" do
|
935
|
-
[
|
936
|
-
["76 in", %Q{6'4"}],
|
937
|
-
["77 in", %Q{6'5"}],
|
938
|
-
["78 in", %Q{6'6"}],
|
939
|
-
["79 in", %Q{6'7"}],
|
940
|
-
["80 in", %Q{6'8"}],
|
941
|
-
["87 in", %Q{7'3"}],
|
942
|
-
["88 in", %Q{7'4"}],
|
943
|
-
["89 in", %Q{7'5"}]
|
944
|
-
].each do |inches, feet|
|
945
|
-
specify { Unit(inches).convert_to("ft").should == Unit(feet)}
|
946
|
-
specify { Unit(inches).to_s(:ft).should == feet}
|
947
|
-
end
|
948
|
-
end
|
949
|
-
|
950
|
-
describe "pound-ounce conversions" do
|
951
|
-
[
|
952
|
-
["76 oz", "4 lbs, 12 oz"],
|
953
|
-
["77 oz", "4 lbs, 13 oz"],
|
954
|
-
["78 oz", "4 lbs, 14 oz"],
|
955
|
-
["79 oz", "4 lbs, 15 oz"],
|
956
|
-
["80 oz", "5 lbs, 0 oz"],
|
957
|
-
["87 oz", "5 lbs, 7 oz"],
|
958
|
-
["88 oz", "5 lbs, 8 oz"],
|
959
|
-
["89 oz", "5 lbs, 9 oz"]
|
960
|
-
].each do |ounces, pounds|
|
961
|
-
specify { Unit(ounces).convert_to("lbs").should == Unit(pounds)}
|
962
|
-
specify { Unit(ounces).to_s(:lbs).should == pounds}
|
963
|
-
end
|
964
|
-
end
|
965
|
-
|