spec/money/formatting_spec.rb in money-6.11.0 vs spec/money/formatting_spec.rb in money-6.11.1

- old
+ new

@@ -22,11 +22,11 @@ before :each do reset_i18n I18n.locale = :de I18n.backend.store_translations( :de, - :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } } + number: { currency: { format: { delimiter: ".", separator: "," } } } ) Money.use_i18n = false end after :each do @@ -56,11 +56,11 @@ before :each do reset_i18n I18n.locale = :de I18n.backend.store_translations( :de, - :number => { :format => { :delimiter => ".", :separator => "," } } + number: { format: { delimiter: ".", separator: "," } } ) end subject(:money) { Money.empty("USD") } @@ -77,11 +77,11 @@ before :each do reset_i18n I18n.locale = :de I18n.backend.store_translations( :de, - :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } } + number: { currency: { format: { delimiter: ".", separator: "," } } } ) end subject(:money) { Money.empty("USD") } @@ -98,28 +98,28 @@ before :each do reset_i18n I18n.locale = :de I18n.backend.store_translations( :de, - :number => { :currency => { :symbol => { :CAD => "CAD$" } } } + number: { currency: { symbol: { CAD: "CAD$" } } } ) end subject(:money) { Money.empty("CAD") } it "should use 'CAD$' as the currency symbol" do - expect(money.format(:translate => true)).to eq("CAD$0.00") + expect(money.format(translate: true)).to eq("CAD$0.00") end end context "with overridden i18n settings" do it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do - expect(Money.new(300_000, 'ISK').format(:thousands_separator => ".", decimal_mark: ',')).to eq "kr300.000" + expect(Money.new(300_000, 'ISK').format(thousands_separator: ".", decimal_mark: ',')).to eq "kr300.000" end it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do - expect(Money.new(300_000, 'USD').format(:thousands_separator => ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000" + expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000" end end end describe "#format" do @@ -127,11 +127,11 @@ before { @_locale = I18n.locale; I18n.locale = :ja } it "formats Japanese currency in Japanese properly" do money = Money.new(1000, "JPY") expect(money.format).to eq "1,000円" - expect(money.format(:symbol => false)).to eq "1,000" + expect(money.format(symbol: false)).to eq "1,000" end after { I18n.locale = @_locale } end @@ -142,12 +142,22 @@ it "respects :subunit_to_unit currency property" do expect(Money.new(10_00, "BHD").format).to eq "ب.د1.000" end - it "does not display a decimal when :subunit_to_unit is 1" do - expect(Money.new(10_00, "VUV").format).to eq "Vt1,000" + context "when :subunit_to_unit is 1" do + it "does not display a decimal part" do + expect(Money.new(10_00, "VUV").format).to eq "Vt1,000" + end + + it "does not displays a decimal part when infinite_precision is false" do + expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000" + end + + it "displays a decimal part when infinite_precision is true", :infinite_precision do + expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000.1" + end end it "respects the thousands_separator and decimal_mark defaults" do one_thousand = Proc.new do |currency| Money.new(1000_00, currency).format @@ -184,21 +194,21 @@ expect(one_thousand["GHC"]).to eq "₵1,000.00" end it "inserts commas into the result if the amount is sufficiently large" do expect(Money.us_dollar(1_000_000_000_12).format).to eq "$1,000,000,000.12" - expect(Money.us_dollar(1_000_000_000_12).format(:no_cents => true)).to eq "$1,000,000,000" + expect(Money.us_dollar(1_000_000_000_12).format(no_cents: true)).to eq "$1,000,000,000" end it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do expect(Money.euro(1_234_567_12).format).to eq "€1.234.567,12" - expect(Money.euro(1_234_567_12).format(:no_cents => true)).to eq "€1.234.567" + expect(Money.euro(1_234_567_12).format(no_cents: true)).to eq "€1.234.567" end context 'when default_formatting_rules defines (display_free: true)' do before do - Money.default_formatting_rules = { :display_free => "you won't pay a thing" } + Money.default_formatting_rules = { display_free: "you won't pay a thing" } end after do Money.default_formatting_rules = nil end @@ -209,11 +219,11 @@ end end context 'with rule (display_free: false) provided' do it 'acknowledges provided rule' do - expect(Money.new(0, 'USD').format(:display_free => false)).to eq '$0.00' + expect(Money.new(0, 'USD').format(display_free: false)).to eq '$0.00' end end end context 'when default_formatting_rules is not defined' do @@ -221,83 +231,83 @@ Money.default_formatting_rules = nil end context 'acknowledges provided rule' do it 'acknowledges provided rule' do - expect(Money.new(100, 'USD').format(:with_currency => true)).to eq '$1.00 USD' + expect(Money.new(100, 'USD').format(with_currency: true)).to eq '$1.00 USD' end end end describe ":with_currency option" do specify "(:with_currency option => true) works as documented" do - expect(Money.ca_dollar(100).format(:with_currency => true)).to eq "$1.00 CAD" - expect(Money.us_dollar(85).format(:with_currency => true)).to eq "$0.85 USD" + expect(Money.ca_dollar(100).format(with_currency: true)).to eq "$1.00 CAD" + expect(Money.us_dollar(85).format(with_currency: true)).to eq "$0.85 USD" end end describe ":no_cents option" do specify "(:with_currency option => true) works as documented" do - expect(Money.ca_dollar(100).format(:no_cents => true)).to eq "$1" - expect(Money.ca_dollar(599).format(:no_cents => true)).to eq "$5" - expect(Money.ca_dollar(570).format(:no_cents => true, :with_currency => true)).to eq "$5 CAD" - expect(Money.ca_dollar(39000).format(:no_cents => true)).to eq "$390" + expect(Money.ca_dollar(100).format(no_cents: true)).to eq "$1" + expect(Money.ca_dollar(599).format(no_cents: true)).to eq "$5" + expect(Money.ca_dollar(570).format(no_cents: true, with_currency: true)).to eq "$5 CAD" + expect(Money.ca_dollar(39000).format(no_cents: true)).to eq "$390" end it "respects :subunit_to_unit currency property" do - expect(Money.new(10_00, "BHD").format(:no_cents => true)).to eq "ب.د1" + expect(Money.new(10_00, "BHD").format(no_cents: true)).to eq "ب.د1" end it "inserts thousand separators if symbol contains decimal mark and no_cents is true" do - expect(Money.new(100000000, "AMD").format(:no_cents => true)).to eq "1,000,000 դր." - expect(Money.new(100000000, "USD").format(:no_cents => true)).to eq "$1,000,000" - expect(Money.new(100000000, "RUB").format(:no_cents => true)).to eq "1.000.000 ₽" + expect(Money.new(100000000, "AMD").format(no_cents: true)).to eq "1,000,000 դր." + expect(Money.new(100000000, "USD").format(no_cents: true)).to eq "$1,000,000" + expect(Money.new(100000000, "RUB").format(no_cents: true)).to eq "1.000.000 ₽" end it "doesn't incorrectly format HTML" do money = ::Money.new(1999, "RUB") - output = money.format(:html => true, :no_cents => true) + output = money.format(html: true, no_cents: true) expect(output).to eq "19 &#x20BD;" end end describe ":no_cents_if_whole option" do - specify "(:no_cents_if_whole => true) works as documented" do - expect(Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false)).to eq "10,000" - expect(Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false)).to eq "10,034" - expect(Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false)).to eq "2,000" - expect(Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false)).to eq "2,006.8" - expect(Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.000" - expect(Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.034" - expect(Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false)).to eq "100" - expect(Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false)).to eq "100.34" - expect(Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false)).to eq "10" - expect(Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.034" + specify "(no_cents_if_whole: true) works as documented" do + expect(Money.new(10000, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,000" + expect(Money.new(10034, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,034" + expect(Money.new(10000, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,000" + expect(Money.new(10034, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,006.8" + expect(Money.new(10000, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.000" + expect(Money.new(10034, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.034" + expect(Money.new(10000, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100" + expect(Money.new(10034, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100.34" + expect(Money.new(10000, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10" + expect(Money.new(10034, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10.034" end - specify "(:no_cents_if_whole => false) works as documented" do - expect(Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false)).to eq "10,000" - expect(Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false)).to eq "10,034" - expect(Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false)).to eq "2,000.0" - expect(Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false)).to eq "2,006.8" - expect(Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.000" - expect(Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.034" - expect(Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false)).to eq "100.00" - expect(Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false)).to eq "100.34" - expect(Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.000" - expect(Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.034" + specify "(no_cents_if_whole: false) works as documented" do + expect(Money.new(10000, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,000" + expect(Money.new(10034, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,034" + expect(Money.new(10000, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,000.0" + expect(Money.new(10034, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,006.8" + expect(Money.new(10000, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.000" + expect(Money.new(10034, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.034" + expect(Money.new(10000, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.00" + expect(Money.new(10034, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.34" + expect(Money.new(10000, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.000" + expect(Money.new(10034, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.034" end end describe ":symbol option" do - specify "(:symbol => a symbol string) uses the given value as the money symbol" do - expect(Money.new(100, "GBP").format(:symbol => "£")).to eq "£1.00" + specify "(symbol: a symbol string) uses the given value as the money symbol" do + expect(Money.new(100, "GBP").format(symbol: "£")).to eq "£1.00" end - specify "(:symbol => true) returns symbol based on the given currency code" do + specify "(symbol: true) returns symbol based on the given currency code" do one = Proc.new do |currency| - Money.new(100, currency).format(:symbol => true) + Money.new(100, currency).format(symbol: true) end # Pounds expect(one["GBP"]).to eq "£1.00" @@ -330,30 +340,30 @@ # Other expect(one["SEK"]).to eq "1,00 kr" expect(one["GHC"]).to eq "₵1.00" end - specify "(:symbol => true) returns $ when currency code is not recognized" do + specify "(symbol: true) returns $ when currency code is not recognized" do currency = Money::Currency.new("EUR") expect(currency).to receive(:symbol).and_return(nil) - expect(Money.new(100, currency).format(:symbol => true)).to eq "¤1,00" + expect(Money.new(100, currency).format(symbol: true)).to eq "¤1,00" end - specify "(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do - expect(Money.new(100, "GBP").format(:symbol => true)).to eq "£1.00" - expect(Money.new(100, "EUR").format(:symbol => true)).to eq "€1,00" - expect(Money.new(100, "SEK").format(:symbol => true)).to eq "1,00 kr" + specify "(symbol: some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do + expect(Money.new(100, "GBP").format(symbol: true)).to eq "£1.00" + expect(Money.new(100, "EUR").format(symbol: true)).to eq "€1,00" + expect(Money.new(100, "SEK").format(symbol: true)).to eq "1,00 kr" end - specify "(:symbol => "", nil or false) returns the amount without a symbol" do + specify "(symbol: "", nil or false) returns the amount without a symbol" do money = Money.new(100, "GBP") - expect(money.format(:symbol => "")).to eq "1.00" - expect(money.format(:symbol => nil)).to eq "1.00" - expect(money.format(:symbol => false)).to eq "1.00" + expect(money.format(symbol: "")).to eq "1.00" + expect(money.format(symbol: nil)).to eq "1.00" + expect(money.format(symbol: false)).to eq "1.00" money = Money.new(100, "JPY") - expect(money.format(:symbol => false)).to eq "100" + expect(money.format(symbol: false)).to eq "100" end it "defaults :symbol to true" do money = Money.new(100) expect(money.format).to eq "$1.00" @@ -363,223 +373,223 @@ money = Money.new(100, "EUR") expect(money.format).to eq "€1,00" end - specify "(:symbol => false) returns a signed amount without a symbol" do + specify "(symbol: false) returns a signed amount without a symbol" do money = Money.new(-100, "EUR") - expect(money.format(:symbol => false)).to eq "-1,00" + expect(money.format(symbol: false)).to eq "-1,00" money = Money.new(100, "EUR") - expect(money.format(:symbol => false, - :sign_positive => true)).to eq "+1,00" + expect(money.format(symbol: false, + sign_positive: true)).to eq "+1,00" end end describe ":decimal_mark option" do - specify "(:decimal_mark => a decimal_mark string) works as documented" do - expect(Money.us_dollar(100).format(:decimal_mark => ",")).to eq "$1,00" + specify "(decimal_mark: a decimal_mark string) works as documented" do + expect(Money.us_dollar(100).format(decimal_mark: ",")).to eq "$1,00" end it "defaults to '.' if currency isn't recognized" do expect(Money.new(100, "ZWD").format).to eq "$1.00" end end describe ":separator option" do - specify "(:separator => a separator string) works as documented" do - expect(Money.us_dollar(100).format(:separator => ",")).to eq "$1,00" + specify "(separator: a separator string) works as documented" do + expect(Money.us_dollar(100).format(separator: ",")).to eq "$1,00" end end describe ":south_asian_number_formatting delimiter" do before(:each) do - Money::Currency.register(JSON.parse(INDIAN_BAR, :symbolize_names => true)) + Money::Currency.register(JSON.parse(INDIAN_BAR, symbolize_names: true)) end after(:each) do - Money::Currency.unregister(JSON.parse(INDIAN_BAR, :symbolize_names => true)) + Money::Currency.unregister(JSON.parse(INDIAN_BAR, symbolize_names: true)) end - specify "(:south_asian_number_formatting => true) works as documented" do - expect(Money.new(10000000, 'INR').format(:south_asian_number_formatting => true, :symbol => false)).to eq "1,00,000.00" - expect(Money.new(1000000000, 'INDIAN_BAR').format(:south_asian_number_formatting => true, :symbol => false)).to eq "1,00,000.0000" - expect(Money.new(10000000).format(:south_asian_number_formatting => true)).to eq "$1,00,000.00" + specify "(south_asian_number_formatting: true) works as documented" do + expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.00" + expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.0000" + expect(Money.new(10000000).format(south_asian_number_formatting: true)).to eq "$1,00,000.00" end - specify "(:south_asian_number_formatting => true and no_cents_if_whole => true) works as documented" do - expect(Money.new(10000000, 'INR').format(:south_asian_number_formatting => true, :symbol => false, :no_cents_if_whole => true)).to eq "1,00,000" - expect(Money.new(1000000000, 'INDIAN_BAR').format(:south_asian_number_formatting => true, :symbol => false, :no_cents_if_whole => true)).to eq "1,00,000" + specify "(south_asian_number_formatting: true and no_cents_if_whole => true) works as documented" do + expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000" + expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000" end end describe ":thousands_separator option" do - specify "(:thousands_separator => a thousands_separator string) works as documented" do - expect(Money.us_dollar(100000).format(:thousands_separator => ".")).to eq "$1.000.00" - expect(Money.us_dollar(200000).format(:thousands_separator => "")).to eq "$2000.00" + specify "(thousands_separator: a thousands_separator string) works as documented" do + expect(Money.us_dollar(100000).format(thousands_separator: ".")).to eq "$1.000.00" + expect(Money.us_dollar(200000).format(thousands_separator: "")).to eq "$2000.00" end - specify "(:thousands_separator => false or nil) works as documented" do - expect(Money.us_dollar(100000).format(:thousands_separator => false)).to eq "$1000.00" - expect(Money.us_dollar(200000).format(:thousands_separator => nil)).to eq "$2000.00" + specify "(thousands_separator: false or nil) works as documented" do + expect(Money.us_dollar(100000).format(thousands_separator: false)).to eq "$1000.00" + expect(Money.us_dollar(200000).format(thousands_separator: nil)).to eq "$2000.00" end - specify "(:delimiter => a delimiter string) works as documented" do - expect(Money.us_dollar(100000).format(:delimiter => ".")).to eq "$1.000.00" - expect(Money.us_dollar(200000).format(:delimiter => "")).to eq "$2000.00" + specify "(delimiter: a delimiter string) works as documented" do + expect(Money.us_dollar(100000).format(delimiter: ".")).to eq "$1.000.00" + expect(Money.us_dollar(200000).format(delimiter: "")).to eq "$2000.00" end - specify "(:delimiter => false or nil) works as documented" do - expect(Money.us_dollar(100000).format(:delimiter => false)).to eq "$1000.00" - expect(Money.us_dollar(200000).format(:delimiter => nil)).to eq "$2000.00" + specify "(delimiter: false or nil) works as documented" do + expect(Money.us_dollar(100000).format(delimiter: false)).to eq "$1000.00" + expect(Money.us_dollar(200000).format(delimiter: nil)).to eq "$2000.00" end it "defaults to ',' if currency isn't recognized" do expect(Money.new(100000, "ZWD").format).to eq "$1,000.00" end context "without i18n" do before { Money.use_i18n = false } it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do - expect(Money.new(300_000, 'ISK').format(:thousands_separator => ",", decimal_mark: '.')).to eq "kr300,000" + expect(Money.new(300_000, 'ISK').format(thousands_separator: ",", decimal_mark: '.')).to eq "kr300,000" end it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do - expect(Money.new(300_000, 'USD').format(:thousands_separator => ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000" + expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000" end after { Money.use_i18n = true} end end describe ":thousands_separator and :decimal_mark option" do - specify "(:thousands_separator => a thousands_separator string, :decimal_mark => a decimal_mark string) works as documented" do + specify "(thousands_separator: a thousands_separator string, decimal_mark: a decimal_mark string) works as documented" do expect(Money.new(123_456_789, "USD").format(thousands_separator: ".", decimal_mark: ",")).to eq("$1.234.567,89") expect(Money.new(987_654_321, "USD").format(thousands_separator: " ", decimal_mark: ".")).to eq("$9 876 543.21") end end describe ":html option" do - specify "(:html => true) works as documented" do - string = Money.ca_dollar(570).format(:html => true, :with_currency => true) + specify "(html: true) works as documented" do + string = Money.ca_dollar(570).format(html: true, with_currency: true) expect(string).to eq "$5.70 <span class=\"currency\">CAD</span>" end specify "should fallback to symbol if entity is not available" do - string = Money.new(570, 'DKK').format(:html => true) + string = Money.new(570, 'DKK').format(html: true) expect(string).to eq "5,70 kr." end end describe ":html_wrap_symbol option" do - specify "(:html_wrap_symbol => true) works as documented" do - string = Money.ca_dollar(570).format(:html_wrap_symbol => true) + specify "(html_wrap_symbol: true) works as documented" do + string = Money.ca_dollar(570).format(html_wrap_symbol: true) expect(string).to eq "<span class=\"currency_symbol\">$</span>5.70" end end describe ":symbol_position option" do it "inserts currency symbol before the amount when set to :before" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :before)).to eq "€1.234.567,12" + expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12" end it "inserts currency symbol after the amount when set to :after" do - expect(Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after)).to eq "1,000,000,000.12 $" + expect(Money.us_dollar(1_000_000_000_12).format(symbol_position: :after)).to eq "1,000,000,000.12 $" end it "raises an ArgumentError when passed an invalid option" do - expect{Money.euro(0).format(:symbol_position => :befor)}.to raise_error(ArgumentError) + expect{Money.euro(0).format(symbol_position: :befor)}.to raise_error(ArgumentError) end end describe ":sign_before_symbol option" do - specify "(:sign_before_symbol => true) works as documented" do - expect(Money.us_dollar(-100000).format(:sign_before_symbol => true)).to eq "-$1,000.00" + specify "(sign_before_symbol: true) works as documented" do + expect(Money.us_dollar(-100000).format(sign_before_symbol: true)).to eq "-$1,000.00" end - specify "(:sign_before_symbol => false) works as documented" do - expect(Money.us_dollar(-100000).format(:sign_before_symbol => false)).to eq "$-1,000.00" - expect(Money.us_dollar(-100000).format(:sign_before_symbol => nil)).to eq "$-1,000.00" + specify "(sign_before_symbol: false) works as documented" do + expect(Money.us_dollar(-100000).format(sign_before_symbol: false)).to eq "$-1,000.00" + expect(Money.us_dollar(-100000).format(sign_before_symbol: nil)).to eq "$-1,000.00" end end describe ":symbol_before_without_space option" do it "does not insert space between currency symbol and amount when set to true" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => true)).to eq "€1.234.567,12" + expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: true)).to eq "€1.234.567,12" end it "inserts space between currency symbol and amount when set to false" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => false)).to eq "€ 1.234.567,12" + expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: false)).to eq "€ 1.234.567,12" end it "defaults to true" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :before)).to eq "€1.234.567,12" + expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12" end end describe ":symbol_after_without_space option" do it "does not insert space between amount and currency symbol when set to true" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => true)).to eq "1.234.567,12€" + expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: true)).to eq "1.234.567,12€" end it "inserts space between amount and currency symbol when set to false" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => false)).to eq "1.234.567,12 €" + expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: false)).to eq "1.234.567,12 €" end it "defaults to false" do - expect(Money.euro(1_234_567_12).format(:symbol_position => :after)).to eq "1.234.567,12 €" + expect(Money.euro(1_234_567_12).format(symbol_position: :after)).to eq "1.234.567,12 €" end end describe ":sign_positive option" do - specify "(:sign_positive => true, :sign_before_symbol => true) works as documented" do - expect(Money.us_dollar( 0).format(:sign_positive => true, :sign_before_symbol => true)).to eq "$0.00" - expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => true)).to eq "+$1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => true)).to eq "-$1,000.00" + specify "(sign_positive: true, sign_before_symbol: true) works as documented" do + expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: true)).to eq "$0.00" + expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: true)).to eq "+$1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: true)).to eq "-$1,000.00" end - specify "(:sign_positive => true, :sign_before_symbol => false) works as documented" do - expect(Money.us_dollar( 0).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$0.00" - expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$+1,000.00" - expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => nil)).to eq "$+1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$-1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => nil)).to eq "$-1,000.00" + specify "(sign_positive: true, sign_before_symbol: false) works as documented" do + expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: false)).to eq "$0.00" + expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$+1,000.00" + expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$+1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$-1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$-1,000.00" end - specify "(:sign_positive => false, :sign_before_symbol => true) works as documented" do - expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => true)).to eq "$1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => true)).to eq "-$1,000.00" + specify "(sign_positive: false, sign_before_symbol: true) works as documented" do + expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: true)).to eq "$1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: true)).to eq "-$1,000.00" end - specify "(:sign_positive => false, :sign_before_symbol => false) works as documented" do - expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => false)).to eq "$1,000.00" - expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => nil)).to eq "$1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => false)).to eq "$-1,000.00" - expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => nil)).to eq "$-1,000.00" + specify "(sign_positive: false, sign_before_symbol: false) works as documented" do + expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$1,000.00" + expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$-1,000.00" + expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$-1,000.00" end end describe ":rounded_infinite_precision option", :infinite_precision do it "does round fractional when set to true" do - expect(Money.new(BigDecimal('12.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$0.12" - expect(Money.new(BigDecimal('12.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$0.13" - expect(Money.new(BigDecimal('123.1'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0.123" - expect(Money.new(BigDecimal('123.5'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0.124" - expect(Money.new(BigDecimal('100.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$1.00" - expect(Money.new(BigDecimal('109.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$1.10" - expect(Money.new(BigDecimal('1.7'), "MGA").format(:rounded_infinite_precision => true)).to eq "Ar0.4" + expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0.12" + expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0.13" + expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.123" + expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.124" + expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1.00" + expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1.10" + expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0.4" end it "does not round fractional when set to false" do - expect(Money.new(BigDecimal('12.1'), "USD").format(:rounded_infinite_precision => false)).to eq "$0.121" - expect(Money.new(BigDecimal('12.5'), "USD").format(:rounded_infinite_precision => false)).to eq "$0.125" - expect(Money.new(BigDecimal('123.1'), "BHD").format(:rounded_infinite_precision => false)).to eq "ب.د0.1231" - expect(Money.new(BigDecimal('123.5'), "BHD").format(:rounded_infinite_precision => false)).to eq "ب.د0.1235" - expect(Money.new(BigDecimal('100.1'), "USD").format(:rounded_infinite_precision => false)).to eq "$1.001" - expect(Money.new(BigDecimal('109.5'), "USD").format(:rounded_infinite_precision => false)).to eq "$1.095" - expect(Money.new(BigDecimal('1.7'), "MGA").format(:rounded_infinite_precision => false)).to eq "Ar0.34" + expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: false)).to eq "$0.121" + expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: false)).to eq "$0.125" + expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1231" + expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1235" + expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: false)).to eq "$1.001" + expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: false)).to eq "$1.095" + expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: false)).to eq "Ar0.34" end describe "with i18n = false" do before do Money.use_i18n = false @@ -588,76 +598,76 @@ after do Money.use_i18n = true end it 'does round fractional when set to true' do - expect(Money.new(BigDecimal('12.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€0,12" - expect(Money.new(BigDecimal('12.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€0,13" - expect(Money.new(BigDecimal('100.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1,00" - expect(Money.new(BigDecimal('109.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1,10" + expect(Money.new(BigDecimal('12.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,12" + expect(Money.new(BigDecimal('12.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,13" + expect(Money.new(BigDecimal('100.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,00" + expect(Money.new(BigDecimal('109.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,10" - expect(Money.new(BigDecimal('100012.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1.000,12" - expect(Money.new(BigDecimal('100012.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1.000,13" + expect(Money.new(BigDecimal('100012.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,12" + expect(Money.new(BigDecimal('100012.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,13" end end describe "with i18n = true" do before do Money.use_i18n = true reset_i18n I18n.locale = :de I18n.backend.store_translations( :de, - :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } } + number: { currency: { format: { delimiter: ".", separator: "," } } } ) end after do reset_i18n I18n.locale = :en end it 'does round fractional when set to true' do - expect(Money.new(BigDecimal('12.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$0,12" - expect(Money.new(BigDecimal('12.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$0,13" - expect(Money.new(BigDecimal('123.1'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0,123" - expect(Money.new(BigDecimal('123.5'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0,124" - expect(Money.new(BigDecimal('100.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$1,00" - expect(Money.new(BigDecimal('109.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$1,10" - expect(Money.new(BigDecimal('1'), "MGA").format(:rounded_infinite_precision => true)).to eq "Ar0,2" + expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0,12" + expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0,13" + expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,123" + expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,124" + expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1,00" + expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1,10" + expect(Money.new(BigDecimal('1'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0,2" end end end context "when the monetary value is 0" do let(:money) { Money.us_dollar(0) } it "returns 'free' when :display_free is true" do - expect(money.format(:display_free => true)).to eq 'free' + expect(money.format(display_free: true)).to eq 'free' end it "returns '$0.00' when :display_free is false or not given" do expect(money.format).to eq '$0.00' - expect(money.format(:display_free => false)).to eq '$0.00' - expect(money.format(:display_free => nil)).to eq '$0.00' + expect(money.format(display_free: false)).to eq '$0.00' + expect(money.format(display_free: nil)).to eq '$0.00' end it "returns the value specified by :display_free if it's a string-like object" do - expect(money.format(:display_free => 'gratis')).to eq 'gratis' + expect(money.format(display_free: 'gratis')).to eq 'gratis' end end end context "custom currencies with 4 decimal places" do before :each do - Money::Currency.register(JSON.parse(BAR, :symbolize_names => true)) - Money::Currency.register(JSON.parse(EU4, :symbolize_names => true)) + Money::Currency.register(JSON.parse(BAR, symbolize_names: true)) + Money::Currency.register(JSON.parse(EU4, symbolize_names: true)) end after :each do - Money::Currency.unregister(JSON.parse(BAR, :symbolize_names => true)) - Money::Currency.unregister(JSON.parse(EU4, :symbolize_names => true)) + Money::Currency.unregister(JSON.parse(BAR, symbolize_names: true)) + Money::Currency.unregister(JSON.parse(EU4, symbolize_names: true)) end it "respects custom subunit to unit, decimal and thousands separator" do expect(Money.new(4, "BAR").format).to eq "$0.0004" expect(Money.new(4, "EU4").format).to eq "€0,0004" @@ -742,21 +752,21 @@ formatted_results[format] = currency end end describe ":drop_trailing_zeros option" do - specify "(:drop_trailing_zeros => true) works as documented" do - expect(Money.new(89000, "BTC").format(:drop_trailing_zeros => true, :symbol => false)).to eq "0.00089" - expect(Money.new(100089000, "BTC").format(:drop_trailing_zeros => true, :symbol => false)).to eq "1.00089" - expect(Money.new(100000000, "BTC").format(:drop_trailing_zeros => true, :symbol => false)).to eq "1" - expect(Money.new(110, "AUD").format(:drop_trailing_zeros => true, :symbol => false)).to eq "1.1" + specify "(drop_trailing_zeros: true) works as documented" do + expect(Money.new(89000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089" + expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089" + expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1" + expect(Money.new(110, "AUD").format(drop_trailing_zeros: true, symbol: false)).to eq "1.1" end - specify "(:drop_trailing_zeros => false) works as documented" do - expect(Money.new(89000, "BTC").format(:drop_trailing_zeros => false, :symbol => false)).to eq "0.00089000" - expect(Money.new(100089000, "BTC").format(:drop_trailing_zeros => false, :symbol => false)).to eq "1.00089000" - expect(Money.new(100000000, "BTC").format(:drop_trailing_zeros => false, :symbol => false)).to eq "1.00000000" - expect(Money.new(110, "AUD").format(:drop_trailing_zeros => false, :symbol => false)).to eq "1.10" + specify "(drop_trailing_zeros: false) works as documented" do + expect(Money.new(89000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000" + expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000" + expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000" + expect(Money.new(110, "AUD").format(drop_trailing_zeros: false, symbol: false)).to eq "1.10" end end end end