measured 2.0.0.pre1 → 2.0.0.pre2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 430f3e67cfe5f11eaa88db5f2b3d666ca5a7d557
4
- data.tar.gz: 7ff421483024bec50b0703850d042eae46295b8c
3
+ metadata.gz: 10bd5a0ef33c55999eb30825b49399c3ab2ec183
4
+ data.tar.gz: 9307127d9fb8b0e7610919aaa18f53f4b9465e75
5
5
  SHA512:
6
- metadata.gz: 6c15fc5aa920d22f6db7c7edd7fb52a66eb0af84b3c3326bbec64a37a387b7a764fe69d7c6d1f2ce41e2335045cfe8af5fb4c581b76ae72507c0eec7e7e21926
7
- data.tar.gz: da4b602746ac0d5cb06353cc75deae90bfbcf56cec313196547dec347bbc7db50ff1f50a0d679ac95fa0fbcd715a22d0b678cef943c4ff7163aba36010ed6077
6
+ metadata.gz: 98b681bbf014999d0f328067af5d30259452deef6588b13b45d3db1afbadb117b20e19abcfb973620fe1a06984936807a9acea2a9c8433bc23e1332080f1b05e
7
+ data.tar.gz: 4bb6841e99399a8bc73f1063f3973f0b3e92f7273540b18393547e162b941a3d4f6fbddcd846e5fda6f7e61b8b22292112914b3dfc59879f23060fa4d4a035c0
@@ -1,13 +1,5 @@
1
1
  class Measured::CaseInsensitiveUnit < Measured::Unit
2
- def initialize(name, aliases: [], value: nil)
3
- super(name.to_s.downcase, aliases: aliases.map(&:to_s).map!(&:downcase), value: value)
4
- end
5
-
6
- def name_eql?(name_to_compare)
7
- super(name_to_compare.to_s.downcase)
8
- end
9
-
10
- def names_include?(name_to_compare)
11
- super(name_to_compare.to_s.downcase)
2
+ def initialize(name, aliases: [], value: nil, unit_system: nil)
3
+ super(name.to_s.downcase, aliases: aliases.map(&:to_s).map!(&:downcase), value: value, unit_system: unit_system)
12
4
  end
13
5
  end
@@ -3,8 +3,6 @@ class Measured::CaseInsensitiveUnitSystem < Measured::UnitSystem
3
3
  super(name.to_s.downcase)
4
4
  end
5
5
 
6
- protected
7
-
8
6
  def unit_for(name)
9
7
  unit_name_to_unit[name.to_s.downcase]
10
8
  end
@@ -6,7 +6,7 @@ class Measured::Measurable < Numeric
6
6
  def initialize(value, unit)
7
7
  raise Measured::UnitError, "Unit value cannot be blank" if value.blank?
8
8
 
9
- @unit = self.class.unit_system.to_unit_name!(unit)
9
+ @unit = unit_from_unit_or_name!(unit)
10
10
  @value = case value
11
11
  when Float
12
12
  BigDecimal(value, Float::DIG + 1)
@@ -20,12 +20,12 @@ class Measured::Measurable < Numeric
20
20
  end
21
21
 
22
22
  def convert_to(new_unit)
23
- new_unit_name = self.class.unit_system.to_unit_name(new_unit)
24
- return self if new_unit_name == @unit
23
+ new_unit = unit_from_unit_or_name!(new_unit)
24
+ return self if new_unit == unit
25
25
 
26
- value = self.class.unit_system.convert(@value, from: @unit, to: new_unit_name)
26
+ new_value = unit.unit_system.convert(value, from: unit, to: new_unit)
27
27
 
28
- self.class.new(value, new_unit)
28
+ self.class.new(new_value, new_unit)
29
29
  end
30
30
 
31
31
  def to_s
@@ -62,6 +62,10 @@ class Measured::Measurable < Numeric
62
62
 
63
63
  private
64
64
 
65
+ def unit_from_unit_or_name!(value)
66
+ value.is_a?(Measured::Unit) ? value : self.class.unit_system.unit_for!(value)
67
+ end
68
+
65
69
  def value_string
66
70
  @value_string ||= begin
67
71
  str = case value
data/lib/measured/unit.rb CHANGED
@@ -1,12 +1,22 @@
1
1
  class Measured::Unit
2
2
  include Comparable
3
3
 
4
- attr_reader :name, :names, :conversion_amount, :conversion_unit
4
+ attr_reader :name, :names, :conversion_amount, :conversion_unit, :unit_system
5
5
 
6
- def initialize(name, aliases: [], value: nil)
6
+ def initialize(name, aliases: [], value: nil, unit_system: nil)
7
7
  @name = name.to_s
8
8
  @names = ([@name] + aliases.map(&:to_s)).sort
9
9
  @conversion_amount, @conversion_unit = parse_value(value) if value
10
+ @unit_system = unit_system
11
+ end
12
+
13
+ def with_unit_system(unit_system)
14
+ self.class.new(
15
+ name,
16
+ aliases: names - [name],
17
+ value: conversion_string,
18
+ unit_system: unit_system
19
+ )
10
20
  end
11
21
 
12
22
  def to_s
@@ -23,10 +33,11 @@ class Measured::Unit
23
33
 
24
34
  def <=>(other)
25
35
  if self.class == other.class
26
- if other.names != @names
27
- other.names <=> @names
36
+ names_comparison = @names <=> other.names
37
+ if names_comparison != 0
38
+ names_comparison
28
39
  else
29
- other.conversion_amount <=> @conversion_amount
40
+ @conversion_amount <=> other.conversion_amount
30
41
  end
31
42
  else
32
43
  @name <=> other
@@ -2,7 +2,7 @@ class Measured::UnitSystem
2
2
  attr_reader :units
3
3
 
4
4
  def initialize(units)
5
- @units = units.dup
5
+ @units = units.map { |unit| unit.with_unit_system(self) }
6
6
  end
7
7
 
8
8
  def unit_names_with_aliases
@@ -22,20 +22,18 @@ class Measured::UnitSystem
22
22
  unit ? unit.name == name.to_s : false
23
23
  end
24
24
 
25
- def to_unit_name(name)
26
- to_unit_name!(name)
27
- rescue Measured::UnitError
28
- nil
25
+ def unit_for(name)
26
+ unit_name_to_unit[name.to_s]
29
27
  end
30
28
 
31
- def to_unit_name!(name)
32
- unit_for!(name).name
29
+ def unit_for!(name)
30
+ unit = unit_for(name)
31
+ raise Measured::UnitError, "Unit '#{name}' does not exist" unless unit
32
+ unit
33
33
  end
34
34
 
35
35
  def convert(value, from:, to:)
36
- from_unit = unit_for!(from)
37
- to_unit = unit_for!(to)
38
- conversion = conversion_table[from][to]
36
+ conversion = conversion_table.fetch(from.name, {})[to.name]
39
37
 
40
38
  raise Measured::UnitError, "Cannot find conversion entry from #{from} to #{to}" unless conversion
41
39
 
@@ -54,14 +52,4 @@ class Measured::UnitSystem
54
52
  hash
55
53
  end
56
54
  end
57
-
58
- def unit_for(name)
59
- unit_name_to_unit[name.to_s]
60
- end
61
-
62
- def unit_for!(name)
63
- unit = unit_for(name)
64
- raise Measured::UnitError, "Unit '#{name}' does not exist" unless unit
65
- unit
66
- end
67
55
  end
@@ -1,3 +1,3 @@
1
1
  module Measured
2
- VERSION = "2.0.0.pre1"
2
+ VERSION = "2.0.0.pre2"
3
3
  end
@@ -79,9 +79,10 @@ class Measured::ArithmeticTest < ActiveSupport::TestCase
79
79
  test "arithmetic operations favours unit of left" do
80
80
  left = Magic.new(1, :arcane)
81
81
  right = Magic.new(1, :magic_missile)
82
+ arcane = Magic.unit_system.unit_for!(:arcane)
82
83
 
83
- assert_equal "arcane", (left + right).unit
84
- assert_equal "arcane", (left - right).unit
84
+ assert_equal arcane, (left + right).unit
85
+ assert_equal arcane, (left - right).unit
85
86
  end
86
87
 
87
88
  test "#coerce should return other as-is when same class" do
@@ -2,19 +2,20 @@ require "test_helper"
2
2
 
3
3
  class Measured::CaseInsensitiveUnitSystemTest < ActiveSupport::TestCase
4
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
- ])
5
+ @unit_fireball = Magic.unit_system.unit_for!(:fireball)
6
+
7
+ @unit_m = Measured::CaseInsensitiveUnit.new(:m)
8
+ @unit_in = Measured::CaseInsensitiveUnit.new(:in, aliases: [:inch], value: "0.0254 m")
9
+ @unit_ft = Measured::CaseInsensitiveUnit.new(:ft, aliases: %w(Feet FOOT), value: "0.3048 m")
10
+ @conversion = Measured::CaseInsensitiveUnitSystem.new([@unit_m, @unit_in, @unit_ft])
10
11
  end
11
12
 
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
13
+ test "#unit_names_with_aliases lists all allowed unit names in lowercase" do
14
+ assert_equal %w(feet foot ft in inch m), @conversion.unit_names_with_aliases
14
15
  end
15
16
 
16
- test "#unit_names lists all base unit names without aliases" do
17
- assert_equal ["ft", "in", "m"], @conversion.unit_names
17
+ test "#unit_names lists all base unit names without aliases in lowercase" do
18
+ assert_equal %w(ft in m), @conversion.unit_names
18
19
  end
19
20
 
20
21
  test "#unit? checks if the unit is part of the units but not aliases" do
@@ -43,56 +44,58 @@ class Measured::CaseInsensitiveUnitSystemTest < ActiveSupport::TestCase
43
44
  refute @conversion.unit_or_alias?(nil)
44
45
  end
45
46
 
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")
47
+ test "#unit_for converts a unit name to its base unit" do
48
+ assert_equal @unit_fireball, Magic.unit_system.unit_for("fire")
48
49
  end
49
50
 
50
- test "#to_unit_name does not care about string or symbol" do
51
- assert_equal "fireball", Magic.unit_system.to_unit_name(:fire)
51
+ test "#unit_for does not care about string or symbol" do
52
+ assert_equal @unit_fireball, Magic.unit_system.unit_for(:fire)
52
53
  end
53
54
 
54
- test "#to_unit_name passes through if already base unit name" do
55
- assert_equal "fireball", Magic.unit_system.to_unit_name("fireball")
55
+ test "#unit_for passes through if already base unit name" do
56
+ assert_equal @unit_fireball, Magic.unit_system.unit_for("fireball")
56
57
  end
57
58
 
58
- test "#to_unit_name returns nil if not found" do
59
- assert_nil Magic.unit_system.to_unit_name("thunder")
59
+ test "#unit_for returns nil if not found" do
60
+ assert_nil Magic.unit_system.unit_for("thunder")
60
61
  end
61
62
 
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")
63
+ test "#unit_for! converts a unit name to its base unit" do
64
+ assert_equal @unit_fireball, Magic.unit_system.unit_for!("fire")
64
65
  end
65
66
 
66
- test "#to_unit_name! does not care about string or symbol" do
67
- assert_equal "fireball", Magic.unit_system.to_unit_name!(:fire)
67
+ test "#unit_for! does not care about string or symbol" do
68
+ assert_equal @unit_fireball, Magic.unit_system.unit_for!(:fire)
68
69
  end
69
70
 
70
- test "#to_unit_name! passes through if already base unit name" do
71
- assert_equal "fireball", Magic.unit_system.to_unit_name!("fireball")
71
+ test "#unit_for! passes through if already base unit name" do
72
+ assert_equal @unit_fireball, Magic.unit_system.unit_for!("fireball")
72
73
  end
73
74
 
74
- test "#to_unit_name! raises if not found" do
75
+ test "#unit_for! raises if not found" do
75
76
  assert_raises_with_message(Measured::UnitError, "Unit 'thunder' does not exist") do
76
- Magic.unit_system.to_unit_name!("thunder")
77
+ Magic.unit_system.unit_for!("thunder")
77
78
  end
78
79
  end
79
80
 
80
81
  test "#convert raises if either unit is not found" do
82
+ unit_bad = Measured::Unit.new(:doesnt_exist)
83
+
81
84
  assert_raises Measured::UnitError do
82
- Magic.unit_system.convert(1, from: "fire", to: "doesnt_exist")
85
+ Magic.unit_system.convert(1, from: @unit_fireball, to: unit_bad)
83
86
  end
84
87
 
85
88
  assert_raises Measured::UnitError do
86
- Magic.unit_system.convert(1, from: "doesnt_exist", to: "fire")
89
+ Magic.unit_system.convert(1, from: unit_bad, to: @unit_fireball)
87
90
  end
88
91
  end
89
92
 
90
93
  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")
94
+ assert_equal 3, @conversion.convert(36, from: @unit_in, to: @unit_ft)
95
+ assert_equal 18, @conversion.convert(Rational(3, 2), from: @unit_ft, to: @unit_in)
93
96
  end
94
97
 
95
98
  test "#convert handles the same unit" do
96
- assert_equal BigDecimal("2"), @conversion.convert(BigDecimal("2"), from: "in", to: "in")
99
+ assert_equal 2, @conversion.convert(2, from: @unit_in, to: @unit_in)
97
100
  end
98
101
  end
@@ -3,7 +3,7 @@ require "test_helper"
3
3
  class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
4
4
  setup do
5
5
  @unit = Measured::CaseInsensitiveUnit.new(:Pie, value: "10 Cake")
6
- @unit_with_aliases = Measured::CaseInsensitiveUnit.new(:Pie, aliases: ["Cake", "Tart"])
6
+ @unit_with_aliases = Measured::CaseInsensitiveUnit.new(:Pie, aliases: %w(Cake Tart))
7
7
  end
8
8
 
9
9
  test "#initialize converts the name to a downcased string" do
@@ -15,7 +15,7 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
15
15
  end
16
16
 
17
17
  test "#initialize parses out the unit and the number part" do
18
- assert_equal BigDecimal(10), @unit.conversion_amount
18
+ assert_equal 10, @unit.conversion_amount
19
19
  assert_equal "Cake", @unit.conversion_unit
20
20
 
21
21
  unit = Measured::CaseInsensitiveUnit.new(:pie, value: "5.5 sweets")
@@ -39,12 +39,12 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
39
39
 
40
40
  test "#to_s returns an expected string" do
41
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
42
+ assert_equal "pie (1/2 sweet)", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: "0.5 sweet").to_s
43
43
  end
44
44
 
45
45
  test "#inspect returns an expected string" do
46
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
47
+ assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::CaseInsensitiveUnit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
48
48
  end
49
49
 
50
50
  test "includes Comparable mixin" do
@@ -62,18 +62,18 @@ class Measured::CaseInsensitiveUnitTest < ActiveSupport::TestCase
62
62
  assert_equal 0, @unit <=> Measured::CaseInsensitiveUnit.new("Pie", value: [10, :cake])
63
63
  end
64
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])
65
+ test "#<=> is 1 for units with names that come after Pie lexicographically" do
66
+ assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new(:pancake, value: "10 cake")
67
+ assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new("pie", aliases: ["pancakes"], value: "10 cake")
69
68
  end
70
69
 
71
- test "#inverse_conversion_amount returns 1/amount for BigDecimal" do
72
- assert_equal BigDecimal(1) / 10, @unit.inverse_conversion_amount
70
+ test "#<=> compares #conversion_amount when unit names the same" do
71
+ assert_equal -1, @unit <=> Measured::CaseInsensitiveUnit.new(:pie, value: [11, :pancake])
72
+ assert_equal 0, @unit <=> Measured::CaseInsensitiveUnit.new(:pie, value: [10, :foo])
73
+ assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new(:pie, value: [9, :pancake])
73
74
  end
74
75
 
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
76
+ test "#inverse_conversion_amount returns 1/amount" do
77
+ assert_equal Rational(1, 10), @unit.inverse_conversion_amount
78
78
  end
79
79
  end
@@ -3,7 +3,10 @@ require "test_helper"
3
3
  class Measured::MeasurableTest < ActiveSupport::TestCase
4
4
 
5
5
  setup do
6
- @magic = Magic.new(10, :magic_missile)
6
+ @arcane = Magic.unit_system.unit_for!(:arcane)
7
+ @fireball = Magic.unit_system.unit_for!(:fireball)
8
+ @magic_missile = Magic.unit_system.unit_for!(:magic_missile)
9
+ @magic = Magic.new(10, @magic_missile)
7
10
  end
8
11
 
9
12
  test "#initialize requires two params, the amount and the unit" do
@@ -16,9 +19,9 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
16
19
  end
17
20
  end
18
21
 
19
- test "#initialize converts unit to string from symbol" do
20
- magic = Magic.new(1, :arcane)
21
- assert_equal "arcane", magic.unit
22
+ test "#initialize converts unit string and symbol to Unit instance" do
23
+ assert_equal @arcane, Magic.new(1, "arcane").unit
24
+ assert_equal @arcane, Magic.new(1, :arcane).unit
22
25
  end
23
26
 
24
27
  test "#initialize raises if it is an unknown unit" do
@@ -27,23 +30,19 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
27
30
  end
28
31
  end
29
32
 
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
33
+ test "#initialize keeps Rationals, BigDecimals, and Integers as-is" do
34
+ assert_equal 10, Magic.new(10, :fire).value
35
+ assert_equal Rational(1, 3), Magic.new(Rational(1, 3), :fire).value
36
+ assert_equal BigDecimal("123.887788"), Magic.new(BigDecimal("123.887788"), :fire).value
34
37
  end
35
38
 
36
- test "#initialize converts floats to strings and then to BigDecimal so it does not raise" do
37
- assert_equal BigDecimal("1.2345"), Magic.new(1.2345, :fire).value
38
- end
39
-
40
- test "#initialize converts numbers and strings BigDecimal and does not round large numbers" do
39
+ test "#initialize converts floats and strings to BigDecimal and does not round large numbers" do
41
40
  assert_equal BigDecimal(9.1234572342342, 14), Magic.new(9.1234572342342, :fire).value
42
- assert_equal BigDecimal("9.1234572342342"), Magic.new(9.1234572342342, :fire).value
41
+ assert_equal BigDecimal("9.1234572342342"), Magic.new("9.1234572342342", :fire).value
43
42
  end
44
43
 
45
- test "#initialize converts to the base unit name" do
46
- assert_equal "fireball", Magic.new(1, :fire).unit
44
+ test "#initialize converts to the base unit" do
45
+ assert_equal @fireball, Magic.new(1, :fire).unit
47
46
  end
48
47
 
49
48
  test "#initialize raises an expected error when initializing with nil value" do
@@ -74,19 +73,19 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
74
73
  assert_equal "Unit '' does not exist", exception.message
75
74
  end
76
75
 
77
- test "#unit allows you to read the unit string" do
78
- assert_equal "magic_missile", @magic.unit
76
+ test "#unit allows you to read the unit" do
77
+ assert_equal @magic_missile, @magic.unit
79
78
  end
80
79
 
81
80
  test "#value allows you to read the numeric value" do
82
- assert_equal BigDecimal(10), @magic.value
81
+ assert_equal 10, @magic.value
83
82
  end
84
83
 
85
- test ".conversion is set and cached" do
86
- conversion = CaseSensitiveMagic.unit_system
84
+ test ".unit_system is set and cached" do
85
+ unit_system = CaseSensitiveMagic.unit_system
87
86
 
88
- assert_instance_of Measured::UnitSystem, conversion
89
- assert_equal conversion.__id__, CaseSensitiveMagic.unit_system.__id__
87
+ assert_instance_of Measured::UnitSystem, unit_system
88
+ assert_equal unit_system.__id__, CaseSensitiveMagic.unit_system.__id__
90
89
  end
91
90
 
92
91
  test ".unit_names returns just the base unit names" do
@@ -118,19 +117,19 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
118
117
 
119
118
  test "#convert_to raises on an invalid unit" do
120
119
  assert_raises Measured::UnitError do
121
- @magic.convert_to(:punch)
120
+ @magic.convert_to(Measured::Unit.new(:punch))
122
121
  end
123
122
  end
124
123
 
125
124
  test "#convert_to returns a new object of the same type in the new unit" do
126
- converted = @magic.convert_to(:arcane)
125
+ converted = @magic.convert_to(@arcane)
127
126
 
128
127
  assert_equal converted, @magic
129
128
  refute_equal converted.object_id, @magic.object_id
130
129
  assert_equal BigDecimal(10), @magic.value
131
- assert_equal "magic_missile", @magic.unit
130
+ assert_equal @magic_missile, @magic.unit
132
131
  assert_equal BigDecimal(1), converted.value
133
- assert_equal "arcane", converted.unit
132
+ assert_equal @arcane, converted.unit
134
133
  end
135
134
 
136
135
  test "#convert_to from and to the same unit returns the same object" do
@@ -139,12 +138,12 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
139
138
  end
140
139
 
141
140
  test "#to_s outputs the number and the unit" do
142
- assert_equal "10 fireball", Magic.new(10, :fire).to_s
141
+ assert_equal "10 fireball (2/3 magic_missile)", Magic.new(10, :fire).to_s
143
142
  assert_equal "1.234 magic_missile", Magic.new("1.234", :magic_missile).to_s
144
143
  end
145
144
 
146
145
  test "#inspect shows the number and the unit" do
147
- assert_equal "#<Magic: 10 fireball>", Magic.new(10, :fire).inspect
146
+ assert_equal "#<Magic: 10 fireball (2/3 magic_missile)>", Magic.new(10, :fire).inspect
148
147
  assert_equal "#<Magic: 1.234 magic_missile>", Magic.new(1.234, :magic_missile).inspect
149
148
  end
150
149
 
@@ -204,11 +203,4 @@ class Measured::MeasurableTest < ActiveSupport::TestCase
204
203
  assert_raises(ArgumentError) { @magic < 0.00 }
205
204
  end
206
205
 
207
- test "#eql? should be the same if the classes and amount match, and unit is converted" do
208
- assert @magic == @magic
209
- assert Magic.new(10, :magic_missile) == Magic.new("10", "magic_missile")
210
- assert Magic.new(1, :arcane) == Magic.new(10, :magic_missile)
211
- refute Magic.new(1, :arcane) == Magic.new(10.1, :magic_missile)
212
- end
213
-
214
206
  end
data/test/test_helper.rb CHANGED
@@ -23,6 +23,7 @@ class ActiveSupport::TestCase
23
23
  from_amount, from_unit = from.split(" ")
24
24
  to_amount, to_unit = to.split(" ")
25
25
 
26
+ to_unit = klass.unit_system.unit_for!(to_unit)
26
27
  assert_close_bigdecimal BigDecimal(to_amount), klass.new(from_amount, from_unit).convert_to(to_unit).value
27
28
  end
28
29
 
@@ -30,6 +31,7 @@ class ActiveSupport::TestCase
30
31
  from_amount, from_unit = from.split(" ")
31
32
  to_amount, to_unit = to.split(" ")
32
33
 
34
+ to_unit = klass.unit_system.unit_for!(to_unit)
33
35
  assert_equal BigDecimal(to_amount), klass.new(from_amount, from_unit).convert_to(to_unit).value
34
36
  end
35
37
 
@@ -43,7 +43,7 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
43
43
  unit :BOLD, value: "100 normal"
44
44
  end
45
45
 
46
- assert_equal 2, measurable.new(200, :normal).convert_to(:BOLD).value
46
+ assert_equal 'BOLD', measurable.unit_system.unit_for!(:BOLD).name
47
47
  end
48
48
 
49
49
  test "case-insensitive conversion is produced by default" do
@@ -53,6 +53,6 @@ class Measured::UnitSystemBuilderTest < ActiveSupport::TestCase
53
53
  unit :bolder, value: "100 normal"
54
54
  end
55
55
 
56
- assert_equal 20, measurable.new(200, :normal).convert_to(:bOlD).value
56
+ assert_equal 'bold', measurable.unit_system.unit_for!(:bOlD).name
57
57
  end
58
58
  end
@@ -2,19 +2,20 @@ require "test_helper"
2
2
 
3
3
  class Measured::UnitSystemTest < ActiveSupport::TestCase
4
4
  setup do
5
- @conversion = Measured::UnitSystem.new([
6
- Measured::Unit.new(:m),
7
- Measured::Unit.new(:in, aliases: [:Inch], value: "0.0254 m"),
8
- Measured::Unit.new(:ft, aliases: [:Feet, :Foot], value: "0.3048 m"),
9
- ])
5
+ @unit_fireball = CaseSensitiveMagic.unit_system.unit_for!(:fireball)
6
+
7
+ @unit_m = Measured::Unit.new(:m)
8
+ @unit_in = Measured::Unit.new(:in, aliases: [:Inch], value: "0.0254 m")
9
+ @unit_ft = Measured::Unit.new(:ft, aliases: %w(Feet Foot), value: "0.3048 m")
10
+ @conversion = Measured::UnitSystem.new([@unit_m, @unit_in, @unit_ft])
10
11
  end
11
12
 
12
13
  test "#unit_names_with_aliases lists all allowed unit names" do
13
- assert_equal ["Feet", "Foot", "Inch", "ft", "in", "m"], @conversion.unit_names_with_aliases
14
+ assert_equal %w(Feet Foot Inch ft in m), @conversion.unit_names_with_aliases
14
15
  end
15
16
 
16
17
  test "#unit_names lists all unit names without aliases" do
17
- assert_equal ["ft", "in", "m"], @conversion.unit_names
18
+ assert_equal %w(ft in m), @conversion.unit_names
18
19
  end
19
20
 
20
21
  test "#unit? checks if the unit is part of the units but not aliases" do
@@ -44,56 +45,58 @@ class Measured::UnitSystemTest < ActiveSupport::TestCase
44
45
  refute @conversion.unit_or_alias?(nil)
45
46
  end
46
47
 
47
- test "#to_unit_name converts a unit name to its base unit" do
48
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name("fire")
48
+ test "#unit_for converts a unit name to its base unit" do
49
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for("fire")
49
50
  end
50
51
 
51
- test "#to_unit_name does not care about string or symbol" do
52
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name(:fire)
52
+ test "#unit_for does not care about string or symbol" do
53
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for(:fire)
53
54
  end
54
55
 
55
- test "#to_unit_name passes through if already base unit name" do
56
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name("fireball")
56
+ test "#unit_for passes through if already base unit name" do
57
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for("fireball")
57
58
  end
58
59
 
59
- test "#to_unit_name returns nil if not found" do
60
- assert_nil CaseSensitiveMagic.unit_system.to_unit_name("thunder")
60
+ test "#unit_for returns nil if not found" do
61
+ assert_nil CaseSensitiveMagic.unit_system.unit_for("thunder")
61
62
  end
62
63
 
63
- test "#to_unit_name! converts a unit name to its base unit" do
64
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name!("fire")
64
+ test "#unit_for! converts a unit name to its base unit" do
65
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for!("fire")
65
66
  end
66
67
 
67
- test "#to_unit_name! does not care about string or symbol" do
68
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name!(:fire)
68
+ test "#unit_for! does not care about string or symbol" do
69
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for!(:fire)
69
70
  end
70
71
 
71
- test "#to_unit_name! passes through if already base unit name" do
72
- assert_equal "fireball", CaseSensitiveMagic.unit_system.to_unit_name!("fireball")
72
+ test "#unit_for! passes through if already base unit name" do
73
+ assert_equal @unit_fireball, CaseSensitiveMagic.unit_system.unit_for!("fireball")
73
74
  end
74
75
 
75
- test "#to_unit_name! raises if not found" do
76
+ test "#unit_for! raises if not found" do
76
77
  assert_raises_with_message(Measured::UnitError, "Unit 'thunder' does not exist") do
77
- CaseSensitiveMagic.unit_system.to_unit_name!("thunder")
78
+ CaseSensitiveMagic.unit_system.unit_for!("thunder")
78
79
  end
79
80
  end
80
81
 
81
82
  test "#convert raises if either unit is not found" do
83
+ unit_bad = Measured::Unit.new(:doesnt_exist)
84
+
82
85
  assert_raises Measured::UnitError do
83
- CaseSensitiveMagic.unit_system.convert(1, from: "fire", to: "doesnt_exist")
86
+ CaseSensitiveMagic.unit_system.convert(1, from: @unit_fireball, to: unit_bad)
84
87
  end
85
88
 
86
89
  assert_raises Measured::UnitError do
87
- CaseSensitiveMagic.unit_system.convert(1, from: "doesnt_exist", to: "fire")
90
+ CaseSensitiveMagic.unit_system.convert(1, from: unit_bad, to: @unit_fireball)
88
91
  end
89
92
  end
90
93
 
91
94
  test "#convert converts between two known units" do
92
- assert_equal BigDecimal("3"), @conversion.convert(BigDecimal("36"), from: "in", to: "ft")
93
- assert_equal BigDecimal("18"), @conversion.convert(BigDecimal("1.5"), from: "ft", to: "in")
95
+ assert_equal 3, @conversion.convert(36, from: @unit_in, to: @unit_ft)
96
+ assert_equal 18, @conversion.convert(Rational(3, 2), from: @unit_ft, to: @unit_in)
94
97
  end
95
98
 
96
99
  test "#convert handles the same unit" do
97
- assert_equal BigDecimal("2"), @conversion.convert(BigDecimal("2"), from: "in", to: "in")
100
+ assert_equal 2, @conversion.convert(2, from: @unit_in, to: @unit_in)
98
101
  end
99
102
  end
data/test/unit_test.rb CHANGED
@@ -3,7 +3,7 @@ require "test_helper"
3
3
  class Measured::UnitTest < ActiveSupport::TestCase
4
4
  setup do
5
5
  @unit = Measured::Unit.new(:Pie, value: "10 Cake")
6
- @unit_with_aliases = Measured::Unit.new(:Pie, aliases: ["Cake", "Tart"])
6
+ @unit_with_aliases = Measured::Unit.new(:Pie, aliases: %w(Cake Tart))
7
7
  end
8
8
 
9
9
  test "#initialize converts the name to a string" do
@@ -15,7 +15,7 @@ class Measured::UnitTest < ActiveSupport::TestCase
15
15
  end
16
16
 
17
17
  test "#initialize parses out the unit and the number part" do
18
- assert_equal BigDecimal(10), @unit.conversion_amount
18
+ assert_equal 10, @unit.conversion_amount
19
19
  assert_equal "Cake", @unit.conversion_unit
20
20
 
21
21
  unit = Measured::Unit.new(:pie, value: "5.5 sweets")
@@ -39,12 +39,12 @@ class Measured::UnitTest < ActiveSupport::TestCase
39
39
 
40
40
  test "#to_s returns an expected string" do
41
41
  assert_equal "pie", Measured::Unit.new(:pie).to_s
42
- assert_equal "pie (1/2 sweet)", Measured::Unit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).to_s
42
+ assert_equal "pie (1/2 sweet)", Measured::Unit.new(:pie, aliases: ["cake"], value: "0.5 sweet").to_s
43
43
  end
44
44
 
45
45
  test "#inspect returns an expected string" do
46
46
  assert_equal "#<Measured::Unit: pie (pie) >", Measured::Unit.new(:pie).inspect
47
- assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::Unit.new(:pie, aliases: ["cake"], value: [Rational(1,2), "sweet"]).inspect
47
+ assert_equal "#<Measured::Unit: pie (cake, pie) 1/2 sweet>", Measured::Unit.new(:pie, aliases: ["cake"], value: "1/2 sweet").inspect
48
48
  end
49
49
 
50
50
  test "includes Comparable mixin" do
@@ -62,18 +62,18 @@ class Measured::UnitTest < ActiveSupport::TestCase
62
62
  assert_equal 0, @unit <=> Measured::Unit.new("Pie", value: [10, :cake])
63
63
  end
64
64
 
65
- test "#<=> is 1 for units with names that come after Pie lexicographically" do
66
- assert_equal 1, @unit <=> Measured::Unit.new(:Pigs, value: "10 bacon")
67
- assert_equal 1, @unit <=> Measured::Unit.new("Pig", aliases: %w(Pigs), value: "10 bacon")
68
- assert_equal 1, @unit <=> Measured::Unit.new(:Pig, value: [11, :bacon])
65
+ test "#<=> is -1 for units with names that come after Pie lexicographically" do
66
+ assert_equal -1, @unit <=> Measured::Unit.new(:Pigs, value: "10 bacon")
67
+ assert_equal -1, @unit <=> Measured::Unit.new("Pig", aliases: %w(Pigs), value: "10 bacon")
69
68
  end
70
-
71
- test "#inverse_conversion_amount returns 1/amount for BigDecimal" do
72
- assert_equal BigDecimal(1) / 10, @unit.inverse_conversion_amount
69
+
70
+ test "#<=> compares #conversion_amount when unit names the same" do
71
+ assert_equal -1, @unit <=> Measured::Unit.new(:Pie, value: [11, :pancake])
72
+ assert_equal 0, @unit <=> Measured::Unit.new(:Pie, value: [10, :foo])
73
+ assert_equal 1, @unit <=> Measured::Unit.new(:Pie, value: [9, :pancake])
73
74
  end
74
75
 
75
- test "#inverse_conversion_amount swaps the numerator and denominator for Rational" do
76
- unit = Measured::Unit.new(:pie, value: [Rational(3, 7), "cake"])
77
- assert_equal Rational(7, 3), unit.inverse_conversion_amount
76
+ test "#inverse_conversion_amount returns 1/amount" do
77
+ assert_equal Rational(1, 10), @unit.inverse_conversion_amount
78
78
  end
79
79
  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.0.0.pre1
4
+ version: 2.0.0.pre2
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-01-17 00:00:00.000000000 Z
11
+ date: 2017-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport