1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00

Multi-currency support: Money + Currency class improvements (#553)

* Money improvements

* Replace all old money usage
This commit is contained in:
Zach Gollwitzer 2024-03-18 11:21:00 -04:00 committed by GitHub
parent e5750d1a13
commit fe2fa0eac1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 2982 additions and 196 deletions

View file

@ -38,15 +38,15 @@ class FamilyTest < ActiveSupport::TestCase
end
test "should calculate total assets" do
assert_equal BigDecimal("25550"), @family.assets
assert_equal Money.new(25550), @family.assets_money
end
test "should calculate total liabilities" do
assert_equal BigDecimal("1000"), @family.liabilities
assert_equal Money.new(1000), @family.liabilities_money
end
test "should calculate net worth" do
assert_equal BigDecimal("24550"), @family.net_worth
assert_equal Money.new(24550), @family.net_worth_money
end
test "should calculate snapshot correctly" do

View file

@ -1,45 +0,0 @@
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