measured 1.6.0 → 2.0.0.pre1

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.
@@ -1,26 +1,8 @@
1
- class Measured::Length < Measured::Measurable
2
-
3
- conversion.set_base :m,
4
- aliases: [:meter, :metre, :meters, :metres]
5
-
6
- conversion.add :cm,
7
- aliases: [:centimeter, :centimetre, :centimeters, :centimetres],
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
- class Measured::Weight < Measured::Measurable
2
-
3
- conversion.set_base :g,
4
- aliases: [:gram, :grams]
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
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "1.6.0"
2
+ VERSION = "2.0.0.pre1"
3
3
  end
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.0"
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"
@@ -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 "#+ should add a number to the value" do
16
- assert_equal Magic.new(5, :magic_missile), @two + 3
17
- assert_equal Magic.new(5, :magic_missile), 2 + @three
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 "#- should subtract a number from the value" do
46
- assert_equal Magic.new(-1, :magic_missile), @two - 3
47
- assert_equal Magic.new(1, :magic_missile), 2 - @three
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 "#* should multiply together same units" do
71
- assert_equal Magic.new(6, :magic_missile), @two * @three
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 "#/ should divide a number to the value" do
106
- assert_equal Magic.new("0.5", :magic_missile), @two / 4
107
- assert_equal Magic.new(2, :magic_missile), 2 / @four
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 "#/ should raise if different unit system" do
111
- assert_raises TypeError do
112
- OtherFakeSystem.new(1, :other_fake_base) / @two
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
- assert_raises TypeError do
116
- @two / OtherFakeSystem.new(1, :other_fake_base)
117
- end
83
+ assert_equal "arcane", (left + right).unit
84
+ assert_equal "arcane", (left - right).unit
118
85
  end
119
86
 
120
- test "#/ should raise if dividing something nonsense" do
121
- assert_raises TypeError do
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 "#-@ returns the negative version" do
131
- assert_equal Magic.new(-2, :magic_missile), -@two
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
- setup do
5
- @unit = Measured::Unit.new(:test)
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 "#initialize accepts a list of units and a base unit" do
9
- Measured::ConversionTable.new([@unit])
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 "#to_h should return a hash for the simple case" do
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
- "test" => {"test" => BigDecimal("1")}
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, Measured::ConversionTable.new([@unit]).to_h
95
+ assert_equal expected, conversion_table
18
96
  end
97
+
19
98
  end
@@ -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 nil", exception.message
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 cannot be blank", exception.message
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 cannot be blank", exception.message
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 = Magic.conversion
86
+ conversion = CaseSensitiveMagic.unit_system
87
87
 
88
- assert_instance_of Measured::Conversion, conversion
89
- assert_equal conversion, Magic.conversion
88
+ assert_instance_of Measured::UnitSystem, conversion
89
+ assert_equal conversion.__id__, CaseSensitiveMagic.unit_system.__id__
90
90
  end
91
91
 
92
- test ".units returns just the base units" do
93
- assert_equal ["arcane", "fireball", "ice", "magic_missile", "ultima"], Magic.units
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 ".units_with_aliases returns all units" do
97
- assert_equal ["arcane", "fire", "fireball", "fireballs", "ice", "magic_missile", "magic_missiles", "ultima"], Magic.units_with_aliases
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 ".valid_unit? looks at the list of units and aliases" do
101
- assert Magic.valid_unit?("fire")
102
- assert Magic.valid_unit?("fireball")
103
- assert Magic.valid_unit?(:ice)
104
- refute Magic.valid_unit?("junk")
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
- class VeryComplexThing < Measured::Measurable ; end
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.0 fireball>", Magic.new(10, :fire).inspect
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? delegates to the value" do
160
- assert Magic.new(0, :fire).zero?
161
- refute Magic.new(2, :fire).zero?
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(10, :fire)
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(10, :magic_missile)
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 "#<=> compares against zero" do
172
- assert_equal 1, @magic <=> 0
173
- assert_equal 1, @magic <=> BigDecimal.new(0)
174
- assert_equal 1, @magic <=> 0.00
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
- assert @magic == @magic
180
- assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
181
- assert Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
182
- refute Magic.new(1, :arcane) == Magic.new(10.1, :magic_missile)
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
- assert Magic.new(2, :magic_missile) == Magic.new("1", "ice")
178
+ assert_equal Magic.new(2, :magic_missile), Magic.new("1", "ice")
187
179
  end
188
180
 
189
- test "#== compares against zero" do
190
- assert Magic.new(0, :fire) == 0
191
- assert Magic.new(0, :magic_missile) == 0
192
- assert Magic.new(0, :fire) == BigDecimal.new(0)
193
- assert Magic.new(0, :fire) == 0.00
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
- assert @magic > 0
210
- assert @magic > BigDecimal.new(0)
211
- assert @magic > 0.00
212
- assert Magic.new(-1, :arcane) < 0
213
- refute @magic < 0
214
- refute Magic.new(-1, :arcane) > 0
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