measured 1.6.0 → 2.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/README.md +27 -60
- data/lib/measured/arithmetic.rb +14 -15
- data/lib/measured/base.rb +18 -3
- data/lib/measured/case_insensitive_unit.rb +13 -0
- data/lib/measured/case_insensitive_unit_system.rb +11 -0
- data/lib/measured/conversion_table.rb +17 -20
- data/lib/measured/measurable.rb +37 -61
- data/lib/measured/unit.rb +5 -27
- data/lib/measured/unit_system.rb +67 -0
- data/lib/measured/unit_system_builder.rb +38 -0
- data/lib/measured/units/length.rb +7 -25
- data/lib/measured/units/weight.rb +5 -17
- data/lib/measured/version.rb +1 -1
- data/measured.gemspec +1 -1
- data/test/arithmetic_test.rb +22 -59
- data/test/case_insensitive_unit_system_test.rb +98 -0
- data/test/case_insensitive_unit_test.rb +79 -0
- data/test/conversion_table_test.rb +86 -7
- data/test/measurable_test.rb +47 -57
- data/test/support/fake_system.rb +15 -41
- data/test/test_helper.rb +5 -0
- data/test/unit_system_builder_test.rb +58 -0
- data/test/unit_system_test.rb +99 -0
- data/test/unit_test.rb +20 -82
- data/test/units/length_test.rb +13 -6
- data/test/units/weight_test.rb +7 -4
- metadata +18 -12
- data/lib/measured/case_sensitive_measurable.rb +0 -7
- data/lib/measured/conversion.rb +0 -95
- data/test/case_sensitive_measurable_test.rb +0 -227
- data/test/conversion_test.rb +0 -243
@@ -1,26 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
value: "0.01 m"
|
9
|
-
|
10
|
-
conversion.add :mm,
|
11
|
-
aliases: [:millimeter, :millimetre, :millimeters, :millimetres],
|
12
|
-
value: "0.001 m"
|
13
|
-
|
14
|
-
conversion.add :in,
|
15
|
-
aliases: [:inch, :inches],
|
16
|
-
value: "0.0254 m"
|
17
|
-
|
18
|
-
conversion.add :ft,
|
19
|
-
aliases: [:foot, :feet],
|
20
|
-
value: "0.3048 m"
|
21
|
-
|
22
|
-
conversion.add :yd,
|
23
|
-
aliases: [:yard, :yards],
|
24
|
-
value: "0.9144 m"
|
25
|
-
|
1
|
+
Measured::Length = Measured.build do
|
2
|
+
unit :m, aliases: [:meter, :metre, :meters, :metres]
|
3
|
+
unit :cm, value: "1/100 m", aliases: [:centimeter, :centimetre, :centimeters, :centimetres]
|
4
|
+
unit :mm, value: "1/1000 m", aliases: [:millimeter, :millimetre, :millimeters, :millimetres]
|
5
|
+
unit :in, value: "0.0254 m", aliases: [:inch, :inches]
|
6
|
+
unit :ft, value: "12 in", aliases: [:foot, :feet]
|
7
|
+
unit :yd, value: "3 ft", aliases: [:yard, :yards]
|
26
8
|
end
|
@@ -1,18 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
conversion.add :kg,
|
7
|
-
aliases: [:kilogram, :kilograms],
|
8
|
-
value: "1000 g"
|
9
|
-
|
10
|
-
conversion.add :lb,
|
11
|
-
aliases: [:lbs, :pound, :pounds],
|
12
|
-
value: [Rational(45359237,1e8), "kg"]
|
13
|
-
|
14
|
-
conversion.add :oz,
|
15
|
-
aliases: [:ounce, :ounces],
|
16
|
-
value: [Rational(1,16), "lb"]
|
17
|
-
|
1
|
+
Measured::Weight = Measured.build do
|
2
|
+
unit :g, aliases: [:gram, :grams]
|
3
|
+
unit :kg, value: "1000 g", aliases: [:kilogram, :kilograms]
|
4
|
+
unit :lb, value: "0.45359237 kg", aliases: [:lbs, :pound, :pounds]
|
5
|
+
unit :oz, value: "1/16 lb", aliases: [:ounce, :ounces]
|
18
6
|
end
|
data/lib/measured/version.rb
CHANGED
data/measured.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "activesupport", ">= 4.
|
21
|
+
spec.add_runtime_dependency "activesupport", ">= 4.2"
|
22
22
|
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "minitest", "~> 5.5.1"
|
data/test/arithmetic_test.rb
CHANGED
@@ -12,9 +12,9 @@ class Measured::ArithmeticTest < ActiveSupport::TestCase
|
|
12
12
|
assert_equal Magic.new(5, :magic_missile), @three + @two
|
13
13
|
end
|
14
14
|
|
15
|
-
test "#+
|
16
|
-
|
17
|
-
|
15
|
+
test "#+ shouldn't add with a Integer" do
|
16
|
+
assert_raises(TypeError) { @two + 3 }
|
17
|
+
assert_raises(TypeError) { 2 + @three }
|
18
18
|
end
|
19
19
|
|
20
20
|
test "#+ should raise if different unit system" do
|
@@ -42,9 +42,9 @@ class Measured::ArithmeticTest < ActiveSupport::TestCase
|
|
42
42
|
assert_equal Magic.new(1, :magic_missile), @three - @two
|
43
43
|
end
|
44
44
|
|
45
|
-
test "#-
|
46
|
-
|
47
|
-
|
45
|
+
test "#- shouldn't subtract with a Integer" do
|
46
|
+
assert_raises(TypeError) { @two - 3 }
|
47
|
+
assert_raises(TypeError) { 2 - @three }
|
48
48
|
end
|
49
49
|
|
50
50
|
test "#- should raise if different unit system" do
|
@@ -67,67 +67,30 @@ class Measured::ArithmeticTest < ActiveSupport::TestCase
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
test "
|
71
|
-
assert_equal Magic.new(
|
72
|
-
assert_equal Magic.new(6, :magic_missile), @three * @two
|
73
|
-
end
|
74
|
-
|
75
|
-
test "#* should multiply a number to the value" do
|
76
|
-
assert_equal Magic.new(6, :magic_missile), @two * 3
|
77
|
-
assert_equal Magic.new(6, :magic_missile), 2 * @three
|
78
|
-
end
|
79
|
-
|
80
|
-
test "#* should raise if different unit system" do
|
81
|
-
assert_raises TypeError do
|
82
|
-
OtherFakeSystem.new(1, :other_fake_base) * @two
|
83
|
-
end
|
84
|
-
|
85
|
-
assert_raises TypeError do
|
86
|
-
@two * OtherFakeSystem.new(1, :other_fake_base)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
test "#* should raise if multiplying something nonsense" do
|
91
|
-
assert_raises TypeError do
|
92
|
-
@two * "thing"
|
93
|
-
end
|
94
|
-
|
95
|
-
assert_raises TypeError do
|
96
|
-
"thing" * @two
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
test "#/ should divide together same units" do
|
101
|
-
assert_equal Magic.new("0.5", :magic_missile), @two / @four
|
102
|
-
assert_equal Magic.new(2, :magic_missile), @four / @two
|
70
|
+
test "#-@ returns the negative version" do
|
71
|
+
assert_equal Magic.new(-2, :magic_missile), -@two
|
103
72
|
end
|
104
73
|
|
105
|
-
test "
|
106
|
-
assert_equal Magic.new(
|
107
|
-
assert_equal Magic.new(
|
74
|
+
test "#scale should multiply the value by a given scalar" do
|
75
|
+
assert_equal Magic.new(-2, :magic_missile), @two.scale(-1)
|
76
|
+
assert_equal Magic.new(5, :magic_missile), @two.scale(2.5)
|
108
77
|
end
|
109
78
|
|
110
|
-
test "
|
111
|
-
|
112
|
-
|
113
|
-
end
|
79
|
+
test "arithmetic operations favours unit of left" do
|
80
|
+
left = Magic.new(1, :arcane)
|
81
|
+
right = Magic.new(1, :magic_missile)
|
114
82
|
|
115
|
-
|
116
|
-
|
117
|
-
end
|
83
|
+
assert_equal "arcane", (left + right).unit
|
84
|
+
assert_equal "arcane", (left - right).unit
|
118
85
|
end
|
119
86
|
|
120
|
-
test "
|
121
|
-
|
122
|
-
@two / "thing"
|
123
|
-
end
|
124
|
-
|
125
|
-
assert_raises NoMethodError do
|
126
|
-
"thing" / @two
|
127
|
-
end
|
87
|
+
test "#coerce should return other as-is when same class" do
|
88
|
+
assert_equal [@two, @three], @three.coerce(@two)
|
128
89
|
end
|
129
90
|
|
130
|
-
test "
|
131
|
-
|
91
|
+
test "#coerce should raise TypeError when other cannot be coerced" do
|
92
|
+
assert_raises(TypeError) { @two.coerce(2) }
|
93
|
+
assert_raises(TypeError) { @three.coerce(5.2) }
|
94
|
+
assert_raises(TypeError) { @four.coerce(Object.new) }
|
132
95
|
end
|
133
96
|
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Measured::CaseInsensitiveUnitSystemTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
@conversion = Measured::CaseInsensitiveUnitSystem.new([
|
6
|
+
Measured::CaseInsensitiveUnit.new(:m),
|
7
|
+
Measured::CaseInsensitiveUnit.new(:in, aliases: [:inch], value: "0.0254 m"),
|
8
|
+
Measured::CaseInsensitiveUnit.new(:ft, aliases: [:feet, :foot], value: "0.3048 m"),
|
9
|
+
])
|
10
|
+
end
|
11
|
+
|
12
|
+
test "#unit_names_with_aliases lists all allowed unit names" do
|
13
|
+
assert_equal ["feet", "foot", "ft", "in", "inch", "m"], @conversion.unit_names_with_aliases
|
14
|
+
end
|
15
|
+
|
16
|
+
test "#unit_names lists all base unit names without aliases" do
|
17
|
+
assert_equal ["ft", "in", "m"], @conversion.unit_names
|
18
|
+
end
|
19
|
+
|
20
|
+
test "#unit? checks if the unit is part of the units but not aliases" do
|
21
|
+
assert @conversion.unit?(:in)
|
22
|
+
assert @conversion.unit?("m")
|
23
|
+
assert @conversion.unit?("M")
|
24
|
+
refute @conversion.unit?("inch")
|
25
|
+
refute @conversion.unit?(:yard)
|
26
|
+
end
|
27
|
+
|
28
|
+
test "#unit? with blank and nil arguments" do
|
29
|
+
refute @conversion.unit?("")
|
30
|
+
refute @conversion.unit?(nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
test "#unit_or_alias? checks if the unit is part of the units or aliases" do
|
34
|
+
assert @conversion.unit_or_alias?(:inch)
|
35
|
+
assert @conversion.unit_or_alias?("m")
|
36
|
+
assert @conversion.unit_or_alias?(:IN)
|
37
|
+
assert @conversion.unit_or_alias?("in")
|
38
|
+
refute @conversion.unit_or_alias?(:yard)
|
39
|
+
end
|
40
|
+
|
41
|
+
test "#unit_or_alias? with blank and nil arguments" do
|
42
|
+
refute @conversion.unit_or_alias?("")
|
43
|
+
refute @conversion.unit_or_alias?(nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
test "#to_unit_name converts a unit name to its base unit" do
|
47
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name("fire")
|
48
|
+
end
|
49
|
+
|
50
|
+
test "#to_unit_name does not care about string or symbol" do
|
51
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name(:fire)
|
52
|
+
end
|
53
|
+
|
54
|
+
test "#to_unit_name passes through if already base unit name" do
|
55
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name("fireball")
|
56
|
+
end
|
57
|
+
|
58
|
+
test "#to_unit_name returns nil if not found" do
|
59
|
+
assert_nil Magic.unit_system.to_unit_name("thunder")
|
60
|
+
end
|
61
|
+
|
62
|
+
test "#to_unit_name! converts a unit name to its base unit" do
|
63
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name!("fire")
|
64
|
+
end
|
65
|
+
|
66
|
+
test "#to_unit_name! does not care about string or symbol" do
|
67
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name!(:fire)
|
68
|
+
end
|
69
|
+
|
70
|
+
test "#to_unit_name! passes through if already base unit name" do
|
71
|
+
assert_equal "fireball", Magic.unit_system.to_unit_name!("fireball")
|
72
|
+
end
|
73
|
+
|
74
|
+
test "#to_unit_name! raises if not found" do
|
75
|
+
assert_raises_with_message(Measured::UnitError, "Unit 'thunder' does not exist") do
|
76
|
+
Magic.unit_system.to_unit_name!("thunder")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
test "#convert raises if either unit is not found" do
|
81
|
+
assert_raises Measured::UnitError do
|
82
|
+
Magic.unit_system.convert(1, from: "fire", to: "doesnt_exist")
|
83
|
+
end
|
84
|
+
|
85
|
+
assert_raises Measured::UnitError do
|
86
|
+
Magic.unit_system.convert(1, from: "doesnt_exist", to: "fire")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
test "#convert converts between two known units" do
|
91
|
+
assert_equal BigDecimal("3"), @conversion.convert(BigDecimal("36"), from: "in", to: "ft")
|
92
|
+
assert_equal BigDecimal("18"), @conversion.convert(BigDecimal("1.5"), from: "ft", to: "in")
|
93
|
+
end
|
94
|
+
|
95
|
+
test "#convert handles the same unit" do
|
96
|
+
assert_equal BigDecimal("2"), @conversion.convert(BigDecimal("2"), from: "in", to: "in")
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
@unit = Measured::CaseInsensitiveUnit.new(:Pie, value: "10 Cake")
|
6
|
+
@unit_with_aliases = Measured::CaseInsensitiveUnit.new(:Pie, aliases: ["Cake", "Tart"])
|
7
|
+
end
|
8
|
+
|
9
|
+
test "#initialize converts the name to a downcased string" do
|
10
|
+
assert_equal "pie", @unit.name
|
11
|
+
end
|
12
|
+
|
13
|
+
test "#initialize converts aliases to strings and makes a list of sorted, downcased names" do
|
14
|
+
assert_equal %w(cake pie sweets), Measured::CaseInsensitiveUnit.new(:pie, aliases: ["Cake", :Sweets]).names
|
15
|
+
end
|
16
|
+
|
17
|
+
test "#initialize parses out the unit and the number part" do
|
18
|
+
assert_equal BigDecimal(10), @unit.conversion_amount
|
19
|
+
assert_equal "Cake", @unit.conversion_unit
|
20
|
+
|
21
|
+
unit = Measured::CaseInsensitiveUnit.new(:pie, value: "5.5 sweets")
|
22
|
+
assert_equal BigDecimal("5.5"), unit.conversion_amount
|
23
|
+
assert_equal "sweets", unit.conversion_unit
|
24
|
+
end
|
25
|
+
|
26
|
+
test "#initialize raises if the format of the value is incorrect" do
|
27
|
+
assert_raises Measured::UnitError do
|
28
|
+
Measured::CaseInsensitiveUnit.new(:pie, value: "hello")
|
29
|
+
end
|
30
|
+
|
31
|
+
assert_raises Measured::UnitError do
|
32
|
+
Measured::CaseInsensitiveUnit.new(:pie, value: "pie is delicious")
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_raises Measured::UnitError do
|
36
|
+
Measured::CaseInsensitiveUnit.new(:pie, value: "123456")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
test "#to_s returns an expected string" do
|
41
|
+
assert_equal "pie", Measured::CaseInsensitiveUnit.new(:pie).to_s
|
42
|
+
assert_equal "pie (1/2 sweet)", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
test "#inspect returns an expected string" do
|
46
|
+
assert_equal "#<Measured::Unit: pie (pie) >", Measured::CaseInsensitiveUnit.new(:pie).inspect
|
47
|
+
assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).inspect
|
48
|
+
end
|
49
|
+
|
50
|
+
test "includes Comparable mixin" do
|
51
|
+
assert Measured::Unit.ancestors.include?(Comparable)
|
52
|
+
end
|
53
|
+
|
54
|
+
test "#<=> compares non-Unit classes against name" do
|
55
|
+
assert_equal 1, @unit <=> "pap"
|
56
|
+
assert_equal -1, @unit <=> "pop"
|
57
|
+
end
|
58
|
+
|
59
|
+
test "#<=> is 0 for Unit instances that should be equivalent" do
|
60
|
+
assert_equal 0, @unit <=> Measured::CaseInsensitiveUnit.new(:Pie, value: "10 cake")
|
61
|
+
assert_equal 0, @unit <=> Measured::CaseInsensitiveUnit.new("Pie", value: "10 cake")
|
62
|
+
assert_equal 0, @unit <=> Measured::CaseInsensitiveUnit.new("Pie", value: [10, :cake])
|
63
|
+
end
|
64
|
+
|
65
|
+
test "#<=> is 1 for " do
|
66
|
+
assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new(:pies, value: "10 cake")
|
67
|
+
assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new("pie", aliases: ["pies"], value: "10 cake")
|
68
|
+
assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new(:pie, value: [11, :cake])
|
69
|
+
end
|
70
|
+
|
71
|
+
test "#inverse_conversion_amount returns 1/amount for BigDecimal" do
|
72
|
+
assert_equal BigDecimal(1) / 10, @unit.inverse_conversion_amount
|
73
|
+
end
|
74
|
+
|
75
|
+
test "#inverse_conversion_amount swaps the numerator and denominator for Rational" do
|
76
|
+
unit = Measured::CaseInsensitiveUnit.new(:pie, value: [Rational(3, 7), "cake"])
|
77
|
+
assert_equal Rational(7, 3), unit.inverse_conversion_amount
|
78
|
+
end
|
79
|
+
end
|
@@ -1,19 +1,98 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class Measured::ConversionTableTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
test ".build should return a hash for the simple case" do
|
5
|
+
expected = {
|
6
|
+
"test" => {"test" => BigDecimal("1")}
|
7
|
+
}
|
8
|
+
|
9
|
+
assert_equal expected, Measured::ConversionTable.build([Measured::Unit.new(:test)])
|
6
10
|
end
|
7
11
|
|
8
|
-
test "
|
9
|
-
Measured::ConversionTable.
|
12
|
+
test ".build returns expected nested hashes with BigDecimal conversion factors in a tiny data set" do
|
13
|
+
conversion_table = Measured::ConversionTable.build([
|
14
|
+
Measured::Unit.new(:m),
|
15
|
+
Measured::Unit.new(:cm, value: "0.01 m"),
|
16
|
+
])
|
17
|
+
|
18
|
+
expected = {
|
19
|
+
"m" => {
|
20
|
+
"m" => BigDecimal("1"),
|
21
|
+
"cm" => BigDecimal("100"),
|
22
|
+
},
|
23
|
+
"cm" => {
|
24
|
+
"m" => BigDecimal("0.01"),
|
25
|
+
"cm" => BigDecimal("1"),
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
assert_equal expected, conversion_table
|
10
30
|
end
|
11
31
|
|
12
|
-
test "
|
32
|
+
test ".build returns expected nested hashes with BigDecimal conversion factors" do
|
33
|
+
conversion_table = Measured::ConversionTable.build([
|
34
|
+
Measured::Unit.new(:m),
|
35
|
+
Measured::Unit.new(:cm, value: "0.01 m"),
|
36
|
+
Measured::Unit.new(:mm, value: "0.001 m"),
|
37
|
+
])
|
38
|
+
|
13
39
|
expected = {
|
14
|
-
"
|
40
|
+
"m" => {
|
41
|
+
"m" => BigDecimal("1"),
|
42
|
+
"cm" => BigDecimal("100"),
|
43
|
+
"mm" => BigDecimal("1000"),
|
44
|
+
},
|
45
|
+
"cm" => {
|
46
|
+
"m" => BigDecimal("0.01"),
|
47
|
+
"cm" => BigDecimal("1"),
|
48
|
+
"mm" => BigDecimal("10"),
|
49
|
+
},
|
50
|
+
"mm" => {
|
51
|
+
"m" => BigDecimal("0.001"),
|
52
|
+
"cm" => BigDecimal("0.1"),
|
53
|
+
"mm" => BigDecimal("1"),
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
assert_equal expected, conversion_table
|
58
|
+
end
|
59
|
+
|
60
|
+
test ".build returns expected nested hashes with BigDecimal conversion factors in an indrect path" do
|
61
|
+
conversion_table = Measured::ConversionTable.build([
|
62
|
+
Measured::Unit.new(:mm),
|
63
|
+
Measured::Unit.new(:cm, value: "10 mm"),
|
64
|
+
Measured::Unit.new(:dm, value: "10 cm"),
|
65
|
+
Measured::Unit.new(:m, value: "10 dm"),
|
66
|
+
])
|
67
|
+
|
68
|
+
expected = {
|
69
|
+
"m" => {
|
70
|
+
"m" => BigDecimal("1"),
|
71
|
+
"dm" => BigDecimal("10"),
|
72
|
+
"cm" => BigDecimal("100"),
|
73
|
+
"mm" => BigDecimal("1000"),
|
74
|
+
},
|
75
|
+
"cm" => {
|
76
|
+
"m" => BigDecimal("0.01"),
|
77
|
+
"dm" => BigDecimal("0.1"),
|
78
|
+
"cm" => BigDecimal("1"),
|
79
|
+
"mm" => BigDecimal("10"),
|
80
|
+
},
|
81
|
+
"dm" => {
|
82
|
+
"m" => BigDecimal("0.1"),
|
83
|
+
"cm" => BigDecimal("10"),
|
84
|
+
"dm" => BigDecimal("1"),
|
85
|
+
"mm" => BigDecimal("100"),
|
86
|
+
},
|
87
|
+
"mm" => {
|
88
|
+
"m" => BigDecimal("0.001"),
|
89
|
+
"dm" => BigDecimal("0.01"),
|
90
|
+
"cm" => BigDecimal("0.1"),
|
91
|
+
"mm" => BigDecimal("1"),
|
92
|
+
}
|
15
93
|
}
|
16
94
|
|
17
|
-
assert_equal expected,
|
95
|
+
assert_equal expected, conversion_table
|
18
96
|
end
|
97
|
+
|
19
98
|
end
|
data/test/measurable_test.rb
CHANGED
@@ -50,14 +50,14 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
|
|
50
50
|
exception = assert_raises(Measured::UnitError) do
|
51
51
|
Magic.new(nil, :fire)
|
52
52
|
end
|
53
|
-
assert_equal "Unit value cannot be
|
53
|
+
assert_equal "Unit value cannot be blank", exception.message
|
54
54
|
end
|
55
55
|
|
56
56
|
test "#initialize raises an expected error when initializing with nil unit" do
|
57
57
|
exception = assert_raises(Measured::UnitError) do
|
58
58
|
Magic.new(1, nil)
|
59
59
|
end
|
60
|
-
assert_equal "Unit
|
60
|
+
assert_equal "Unit '' does not exist", exception.message
|
61
61
|
end
|
62
62
|
|
63
63
|
test "#initialize raises an expected error when initializing with empty string value" do
|
@@ -71,7 +71,7 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
|
|
71
71
|
exception = assert_raises(Measured::UnitError) do
|
72
72
|
Magic.new(1, "")
|
73
73
|
end
|
74
|
-
assert_equal "Unit
|
74
|
+
assert_equal "Unit '' does not exist", exception.message
|
75
75
|
end
|
76
76
|
|
77
77
|
test "#unit allows you to read the unit string" do
|
@@ -83,34 +83,36 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
|
|
83
83
|
end
|
84
84
|
|
85
85
|
test ".conversion is set and cached" do
|
86
|
-
conversion =
|
86
|
+
conversion = CaseSensitiveMagic.unit_system
|
87
87
|
|
88
|
-
assert_instance_of Measured::
|
89
|
-
assert_equal conversion,
|
88
|
+
assert_instance_of Measured::UnitSystem, conversion
|
89
|
+
assert_equal conversion.__id__, CaseSensitiveMagic.unit_system.__id__
|
90
90
|
end
|
91
91
|
|
92
|
-
test ".
|
93
|
-
assert_equal
|
92
|
+
test ".unit_names returns just the base unit names" do
|
93
|
+
assert_equal %w(arcane fireball ice magic_missile ultima), Magic.unit_names
|
94
94
|
end
|
95
95
|
|
96
|
-
test ".
|
97
|
-
assert_equal
|
96
|
+
test ".unit_names_with_aliases returns all units" do
|
97
|
+
assert_equal(
|
98
|
+
%w(arcane fire fireball fireballs ice magic_missile magic_missiles ultima),
|
99
|
+
Magic.unit_names_with_aliases
|
100
|
+
)
|
98
101
|
end
|
99
102
|
|
100
|
-
test ".
|
101
|
-
assert Magic.
|
102
|
-
assert Magic.
|
103
|
-
assert Magic.
|
104
|
-
refute Magic.
|
103
|
+
test ".unit_or_alias? looks at the list of units and aliases" do
|
104
|
+
assert Magic.unit_or_alias?("fire")
|
105
|
+
assert Magic.unit_or_alias?("fireball")
|
106
|
+
assert Magic.unit_or_alias?(:ice)
|
107
|
+
refute Magic.unit_or_alias?("junk")
|
105
108
|
end
|
106
109
|
|
107
110
|
test ".name looks at the class name" do
|
108
111
|
module Example
|
109
|
-
|
112
|
+
VeryComplexThing = Measured.build { unit :foo }
|
110
113
|
end
|
111
114
|
|
112
115
|
assert_equal "magic", Magic.name
|
113
|
-
assert_equal "measurable", Measured::Measurable.name
|
114
116
|
assert_equal "very complex thing", Example::VeryComplexThing.name
|
115
117
|
end
|
116
118
|
|
@@ -136,63 +138,51 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
|
|
136
138
|
assert_equal converted.object_id, @magic.object_id
|
137
139
|
end
|
138
140
|
|
139
|
-
test "#convert_to! replaces the existing object with a new version in the new unit" do
|
140
|
-
converted = @magic.convert_to!(:arcane)
|
141
|
-
|
142
|
-
assert_equal converted, @magic
|
143
|
-
assert_equal BigDecimal(1), @magic.value
|
144
|
-
assert_equal "arcane", @magic.unit
|
145
|
-
assert_equal BigDecimal(1), converted.value
|
146
|
-
assert_equal "arcane", converted.unit
|
147
|
-
end
|
148
|
-
|
149
141
|
test "#to_s outputs the number and the unit" do
|
150
142
|
assert_equal "10 fireball", Magic.new(10, :fire).to_s
|
151
143
|
assert_equal "1.234 magic_missile", Magic.new("1.234", :magic_missile).to_s
|
152
144
|
end
|
153
145
|
|
154
146
|
test "#inspect shows the number and the unit" do
|
155
|
-
assert_equal "#<Magic: 10
|
147
|
+
assert_equal "#<Magic: 10 fireball>", Magic.new(10, :fire).inspect
|
156
148
|
assert_equal "#<Magic: 1.234 magic_missile>", Magic.new(1.234, :magic_missile).inspect
|
157
149
|
end
|
158
150
|
|
159
|
-
test "#zero?
|
160
|
-
|
161
|
-
|
151
|
+
test "#zero? always returns false" do
|
152
|
+
refute_predicate Magic.new(0, :fire), :zero?
|
153
|
+
refute_predicate Magic.new(0.0, :fire), :zero?
|
154
|
+
refute_predicate Magic.new("0.0", :fire), :zero?
|
162
155
|
end
|
163
156
|
|
164
157
|
test "#<=> compares regardless of the unit" do
|
165
|
-
assert_equal -1, @magic <=> Magic.new(
|
158
|
+
assert_equal -1, @magic <=> Magic.new(20, :fire)
|
166
159
|
assert_equal 1, @magic <=> Magic.new(9, :magic_missile)
|
167
|
-
assert_equal 0, @magic <=> Magic.new(
|
160
|
+
assert_equal 0, @magic <=> Magic.new(Rational(30, 2), :fire)
|
168
161
|
assert_equal -1, @magic <=> Magic.new(11, :magic_missile)
|
169
162
|
end
|
170
163
|
|
171
|
-
test "#<=>
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
assert_equal -1, Magic.new(-1, :magic_missile) <=> 0
|
164
|
+
test "#<=> doesn't compare against zero" do
|
165
|
+
assert_nil @magic <=> 0
|
166
|
+
assert_nil @magic <=> BigDecimal.new(0)
|
167
|
+
assert_nil @magic <=> 0.00
|
176
168
|
end
|
177
169
|
|
178
170
|
test "#== should be the same if the classes, unit, and amount match" do
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
171
|
+
assert_equal @magic, @magic
|
172
|
+
assert_equal Magic.new(10, :magic_missile), Magic.new("10", "magic_missile")
|
173
|
+
assert_equal Magic.new(1, :arcane), Magic.new(10, :magic_missile)
|
174
|
+
refute_equal Magic.new(1, :arcane), Magic.new(10.1, :magic_missile)
|
183
175
|
end
|
184
176
|
|
185
177
|
test "#== should be the same if the classes and amount match but the unit does not so they convert" do
|
186
|
-
|
178
|
+
assert_equal Magic.new(2, :magic_missile), Magic.new("1", "ice")
|
187
179
|
end
|
188
180
|
|
189
|
-
test "#==
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
refute @magic == 0
|
195
|
-
refute @magic == BigDecimal.new(0)
|
181
|
+
test "#== doesn't compare against zero" do
|
182
|
+
arcane_zero = Magic.new(0, :arcane)
|
183
|
+
refute_equal arcane_zero, 0
|
184
|
+
refute_equal arcane_zero, BigDecimal.new(0)
|
185
|
+
refute_equal arcane_zero, 0.0
|
196
186
|
end
|
197
187
|
|
198
188
|
test "#> and #< should compare measurements" do
|
@@ -205,13 +195,13 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
|
|
205
195
|
refute Magic.new(10, :magic_missile) > Magic.new(100, :ice)
|
206
196
|
end
|
207
197
|
|
208
|
-
test "#> and #< should compare against zero" do
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
198
|
+
test "#> and #< should not compare against zero" do
|
199
|
+
assert_raises(ArgumentError) { @magic > 0 }
|
200
|
+
assert_raises(ArgumentError) { @magic > BigDecimal.new(0) }
|
201
|
+
assert_raises(ArgumentError) { @magic > 0.00 }
|
202
|
+
assert_raises(ArgumentError) { @magic < 0 }
|
203
|
+
assert_raises(ArgumentError) { @magic < BigDecimal.new(0) }
|
204
|
+
assert_raises(ArgumentError) { @magic < 0.00 }
|
215
205
|
end
|
216
206
|
|
217
207
|
test "#eql? should be the same if the classes and amount match, and unit is converted" do
|