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 +4 -4
- data/lib/measured/case_insensitive_unit.rb +2 -10
- data/lib/measured/case_insensitive_unit_system.rb +0 -2
- data/lib/measured/measurable.rb +9 -5
- data/lib/measured/unit.rb +16 -5
- data/lib/measured/unit_system.rb +8 -20
- data/lib/measured/version.rb +1 -1
- data/test/arithmetic_test.rb +3 -2
- data/test/case_insensitive_unit_system_test.rb +33 -30
- data/test/case_insensitive_unit_test.rb +13 -13
- data/test/measurable_test.rb +28 -36
- data/test/test_helper.rb +2 -0
- data/test/unit_system_builder_test.rb +2 -2
- data/test/unit_system_test.rb +31 -28
- data/test/unit_test.rb +14 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10bd5a0ef33c55999eb30825b49399c3ab2ec183
|
4
|
+
data.tar.gz: 9307127d9fb8b0e7610919aaa18f53f4b9465e75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/measured/measurable.rb
CHANGED
@@ -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 =
|
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
|
-
|
24
|
-
return self if
|
23
|
+
new_unit = unit_from_unit_or_name!(new_unit)
|
24
|
+
return self if new_unit == unit
|
25
25
|
|
26
|
-
|
26
|
+
new_value = unit.unit_system.convert(value, from: unit, to: new_unit)
|
27
27
|
|
28
|
-
self.class.new(
|
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
|
-
|
27
|
-
|
36
|
+
names_comparison = @names <=> other.names
|
37
|
+
if names_comparison != 0
|
38
|
+
names_comparison
|
28
39
|
else
|
29
|
-
|
40
|
+
@conversion_amount <=> other.conversion_amount
|
30
41
|
end
|
31
42
|
else
|
32
43
|
@name <=> other
|
data/lib/measured/unit_system.rb
CHANGED
@@ -2,7 +2,7 @@ class Measured::UnitSystem
|
|
2
2
|
attr_reader :units
|
3
3
|
|
4
4
|
def initialize(units)
|
5
|
-
@units = units.
|
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
|
26
|
-
|
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
|
32
|
-
unit_for
|
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
|
-
|
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
|
data/lib/measured/version.rb
CHANGED
data/test/arithmetic_test.rb
CHANGED
@@ -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
|
84
|
-
assert_equal
|
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
|
-
@
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
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
|
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 "#
|
47
|
-
assert_equal
|
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 "#
|
51
|
-
assert_equal
|
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 "#
|
55
|
-
assert_equal
|
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 "#
|
59
|
-
assert_nil Magic.unit_system.
|
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 "#
|
63
|
-
assert_equal
|
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 "#
|
67
|
-
assert_equal
|
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 "#
|
71
|
-
assert_equal
|
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 "#
|
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.
|
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:
|
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:
|
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
|
92
|
-
assert_equal
|
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
|
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:
|
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
|
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:
|
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:
|
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(:
|
67
|
-
assert_equal 1, @unit <=> Measured::CaseInsensitiveUnit.new("pie", aliases: ["
|
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 "#
|
72
|
-
assert_equal
|
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
|
76
|
-
|
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
|
data/test/measurable_test.rb
CHANGED
@@ -3,7 +3,10 @@ require "test_helper"
|
|
3
3
|
class Measured::MeasurableTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
setup do
|
6
|
-
@
|
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
|
20
|
-
|
21
|
-
assert_equal
|
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
|
31
|
-
assert_equal
|
32
|
-
assert_equal
|
33
|
-
assert_equal BigDecimal("
|
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
|
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
|
46
|
-
assert_equal
|
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
|
78
|
-
assert_equal
|
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
|
81
|
+
assert_equal 10, @magic.value
|
83
82
|
end
|
84
83
|
|
85
|
-
test ".
|
86
|
-
|
84
|
+
test ".unit_system is set and cached" do
|
85
|
+
unit_system = CaseSensitiveMagic.unit_system
|
87
86
|
|
88
|
-
assert_instance_of Measured::UnitSystem,
|
89
|
-
assert_equal
|
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(
|
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
|
130
|
+
assert_equal @magic_missile, @magic.unit
|
132
131
|
assert_equal BigDecimal(1), converted.value
|
133
|
-
assert_equal
|
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
|
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
|
56
|
+
assert_equal 'bold', measurable.unit_system.unit_for!(:bOlD).name
|
57
57
|
end
|
58
58
|
end
|
data/test/unit_system_test.rb
CHANGED
@@ -2,19 +2,20 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class Measured::UnitSystemTest < ActiveSupport::TestCase
|
4
4
|
setup do
|
5
|
-
@
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
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
|
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 "#
|
48
|
-
assert_equal
|
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 "#
|
52
|
-
assert_equal
|
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 "#
|
56
|
-
assert_equal
|
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 "#
|
60
|
-
assert_nil CaseSensitiveMagic.unit_system.
|
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 "#
|
64
|
-
assert_equal
|
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 "#
|
68
|
-
assert_equal
|
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 "#
|
72
|
-
assert_equal
|
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 "#
|
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.
|
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:
|
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:
|
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
|
93
|
-
assert_equal
|
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
|
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:
|
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
|
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:
|
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:
|
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 "#
|
72
|
-
assert_equal
|
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
|
76
|
-
|
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.
|
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-
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|