measured 2.0.0 → 2.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.
- checksums.yaml +4 -4
- data/README.md +31 -5
- data/lib/measured/unit_system_builder.rb +37 -0
- data/lib/measured/units/length.rb +3 -3
- data/lib/measured/units/weight.rb +7 -2
- data/lib/measured/version.rb +1 -1
- data/test/unit_system_builder_test.rb +72 -0
- data/test/units/length_test.rb +122 -10
- data/test/units/weight_test.rb +289 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4648c97b03e2458e77b1d85bba0c5509059a0d86
|
4
|
+
data.tar.gz: 233b64d44b798c9e745cba485410a72180436009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a48e99cdcd87f090ab3138228391c6ec682de456f49b34a7afb88480b3ea8c985e5b269e243c40f0f941a69141b97be1ef4417af6b0dbcf0a1061dbc222af59c
|
7
|
+
data.tar.gz: a8d54d54cc160e18947c40e405efe57caab4f1d1b9bdd84521e00b5390410b271a49f1325daa6b8f302c2fb5285cd327018cd16aa40fb79ab8499e32a232bb7c
|
data/README.md
CHANGED
@@ -143,20 +143,46 @@ Measured::Weight.unit_names_with_aliases
|
|
143
143
|
|
144
144
|
## Units and conversions
|
145
145
|
|
146
|
+
### SI units support
|
147
|
+
|
148
|
+
There is support for SI units through the use of `si_unit`. Units declared through it will have automatic support for all SI prefixes:
|
149
|
+
|
150
|
+
| Multiplying Factor | SI Prefix | Scientific Notation |
|
151
|
+
| --------------------------------- | --------- | --------------------- |
|
152
|
+
| 1 000 000 000 000 000 000 000 000 | yotta (Y) | 10^24 |
|
153
|
+
| 1 000 000 000 000 000 000 000 | zetta (Z) | 10^21 |
|
154
|
+
| 1 000 000 000 000 000 000 | exa (E) | 10^18 |
|
155
|
+
| 1 000 000 000 000 000 | peta (P) | 10^15 |
|
156
|
+
| 1 000 000 000 000 | tera (T) | 10^12 |
|
157
|
+
| 1 000 000 000 | giga (G) | 10^9 |
|
158
|
+
| 1 000 000 | mega (M) | 10^6 |
|
159
|
+
| 1 000 | kilo (k) | 10^3 |
|
160
|
+
| 0.001 | milli (m) | 10^-3 |
|
161
|
+
| 0.000 001 | micro (µ) | 10^-6 |
|
162
|
+
| 0.000 000 001 | nano (n) | 10^-9 |
|
163
|
+
| 0.000 000 000 001 | pico (p) | 10^-12 |
|
164
|
+
| 0.000 000 000 000 001 | femto (f) | 10^-15 |
|
165
|
+
| 0.000 000 000 000 000 001 | atto (a) | 10^-18 |
|
166
|
+
| 0.000 000 000 000 000 000 001 | zepto (z) | 10^-21 |
|
167
|
+
| 0.000 000 000 000 000 000 000 001 | yocto (y) | 10^-24 |
|
168
|
+
|
146
169
|
### Bundled unit conversion
|
147
170
|
|
148
171
|
* `Measured::Weight`
|
149
|
-
* g, gram, grams
|
150
|
-
*
|
172
|
+
* g, gram, grams, and all SI prefixes
|
173
|
+
* t, metric_ton, metric_tons
|
174
|
+
* slug, slugs
|
175
|
+
* N, newtons, newton
|
176
|
+
* long_ton, long_tons, weight_ton, weight_tons, 'W/T', imperial_ton, imperial_tons, displacement_ton, displacement_tons
|
177
|
+
* short_ton, short_tons
|
151
178
|
* lb, lbs, pound, pounds
|
152
179
|
* oz, ounce, ounces
|
153
180
|
* `Measured::Length`
|
154
|
-
* m, meter, metre, meters, metres
|
155
|
-
* cm, centimeter, centimetre, centimeters, centimetres
|
156
|
-
* mm, millimeter, millimetre, millimeters, millimetres
|
181
|
+
* m, meter, metre, meters, metres, and all SI prefixes
|
157
182
|
* in, inch, inches
|
158
183
|
* ft, foot, feet
|
159
184
|
* yd, yard, yards
|
185
|
+
* mi, mile, miles
|
160
186
|
|
161
187
|
You can skip these and only define your own units by doing:
|
162
188
|
|
@@ -8,12 +8,49 @@ class Measured::UnitSystemBuilder
|
|
8
8
|
nil
|
9
9
|
end
|
10
10
|
|
11
|
+
def si_unit(unit_name, aliases: [], value: nil)
|
12
|
+
@units += build_si_units(unit_name, aliases: aliases, value: value)
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
11
16
|
def build
|
12
17
|
Measured::UnitSystem.new(@units)
|
13
18
|
end
|
14
19
|
|
15
20
|
private
|
16
21
|
|
22
|
+
SI_PREFIXES = [
|
23
|
+
["y", "yocto", -24],
|
24
|
+
["z", "zepto", -21],
|
25
|
+
["a", "atto", -18],
|
26
|
+
["f", "femto", -15],
|
27
|
+
["p", "pico", -12],
|
28
|
+
["n", "nano", -9],
|
29
|
+
["μ", "micro", -6],
|
30
|
+
["m", "milli", -3],
|
31
|
+
["c", "centi", -2],
|
32
|
+
["d", "deci", -1],
|
33
|
+
["da", "deca", 1],
|
34
|
+
["h", "hecto", 2],
|
35
|
+
["k", "kilo", 3],
|
36
|
+
["M", "mega", 6],
|
37
|
+
["G", "giga", 9],
|
38
|
+
["T", "tera", 12],
|
39
|
+
["P", "peta", 15],
|
40
|
+
["E", "exa", 18],
|
41
|
+
["Z", "zetta", 21],
|
42
|
+
["Y", "yotta", 24]
|
43
|
+
]
|
44
|
+
|
45
|
+
def build_si_units(name, aliases: [], value: nil)
|
46
|
+
si_units = [build_unit(name, aliases: aliases, value: value)]
|
47
|
+
SI_PREFIXES.each do |short, long, exp|
|
48
|
+
long_names = aliases.map { |suffix| "#{long}#{suffix}" }
|
49
|
+
si_units << build_unit("#{short}#{name}", aliases: long_names, value: "#{10 ** exp} #{name}")
|
50
|
+
end
|
51
|
+
si_units
|
52
|
+
end
|
53
|
+
|
17
54
|
def build_unit(name, aliases: [], value: nil)
|
18
55
|
unit = Measured::Unit.new(name, aliases: aliases, value: value)
|
19
56
|
check_for_duplicate_unit_names!(unit)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Measured::Length = Measured.build do
|
2
|
-
|
3
|
-
|
4
|
-
unit :mm, value: "1/1000 m", aliases: [:millimeter, :millimetre, :millimeters, :millimetres]
|
2
|
+
si_unit :m, aliases: [:meter, :metre, :meters, :metres]
|
3
|
+
|
5
4
|
unit :in, value: "0.0254 m", aliases: [:inch, :inches]
|
6
5
|
unit :ft, value: "12 in", aliases: [:foot, :feet]
|
7
6
|
unit :yd, value: "3 ft", aliases: [:yard, :yards]
|
7
|
+
unit :mi, value: "5280 ft", aliases: [:mile, :miles]
|
8
8
|
end
|
@@ -1,6 +1,11 @@
|
|
1
1
|
Measured::Weight = Measured.build do
|
2
|
-
|
3
|
-
|
2
|
+
si_unit :g, aliases: [:gram, :grams]
|
3
|
+
|
4
|
+
unit :t, value: "1000 kg", aliases: [:metric_ton, :metric_tons]
|
5
|
+
unit :slug, value: "14.593903 kg", aliases: [:slugs]
|
6
|
+
unit :N, value: "0.10197162129779283 kg", aliases: [:newtons, :newton]
|
7
|
+
unit :long_ton, value: "2240 lb", aliases: [:long_tons, :weight_ton, :weight_tons, 'W/T', :imperial_ton, :imperial_tons, :displacement_ton, :displacement_tons]
|
8
|
+
unit :short_ton, value: "2000 lb", aliases: [:short_tons]
|
4
9
|
unit :lb, value: "0.45359237 kg", aliases: [:lbs, :pound, :pounds]
|
5
10
|
unit :oz, value: "1/16 lb", aliases: [:ounce, :ounces]
|
6
11
|
end
|
data/lib/measured/version.rb
CHANGED
@@ -37,4 +37,76 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
|
|
37
37
|
|
38
38
|
assert_equal 'BOLD', measurable.unit_system.unit_for!(:BOLD).name
|
39
39
|
end
|
40
|
+
|
41
|
+
test "#si_unit adds 21 new units" do
|
42
|
+
measurable = Measured.build do
|
43
|
+
unit :ft
|
44
|
+
si_unit :in, value: "12 ft", aliases: [:ins]
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_equal 22, measurable.unit_names.count
|
48
|
+
end
|
49
|
+
|
50
|
+
test "#si_unit creates units with si prefixes" do
|
51
|
+
measurable = Measured.build do
|
52
|
+
unit :in
|
53
|
+
si_unit :ft, value: "12 in", aliases: [:fts]
|
54
|
+
end
|
55
|
+
|
56
|
+
prefixes = Measured::UnitSystemBuilder::SI_PREFIXES.map { |short, _, _| "#{short}ft" }
|
57
|
+
prefixes += %w(in ft)
|
58
|
+
assert_equal prefixes.sort, measurable.unit_names
|
59
|
+
end
|
60
|
+
|
61
|
+
test "#si_unit cannot add duplicate unit names" do
|
62
|
+
assert_raises Measured::UnitError do
|
63
|
+
Measured.build do
|
64
|
+
unit :m
|
65
|
+
si_unit :in, aliases: [:inch], value: "0.0254 m"
|
66
|
+
si_unit :in, aliases: [:thing], value: "123 m"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
assert_raises Measured::UnitError do
|
71
|
+
Measured.build do
|
72
|
+
unit :m
|
73
|
+
si_unit :in, aliases: [:inch], value: "0.0254 m"
|
74
|
+
si_unit :inch, aliases: [:thing], value: "123 m"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
test "#si_unit is case sensitive" do
|
80
|
+
measurable = Measured.build do
|
81
|
+
unit :normal
|
82
|
+
si_unit :bold, value: "10 normal"
|
83
|
+
si_unit :BOLD, value: "100 normal"
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_equal "yBOLD", measurable.unit_system.unit_for!(:yBOLD).name
|
87
|
+
assert_equal "YBOLD", measurable.unit_system.unit_for!(:YBOLD).name
|
88
|
+
assert_equal "BOLD", measurable.unit_system.unit_for!(:BOLD).name
|
89
|
+
end
|
90
|
+
|
91
|
+
test "#si_unit generates working units when given base unit" do
|
92
|
+
measurable = Measured.build do
|
93
|
+
si_unit :g, aliases: [:gram, :grams]
|
94
|
+
unit :bigg, value: "1000 g"
|
95
|
+
end
|
96
|
+
|
97
|
+
assert_equal 1000, measurable.unit_system.unit_for!(:bigg).conversion_amount
|
98
|
+
assert_equal "g", measurable.unit_system.unit_for!(:bigg).conversion_unit
|
99
|
+
assert_equal 1000, measurable.unit_system.unit_for!(:kg).conversion_amount
|
100
|
+
assert_equal "g", measurable.unit_system.unit_for!(:kg).conversion_unit
|
101
|
+
end
|
102
|
+
|
103
|
+
test "#si_unit generates working units when given non base unit" do
|
104
|
+
measurable = Measured.build do
|
105
|
+
unit :lb
|
106
|
+
si_unit :long_ton, value: "2240 lb", aliases: ["long ton", "long tons"]
|
107
|
+
end
|
108
|
+
|
109
|
+
assert_equal (2240), measurable.unit_system.unit_for!(:long_ton).conversion_amount
|
110
|
+
assert_equal "lb", measurable.unit_system.unit_for!(:long_ton).conversion_unit
|
111
|
+
end
|
40
112
|
end
|
data/test/units/length_test.rb
CHANGED
@@ -2,18 +2,18 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class Measured::LengthTest < ActiveSupport::TestCase
|
4
4
|
test ".unit_names_with_aliases should be the expected list of valid units" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Measured::Length.unit_names_with_aliases
|
12
|
-
)
|
5
|
+
expected_units = %w(m meter metre meters metres feet foot ft in inch inches mi mile miles yard yards yd)
|
6
|
+
expected_units += Measured::UnitSystemBuilder::SI_PREFIXES.flat_map do |short, long, _|
|
7
|
+
["#{short}m", "#{long}meter", "#{long}metre", "#{long}meters", "#{long}metres"]
|
8
|
+
end
|
9
|
+
|
10
|
+
assert_equal expected_units.sort, Measured::Length.unit_names_with_aliases
|
13
11
|
end
|
14
12
|
|
15
13
|
test ".unit_names should be the list of base unit names" do
|
16
|
-
|
14
|
+
expected_units = %w(m ft in mi yd)
|
15
|
+
expected_units += Measured::UnitSystemBuilder::SI_PREFIXES.map { |short, _, _| "#{short}m" }
|
16
|
+
assert_equal expected_units.sort, Measured::Length.unit_names
|
17
17
|
end
|
18
18
|
|
19
19
|
test ".name" do
|
@@ -37,10 +37,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
37
37
|
assert_conversion Measured::Length, "2000 cm", "0.7874015748E3 in"
|
38
38
|
end
|
39
39
|
|
40
|
+
test ".convert_to from cm to km" do
|
41
|
+
assert_exact_conversion Measured::Length, "2000 cm", "0.02 km"
|
42
|
+
end
|
43
|
+
|
40
44
|
test ".convert_to from cm to m" do
|
41
45
|
assert_exact_conversion Measured::Length, "2000 cm", "20 m"
|
42
46
|
end
|
43
47
|
|
48
|
+
test ".convert_to from cm to mi" do
|
49
|
+
assert_conversion Measured::Length, "2000 cm", "0.1242742E-1 mi"
|
50
|
+
end
|
51
|
+
|
44
52
|
test ".convert_to from cm to mm" do
|
45
53
|
assert_exact_conversion Measured::Length, "2000 cm", "20000 mm"
|
46
54
|
end
|
@@ -61,10 +69,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
61
69
|
assert_exact_conversion Measured::Length, "2000 ft", "24000 in"
|
62
70
|
end
|
63
71
|
|
72
|
+
test ".convert_to from ft to km" do
|
73
|
+
assert_conversion Measured::Length, "2000 ft", "0.6096 km"
|
74
|
+
end
|
75
|
+
|
64
76
|
test ".convert_to from ft to m" do
|
65
77
|
assert_exact_conversion Measured::Length, "2000 ft", "609.6 m"
|
66
78
|
end
|
67
79
|
|
80
|
+
test ".convert_to from ft to mi" do
|
81
|
+
assert_conversion Measured::Length, "2000 ft", "0.3787879 mi"
|
82
|
+
end
|
83
|
+
|
68
84
|
test ".convert_to from ft to mm" do
|
69
85
|
assert_conversion Measured::Length, "2000 ft", "609600 mm"
|
70
86
|
end
|
@@ -85,10 +101,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
85
101
|
assert_exact_conversion Measured::Length, "2000 in", "2000 in"
|
86
102
|
end
|
87
103
|
|
104
|
+
test ".convert_to from in to km" do
|
105
|
+
assert_conversion Measured::Length, "2000 in", "0.508E-1 km"
|
106
|
+
end
|
107
|
+
|
88
108
|
test ".convert_to from in to m" do
|
89
109
|
assert_conversion Measured::Length, "2000 in", "50.8 m"
|
90
110
|
end
|
91
111
|
|
112
|
+
test ".convert_to from in to mi" do
|
113
|
+
assert_conversion Measured::Length, "2000 in", "0.31565658E-1 mi"
|
114
|
+
end
|
115
|
+
|
92
116
|
test ".convert_to from in to mm" do
|
93
117
|
assert_conversion Measured::Length, "2000 in", "50800 mm"
|
94
118
|
end
|
@@ -97,8 +121,40 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
97
121
|
assert_conversion Measured::Length, "2000 in", "0.555555555384E2 yd"
|
98
122
|
end
|
99
123
|
|
124
|
+
test ".convert_to from km to cm" do
|
125
|
+
assert_exact_conversion Measured::Length, "2000 km", "2.0E8 cm"
|
126
|
+
end
|
127
|
+
|
128
|
+
test ".convert_to from km to ft" do
|
129
|
+
assert_conversion Measured::Length, "2000 km", "0.6561679790026247E7 ft"
|
130
|
+
end
|
131
|
+
|
132
|
+
test ".convert_to from km to in" do
|
133
|
+
assert_conversion Measured::Length, "2000 km", "0.78740157480315E8 in"
|
134
|
+
end
|
135
|
+
|
136
|
+
test ".convert_to from km to km" do
|
137
|
+
assert_exact_conversion Measured::Length, "2000 km", "2000 km"
|
138
|
+
end
|
139
|
+
|
140
|
+
test ".convert_to from km to m" do
|
141
|
+
assert_exact_conversion Measured::Length, "2000 km", "2.0E6 m"
|
142
|
+
end
|
143
|
+
|
144
|
+
test ".convert_to from km to mi" do
|
145
|
+
assert_conversion Measured::Length, "2000 km", "0.1242742384475E4 mi"
|
146
|
+
end
|
147
|
+
|
148
|
+
test ".convert_to from km to mm" do
|
149
|
+
assert_exact_conversion Measured::Length, "2000 km", "2.0E9 mm"
|
150
|
+
end
|
151
|
+
|
152
|
+
test ".convert_to from km to yd" do
|
153
|
+
assert_conversion Measured::Length, "2000 km", "0.218722659667542E7 yd"
|
154
|
+
end
|
155
|
+
|
100
156
|
test ".convert_to from m to cm" do
|
101
|
-
|
157
|
+
assert_exact_conversion Measured::Length, "2000 m", "200000 cm"
|
102
158
|
end
|
103
159
|
|
104
160
|
test ".convert_to from m to ft" do
|
@@ -109,10 +165,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
109
165
|
assert_conversion Measured::Length, "2000 m", "0.7874015748E5 in"
|
110
166
|
end
|
111
167
|
|
168
|
+
test ".convert_to from m to km" do
|
169
|
+
assert_exact_conversion Measured::Length, "2000 m", "2 km"
|
170
|
+
end
|
171
|
+
|
112
172
|
test ".convert_to from m to m" do
|
113
173
|
assert_exact_conversion Measured::Length, "2000 m", "2000 m"
|
114
174
|
end
|
115
175
|
|
176
|
+
test ".convert_to from m to mi" do
|
177
|
+
assert_conversion Measured::Length, "2000 m", "0.1242742E1 mi"
|
178
|
+
end
|
179
|
+
|
116
180
|
test ".convert_to from m to mm" do
|
117
181
|
assert_exact_conversion Measured::Length, "2000 m", "2000000 mm"
|
118
182
|
end
|
@@ -121,6 +185,38 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
121
185
|
assert_conversion Measured::Length, "2000 m", "0.2187226596E4 yd"
|
122
186
|
end
|
123
187
|
|
188
|
+
test ".convert_to from mi to cm" do
|
189
|
+
assert_conversion Measured::Length, "2000 mi", "0.3218688E9 cm"
|
190
|
+
end
|
191
|
+
|
192
|
+
test ".convert_to from mi to ft" do
|
193
|
+
assert_exact_conversion Measured::Length, "2000 mi", "0.1056E8 ft"
|
194
|
+
end
|
195
|
+
|
196
|
+
test ".convert_to from mi to in" do
|
197
|
+
assert_exact_conversion Measured::Length, "2000 mi", "0.12672E9 in"
|
198
|
+
end
|
199
|
+
|
200
|
+
test ".convert_to from mi to km" do
|
201
|
+
assert_conversion Measured::Length, "2000 mi", "0.3218688E4 km"
|
202
|
+
end
|
203
|
+
|
204
|
+
test ".convert_to from mi to m" do
|
205
|
+
assert_conversion Measured::Length, "2000 mi", "0.3218688E7 m"
|
206
|
+
end
|
207
|
+
|
208
|
+
test ".convert_to from mi to mi" do
|
209
|
+
assert_exact_conversion Measured::Length, "2000 mi", "2000 mi"
|
210
|
+
end
|
211
|
+
|
212
|
+
test ".convert_to from mi to mm" do
|
213
|
+
assert_conversion Measured::Length, "2000 mi", "0.3218688E10 mm"
|
214
|
+
end
|
215
|
+
|
216
|
+
test ".convert_to from mi to yd" do
|
217
|
+
assert_exact_conversion Measured::Length, "2000 mi", "3520000 yd"
|
218
|
+
end
|
219
|
+
|
124
220
|
test ".convert_to from mm to cm" do
|
125
221
|
assert_exact_conversion Measured::Length, "2000 mm", "200 cm"
|
126
222
|
end
|
@@ -133,10 +229,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
133
229
|
assert_conversion Measured::Length, "2000 mm", "0.7874015748E2 in"
|
134
230
|
end
|
135
231
|
|
232
|
+
test ".convert_to from mm to km" do
|
233
|
+
assert_exact_conversion Measured::Length, "2000 mm", "0.002 km"
|
234
|
+
end
|
235
|
+
|
136
236
|
test ".convert_to from mm to m" do
|
137
237
|
assert_exact_conversion Measured::Length, "2000 mm", "2 m"
|
138
238
|
end
|
139
239
|
|
240
|
+
test ".convert_to from mm to mi" do
|
241
|
+
assert_conversion Measured::Length, "2000 mm", "0.1242742E-2 mi"
|
242
|
+
end
|
243
|
+
|
140
244
|
test ".convert_to from mm to mm" do
|
141
245
|
assert_exact_conversion Measured::Length, "2000 mm", "2000 mm"
|
142
246
|
end
|
@@ -157,10 +261,18 @@ class Measured::LengthTest < ActiveSupport::TestCase
|
|
157
261
|
assert_exact_conversion Measured::Length, "2000 yd", "72000 in"
|
158
262
|
end
|
159
263
|
|
264
|
+
test ".convert_to from yd to km" do
|
265
|
+
assert_conversion Measured::Length, "2000 yd", "0.18288E1 km"
|
266
|
+
end
|
267
|
+
|
160
268
|
test ".convert_to from yd to m" do
|
161
269
|
assert_conversion Measured::Length, "2000 yd", "1828.8 m"
|
162
270
|
end
|
163
271
|
|
272
|
+
test ".convert_to from yd to mi" do
|
273
|
+
assert_conversion Measured::Length, "2000 yd", "1.136364 mi"
|
274
|
+
end
|
275
|
+
|
164
276
|
test ".convert_to from yd to mm" do
|
165
277
|
assert_conversion Measured::Length, "2000 yd", "1828800 mm"
|
166
278
|
end
|
data/test/units/weight_test.rb
CHANGED
@@ -6,17 +6,50 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
test ".unit_names_with_aliases should be the expected list of valid units" do
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
expected_units = %w(
|
10
|
+
N
|
11
|
+
W/T
|
12
|
+
displacement_ton
|
13
|
+
displacement_tons
|
14
|
+
g
|
15
|
+
gram
|
16
|
+
grams
|
17
|
+
imperial_ton
|
18
|
+
imperial_tons
|
19
|
+
lb
|
20
|
+
lbs
|
21
|
+
long_ton
|
22
|
+
long_tons
|
23
|
+
metric_ton
|
24
|
+
metric_tons
|
25
|
+
newton
|
26
|
+
newtons
|
27
|
+
ounce
|
28
|
+
ounces
|
29
|
+
oz
|
30
|
+
pound
|
31
|
+
pounds
|
32
|
+
short_ton
|
33
|
+
short_tons
|
34
|
+
slug
|
35
|
+
slugs
|
36
|
+
t
|
37
|
+
weight_ton
|
38
|
+
weight_tons
|
12
39
|
)
|
40
|
+
expected_units += Measured::UnitSystemBuilder::SI_PREFIXES.flat_map do |short, long, _|
|
41
|
+
["#{short}g", "#{long}gram", "#{long}grams"]
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal expected_units.sort, Measured::Weight.unit_names_with_aliases
|
13
45
|
end
|
14
46
|
|
15
47
|
test ".unit_names should be the list of base unit names" do
|
16
|
-
|
48
|
+
expected_units = %w(N g lb long_ton oz short_ton slug t)
|
49
|
+
expected_units += Measured::UnitSystemBuilder::SI_PREFIXES.map { |short, _, _| "#{short}g" }
|
50
|
+
assert_equal expected_units.sort, Measured::Weight.unit_names
|
17
51
|
end
|
18
52
|
|
19
|
-
|
20
53
|
test "Measured::Weight() delegates automatically to .new" do
|
21
54
|
assert_equal Measured::Weight.new(1, :lb), Measured::Weight(1, :lb)
|
22
55
|
assert_equal Measured::Weight.new(2000, :g), Measured::Weight(2, :kg)
|
@@ -42,6 +75,26 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
42
75
|
assert_conversion Measured::Weight, "2000 g", "70.54792384 oz"
|
43
76
|
end
|
44
77
|
|
78
|
+
test ".convert_to from g to slug" do
|
79
|
+
assert_conversion Measured::Weight, "2000 g", "0.1370435 slug"
|
80
|
+
end
|
81
|
+
|
82
|
+
test ".convert_to from g to N" do
|
83
|
+
assert_conversion Measured::Weight, "2000 g", "19.6133 N"
|
84
|
+
end
|
85
|
+
|
86
|
+
test ".convert_to from g to short_ton" do
|
87
|
+
assert_conversion Measured::Weight, "2000 g", "0.002204623 short_ton"
|
88
|
+
end
|
89
|
+
|
90
|
+
test ".convert_to from g to long_ton" do
|
91
|
+
assert_conversion Measured::Weight, "2000 g", "0.001968413055222 long_ton"
|
92
|
+
end
|
93
|
+
|
94
|
+
test ".convert_to from g to t" do
|
95
|
+
assert_exact_conversion Measured::Weight, "2000 g", "0.002 t"
|
96
|
+
end
|
97
|
+
|
45
98
|
test ".convert_to from kg to g" do
|
46
99
|
assert_exact_conversion Measured::Weight, "2000 kg", "2000000 g"
|
47
100
|
end
|
@@ -58,6 +111,26 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
58
111
|
assert_conversion Measured::Weight, "2000 kg", "70547.92390 oz"
|
59
112
|
end
|
60
113
|
|
114
|
+
test ".convert_to from kg to slug" do
|
115
|
+
assert_conversion Measured::Weight, "2000 kg", "137.0435311239221 slug"
|
116
|
+
end
|
117
|
+
|
118
|
+
test ".convert_to from kg to N" do
|
119
|
+
assert_conversion Measured::Weight, "2000 kg", "19613.3 N"
|
120
|
+
end
|
121
|
+
|
122
|
+
test ".convert_to from kg to short_ton" do
|
123
|
+
assert_conversion Measured::Weight, "2000 kg", "2.204622621848 short_ton"
|
124
|
+
end
|
125
|
+
|
126
|
+
test ".convert_to from kg to long_ton" do
|
127
|
+
assert_conversion Measured::Weight, "2000 kg", "1.9684130552220003 long_ton"
|
128
|
+
end
|
129
|
+
|
130
|
+
test ".convert_to from kg to t" do
|
131
|
+
assert_exact_conversion Measured::Weight, "2000 kg", "2 t"
|
132
|
+
end
|
133
|
+
|
61
134
|
test ".convert_to from lb to g" do
|
62
135
|
assert_exact_conversion Measured::Weight, "2000 lb", "907184.74 g"
|
63
136
|
end
|
@@ -74,6 +147,26 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
74
147
|
assert_exact_conversion Measured::Weight, "2000 lb", "32000 oz"
|
75
148
|
end
|
76
149
|
|
150
|
+
test ".convert_to from lb to slug" do
|
151
|
+
assert_conversion Measured::Weight, "2000 lb", "62.16190007566859 slug"
|
152
|
+
end
|
153
|
+
|
154
|
+
test ".convert_to from lb to N" do
|
155
|
+
assert_conversion Measured::Weight, "2000 lb", "8896.443230521 N"
|
156
|
+
end
|
157
|
+
|
158
|
+
test ".convert_to from lb to short_ton" do
|
159
|
+
assert_exact_conversion Measured::Weight, "2000 lb", "1 short_ton"
|
160
|
+
end
|
161
|
+
|
162
|
+
test ".convert_to from lb to long_ton" do
|
163
|
+
assert_conversion Measured::Weight, "2000 lb", "0.892857142857088 long_ton"
|
164
|
+
end
|
165
|
+
|
166
|
+
test ".convert_to from lb to t" do
|
167
|
+
assert_conversion Measured::Weight, "2000 lb", "0.90718474 t"
|
168
|
+
end
|
169
|
+
|
77
170
|
test ".convert_to from oz to g" do
|
78
171
|
assert_conversion Measured::Weight, "2000 oz", "56699.04625 g"
|
79
172
|
end
|
@@ -90,4 +183,195 @@ class Measured::WeightTest < ActiveSupport::TestCase
|
|
90
183
|
assert_exact_conversion Measured::Weight, "2000 oz", "2000 oz"
|
91
184
|
end
|
92
185
|
|
186
|
+
test ".convert_to from oz to slug" do
|
187
|
+
assert_conversion Measured::Weight, "2000 oz", "3.885119 slug"
|
188
|
+
end
|
189
|
+
|
190
|
+
test ".convert_to from oz to N" do
|
191
|
+
assert_conversion Measured::Weight, "2000 oz", "556.0277019075625 N"
|
192
|
+
end
|
193
|
+
|
194
|
+
test ".convert_to from oz to short_ton" do
|
195
|
+
assert_conversion Measured::Weight, "2000 oz", "0.06249999999997801 short_ton"
|
196
|
+
end
|
197
|
+
|
198
|
+
test ".convert_to from oz to long_ton" do
|
199
|
+
assert_conversion Measured::Weight, "2000 oz", "0.055803571428568 long_ton"
|
200
|
+
end
|
201
|
+
|
202
|
+
test ".convert_to from oz to t" do
|
203
|
+
assert_conversion Measured::Weight, "2000 oz", "0.05669904625 t"
|
204
|
+
end
|
205
|
+
|
206
|
+
test ".convert_to from slug to g" do
|
207
|
+
assert_conversion Measured::Weight, "2000 slug", "29187806 g"
|
208
|
+
end
|
209
|
+
|
210
|
+
test ".convert_to from slug to kg" do
|
211
|
+
assert_conversion Measured::Weight, "2000 slug", "29187.805999999997 kg"
|
212
|
+
end
|
213
|
+
|
214
|
+
test ".convert_to from slug to lb" do
|
215
|
+
assert_conversion Measured::Weight, "2000 slug", "64348.09738973343 lb"
|
216
|
+
end
|
217
|
+
|
218
|
+
test ".convert_to from slug to oz" do
|
219
|
+
assert_conversion Measured::Weight, "2000 slug", "1029569.5582357347 oz"
|
220
|
+
end
|
221
|
+
|
222
|
+
test ".convert_to from slug to slug" do
|
223
|
+
assert_exact_conversion Measured::Weight, "2000 slug", "2000 slug"
|
224
|
+
end
|
225
|
+
|
226
|
+
test ".convert_to from slug to N" do
|
227
|
+
assert_conversion Measured::Weight, "2000 slug", "286234.59770989994 N"
|
228
|
+
end
|
229
|
+
|
230
|
+
test ".convert_to from slug to short_ton" do
|
231
|
+
assert_conversion Measured::Weight, "2000 slug", "32.17404869485539 short_ton"
|
232
|
+
end
|
233
|
+
|
234
|
+
test ".convert_to from slug to long_ton" do
|
235
|
+
assert_conversion Measured::Weight, "2000 slug", "28.726829191843514 long_ton"
|
236
|
+
end
|
237
|
+
|
238
|
+
test ".convert_to from slug to t" do
|
239
|
+
assert_conversion Measured::Weight, "2000 slug", "29.187806 t"
|
240
|
+
end
|
241
|
+
|
242
|
+
test ".convert_to from short_ton to g" do
|
243
|
+
assert_conversion Measured::Weight, "2000 short_ton", "1814369480 g"
|
244
|
+
end
|
245
|
+
|
246
|
+
test ".convert_to from short_ton to kg" do
|
247
|
+
assert_conversion Measured::Weight, "2000 short_ton", "1814369.48 kg"
|
248
|
+
end
|
249
|
+
|
250
|
+
test ".convert_to from short_ton to lb" do
|
251
|
+
assert_exact_conversion Measured::Weight, "2000 short_ton", "4000000 lb"
|
252
|
+
end
|
253
|
+
|
254
|
+
test ".convert_to from short_ton to oz" do
|
255
|
+
assert_exact_conversion Measured::Weight, "2000 short_ton", "64000000 oz"
|
256
|
+
end
|
257
|
+
|
258
|
+
test ".convert_to from short_ton to slug" do
|
259
|
+
assert_conversion Measured::Weight, "2000 short_ton", "124323.80015133717 slug"
|
260
|
+
end
|
261
|
+
|
262
|
+
test ".convert_to from short_ton to N" do
|
263
|
+
assert_conversion Measured::Weight, "2000 short_ton", "17792886.461041998 N"
|
264
|
+
end
|
265
|
+
|
266
|
+
test ".convert_to from short_ton to short_ton" do
|
267
|
+
assert_exact_conversion Measured::Weight, "2000 short_ton", "2000 short_ton"
|
268
|
+
end
|
269
|
+
|
270
|
+
test ".convert_to from short_ton to long_ton" do
|
271
|
+
assert_conversion Measured::Weight, "2000 short_ton", "1785.714285714176 long_ton"
|
272
|
+
end
|
273
|
+
|
274
|
+
test ".convert_to from short_ton to t" do
|
275
|
+
assert_conversion Measured::Weight, "2000 short_ton", "1814.36948 t"
|
276
|
+
end
|
277
|
+
|
278
|
+
test ".convert_to from long_ton to g" do
|
279
|
+
assert_conversion Measured::Weight, "2000 long_ton", "2032093817.6 g"
|
280
|
+
end
|
281
|
+
|
282
|
+
test ".convert_to from long_ton to kg" do
|
283
|
+
assert_conversion Measured::Weight, "2000 long_ton", "2032093.8176 kg"
|
284
|
+
end
|
285
|
+
|
286
|
+
test ".convert_to from long_ton to lb" do
|
287
|
+
assert_exact_conversion Measured::Weight, "2000 long_ton", "4480000 lb"
|
288
|
+
end
|
289
|
+
|
290
|
+
test ".convert_to from long_ton to oz" do
|
291
|
+
assert_exact_conversion Measured::Weight, "2000 long_ton", "71680000 oz"
|
292
|
+
end
|
293
|
+
|
294
|
+
test ".convert_to from long_ton to slug" do
|
295
|
+
assert_conversion Measured::Weight, "2000 long_ton", "139242.65616949764 slug"
|
296
|
+
end
|
297
|
+
|
298
|
+
test ".convert_to from long_ton to N" do
|
299
|
+
assert_conversion Measured::Weight, "2000 long_ton", "19928032.836367033 N"
|
300
|
+
end
|
301
|
+
|
302
|
+
test ".convert_to from long_ton to short_ton" do
|
303
|
+
assert_exact_conversion Measured::Weight, "2000 long_ton", "2240 short_ton"
|
304
|
+
end
|
305
|
+
|
306
|
+
test ".convert_to from long_ton to long_ton" do
|
307
|
+
assert_conversion Measured::Weight, "2000 long_ton", "2000 long_ton"
|
308
|
+
end
|
309
|
+
|
310
|
+
test ".convert_to from long_ton to t" do
|
311
|
+
assert_conversion Measured::Weight, "2000 long_ton", "2032.0938176 t"
|
312
|
+
end
|
313
|
+
|
314
|
+
test ".convert_to from N to g" do
|
315
|
+
assert_conversion Measured::Weight, "2000 N", "203943.24259558567 g"
|
316
|
+
end
|
317
|
+
|
318
|
+
test ".convert_to from N to kg" do
|
319
|
+
assert_conversion Measured::Weight, "2000 N", "203.9432425955857 kg"
|
320
|
+
end
|
321
|
+
|
322
|
+
test ".convert_to from N to lb" do
|
323
|
+
assert_conversion Measured::Weight, "2000 N", "449.6178861994211 lb"
|
324
|
+
end
|
325
|
+
|
326
|
+
test ".convert_to from N to oz" do
|
327
|
+
assert_conversion Measured::Weight, "2000 N", "7193.886179190737 oz"
|
328
|
+
end
|
329
|
+
|
330
|
+
test ".convert_to from N to slug" do
|
331
|
+
assert_conversion Measured::Weight, "2000 N", "13.974551057080868 slug"
|
332
|
+
end
|
333
|
+
|
334
|
+
test ".convert_to from N to N" do
|
335
|
+
assert_exact_conversion Measured::Weight, "2000 N", "2000 N"
|
336
|
+
end
|
337
|
+
|
338
|
+
test ".convert_to from N to short_ton" do
|
339
|
+
assert_conversion Measured::Weight, "2000 N", "0.2248089430996314 short_ton"
|
340
|
+
end
|
341
|
+
|
342
|
+
test ".convert_to from N to long_ton" do
|
343
|
+
assert_conversion Measured::Weight, "2000 N", "0.20072227062472917 long_ton"
|
344
|
+
end
|
345
|
+
|
346
|
+
test ".convert_to from N to t" do
|
347
|
+
assert_conversion Measured::Weight, "2000 N", "0.2039432425955857 t"
|
348
|
+
end
|
349
|
+
|
350
|
+
test ".convert_to from t to g" do
|
351
|
+
assert_exact_conversion Measured::Weight, "2000 t", "2000000000 g"
|
352
|
+
end
|
353
|
+
|
354
|
+
test ".convert_to from t to kg" do
|
355
|
+
assert_exact_conversion Measured::Weight, "2000 t", "2000000 kg"
|
356
|
+
end
|
357
|
+
|
358
|
+
test ".convert_to from t to lb" do
|
359
|
+
assert_conversion Measured::Weight, "2000 t", "4409245.243697552 lb"
|
360
|
+
end
|
361
|
+
|
362
|
+
test ".convert_to from t to oz" do
|
363
|
+
assert_conversion Measured::Weight, "2000 t", "70547923.89916082 oz"
|
364
|
+
end
|
365
|
+
|
366
|
+
test ".convert_to from t to slug" do
|
367
|
+
assert_conversion Measured::Weight, "2000 t", "137043.5311239221 slug"
|
368
|
+
end
|
369
|
+
|
370
|
+
test ".convert_to from t to short_ton" do
|
371
|
+
assert_conversion Measured::Weight, "2000 t", "2204.622621848 short_ton"
|
372
|
+
end
|
373
|
+
|
374
|
+
test ".convert_to from t to t" do
|
375
|
+
assert_conversion Measured::Weight, "2000 t", "2000 t"
|
376
|
+
end
|
93
377
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: measured
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McPhillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|