measured 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,118 @@
1
+ require "test_helper"
2
+
3
+ class Measured::MeasurableTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ @magic = Magic.new(10, :magic_missile)
7
+ end
8
+
9
+ test "#initialize requires two params, the amount and the unit" do
10
+ assert_nothing_raised do
11
+ Magic.new(1, "fireball")
12
+ end
13
+
14
+ assert_raises ArgumentError do
15
+ Magic.new(1)
16
+ end
17
+ end
18
+
19
+ test "#initialize converts unit to string from symbol" do
20
+ magic = Magic.new(1, :arcane)
21
+ assert_equal "arcane", magic.unit
22
+ end
23
+
24
+ test "#initialize raises if it is an unknown unit" do
25
+ assert_raises Measured::UnitError do
26
+ Magic.new(1, "slash")
27
+ end
28
+ end
29
+
30
+ test "#initialize converts numbers and strings into BigDecimal" do
31
+ assert_equal BigDecimal(1), Magic.new(1, :arcane).value
32
+ assert_equal BigDecimal("2.3"), Magic.new("2.3", :arcane).value
33
+ assert_equal BigDecimal("5"), Magic.new("5", :arcane).value
34
+ end
35
+
36
+ test "#initialize converts to the base unit name" do
37
+ assert_equal "fireball", Magic.new(1, :fire).unit
38
+ end
39
+
40
+ test "#unit allows you to read the unit string" do
41
+ assert_equal "magic_missile", @magic.unit
42
+ end
43
+
44
+ test "#value allows you to read the numeric value" do
45
+ assert_equal BigDecimal(10), @magic.value
46
+ end
47
+
48
+ test ".conversion is set and cached" do
49
+ conversion = Magic.conversion
50
+
51
+ assert_instance_of Measured::Conversion, conversion
52
+ assert_equal conversion, Magic.conversion
53
+ end
54
+
55
+ test ".units returns just the base units" do
56
+ assert_equal ["arcane", "fireball", "ice", "magic_missile", "ultima"], Magic.units
57
+ end
58
+
59
+ test ".units_with_aliases returns all units" do
60
+ assert_equal ["arcane", "fire", "fireball", "fireballs", "ice", "magic_missile", "magic_missiles", "ultima"], Magic.units_with_aliases
61
+ end
62
+
63
+ test "#convert_to raises on an invalid unit" do
64
+ assert_raises Measured::UnitError do
65
+ @magic.convert_to(:punch)
66
+ end
67
+ end
68
+
69
+ test "#convert_to returns a new object of the same type in the new unit" do
70
+ converted = @magic.convert_to(:arcane)
71
+
72
+ refute_equal converted, @magic
73
+ assert_equal BigDecimal(10), @magic.value
74
+ assert_equal "magic_missile", @magic.unit
75
+ assert_equal BigDecimal(1), converted.value
76
+ assert_equal "arcane", converted.unit
77
+ end
78
+
79
+ test "#convert_to! replaces the existing object with a new version in the new unit" do
80
+ converted = @magic.convert_to!(:arcane)
81
+
82
+ assert_equal converted, @magic
83
+ assert_equal BigDecimal(1), @magic.value
84
+ assert_equal "arcane", @magic.unit
85
+ assert_equal BigDecimal(1), converted.value
86
+ assert_equal "arcane", converted.unit
87
+ end
88
+
89
+ test "#to_s outputs the number and the unit" do
90
+ assert_equal "10 fireball", Magic.new(10, :fire).to_s
91
+ assert_equal "1.234 magic_missile", Magic.new("1.234", :magic_missile).to_s
92
+ end
93
+
94
+ test "#inspect shows the number and the unit" do
95
+ assert_equal "#<Magic: 0.1E2 fireball>", Magic.new(10, :fire).inspect
96
+ assert_equal "#<Magic: 0.1234E1 magic_missile>", Magic.new("1.234", :magic_missile).inspect
97
+ end
98
+
99
+ test "#<=> compares only if the class and unit are the same" do
100
+ assert_nil @magic <=> Magic.new(10, :fire)
101
+ assert_equal 1, @magic <=> Magic.new(9, :magic_missile)
102
+ assert_equal 0, @magic <=> Magic.new(10, :magic_missile)
103
+ assert_equal -1, @magic <=> Magic.new(11, :magic_missile)
104
+ end
105
+
106
+ test "#== should be the same if the classes, unit, and amount match" do
107
+ assert @magic == @magic
108
+ assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
109
+ refute Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
110
+ end
111
+
112
+ test "#eql? should be the same if the classes, unit, and amount match" do
113
+ assert @magic == @magic
114
+ assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
115
+ refute Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
116
+ end
117
+
118
+ end
@@ -0,0 +1,26 @@
1
+ class Magic < Measured::Measurable
2
+
3
+ conversion.set_base :magic_missile,
4
+ aliases: [:magic_missiles]
5
+
6
+ conversion.add :fireball,
7
+ aliases: [:fire, :fireballs],
8
+ value: "2/3 magic_missile"
9
+
10
+ conversion.add :ice,
11
+ value: "2 magic_missile"
12
+
13
+ conversion.add :arcane,
14
+ value: "10 magic_missile"
15
+
16
+ conversion.add :ultima,
17
+ value: "10 arcane"
18
+
19
+ end
20
+
21
+ class OtherFakeSystem < Measured::Measurable
22
+
23
+ conversion.set_base :other_fake_base
24
+ conversion.add :other_fake1, value: "2 other_fake_base"
25
+
26
+ end
@@ -0,0 +1,20 @@
1
+ require "measured"
2
+ require "minitest/autorun"
3
+ require "mocha/setup"
4
+ require "pry"
5
+
6
+ ActiveSupport.test_order = :random
7
+
8
+ require "support/fake_system"
9
+
10
+ class ActiveSupport::TestCase
11
+
12
+ protected
13
+
14
+ def assert_conversion(klass, from, to)
15
+ from_amount, from_unit = from.split(" ")
16
+ to_amount, to_unit = to.split(" ")
17
+
18
+ assert_equal BigDecimal(to_amount), klass.new(from_amount, from_unit).convert_to(to_unit).value
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ require "test_helper"
2
+
3
+ class Measured::UnitErrorTest < ActiveSupport::TestCase
4
+ test "error class exists and is a subclass of StandardError" do
5
+ assert Measured::UnitError.ancestors.include?(StandardError)
6
+ end
7
+ end
data/test/unit_test.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "test_helper"
2
+
3
+ class Measured::UnitTest < ActiveSupport::TestCase
4
+ setup do
5
+ @unit = Measured::Unit.new(:pie, value: "10 cake")
6
+ end
7
+
8
+ test "#initialize converts the name to a string" do
9
+ assert_equal "pie", @unit.name
10
+ end
11
+
12
+ test "#initialize converts aliases to strings and makes a list of names which includes the base" do
13
+ assert_equal ["cake", "pie", "sweets"], Measured::Unit.new(:pie, aliases: ["cake", :sweets]).names
14
+ end
15
+
16
+ test "#initialize parses out the unit and the number part" do
17
+ assert_equal BigDecimal(10), @unit.conversion_amount
18
+ assert_equal "cake", @unit.conversion_unit
19
+
20
+ unit = Measured::Unit.new(:pie, value: "5.5 sweets")
21
+ assert_equal BigDecimal("5.5"), unit.conversion_amount
22
+ assert_equal "sweets", unit.conversion_unit
23
+ end
24
+
25
+ test "#initialize raises if the format of the value is incorrect" do
26
+ assert_raises Measured::UnitError do
27
+ Measured::Unit.new(:pie, value: "hello")
28
+ end
29
+
30
+ assert_raises Measured::UnitError do
31
+ Measured::Unit.new(:pie, value: "pie is delicious")
32
+ end
33
+
34
+ assert_raises Measured::UnitError do
35
+ Measured::Unit.new(:pie, value: "123456")
36
+ end
37
+ end
38
+
39
+ test "#to_s" do
40
+ assert_equal "pie", Measured::Unit.new(:pie).to_s
41
+ assert_equal "pie (1/2 sweet)", Measured::Unit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).to_s
42
+ end
43
+
44
+ test "#inspect returns an expected string" do
45
+ assert_equal "#<Measured::Unit: pie (pie) >", Measured::Unit.new(:pie).inspect
46
+ assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::Unit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).inspect
47
+ end
48
+
49
+ test "is comparable" do
50
+ assert Measured::Unit.ancestors.include?(Comparable)
51
+ end
52
+
53
+ test "#<=> delegates down to name for non Unit comparisons" do
54
+ assert_equal 1, @unit <=> "anything"
55
+ end
56
+
57
+ test "#<=> is equal for same values" do
58
+ assert_equal 0, @unit <=> Measured::Unit.new(:pie, value: "10 cake")
59
+ assert_equal 0, @unit <=> Measured::Unit.new("pie", value: "10 cake")
60
+ assert_equal 0, @unit <=> Measured::Unit.new("pie", value: [10, :cake])
61
+ end
62
+
63
+ test "#<=> is slightly different" do
64
+ assert_equal 1, @unit <=> Measured::Unit.new(:pies, value: "10 cake")
65
+ assert_equal 1, @unit <=> Measured::Unit.new("pie", aliases: ["pies"], value: "10 cake")
66
+ assert_equal 1, @unit <=> Measured::Unit.new(:pie, value: [11, :cake])
67
+ end
68
+
69
+ test "#inverse_conversion_amount returns 1/amount for BigDecimal" do
70
+ assert_equal BigDecimal(1)/BigDecimal(10), @unit.inverse_conversion_amount
71
+ end
72
+
73
+ test "#inverse_conversion_amount swaps the numerator and denominator for Rational" do
74
+ unit = Measured::Unit.new(:pie, value: [Rational(3, 7), "cake"])
75
+ assert_equal Rational(7, 3), unit.inverse_conversion_amount
76
+ end
77
+ end
@@ -0,0 +1,160 @@
1
+ require "test_helper"
2
+
3
+ class Measured::LengthTest < ActiveSupport::TestCase
4
+ test ".units_with_aliases should be the expected list of valid units" do
5
+ assert_equal ["centimeter", "centimeters", "centimetre", "centimetres", "cm", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "millimeter", "millimeters", "millimetre", "millimetres", "mm", "yard", "yards", "yd"], Measured::Length.units_with_aliases
6
+ end
7
+
8
+ test ".units should be the list of base units" do
9
+ assert_equal ["cm", "ft", "in", "m", "mm", "yd"], Measured::Length.units
10
+ end
11
+
12
+ test ".convert_to from cm to cm" do
13
+ assert_conversion Measured::Length, "2000 cm", "2000 cm"
14
+ end
15
+
16
+ test ".convert_to from cm to ft" do
17
+ assert_conversion Measured::Length, "2000 cm", "0.656167979E2 ft"
18
+ end
19
+
20
+ test ".convert_to from cm to in" do
21
+ assert_conversion Measured::Length, "2000 cm", "0.7874015748E3 in"
22
+ end
23
+
24
+ test ".convert_to from cm to m" do
25
+ assert_conversion Measured::Length, "2000 cm", "20 m"
26
+ end
27
+
28
+ test ".convert_to from cm to mm" do
29
+ assert_conversion Measured::Length, "2000 cm", "20000 mm"
30
+ end
31
+
32
+ test ".convert_to from cm to yd" do
33
+ assert_conversion Measured::Length, "2000 cm", "0.2187226596E2 yd"
34
+ end
35
+
36
+ test ".convert_to from ft to cm" do
37
+ assert_conversion Measured::Length, "2000 ft", "60960 cm"
38
+ end
39
+
40
+ test ".convert_to from ft to ft" do
41
+ assert_conversion Measured::Length, "2000 ft", "2000 ft"
42
+ end
43
+
44
+ test ".convert_to from ft to in" do
45
+ skip "returns 0.23999999999904E5"
46
+ assert_conversion Measured::Length, "2000 ft", "24000 in"
47
+ end
48
+
49
+ test ".convert_to from ft to m" do
50
+ assert_conversion Measured::Length, "2000 ft", "609.6 m"
51
+ end
52
+
53
+ test ".convert_to from ft to mm" do
54
+ assert_conversion Measured::Length, "2000 ft", "609600 mm"
55
+ end
56
+
57
+ test ".convert_to from ft to yd" do
58
+ skip "returns 0.6666666664608E3"
59
+ assert_conversion Measured::Length, "2000 ft", "0.666666667E3 yd"
60
+ end
61
+
62
+ test ".convert_to from in to cm" do
63
+ assert_conversion Measured::Length, "2000 in", "5080 cm"
64
+ end
65
+
66
+ test ".convert_to from in to ft" do
67
+ assert_conversion Measured::Length, "2000 in", "0.166666666666E3 ft"
68
+ end
69
+
70
+ test ".convert_to from in to in" do
71
+ assert_conversion Measured::Length, "2000 in", "2000 in"
72
+ end
73
+
74
+ test ".convert_to from in to m" do
75
+ assert_conversion Measured::Length, "2000 in", "50.8 m"
76
+ end
77
+
78
+ test ".convert_to from in to mm" do
79
+ assert_conversion Measured::Length, "2000 in", "50800 mm"
80
+ end
81
+
82
+ test ".convert_to from in to yd" do
83
+ assert_conversion Measured::Length, "2000 in", "0.555555555384E2 yd"
84
+ end
85
+
86
+ test ".convert_to from m to cm" do
87
+ assert_conversion Measured::Length, "2000 m", "200000 cm"
88
+ end
89
+
90
+ test ".convert_to from m to ft" do
91
+ assert_conversion Measured::Length, "2000 m", "0.656167979E4 ft"
92
+ end
93
+
94
+ test ".convert_to from m to in" do
95
+ assert_conversion Measured::Length, "2000 m", "0.7874015748E5 in"
96
+ end
97
+
98
+ test ".convert_to from m to m" do
99
+ assert_conversion Measured::Length, "2000 m", "2000 m"
100
+ end
101
+
102
+ test ".convert_to from m to mm" do
103
+ assert_conversion Measured::Length, "2000 m", "2000000 mm"
104
+ end
105
+
106
+ test ".convert_to from m to yd" do
107
+ assert_conversion Measured::Length, "2000 m", "0.2187226596E4 yd"
108
+ end
109
+
110
+ test ".convert_to from mm to cm" do
111
+ assert_conversion Measured::Length, "2000 mm", "200 cm"
112
+ end
113
+
114
+ test ".convert_to from mm to ft" do
115
+ assert_conversion Measured::Length, "2000 mm", "0.656167979E1 ft"
116
+ end
117
+
118
+ test ".convert_to from mm to in" do
119
+ assert_conversion Measured::Length, "2000 mm", "0.7874015748E2 in"
120
+ end
121
+
122
+ test ".convert_to from mm to m" do
123
+ assert_conversion Measured::Length, "2000 mm", "2 m"
124
+ end
125
+
126
+ test ".convert_to from mm to mm" do
127
+ assert_conversion Measured::Length, "2000 mm", "2000 mm"
128
+ end
129
+
130
+ test ".convert_to from mm to yd" do
131
+ assert_conversion Measured::Length, "2000 mm", "0.2187226596E1 yd"
132
+ end
133
+
134
+ test ".convert_to from yd to cm" do
135
+ assert_conversion Measured::Length, "2000 yd", "0.18288E6 cm"
136
+ end
137
+
138
+ test ".convert_to from yd to ft" do
139
+ skip "returns 0.5999999999976E4"
140
+ assert_conversion Measured::Length, "2000 yd", "6000 ft"
141
+ end
142
+
143
+ test ".convert_to from yd to in" do
144
+ skip "returns 0.71999999999712E5"
145
+ assert_conversion Measured::Length, "2000 yd", "72000 in"
146
+ end
147
+
148
+ test ".convert_to from yd to m" do
149
+ assert_conversion Measured::Length, "2000 yd", "1828.8 m"
150
+ end
151
+
152
+ test ".convert_to from yd to mm" do
153
+ assert_conversion Measured::Length, "2000 yd", "1828800 mm"
154
+ end
155
+
156
+ test ".convert_to from yd to yd" do
157
+ assert_conversion Measured::Length, "2000 yd", "2000 yd"
158
+ end
159
+
160
+ end
@@ -0,0 +1,80 @@
1
+ require "test_helper"
2
+
3
+ class Measured::WeightTest < ActiveSupport::TestCase
4
+ setup do
5
+ @weight = Measured::Weight.new(1, "g")
6
+ end
7
+
8
+ test ".units_with_aliases should be the expected list of valid units" do
9
+ assert_equal ["g", "gram", "grams", "kg", "kilogram", "kilograms", "lb", "lbs", "ounce", "ounces", "oz", "pound", "pounds"], Measured::Weight.units_with_aliases
10
+ end
11
+
12
+ test ".units should be the list of base units" do
13
+ assert_equal ["g", "kg", "lb", "oz"], Measured::Weight.units
14
+ end
15
+
16
+ test ".convert_to from g to g" do
17
+ assert_conversion Measured::Weight, "2000 g", "2000 g"
18
+ end
19
+
20
+ test ".convert_to from g to kg" do
21
+ assert_conversion Measured::Weight, "2000 g", "2 kg"
22
+ end
23
+
24
+ test ".convert_to from g to lb" do
25
+ assert_conversion Measured::Weight, "2000 g", "4.40924524 lb"
26
+ end
27
+
28
+ test ".convert_to from g to oz" do
29
+ assert_conversion Measured::Weight, "2000 g", "70.54792384 oz"
30
+ end
31
+
32
+ test ".convert_to from kg to g" do
33
+ assert_conversion Measured::Weight, "2000 kg", "2000000 g"
34
+ end
35
+
36
+ test ".convert_to from kg to kg" do
37
+ assert_conversion Measured::Weight, "2000 kg", "2000 kg"
38
+ end
39
+
40
+ test ".convert_to from kg to lb" do
41
+ assert_conversion Measured::Weight, "2000 kg", "4409.24524 lb"
42
+ end
43
+
44
+ test ".convert_to from kg to oz" do
45
+ assert_conversion Measured::Weight, "2000 kg", "70547.92384 oz"
46
+ end
47
+
48
+ test ".convert_to from lb to g" do
49
+ assert_conversion Measured::Weight, "2000 lb", "907184.74 g"
50
+ end
51
+
52
+ test ".convert_to from lb to kg" do
53
+ assert_conversion Measured::Weight, "2000 lb", "907.18474 kg"
54
+ end
55
+
56
+ test ".convert_to from lb to lb" do
57
+ assert_conversion Measured::Weight, "2000 lb", "2000 lb"
58
+ end
59
+
60
+ test ".convert_to from lb to oz" do
61
+ assert_conversion Measured::Weight, "2000 lb", "32000 oz"
62
+ end
63
+
64
+ test ".convert_to from oz to g" do
65
+ assert_conversion Measured::Weight, "2000 oz", "56699.04625 g"
66
+ end
67
+
68
+ test ".convert_to from oz to kg" do
69
+ assert_conversion Measured::Weight, "2000 oz", "56.69904625 kg"
70
+ end
71
+
72
+ test ".convert_to from oz to lb" do
73
+ assert_conversion Measured::Weight, "2000 oz", "125 lb"
74
+ end
75
+
76
+ test ".convert_to from oz to oz" do
77
+ assert_conversion Measured::Weight, "2000 oz", "2000 oz"
78
+ end
79
+
80
+ end