quantify 1.0.5 → 1.1.0
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/lib/quantify.rb +3 -1
- data/lib/quantify/config.rb +243 -243
- data/lib/quantify/core_extensions/numeric.rb +19 -0
- data/lib/quantify/core_extensions/string.rb +29 -0
- data/lib/quantify/core_extensions/symbol.rb +7 -0
- data/lib/quantify/dimensions.rb +19 -21
- data/lib/quantify/quantify.rb +32 -1
- data/lib/quantify/quantity.rb +37 -37
- data/lib/quantify/unit/base_unit.rb +113 -61
- data/lib/quantify/unit/compound_base_unit.rb +54 -14
- data/lib/quantify/unit/compound_unit.rb +42 -18
- data/lib/quantify/unit/non_si_unit.rb +1 -1
- data/lib/quantify/unit/prefix/base_prefix.rb +5 -5
- data/lib/quantify/unit/prefix/prefix.rb +4 -4
- data/lib/quantify/unit/unit.rb +55 -24
- data/spec/compound_unit_spec.rb +205 -0
- data/spec/dimension_spec.rb +49 -1
- data/spec/quantify_spec.rb +20 -0
- data/spec/quantity_spec.rb +9 -9
- data/spec/string_spec.rb +55 -0
- data/spec/unit_spec.rb +292 -10
- metadata +24 -19
- data/lib/quantify/core_extensions.rb +0 -63
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'quantify'
|
2
|
+
include Quantify
|
3
|
+
|
4
|
+
describe Unit do
|
5
|
+
|
6
|
+
describe "compound unit naming algorithms" do
|
7
|
+
|
8
|
+
it "should return pluralised unit name" do
|
9
|
+
Unit.m.pluralized_name.should == 'metres'
|
10
|
+
Unit.ft.pluralized_name.should == 'feet'
|
11
|
+
Unit.lux.pluralized_name.should == 'lux'
|
12
|
+
Unit.kg.pluralized_name.should == 'kilograms'
|
13
|
+
Unit.nautical_league.pluralized_name.should == 'nautical leagues'
|
14
|
+
Unit.centimetre_of_water.pluralized_name.should == 'centimetres of water'
|
15
|
+
(Unit.t*Unit.km).pluralized_name.should == 'tonne kilometres'
|
16
|
+
(Unit.t*Unit.km/Unit.year).pluralized_name.should == 'tonne kilometres per year'
|
17
|
+
(Unit.kg*Unit.m*Unit.m/Unit.s/Unit.s).or_equivalent.pluralized_name.should == 'joules'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create unit with dynamic method and pluralized name" do
|
21
|
+
Unit.feet.symbol.should == 'ft'
|
22
|
+
Unit.gigagrams.name.should == 'gigagram'
|
23
|
+
(Unit.kilograms/(Unit.tonne*Unit.km)).pluralized_name.should == 'kilograms per tonne kilometre'
|
24
|
+
(Unit.kilograms/(Unit.megagrams*Unit.km)).pluralized_name.should == 'kilograms per megagram kilometre'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "squared unit should be called that" do
|
28
|
+
(Unit.m**2).name.should == "square metre"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "cubed unit should be called that" do
|
32
|
+
(Unit.s**3).name.should == "cubic second"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raised unit should be called that" do
|
36
|
+
(Unit.kg**4).name.should == "kilogram to the 4th power"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should derive correct label for compound unit" do
|
40
|
+
unit = (Unit.kg/(Unit.t*Unit.km))
|
41
|
+
unit.label.should == "kg/t·km"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should derive correct label for compound unit" do
|
45
|
+
unit = 1/Unit.m
|
46
|
+
unit.label.should == "m^-1"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should derive correct label for compound unit" do
|
50
|
+
unit = Unit.MJ*(Unit.m**3)/(Unit.kg**2)
|
51
|
+
unit.label.should == "MJ·m³/kg²"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "specific compound unit operations" do
|
57
|
+
|
58
|
+
it "should find equivalent unit for compound unit" do
|
59
|
+
(Unit.m*Unit.m).equivalent_known_unit.name.should == 'square metre'
|
60
|
+
(Unit.km*Unit.lb).equivalent_known_unit.should == nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return equivalent unit if appropriate" do
|
64
|
+
(Unit.m*Unit.m).or_equivalent.name.should == 'square metre'
|
65
|
+
(Unit.km*Unit.lb).or_equivalent.name.should == 'kilometre pound'
|
66
|
+
(Unit.kg*Unit.m*Unit.m/Unit.s/Unit.s).or_equivalent.name.should == 'joule'
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should consolidate across all base units" do
|
70
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
71
|
+
unit.symbol.should == "m² s kg s^-1 m^-3"
|
72
|
+
unit.base_units.size.should == 5
|
73
|
+
unit.consolidate_base_units!
|
74
|
+
unit.symbol.should == "kg m^-1"
|
75
|
+
unit.base_units.size.should == 2
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should cancel base units with one argument which is a symbol" do
|
79
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
80
|
+
unit.symbol.should == "m² s kg s^-1 m^-3"
|
81
|
+
unit.base_units.size.should == 5
|
82
|
+
unit.cancel_base_units! :m
|
83
|
+
unit.symbol.should == "s kg m^-1 s^-1"
|
84
|
+
unit.base_units.size.should == 4
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should cancel base units with multiple arguments including unit objects and strings" do
|
88
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
89
|
+
unit.symbol.should == "m² s kg s^-1 m^-3"
|
90
|
+
unit.base_units.size.should == 5
|
91
|
+
unit.cancel_base_units! Unit.m, 's'
|
92
|
+
unit.symbol.should == "kg m^-1"
|
93
|
+
unit.base_units.size.should == 2
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should refuse to cancel by a compound unit" do
|
97
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
98
|
+
lambda{unit.cancel_base_units!(Unit.m**2)}.should raise_error
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should initialize compound unit with one CompoundBaseUnit" do
|
102
|
+
base = Unit::CompoundBaseUnit.new Unit.h, -1
|
103
|
+
compound_unit = Unit::Compound.new base
|
104
|
+
compound_unit.symbol.should == "h^-1"
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should initialize compound unit with multiple CompoundBaseUnits" do
|
108
|
+
base1 = Unit::CompoundBaseUnit.new Unit.h, -1
|
109
|
+
base2 = Unit::CompoundBaseUnit.new Unit.mi
|
110
|
+
compound_unit = Unit::Compound.new base1, base2
|
111
|
+
compound_unit.symbol.should == "mi h^-1"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should initialize compound unit with multiple individual units" do
|
115
|
+
base1 = Unit.kW
|
116
|
+
base2 = Unit.h
|
117
|
+
compound_unit = Unit::Compound.new base1, base2
|
118
|
+
compound_unit.symbol.should == "kW h"
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should initialize compound unit with array splat of multiple individual units" do
|
122
|
+
base1 = Unit.kW
|
123
|
+
base2 = Unit.h
|
124
|
+
array = [base1,base2]
|
125
|
+
compound_unit = Unit::Compound.new *array
|
126
|
+
compound_unit.symbol.should == "kW h"
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should initialize compound unit with one sub-array" do
|
130
|
+
base1 = [Unit.h, -1]
|
131
|
+
compound_unit = Unit::Compound.new base1
|
132
|
+
compound_unit.symbol.should == "h^-1"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should initialize compound unit with multiple sub-arrays" do
|
136
|
+
base1 = [Unit.h, -1]
|
137
|
+
base2 = [Unit.m, 2]
|
138
|
+
compound_unit = Unit::Compound.new base1, base2
|
139
|
+
compound_unit.symbol.should == "m² h^-1"
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should initialize compound unit with variable arguments" do
|
143
|
+
base1 = Unit.kg
|
144
|
+
base2 = Unit::CompoundBaseUnit.new Unit.m, 2
|
145
|
+
base3 = [Unit.s, -2]
|
146
|
+
compound_unit = Unit::Compound.new base1, base2, base3
|
147
|
+
compound_unit.measures.should == "energy"
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should initialize compound unit with variable arguments in splat array" do
|
151
|
+
base1 = Unit.kg
|
152
|
+
base2 = Unit::CompoundBaseUnit.new Unit.m, 2
|
153
|
+
base3 = [Unit.s, -2]
|
154
|
+
array = [base1,base2,base3]
|
155
|
+
compound_unit = Unit::Compound.new *array
|
156
|
+
compound_unit.measures.should == "energy"
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should throw error if base unit is a compound unit" do
|
160
|
+
base1 = Unit.kg*Unit.m
|
161
|
+
base2 = Unit::CompoundBaseUnit.new Unit.m, 2
|
162
|
+
base3 = [Unit.s, -2]
|
163
|
+
array = [base1,base2,base3]
|
164
|
+
lambda{compound_unit = Unit::Compound.new *array}.should raise_error
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should throw error is base unit is a compound unit" do
|
168
|
+
base1 = Unit.kg
|
169
|
+
base2 = Unit::CompoundBaseUnit.new Unit.m, 2
|
170
|
+
base3 = [Unit.kg*Unit.m, -2]
|
171
|
+
array = [base1,base2,base3]
|
172
|
+
lambda{compound_unit = Unit::Compound.new *array}.should raise_error
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should throw error is base unit array too big" do
|
176
|
+
base1 = Unit.kg
|
177
|
+
base2 = Unit::CompoundBaseUnit.new Unit.m, 2
|
178
|
+
base3 = [Unit.kg, -2, "something else"]
|
179
|
+
array = [base1,base2,base3]
|
180
|
+
lambda{compound_unit = Unit::Compound.new *array}.should raise_error
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should not allow comound unit to be used to initialize CompoundBaseUnit" do
|
184
|
+
lambda{Unit::CompoundBaseUnit.new((Unit.m*Unit.m), 2)}.should raise_error
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
it "should rationalize base units with automatically" do
|
189
|
+
unit = Unit.yard*Unit.foot
|
190
|
+
unit.rationalize_base_units!.label.should eql 'yd²'
|
191
|
+
unit = Unit.metre*Unit.centimetre/Unit.inch
|
192
|
+
unit.rationalize_base_units!(:full).label.should eql 'm²/m'
|
193
|
+
unit.consolidate_base_units!.label.should eql 'm'
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should rationalize base units with specified unit" do
|
197
|
+
unit = Unit.yard*Unit.foot
|
198
|
+
unit.rationalize_base_units!(:partial,:yd).label.should eql 'yd²'
|
199
|
+
unit = Unit.metre*Unit.centimetre/Unit.inch
|
200
|
+
unit.rationalize_base_units!(:full,:m).label.should eql 'm²/m'
|
201
|
+
unit.consolidate_base_units!.label.should eql 'm'
|
202
|
+
end
|
203
|
+
|
204
|
+
end
|
205
|
+
end
|
data/spec/dimension_spec.rb
CHANGED
@@ -276,7 +276,7 @@ describe Dimensions do
|
|
276
276
|
it "should return the correct SI base units" do
|
277
277
|
units = Dimensions.area.si_base_units :symbol
|
278
278
|
units.class.should == Array
|
279
|
-
units.include?('m
|
279
|
+
units.include?('m²').should == true
|
280
280
|
end
|
281
281
|
|
282
282
|
it "should recognise molar quantity" do
|
@@ -290,5 +290,53 @@ describe Dimensions do
|
|
290
290
|
dimension.is_molar_quantity?.should == false
|
291
291
|
dimension.is_specific_quantity?.should == true
|
292
292
|
end
|
293
|
+
|
294
|
+
it "should unload a dimension with instance method" do
|
295
|
+
dimension = Dimensions.length
|
296
|
+
dimension.loaded?.should be_true
|
297
|
+
dimension.unload
|
298
|
+
dimension.loaded?.should be_false
|
299
|
+
dimension.load
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should unload a dimension with class method and object" do
|
303
|
+
dimension = Dimensions.length
|
304
|
+
dimension.loaded?.should be_true
|
305
|
+
Dimensions.unload(dimension)
|
306
|
+
dimension.loaded?.should be_false
|
307
|
+
dimension.load
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should unload a dimension with class method and multiple objects" do
|
311
|
+
dimension1 = Dimensions.length
|
312
|
+
dimension2 = Dimensions.mass
|
313
|
+
dimension1.loaded?.should be_true
|
314
|
+
dimension2.loaded?.should be_true
|
315
|
+
Dimensions.unload(dimension1,dimension2)
|
316
|
+
dimension1.loaded?.should be_false
|
317
|
+
dimension2.loaded?.should be_false
|
318
|
+
dimension1.load
|
319
|
+
dimension2.load
|
320
|
+
end
|
321
|
+
|
322
|
+
it "should unload a dimension with class method and physical_quantity" do
|
323
|
+
dimension = Dimensions.length
|
324
|
+
dimension.loaded?.should be_true
|
325
|
+
Dimensions.unload(dimension.physical_quantity)
|
326
|
+
dimension.loaded?.should be_false
|
327
|
+
dimension.load
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should unload a dimension with class method and multiple objects and physical_quantity" do
|
331
|
+
dimension1 = Dimensions.length
|
332
|
+
dimension2 = Dimensions.mass
|
333
|
+
dimension1.loaded?.should be_true
|
334
|
+
dimension2.loaded?.should be_true
|
335
|
+
Dimensions.unload(dimension1.physical_quantity,dimension2.physical_quantity)
|
336
|
+
dimension1.loaded?.should be_false
|
337
|
+
dimension2.loaded?.should be_false
|
338
|
+
dimension1.load
|
339
|
+
dimension2.load
|
340
|
+
end
|
293
341
|
end
|
294
342
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'quantify'
|
2
|
+
include Quantify
|
3
|
+
|
4
|
+
describe Quantify do
|
5
|
+
|
6
|
+
it "should initialize superscript format" do
|
7
|
+
Quantify.use_superscript_characters?.should be_true
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should set superscript usage to true" do
|
11
|
+
Quantify.use_superscript_characters=true
|
12
|
+
Quantify.use_superscript_characters?.should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should set superscript usage to false" do
|
16
|
+
Quantify.use_superscript_characters=false
|
17
|
+
Quantify.use_superscript_characters?.should be_false
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/spec/quantity_spec.rb
CHANGED
@@ -191,7 +191,7 @@ describe Quantity do
|
|
191
191
|
it "should convert to SI unit correctly" do
|
192
192
|
100.cm.to_si.to_s.should == "1.0 m"
|
193
193
|
2.kWh.to_si.to_s.should == "7200000.0 J"
|
194
|
-
400.ha.to_si.to_s.should == "4000000.0 m
|
194
|
+
400.ha.to_si.to_s.should == "4000000.0 m²"
|
195
195
|
35.degree_celsius.to_si.to_s.should == "308.15 K"
|
196
196
|
end
|
197
197
|
|
@@ -218,23 +218,23 @@ describe Quantity do
|
|
218
218
|
|
219
219
|
it "should raise a quantity to a power correctly" do
|
220
220
|
unit = 50.ft ** 2
|
221
|
-
unit.to_s.should == "2500.0 ft
|
221
|
+
unit.to_s.should == "2500.0 ft²"
|
222
222
|
unit = 50.ft ** 3
|
223
|
-
unit.to_s.should == "125000.0 ft
|
223
|
+
unit.to_s.should == "125000.0 ft³"
|
224
224
|
unit = 50.ft ** -1
|
225
225
|
unit.to_s.should == "0.02 ft^-1"
|
226
226
|
unit = (10.m/1.s)**2
|
227
|
-
unit.to_s.should == "100.0 m
|
227
|
+
unit.to_s.should == "100.0 m² s^-2"
|
228
228
|
unit = (10.m/1.s)**-1
|
229
229
|
unit.to_s.should == "0.1 s m^-1"
|
230
230
|
lambda{ ((10.m/1.s)** 0.5) }.should raise_error
|
231
231
|
end
|
232
232
|
|
233
233
|
it "should raise a quantity to a power correctly" do
|
234
|
-
(50.ft.pow! 2).to_s.should == "2500.0 ft
|
235
|
-
(50.ft.pow! 3).to_s.should == "125000.0 ft
|
234
|
+
(50.ft.pow! 2).to_s.should == "2500.0 ft²"
|
235
|
+
(50.ft.pow! 3).to_s.should == "125000.0 ft³"
|
236
236
|
(50.ft.pow! -1).to_s.should == "0.02 ft^-1"
|
237
|
-
((10.m/1.s).pow! 2).to_s.should == "100.0 m
|
237
|
+
((10.m/1.s).pow! 2).to_s.should == "100.0 m² s^-2"
|
238
238
|
((10.m/1.s).pow! -1).to_s.should == "0.1 s m^-1"
|
239
239
|
lambda{ ((10.m/1.s).pow! 0.5) }.should raise_error
|
240
240
|
end
|
@@ -257,14 +257,14 @@ describe Quantity do
|
|
257
257
|
quantity.to_s.should eql "432.0 yd ft"
|
258
258
|
new_quantity=quantity.rationalize_units
|
259
259
|
quantity.to_s.should eql "432.0 yd ft"
|
260
|
-
new_quantity.to_s.should eql "144.0 yd
|
260
|
+
new_quantity.to_s.should eql "144.0 yd²"
|
261
261
|
end
|
262
262
|
|
263
263
|
it "should rationalize units and modify value in place" do
|
264
264
|
quantity = 12.yards*36.feet
|
265
265
|
quantity.to_s.should eql "432.0 yd ft"
|
266
266
|
quantity.rationalize_units!
|
267
|
-
quantity.to_s.should eql "144.0 yd
|
267
|
+
quantity.to_s.should eql "144.0 yd²"
|
268
268
|
end
|
269
269
|
|
270
270
|
it "should be greater than" do
|
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
require 'quantify'
|
3
|
+
|
4
|
+
describe String do
|
5
|
+
|
6
|
+
describe "applying superscripts" do
|
7
|
+
|
8
|
+
it "should replace 2s with superscripts" do
|
9
|
+
"kg^2".with_superscript_characters.should eql "kg²"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should replace 3s with superscripts" do
|
13
|
+
"kg^3".with_superscript_characters.should eql "kg³"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not replace 4s with superscripts" do
|
17
|
+
"kg^4".with_superscript_characters.should eql "kg^4"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not replace larger numbers starting with 2 with superscripts" do
|
21
|
+
"kg^25".with_superscript_characters.should eql "kg^25"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should replace multiple 2s and 3s with superscripts" do
|
25
|
+
"kg^3 m^2 K^2".with_superscript_characters.should eql "kg³ m² K²"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should replace multiple 2s and 3s with superscripts except negatives or larger numbers" do
|
29
|
+
"kg^3 bq^-2 m^2 K^2 A^2500".with_superscript_characters.should eql "kg³ bq^-2 m² K² A^2500"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "removing superscripts" do
|
35
|
+
|
36
|
+
it "should replace superscripts with 2s" do
|
37
|
+
"kg²".without_superscript_characters.should eql "kg^2"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should replace superscripts with 3s" do
|
41
|
+
"kg³".without_superscript_characters.should eql "kg^3"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should replace multiple superscripts with 2s and 3s" do
|
45
|
+
"kg³ m² K²".without_superscript_characters.should eql "kg^3 m^2 K^2"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should replace multiple superscripts with 2s and 3s except negatives or larger numbers" do
|
49
|
+
"kg³ bq^-2 m² K² A^2500".without_superscript_characters.should eql "kg^3 bq^-2 m^2 K^2 A^2500"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
data/spec/unit_spec.rb
CHANGED
@@ -3,6 +3,236 @@ include Quantify
|
|
3
3
|
|
4
4
|
describe Unit do
|
5
5
|
|
6
|
+
it "unload" do
|
7
|
+
Unit.unload(:rad)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "unit identifiers" do
|
11
|
+
|
12
|
+
describe "default configuration" do
|
13
|
+
|
14
|
+
it "should know that superscripts are to be used" do
|
15
|
+
Quantify.use_superscript_characters?.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should use superscript characters for powers of 2 and 3 by default" do
|
19
|
+
Quantify.use_superscript_characters?.should be_true
|
20
|
+
Unit.cubic_metre.label.should eql 'm³'
|
21
|
+
Unit.square_metre.label.should eql 'm²'
|
22
|
+
(Unit.m**2).label.should eql 'm²'
|
23
|
+
(Unit.kg**3).label.should eql 'kg³'
|
24
|
+
unit = (Unit.m*Unit.m*Unit.K*Unit.K)/(Unit.s**3)
|
25
|
+
unit.label.should eql 'm²·K²/s³'
|
26
|
+
unit = unit*Unit.m
|
27
|
+
unit.label.should eql 'm³·K²/s³'
|
28
|
+
unit = unit*Unit.m
|
29
|
+
unit.label.should eql 'm^4·K²/s³'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should recognize and get unit with alternative syntax" do
|
33
|
+
Unit.for("m^2").label.should eql 'm²'
|
34
|
+
Unit.for("km^2").label.should eql 'km²'
|
35
|
+
Unit.for("Gg^3").label.should eql 'Gg³'
|
36
|
+
(Unit.Gg**3).label.should eql 'Gg³'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "parsing complex unit strings with any syntax should work" do
|
40
|
+
Unit.parse("m^4·K²/s³").label.should eql 'm^4·K²/s³'
|
41
|
+
Unit.parse("m^4 K²/s³").label.should eql 'm^4·K²/s³'
|
42
|
+
Unit.parse("m^4 K^2/s^3").label.should eql 'm^4·K²/s³'
|
43
|
+
Unit.parse("m^4 K^2 s^-3").label.should eql 'm^4·K²/s³'
|
44
|
+
|
45
|
+
Unit.parse("m^4·K²/s³").symbol.should eql 'm^4 K² s^-3'
|
46
|
+
Unit.parse("m^4 K²/s³").symbol.should eql 'm^4 K² s^-3'
|
47
|
+
Unit.parse("m^4 K^2/s^3").symbol.should eql 'm^4 K² s^-3'
|
48
|
+
Unit.parse("m^4 K^2 s^-3").symbol.should eql 'm^4 K² s^-3'
|
49
|
+
|
50
|
+
Unit.parse("m^4·K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
51
|
+
Unit.parse("m^4 K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
52
|
+
Unit.parse("m^4 K^2/s^3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
53
|
+
Unit.parse("m^4 K^2 s^-3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "explicitly turning off superscript characters" do
|
59
|
+
|
60
|
+
before :all do
|
61
|
+
Quantify.use_superscript_characters=false
|
62
|
+
end
|
63
|
+
|
64
|
+
after :all do
|
65
|
+
Quantify.use_superscript_characters!
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should know that superscripts are not to be used" do
|
69
|
+
Quantify.use_superscript_characters?.should be_false
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should NOT use superscript characters for powers of 2 and 3" do
|
73
|
+
Unit.cubic_metre.label.should eql 'm^3'
|
74
|
+
Unit.square_metre.label.should eql 'm^2'
|
75
|
+
(Unit.m**2).label.should eql 'm^2'
|
76
|
+
(Unit.kg**3).label.should eql 'kg^3'
|
77
|
+
unit = (Unit.m*Unit.m*Unit.K*Unit.K)/(Unit.s**3)
|
78
|
+
unit.label.should eql 'm^2·K^2/s^3'
|
79
|
+
unit = unit*Unit.m
|
80
|
+
unit.label.should eql 'm^3·K^2/s^3'
|
81
|
+
unit = unit*Unit.m
|
82
|
+
unit.label.should eql 'm^4·K^2/s^3'
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should recognize and get unit with alternative syntax" do
|
86
|
+
Unit.for("m²").label.should eql "m^2"
|
87
|
+
Unit.for('km²').label.should eql "km^2"
|
88
|
+
Unit.for('Gg³').label.should eql "Gg^3"
|
89
|
+
(Unit.Gg**3).label.should eql "Gg^3"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "parsing complex unit strings with any syntax should work" do
|
93
|
+
Unit.parse("m^4·K²/s³").label.should eql "m^4·K^2/s^3"
|
94
|
+
Unit.parse("m^4 K²/s³").label.should eql "m^4·K^2/s^3"
|
95
|
+
Unit.parse("m^4 K^2/s^3").label.should eql "m^4·K^2/s^3"
|
96
|
+
Unit.parse("m^4 K^2 s^-3").label.should eql "m^4·K^2/s^3"
|
97
|
+
|
98
|
+
|
99
|
+
Unit.parse("m^4·K²/s³").symbol.should eql 'm^4 K^2 s^-3'
|
100
|
+
Unit.parse("m^4 K²/s³").symbol.should eql 'm^4 K^2 s^-3'
|
101
|
+
Unit.parse("m^4 K^2/s^3").symbol.should eql 'm^4 K^2 s^-3'
|
102
|
+
Unit.parse("m^4 K^2 s^-3").symbol.should eql 'm^4 K^2 s^-3'
|
103
|
+
|
104
|
+
Unit.parse("m^4·K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
105
|
+
Unit.parse("m^4 K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
106
|
+
Unit.parse("m^4 K^2/s^3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
107
|
+
Unit.parse("m^4 K^2 s^-3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "explicitly turning on superscript characters" do
|
113
|
+
|
114
|
+
before :all do
|
115
|
+
Quantify.use_superscript_characters=false
|
116
|
+
Quantify.use_superscript_characters=true
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should know that superscripts are to be used" do
|
120
|
+
Quantify.use_superscript_characters?.should be_true
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should use superscript characters for powers of 2 and 3" do
|
124
|
+
Unit.cubic_metre.label.should eql 'm³'
|
125
|
+
Unit.square_metre.label.should eql 'm²'
|
126
|
+
(Unit.m**2).label.should eql 'm²'
|
127
|
+
(Unit.kg**3).label.should eql 'kg³'
|
128
|
+
unit = (Unit.m*Unit.m*Unit.K*Unit.K)/(Unit.s**3)
|
129
|
+
unit.label.should eql 'm²·K²/s³'
|
130
|
+
unit = unit*Unit.m
|
131
|
+
unit.label.should eql 'm³·K²/s³'
|
132
|
+
unit = unit*Unit.m
|
133
|
+
unit.label.should eql 'm^4·K²/s³'
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should recognize and get unit with alternative syntax" do
|
137
|
+
Unit.for("m^2").label.should eql 'm²'
|
138
|
+
Unit.for("km^2").label.should eql 'km²'
|
139
|
+
Unit.for("Gg^3").label.should eql 'Gg³'
|
140
|
+
(Unit.Gg**3).label.should eql 'Gg³'
|
141
|
+
end
|
142
|
+
|
143
|
+
it "parsing complex unit strings with any syntax should work" do
|
144
|
+
Unit.parse("m^4·K²/s³").label.should eql 'm^4·K²/s³'
|
145
|
+
Unit.parse("m^4 K²/s³").label.should eql 'm^4·K²/s³'
|
146
|
+
Unit.parse("m^4 K^2/s^3").label.should eql 'm^4·K²/s³'
|
147
|
+
Unit.parse("m^4 K^2 s^-3").label.should eql 'm^4·K²/s³'
|
148
|
+
|
149
|
+
Unit.parse("m^4·K²/s³").symbol.should eql 'm^4 K² s^-3'
|
150
|
+
Unit.parse("m^4 K²/s³").symbol.should eql 'm^4 K² s^-3'
|
151
|
+
Unit.parse("m^4 K^2/s^3").symbol.should eql 'm^4 K² s^-3'
|
152
|
+
Unit.parse("m^4 K^2 s^-3").symbol.should eql 'm^4 K² s^-3'
|
153
|
+
|
154
|
+
Unit.parse("m^4·K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
155
|
+
Unit.parse("m^4 K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
156
|
+
Unit.parse("m^4 K^2/s^3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
157
|
+
Unit.parse("m^4 K^2 s^-3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "explicitly turning on superscript characters with bang! method" do
|
163
|
+
|
164
|
+
before :all do
|
165
|
+
Quantify.use_superscript_characters=false
|
166
|
+
Quantify.use_superscript_characters!
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should know that superscripts are to be used" do
|
170
|
+
Quantify.use_superscript_characters?.should be_true
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should use superscript characters for powers of 2 and 3" do
|
174
|
+
Unit.cubic_metre.label.should eql 'm³'
|
175
|
+
Unit.square_metre.label.should eql 'm²'
|
176
|
+
(Unit.m**2).label.should eql 'm²'
|
177
|
+
(Unit.kg**3).label.should eql 'kg³'
|
178
|
+
unit = (Unit.m*Unit.m*Unit.K*Unit.K)/(Unit.s**3)
|
179
|
+
unit.label.should eql 'm²·K²/s³'
|
180
|
+
unit = unit*Unit.m
|
181
|
+
unit.label.should eql 'm³·K²/s³'
|
182
|
+
unit = unit*Unit.m
|
183
|
+
unit.label.should eql 'm^4·K²/s³'
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should recognize and get unit with alternative syntax" do
|
187
|
+
Unit.for("m^2").label.should eql 'm²'
|
188
|
+
Unit.for("km^2").label.should eql 'km²'
|
189
|
+
Unit.for("Gg^3").label.should eql 'Gg³'
|
190
|
+
(Unit.Gg**3).label.should eql 'Gg³'
|
191
|
+
end
|
192
|
+
|
193
|
+
it "parsing complex unit strings with any syntax should work" do
|
194
|
+
Unit.parse("m^4·K²/s³").label.should eql 'm^4·K²/s³'
|
195
|
+
Unit.parse("m^4 K²/s³").label.should eql 'm^4·K²/s³'
|
196
|
+
Unit.parse("m^4 K^2/s^3").label.should eql 'm^4·K²/s³'
|
197
|
+
Unit.parse("m^4 K^2 s^-3").label.should eql 'm^4·K²/s³'
|
198
|
+
|
199
|
+
Unit.parse("m^4·K²/s³").symbol.should eql 'm^4 K² s^-3'
|
200
|
+
Unit.parse("m^4 K²/s³").symbol.should eql 'm^4 K² s^-3'
|
201
|
+
Unit.parse("m^4 K^2/s^3").symbol.should eql 'm^4 K² s^-3'
|
202
|
+
Unit.parse("m^4 K^2 s^-3").symbol.should eql 'm^4 K² s^-3'
|
203
|
+
|
204
|
+
Unit.parse("m^4·K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
205
|
+
Unit.parse("m^4 K²/s³").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
206
|
+
Unit.parse("m^4 K^2/s^3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
207
|
+
Unit.parse("m^4 K^2 s^-3").name.should eql 'metre to the 4th power square kelvin per cubic second'
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "identifier case" do
|
213
|
+
|
214
|
+
it "should ignore case when initialising units by name" do
|
215
|
+
Unit.METRE.label.should eql 'm'
|
216
|
+
Unit.metre.label.should eql 'm'
|
217
|
+
Unit.KiLoMeTrE.label.should eql 'km'
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should NOT ignore case when initialising units by symbol" do
|
221
|
+
Unit.m.label.should eql 'm'
|
222
|
+
lambda{Unit.M}.should raise_error
|
223
|
+
Unit.Gg.name.should eql 'gigagram'
|
224
|
+
lambda{Unit.GG}.should raise_error
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should NOT ignore case when initialising units by label" do
|
228
|
+
Unit.ton_us.label.should eql 'ton_us'
|
229
|
+
lambda{Unit.TON_US}.should raise_error
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
6
236
|
describe "unit retrieval" do
|
7
237
|
|
8
238
|
it "symbols list should include" do
|
@@ -86,7 +316,7 @@ describe Unit do
|
|
86
316
|
Unit.cm.factor.should == 0.01
|
87
317
|
Unit.TJ.name.should == 'terajoule'
|
88
318
|
Unit::Prefix::NonSI.load(:name => 'million ', :symbol => 'MM', :factor => 1e6)
|
89
|
-
Unit.MMBTU.name.should == 'million british thermal unit (59 °
|
319
|
+
Unit.MMBTU.name.should == 'million british thermal unit (59 °F)'
|
90
320
|
Unit::Prefix.unload :MM
|
91
321
|
end
|
92
322
|
|
@@ -105,6 +335,58 @@ describe Unit do
|
|
105
335
|
it "empty string should return nil" do
|
106
336
|
Unit.for("").should be_nil
|
107
337
|
end
|
338
|
+
|
339
|
+
it "should recognize m²" do
|
340
|
+
Unit.for("m²").should be_a Unit::Base
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should recognise unit with single word name" do
|
344
|
+
Unit.centimetre.symbol.should eql "cm"
|
345
|
+
end
|
346
|
+
|
347
|
+
it "should recognise unit with multiple word name" do
|
348
|
+
Unit.centimetre_of_mercury.symbol.should eql "cmHg"
|
349
|
+
end
|
350
|
+
|
351
|
+
it "should recognise unit with single word pluralized name" do
|
352
|
+
Unit.centimetres.symbol.should eql "cm"
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should recognise unit with multiple word pluralized name" do
|
356
|
+
Unit.centimetres_of_mercury.symbol.should eql "cmHg"
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should recognise compound unit with single word name" do
|
360
|
+
Unit.centimetre_per_hour.symbol.should eql "cm h^-1"
|
361
|
+
end
|
362
|
+
|
363
|
+
it "should recognise compound unit with multiple word name" do
|
364
|
+
Unit.centimetre_of_mercury_per_hour.symbol.should eql "cmHg h^-1"
|
365
|
+
end
|
366
|
+
|
367
|
+
it "should recognise compound unit with single word pluralized name" do
|
368
|
+
Unit.centimetres_per_hour.symbol.should eql "cm h^-1"
|
369
|
+
end
|
370
|
+
|
371
|
+
it "should recognise compound unit with multiple word pluralized name" do
|
372
|
+
Unit.centimetres_of_mercury_per_hour.symbol.should eql "cmHg h^-1"
|
373
|
+
end
|
374
|
+
|
375
|
+
it "should recognise compound unit with single word name" do
|
376
|
+
Unit.centimetre_per_hour_US_bushel.symbol.should eql "cm h^-1 bu (Imp)^-1"
|
377
|
+
end
|
378
|
+
|
379
|
+
it "should recognise compound unit with multiple word name" do
|
380
|
+
Unit.centimetre_of_mercury_per_hour_US_bushel.symbol.should eql "cmHg h^-1 bu (Imp)^-1"
|
381
|
+
end
|
382
|
+
|
383
|
+
it "should recognise compound unit with single word pluralized name" do
|
384
|
+
Unit.centimetres_per_hour_US_bushels.symbol.should eql "cm h^-1 bu (Imp)^-1"
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should recognise compound unit with multiple word pluralized name" do
|
388
|
+
Unit.centimetres_of_mercury_per_hour_US_bushels.symbol.should eql "cmHg h^-1 bu (Imp)^-1"
|
389
|
+
end
|
108
390
|
|
109
391
|
describe "parsing unit string" do
|
110
392
|
|
@@ -221,7 +503,7 @@ describe Unit do
|
|
221
503
|
it "should mass load units with prefixes" do
|
222
504
|
Unit::SI.prefix_and_load([:kilo,:mega,:giga,:tera],[:metre,:gram,:second])
|
223
505
|
Unit.kilometre.loaded?.should == true
|
224
|
-
Unit.
|
506
|
+
Unit.gigametre.loaded?.should == true
|
225
507
|
Unit.kilogram.loaded?.should == true
|
226
508
|
Unit.teragram.loaded?.should == true
|
227
509
|
Unit.kilosecond.loaded?.should == true
|
@@ -231,13 +513,13 @@ describe Unit do
|
|
231
513
|
it "should mass load unit with prefixes" do
|
232
514
|
Unit::SI.prefix_and_load([:kilo,:mega,:giga,:tera],:metre)
|
233
515
|
Unit.kilometre.loaded?.should == true
|
234
|
-
Unit.
|
516
|
+
Unit.gigametre.loaded?.should == true
|
235
517
|
Unit.nanometre.loaded?.should == false
|
236
518
|
end
|
237
519
|
|
238
520
|
it "should mass load units with prefix" do
|
239
521
|
Unit::SI.prefix_and_load(:giga,[:metre,:gram,:second])
|
240
|
-
Unit.
|
522
|
+
Unit.gigametre.loaded?.should == true
|
241
523
|
Unit.gigagram.loaded?.should == true
|
242
524
|
Unit.gigasecond.loaded?.should == true
|
243
525
|
Unit.nanometre.loaded?.should == false
|
@@ -245,7 +527,7 @@ describe Unit do
|
|
245
527
|
|
246
528
|
it "should mass load units with prefix using objects as arguments" do
|
247
529
|
Unit::SI.prefix_and_load(Unit::Prefix.giga,[Unit.metre,Unit.gram,Unit.second])
|
248
|
-
Unit.
|
530
|
+
Unit.gigametre.loaded?.should == true
|
249
531
|
Unit.gigagram.loaded?.should == true
|
250
532
|
Unit.gigasecond.loaded?.should == true
|
251
533
|
Unit.nanometre.loaded?.should == false
|
@@ -297,7 +579,7 @@ describe Unit do
|
|
297
579
|
|
298
580
|
it "should change the label of canonical unit representation" do
|
299
581
|
unit = Unit.cubic_metre
|
300
|
-
unit.label.should eql "m
|
582
|
+
unit.label.should eql "m³"
|
301
583
|
unit.canonical_label = "m3"
|
302
584
|
unit.label.should eql "m3"
|
303
585
|
Unit.cubic_metre.label.should eql "m3"
|
@@ -612,19 +894,19 @@ describe Unit do
|
|
612
894
|
it "should recognise similar units" do
|
613
895
|
unit_1 = Unit.yard
|
614
896
|
unit_2 = Unit.yard
|
615
|
-
unit_1.
|
897
|
+
unit_1.is_equivalent_to?(unit_2).should == true
|
616
898
|
end
|
617
899
|
|
618
900
|
it "should recognise non-similar units" do
|
619
901
|
unit_1 = Unit.yard
|
620
902
|
unit_2 = Unit.foot
|
621
|
-
unit_1.
|
903
|
+
unit_1.is_equivalent_to?(unit_2).should_not == true
|
622
904
|
end
|
623
905
|
|
624
906
|
it "should recognise non-similar units" do
|
625
907
|
unit_1 = Unit.yard
|
626
908
|
unit_2 = Unit.kelvin
|
627
|
-
(unit_1
|
909
|
+
(unit_1.is_equivalent_to? unit_2).should_not == true
|
628
910
|
end
|
629
911
|
|
630
912
|
it "should recognise known units from compound units based on dimensions and factor" do
|
@@ -639,7 +921,7 @@ describe Unit do
|
|
639
921
|
megagram = Unit.Mg
|
640
922
|
megagram.symbol.should == 'Mg'
|
641
923
|
megagram.factor.should == 1000.0
|
642
|
-
megagram.should
|
924
|
+
megagram.is_equivalent_to?(tonne).should be_true
|
643
925
|
end
|
644
926
|
|
645
927
|
it "should return correct SI unit" do
|