mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
Add Money and Money Series (#505)
* Add Money class * Standardize creation of money series * Formatting * Fix test
This commit is contained in:
parent
89ea12e9a1
commit
0fe9b6d34a
16 changed files with 228 additions and 161 deletions
45
test/models/money_test.rb
Normal file
45
test/models/money_test.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require "test_helper"
|
||||
|
||||
class MoneyTest < ActiveSupport::TestCase
|
||||
test "#symbol returns the currency symbol for a given currency code" do
|
||||
assert_equal "$", Money.from_amount(0, "USD").symbol
|
||||
assert_equal "€", Money.from_amount(0, "EUR").symbol
|
||||
end
|
||||
|
||||
test "#separator returns the currency separator for a given currency code" do
|
||||
assert_equal ".", Money.from_amount(0, "USD").separator
|
||||
assert_equal ",", Money.from_amount(0, "EUR").separator
|
||||
end
|
||||
|
||||
test "#precision returns the currency's precision for a given currency code" do
|
||||
assert_equal 2, Money.from_amount(0, "USD").precision
|
||||
assert_equal 0, Money.from_amount(123.45, "KRW").precision
|
||||
end
|
||||
|
||||
test "#cents returns the cents part with 2 precisions by default" do
|
||||
assert_equal "45", Money.from_amount(123.45, "USD").cents
|
||||
end
|
||||
|
||||
test "#cents returns empty when precision is 0" do
|
||||
assert_equal "", Money.from_amount(123.45, "USD").cents(precision: 0)
|
||||
end
|
||||
|
||||
test "#cents returns the cents part of the string with given precision" do
|
||||
amount = Money.from_amount(123.4862, "USD")
|
||||
assert_equal "4", amount.cents(precision: 1)
|
||||
assert_equal "486", amount.cents(precision: 3)
|
||||
end
|
||||
|
||||
test "#cents pads the cents part with zeros up to the specified precision" do
|
||||
amount_without_decimal = Money.from_amount(123, "USD")
|
||||
amount_with_decimal = Money.from_amount(123.4, "USD")
|
||||
|
||||
assert_equal "00", amount_without_decimal.cents
|
||||
assert_equal "40", amount_with_decimal.cents
|
||||
end
|
||||
|
||||
test "works with BigDecimal" do
|
||||
amount = Money.from_amount(BigDecimal("123.45"), "USD")
|
||||
assert_equal "45", amount.cents
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue