1
0
Fork 0
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:
Zach Gollwitzer 2024-03-01 17:17:34 -05:00 committed by GitHub
parent 89ea12e9a1
commit 0fe9b6d34a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 228 additions and 161 deletions

45
test/models/money_test.rb Normal file
View 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