amounts 0.0.8 → 0.2.0
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/.rubocop.yml +37 -0
- data/CHANGELOG.md +180 -0
- data/Gemfile +4 -0
- data/README.md +274 -2
- data/Rakefile +47 -10
- data/lib/amount/active_record/amount_validator.rb +9 -18
- data/lib/amount/active_record/attribute_definition.rb +14 -8
- data/lib/amount/active_record/calculations.rb +127 -0
- data/lib/amount/active_record/dirty.rb +43 -0
- data/lib/amount/active_record/locale/en.yml +7 -0
- data/lib/amount/active_record/model.rb +19 -3
- data/lib/amount/active_record/persistence.rb +34 -0
- data/lib/amount/active_record/serialization.rb +57 -0
- data/lib/amount/active_record.rb +18 -0
- data/lib/amount/compact.rb +137 -0
- data/lib/amount/{allocation.rb → concerns/allocatable.rb} +1 -1
- data/lib/amount/{arithmetic.rb → concerns/calculable.rb} +5 -2
- data/lib/amount/concerns/coercible.rb +65 -0
- data/lib/amount/concerns/constructible.rb +92 -0
- data/lib/amount/{conversion.rb → concerns/convertible.rb} +1 -1
- data/lib/amount/concerns/formattable.rb +112 -0
- data/lib/amount/concerns/localizable.rb +39 -0
- data/lib/amount/{comparison.rb → concerns/orderable.rb} +2 -2
- data/lib/amount/concerns/parsable.rb +58 -0
- data/lib/amount/concerns/registrable.rb +80 -0
- data/lib/amount/{serialization.rb → concerns/serializable.rb} +21 -4
- data/lib/amount/display.rb +81 -46
- data/lib/amount/locale.rb +102 -0
- data/lib/amount/registry.rb +69 -7
- data/lib/amount/ui_parser.rb +109 -0
- data/lib/amount/version.rb +1 -1
- data/lib/amount.rb +55 -199
- data/test/amount/active_record/amount_validator_test.rb +209 -0
- data/test/amount/active_record/attribute_definition_test.rb +259 -0
- data/test/amount/active_record/calculations_test.rb +165 -0
- data/test/amount/active_record/dirty_test.rb +70 -0
- data/test/amount/active_record/migration_methods_test.rb +60 -0
- data/test/amount/active_record/model_test.rb +72 -0
- data/test/amount/active_record/persistence_test.rb +53 -0
- data/test/amount/active_record/serialization_test.rb +75 -0
- data/test/amount/active_record/type_test.rb +80 -0
- data/test/amount/active_record_test.rb +12 -0
- data/test/amount/compact_test.rb +371 -0
- data/test/amount/concerns/allocatable_test.rb +166 -0
- data/test/amount/concerns/calculable_test.rb +185 -0
- data/test/amount/concerns/coercible_test.rb +49 -0
- data/test/amount/concerns/constructible_test.rb +113 -0
- data/test/amount/concerns/convertible_test.rb +54 -0
- data/test/amount/concerns/formattable_test.rb +336 -0
- data/test/amount/concerns/localizable_test.rb +59 -0
- data/test/amount/concerns/orderable_test.rb +83 -0
- data/test/amount/concerns/parsable_test.rb +98 -0
- data/test/amount/concerns/registrable_test.rb +67 -0
- data/test/amount/concerns/serializable_test.rb +136 -0
- data/test/amount/display_test.rb +181 -0
- data/test/amount/locale_test.rb +590 -0
- data/test/amount/parser_test.rb +29 -0
- data/test/amount/registry/generated_constructors_test.rb +94 -0
- data/test/amount/registry_test.rb +149 -0
- data/test/amount/ui_parser_test.rb +284 -0
- data/test/amount_test.rb +55 -0
- data/test/dummy/config/application.rb +3 -0
- data/test/generators/amount/active_record/registry_generator_test.rb +116 -0
- data/test/integration/postgresql_test.rb +124 -0
- data/test/support/active_record_test_support.rb +85 -0
- data/test/support/error_helpers.rb +22 -0
- data/test/support/locale_test_setup.rb +36 -0
- data/test/test_helper.rb +1 -0
- metadata +55 -11
- data/test/postgresql_integration_test.rb +0 -71
- data/test/test_active_record.rb +0 -375
- data/test/test_amount.rb +0 -675
- data/test/test_registry_generator.rb +0 -104
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require_relative "../../test_helper"
|
|
5
|
+
|
|
6
|
+
class AmountCalculableTest < Minitest::Test
|
|
7
|
+
class GoldAmount < Amount
|
|
8
|
+
def purity_estimate
|
|
9
|
+
"24k"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
AmountTestSupport.register_default_types!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def teardown
|
|
18
|
+
Amount.registry.clear!
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_frozen_amount_addition_returns_correct_sum
|
|
22
|
+
assert_equal Amount.new("3", :USDC), frozen_sum
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_frozen_amount_addition_returns_unfrozen_result
|
|
26
|
+
refute_predicate frozen_sum, :frozen?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_addition_same_type
|
|
30
|
+
assert_equal Amount.new("2.0", :USDC), Amount.new("1.5", :USDC) + Amount.new("0.5", :USDC)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_addition_cross_type_uses_registered_rate
|
|
34
|
+
Amount.register_default_rate :USD, :USDC, 1
|
|
35
|
+
|
|
36
|
+
result = Amount.new("10.00", :USDC) + Amount.new("5.00", :USD)
|
|
37
|
+
|
|
38
|
+
assert_equal Amount.new("15.00", :USDC), result
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_addition_different_type_raises_without_rate
|
|
42
|
+
assert_raises(Amount::TypeMismatch) do
|
|
43
|
+
Amount.new(1, :USDC) + Amount.new(1, :SOL)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_subtraction_cross_type_uses_registered_rate
|
|
48
|
+
Amount.register_default_rate :USD, :USDC, 1
|
|
49
|
+
|
|
50
|
+
result = Amount.new("10.00", :USDC) - Amount.new("5.00", :USD)
|
|
51
|
+
|
|
52
|
+
assert_equal Amount.new("5.00", :USDC), result
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_subtraction_different_type_raises_without_rate
|
|
56
|
+
assert_raises(Amount::TypeMismatch) do
|
|
57
|
+
Amount.new(1, :USDC) - Amount.new(1, :USD)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_asymmetric_rate_allows_configured_direction
|
|
62
|
+
Amount.register_default_rate :USD, :USDC, 1
|
|
63
|
+
|
|
64
|
+
assert_equal Amount.new("15.00", :USDC), Amount.new("10.00", :USDC) + Amount.new("5.00", :USD)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_asymmetric_rate_rejects_reverse_direction
|
|
68
|
+
Amount.register_default_rate :USD, :USDC, 1
|
|
69
|
+
|
|
70
|
+
assert_raises(Amount::TypeMismatch) do
|
|
71
|
+
Amount.new("5.00", :USD) + Amount.new("10.00", :USDC)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_unary_minus
|
|
76
|
+
assert_equal Amount.new("-1", :USDC), -Amount.new("1", :USDC)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_abs
|
|
80
|
+
assert_equal Amount.new("1", :USDC), Amount.new("-1", :USDC).abs
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# name => [expected, amount, factor]
|
|
84
|
+
{
|
|
85
|
+
"integer" => ["3", "1", 3],
|
|
86
|
+
"float" => ["1.5", "1", 1.5],
|
|
87
|
+
"negative_by_negative" => ["2", "-1", -2],
|
|
88
|
+
"bigdecimal" => ["3", "2", BigDecimal("1.5")],
|
|
89
|
+
"rational" => ["3", "2", Rational(3, 2)]
|
|
90
|
+
}.each do |name, (expected, amount, factor)|
|
|
91
|
+
define_method("test_scalar_multiplication_by_#{name}") do
|
|
92
|
+
assert_equal Amount.new(expected, :USDC), Amount.new(amount, :USDC) * factor
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_amount_times_amount_raises
|
|
97
|
+
assert_raises(Amount::TypeMismatch) do
|
|
98
|
+
Amount.new("1", :USDC) * Amount.new("1", :USDC)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# name => [expected, amount, divisor]
|
|
103
|
+
{
|
|
104
|
+
"integer" => ["0.5", "1", 2],
|
|
105
|
+
"negative_float" => ["2", "-1", -0.5],
|
|
106
|
+
"rational" => ["2", "3", Rational(3, 2)],
|
|
107
|
+
"bigdecimal" => ["2", "3", BigDecimal("1.5")]
|
|
108
|
+
}.each do |name, (expected, amount, divisor)|
|
|
109
|
+
define_method("test_scalar_division_by_#{name}_returns_amount") do
|
|
110
|
+
assert_equal Amount.new(expected, :USDC), Amount.new(amount, :USDC) / divisor
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_amount_division_returns_ratio
|
|
115
|
+
ratio = Amount.new("10", :USDC) / Amount.new("2", :USDC)
|
|
116
|
+
|
|
117
|
+
assert_equal BigDecimal(5), ratio
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_division_by_zero
|
|
121
|
+
assert_raises(ZeroDivisionError) { Amount.new(1, :USDC) / 0 }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def test_division_by_integer
|
|
125
|
+
assert_equal Amount.new("5", :USDC), Amount.new("10", :USDC) / 2
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_division_by_integer_truncates_toward_zero
|
|
129
|
+
assert_equal Amount.new(1, :USDC, from: :atomic), Amount.new(3, :USDC, from: :atomic) / 2
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def test_division_of_negative_amount_truncates_toward_zero
|
|
133
|
+
assert_equal Amount.new(-1, :USDC, from: :atomic), Amount.new(-3, :USDC, from: :atomic) / 2
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def test_division_by_decimal
|
|
137
|
+
assert_equal Amount.new("4", :USDC), Amount.new("10", :USDC) / BigDecimal("2.5")
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def test_division_of_large_amount_by_integer_is_exact
|
|
141
|
+
assert_equal Amount.new((10**38) - 1, :SOL, from: :atomic),
|
|
142
|
+
Amount.new((3 * (10**38)) - 1, :SOL, from: :atomic) / 3
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# BigDecimal division rounded this quotient, which came out 1429 atomic
|
|
146
|
+
# units off.
|
|
147
|
+
def test_division_of_large_amount_by_small_decimal_is_exact
|
|
148
|
+
assert_equal Amount.new(1_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571, :SOL, from: :atomic),
|
|
149
|
+
Amount.new(10**40, :SOL, from: :atomic) / BigDecimal("0.000000000000000000007")
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def test_division_of_large_negative_amount_by_small_decimal_is_exact
|
|
153
|
+
assert_equal Amount.new(-1_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571, :SOL, from: :atomic),
|
|
154
|
+
Amount.new(-(10**40), :SOL, from: :atomic) / BigDecimal("0.000000000000000000007")
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def test_cross_type_addition_via_explicit_conversion
|
|
158
|
+
gold = Amount.new("0.0001", :GOLD)
|
|
159
|
+
usdc = Amount.new("100", :USDC)
|
|
160
|
+
result = gold + usdc.to(:GOLD, rate: "0.00000042")
|
|
161
|
+
|
|
162
|
+
assert_equal BigDecimal("0.000142"), result.decimal
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def test_arithmetic_returns_subclass
|
|
166
|
+
register_gold_subclass!
|
|
167
|
+
gold = GoldAmount.new("1", :GOLD)
|
|
168
|
+
|
|
169
|
+
assert_instance_of GoldAmount, gold + gold
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
private
|
|
173
|
+
|
|
174
|
+
def frozen_sum
|
|
175
|
+
Amount.new("1", :USDC).freeze + Amount.new("2", :USDC).freeze
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Replaces the default registry with GOLD bound to GoldAmount.
|
|
179
|
+
def register_gold_subclass!
|
|
180
|
+
Amount.registry.clear!
|
|
181
|
+
Amount.register :GOLD, decimals: 8, display_symbol: "oz t",
|
|
182
|
+
display_position: :suffix, ui_decimals: 4,
|
|
183
|
+
class: GoldAmount
|
|
184
|
+
end
|
|
185
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
|
|
5
|
+
class AmountCoercibleTest < Minitest::Test
|
|
6
|
+
def setup
|
|
7
|
+
AmountTestSupport.register_default_types!
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def teardown
|
|
11
|
+
Amount.registry.clear!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_coerce_decimal_returns_big_decimal_unchanged
|
|
15
|
+
value = BigDecimal("1.25")
|
|
16
|
+
|
|
17
|
+
assert_same value, Amount.coerce_decimal(value)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_coerce_decimal_converts_integer
|
|
21
|
+
assert_equal BigDecimal("3"), Amount.coerce_decimal(3)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_coerce_decimal_converts_string
|
|
25
|
+
assert_equal BigDecimal("1.25"), Amount.coerce_decimal("1.25")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_coerce_decimal_converts_rational
|
|
29
|
+
assert_equal BigDecimal("1.5"), Amount.coerce_decimal(Rational(3, 2))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_coerce_decimal_converts_float_through_its_string_form
|
|
33
|
+
assert_equal BigDecimal("0.1"), Amount.coerce_decimal(0.1)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_cross_type_addition_without_rate_names_both_types
|
|
37
|
+
error = error_from(Amount::TypeMismatch) { Amount.of_usdc("1") + Amount.of_sol("1") }
|
|
38
|
+
|
|
39
|
+
assert_equal "type mismatch: USDC vs SOL", error.message
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_adding_a_non_amount_raises_type_mismatch
|
|
43
|
+
assert_raises(Amount::TypeMismatch) { Amount.of_usdc("1") + 1 }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_adding_a_non_amount_names_its_class
|
|
47
|
+
assert_match(/vs Integer\z/, error_from(Amount::TypeMismatch) { Amount.of_usdc("1") + 1 }.message)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
|
|
5
|
+
class AmountConstructibleTest < Minitest::Test
|
|
6
|
+
class GoldAmount < Amount
|
|
7
|
+
def purity_estimate
|
|
8
|
+
"24k"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def setup
|
|
13
|
+
AmountTestSupport.register_default_types!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def teardown
|
|
17
|
+
Amount.registry.clear!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_construct_from_integer_treats_as_atomic
|
|
21
|
+
assert_equal 1_000_000, Amount.new(1_000_000, :USDC).atomic
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_construct_from_integer_gives_matching_decimal
|
|
25
|
+
assert_equal BigDecimal("1"), Amount.new(1_000_000, :USDC).decimal
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_construct_from_string_treats_as_ui
|
|
29
|
+
assert_equal 1_500_000, Amount.new("1.50", :USDC).atomic
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_construct_from_float_treats_as_decimal
|
|
33
|
+
assert_equal 1_500_000, Amount.new(1.5, :USDC).atomic
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_construct_from_rational_treats_as_ui
|
|
37
|
+
assert_equal 1_500_000, Amount.new(Rational(3, 2), :USDC).atomic
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_construct_from_rational_is_exact_for_repeating_fractions
|
|
41
|
+
# 1/3 has no finite decimal expansion. With six decimals of storage
|
|
42
|
+
# the atomic representation is 333333 (truncated toward zero).
|
|
43
|
+
assert_equal 333_333, Amount.new(Rational(1, 3), :USDC).atomic
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_construct_from_rational_with_explicit_ui_from
|
|
47
|
+
assert_equal 1_500_000, Amount.new(Rational(3, 2), :USDC, from: :ui).atomic
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_explicit_from_overrides_inference_for_atomic
|
|
51
|
+
assert_equal 1_500_000, Amount.new("1500000", :USDC, from: :atomic).atomic
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_explicit_from_overrides_inference_for_decimal
|
|
55
|
+
assert_equal BigDecimal("1.5"), Amount.new("1500000", :USDC, from: :atomic).decimal
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_subclass_instance_from_own_new
|
|
59
|
+
use_gold_amount_registry!
|
|
60
|
+
|
|
61
|
+
assert_instance_of GoldAmount, GoldAmount.new("1", :GOLD)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_subclass_instance_from_own_new_has_subclass_behavior
|
|
65
|
+
use_gold_amount_registry!
|
|
66
|
+
|
|
67
|
+
assert_equal "24k", GoldAmount.new("1", :GOLD).purity_estimate
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_amount_new_dispatches_to_registered_subclass_from_ui
|
|
71
|
+
use_gold_amount_registry!
|
|
72
|
+
|
|
73
|
+
assert_instance_of GoldAmount, Amount.new("1", :GOLD)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_amount_new_dispatches_to_registered_subclass_from_atomic
|
|
77
|
+
use_gold_amount_registry!
|
|
78
|
+
|
|
79
|
+
assert_instance_of GoldAmount, Amount.new(100_000_000, :GOLD, from: :atomic)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_explicit_wrong_subclass_still_raises
|
|
83
|
+
use_gold_amount_registry!
|
|
84
|
+
other = Class.new(Amount)
|
|
85
|
+
|
|
86
|
+
assert_raises(Amount::InvalidInput) { other.new("1", :GOLD) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_explicit_wrong_subclass_message_names_registered_class
|
|
90
|
+
use_gold_amount_registry!
|
|
91
|
+
other = Class.new(Amount)
|
|
92
|
+
|
|
93
|
+
assert_match(/use AmountConstructibleTest::GoldAmount\.new for GOLD/,
|
|
94
|
+
error_from(Amount::InvalidInput) { other.new("1", :GOLD) }.message)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_amount_new_with_default_class_is_unchanged
|
|
98
|
+
use_gold_amount_registry!
|
|
99
|
+
Amount.register :USDC, decimals: 6
|
|
100
|
+
|
|
101
|
+
assert_instance_of Amount, Amount.new("1", :USDC)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
# Replaces the default types with GOLD registered to {GoldAmount}.
|
|
107
|
+
def use_gold_amount_registry!
|
|
108
|
+
Amount.registry.clear!
|
|
109
|
+
Amount.register :GOLD, decimals: 8, display_symbol: "oz t",
|
|
110
|
+
display_position: :suffix, ui_decimals: 4,
|
|
111
|
+
class: GoldAmount
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require_relative "../../test_helper"
|
|
5
|
+
|
|
6
|
+
class AmountConvertibleTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
AmountTestSupport.register_default_types!
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def teardown
|
|
12
|
+
Amount.registry.clear!
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_to_same_symbol_is_identity
|
|
16
|
+
amount = Amount.new("1", :USDC)
|
|
17
|
+
|
|
18
|
+
assert_equal amount, amount.to(:USDC)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_to_with_explicit_rate_converts_value
|
|
22
|
+
assert_equal BigDecimal("0.42"), Amount.new("1000", :USDC).to(:GOLD, rate: "0.00042").decimal
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_to_with_explicit_rate_sets_target_symbol
|
|
26
|
+
assert_equal :GOLD, Amount.new("1000", :USDC).to(:GOLD, rate: "0.00042").symbol
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_to_with_rational_rate_converts_value
|
|
30
|
+
assert_equal BigDecimal("2"), Amount.new("4", :USDC).to(:USD, rate: Rational(1, 2)).decimal
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_to_with_rational_rate_sets_target_symbol
|
|
34
|
+
assert_equal :USD, Amount.new("4", :USDC).to(:USD, rate: Rational(1, 2)).symbol
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_to_without_rate_requires_default
|
|
38
|
+
assert_raises(Amount::Registry::NoDefaultRate) do
|
|
39
|
+
Amount.new("1", :USDC).to(:GOLD)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_to_uses_registered_default_rate_value
|
|
44
|
+
Amount.register_default_rate :USDC, :USD, 1
|
|
45
|
+
|
|
46
|
+
assert_equal BigDecimal("1.5"), Amount.new("1.5", :USDC).to(:USD).decimal
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_to_uses_registered_default_rate_symbol
|
|
50
|
+
Amount.register_default_rate :USDC, :USD, 1
|
|
51
|
+
|
|
52
|
+
assert_equal :USD, Amount.new("1.5", :USDC).to(:USD).symbol
|
|
53
|
+
end
|
|
54
|
+
end
|