diff --git a/.rubocop.yml b/.rubocop.yml index 3cc266ac69..d799642763 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -39,6 +39,14 @@ Lint/EmptyClass: # Allow defining empty classes, since it can be used as a base class. Enabled: false +# RSpec + +RSpec/DescribedClass: + Exclude: + # Prefer Money.new(…) to described_class.new(…) + - 'spec/money_spec.rb' + - 'spec/money/formatting_spec.rb' + # Style Style/ClassAndModuleChildren: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 239279aed4..03306a7d01 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -186,28 +186,6 @@ Naming/VariableNumber: Exclude: - 'spec/money_spec.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -RSpec/BeEq: - Exclude: - - 'spec/bank/base_spec.rb' - - 'spec/locale_backend/currency_spec.rb' - - 'spec/locale_backend/i18n_spec.rb' - - 'spec/money/arithmetic_spec.rb' - - 'spec/money_spec.rb' - -# This cop supports unsafe autocorrection (--autocorrect-all). -RSpec/BeEql: - Exclude: - - 'spec/bank/variable_exchange_spec.rb' - - 'spec/rates_store/memory_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: be_a, be_kind_of -RSpec/ClassCheck: - Exclude: - - 'spec/rates_store/memory_spec.rb' - # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without RSpec/ContextWording: @@ -223,66 +201,14 @@ RSpec/DescribeMethod: Exclude: - 'spec/money/formatting_spec.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants. -# SupportedStyles: described_class, explicit -RSpec/DescribedClass: - Exclude: - - 'spec/money/formatting_rules_spec.rb' - - 'spec/money/formatting_spec.rb' - - 'spec/money_spec.rb' - - 'spec/rates_store/memory_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterExample: - Exclude: - - 'spec/currency_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterFinalLet: - Exclude: - - 'spec/money/arithmetic_spec.rb' - - 'spec/money_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterSubject: - Exclude: - - 'spec/money/constructors_spec.rb' - # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 38 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: CustomTransform, IgnoredWords, DisallowedExamples. -# DisallowedExamples: works -RSpec/ExampleWording: - Exclude: - - 'spec/bank/variable_exchange_spec.rb' - - 'spec/currency/heuristics_spec.rb' - - 'spec/money/formatting_spec.rb' - - 'spec/money_spec.rb' - RSpec/ExpectInHook: Exclude: - 'spec/money_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: implicit, each, example -RSpec/HookArgument: - Exclude: - - 'spec/bank/variable_exchange_spec.rb' - - 'spec/money/formatting_spec.rb' - - 'spec/support/default_currency.rb' - - 'spec/support/i18n.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/HooksBeforeExamples: - Exclude: - - 'spec/money/formatting_spec.rb' - RSpec/IdenticalEqualityAssertion: Exclude: - 'spec/currency_spec.rb' @@ -297,14 +223,6 @@ RSpec/InstanceVariable: - 'spec/money/formatting_spec.rb' - 'spec/money_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -RSpec/LeadingSubject: - Exclude: - - 'spec/locale_backend/i18n_spec.rb' - - 'spec/money/formatting_spec.rb' - - 'spec/money_spec.rb' - - 'spec/support/shared_examples/money_examples.rb' - RSpec/LeakyConstantDeclaration: Exclude: - 'spec/bank/base_spec.rb' @@ -343,22 +261,6 @@ RSpec/NamedSubject: RSpec/NestedGroups: Max: 5 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: not_to, to_not -RSpec/NotToNot: - Exclude: - - 'spec/bank/base_spec.rb' - - 'spec/bank/variable_exchange_spec.rb' - - 'spec/currency_spec.rb' - - 'spec/money/arithmetic_spec.rb' - - 'spec/money_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/RedundantPredicateMatcher: - Exclude: - - 'spec/money/arithmetic_spec.rb' - RSpec/RepeatedDescription: Exclude: - 'spec/currency_spec.rb' diff --git a/spec/bank/base_spec.rb b/spec/bank/base_spec.rb index d4844a0dcd..a645b43680 100644 --- a/spec/bank/base_spec.rb +++ b/spec/bank/base_spec.rb @@ -27,7 +27,7 @@ def setup end bank = MyBank.new - expect(bank.setup_called).to eq true + expect(bank.setup_called).to be true end end @@ -39,19 +39,19 @@ def setup describe "#same_currency?" do it "accepts str/str" do - expect { subject.send(:same_currency?, 'USD', 'EUR') }.to_not raise_error + expect { subject.send(:same_currency?, 'USD', 'EUR') }.not_to raise_error end it "accepts currency/str" do - expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR') }.to_not raise_error + expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), 'EUR') }.not_to raise_error end it "accepts str/currency" do - expect { subject.send(:same_currency?, 'USD', Money::Currency.wrap('EUR')) }.to_not raise_error + expect { subject.send(:same_currency?, 'USD', Money::Currency.wrap('EUR')) }.not_to raise_error end it "accepts currency/currency" do - expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR')) }.to_not raise_error + expect { subject.send(:same_currency?, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR')) }.not_to raise_error end it "returns true when currencies match" do diff --git a/spec/bank/variable_exchange_spec.rb b/spec/bank/variable_exchange_spec.rb index 7a35924aee..a3db602c98 100644 --- a/spec/bank/variable_exchange_spec.rb +++ b/spec/bank/variable_exchange_spec.rb @@ -34,11 +34,11 @@ describe "#exchange_with" do it "accepts str" do - expect { bank.exchange_with(Money.new(100, 'USD'), 'EUR') }.to_not raise_error + expect { bank.exchange_with(Money.new(100, 'USD'), 'EUR') }.not_to raise_error end it "accepts currency" do - expect { bank.exchange_with(Money.new(100, 'USD'), Money::Currency.wrap('EUR')) }.to_not raise_error + expect { bank.exchange_with(Money.new(100, 'USD'), Money::Currency.wrap('EUR')) }.not_to raise_error end it "exchanges one currency to another" do @@ -109,7 +109,7 @@ describe "#add_rate" do it 'delegates to store#add_rate' do expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 - expect(subject.add_rate('USD', 'EUR', 1.25)).to eql 1.25 + expect(subject.add_rate('USD', 'EUR', 1.25)).to be 1.25 end it "adds rates with correct ISO codes" do @@ -129,7 +129,7 @@ describe "#set_rate" do it 'delegates to store#add_rate' do expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 - expect(subject.set_rate('USD', 'EUR', 1.25)).to eql 1.25 + expect(subject.set_rate('USD', 'EUR', 1.25)).to be 1.25 end it "sets a rate" do @@ -159,7 +159,7 @@ end describe "#export_rates" do - before :each do + before do subject.set_rate('USD', 'EUR', 1.25) subject.set_rate('USD', 'JPY', 2.55) @@ -167,20 +167,20 @@ end context "with format == :json" do - it "should return rates formatted as json" do + it "returns rates formatted as json" do json = subject.export_rates(:json) expect(JSON.load(json)).to eq @rates end end context "with format == :ruby" do - it "should return rates formatted as ruby objects" do + it "returns rates formatted as ruby objects" do expect(Marshal.load(subject.export_rates(:ruby))).to eq @rates end end context "with format == :yaml" do - it "should return rates formatted as yaml" do + it "returns rates formatted as yaml" do yaml = subject.export_rates(:yaml) expect(YAML.load(yaml)).to eq @rates end @@ -263,7 +263,7 @@ describe "#marshal_dump" do it "does not raise an error" do - expect { Marshal.dump(subject) }.to_not raise_error + expect { Marshal.dump(subject) }.not_to raise_error end it "works with Marshal.load" do diff --git a/spec/currency/heuristics_spec.rb b/spec/currency/heuristics_spec.rb index 64694d0104..2997fbd0ab 100644 --- a/spec/currency/heuristics_spec.rb +++ b/spec/currency/heuristics_spec.rb @@ -2,7 +2,7 @@ RSpec.describe Money::Currency::Heuristics do describe "#analyze_string" do - it "it raises deprecation error" do + it "raises deprecation error" do expect { Money::Currency.analyze('123') }.to raise_error(StandardError, 'Heuristics deprecated, add `gem "money-heuristics"` to Gemfile') end end diff --git a/spec/currency_spec.rb b/spec/currency_spec.rb index 0e00fbd62f..a87386da55 100644 --- a/spec/currency_spec.rb +++ b/spec/currency_spec.rb @@ -134,14 +134,17 @@ def to_s it "returns an array of currencies" do expect(described_class.all).to include described_class.new(:usd) end + it "includes registered currencies" do register_foo expect(described_class.all).to include described_class.new(:foo) unregister_foo end + it 'is sorted by priority' do expect(described_class.all.first.priority).to eq 1 end + it "raises a MissingAttributeError if any currency has no priority" do register_foo(skip: :priority) @@ -286,7 +289,7 @@ def to_s end it 'returns new object for the different :key' do - expect(described_class.new("USD")).to_not be(described_class.new("EUR")) + expect(described_class.new("USD")).not_to be(described_class.new("EUR")) end it 'is thread safe' do diff --git a/spec/locale_backend/currency_spec.rb b/spec/locale_backend/currency_spec.rb index 39c67b551d..5c86573df7 100644 --- a/spec/locale_backend/currency_spec.rb +++ b/spec/locale_backend/currency_spec.rb @@ -13,7 +13,7 @@ end it 'returns format based as defined in currency' do - expect(subject.lookup(:format, currency)).to eq(nil) + expect(subject.lookup(:format, currency)).to be_nil end end end diff --git a/spec/locale_backend/i18n_spec.rb b/spec/locale_backend/i18n_spec.rb index 6256aaa910..66a1f2099e 100644 --- a/spec/locale_backend/i18n_spec.rb +++ b/spec/locale_backend/i18n_spec.rb @@ -10,12 +10,12 @@ end describe '#lookup' do + subject { described_class.new } + after do I18n.locale = :en end - subject { described_class.new } - context 'with number.currency.format defined' do before do I18n.locale = :de @@ -58,19 +58,19 @@ context 'with no translation defined' do it 'returns thousands_separator based on the current locale' do - expect(subject.lookup(:thousands_separator, nil)).to eq(nil) + expect(subject.lookup(:thousands_separator, nil)).to be_nil end it 'returns decimal_mark based on the current locale' do - expect(subject.lookup(:decimal_mark, nil)).to eq(nil) + expect(subject.lookup(:decimal_mark, nil)).to be_nil end it 'returns symbol based on the current locale' do - expect(subject.lookup(:symbol, nil)).to eq(nil) + expect(subject.lookup(:symbol, nil)).to be_nil end it 'returns format based on the current locale' do - expect(subject.lookup(:format, nil)).to eq(nil) + expect(subject.lookup(:format, nil)).to be_nil end end end diff --git a/spec/money/arithmetic_spec.rb b/spec/money/arithmetic_spec.rb index 88f5b1064e..76283ebc92 100644 --- a/spec/money/arithmetic_spec.rb +++ b/spec/money/arithmetic_spec.rb @@ -36,7 +36,7 @@ expect(Money.new(1_00, "USD")).not_to eq Class expect(Money.new(1_00, "USD")).not_to eq Kernel expect(Money.new(1_00, "USD")).not_to eq(/foo/) - expect(Money.new(1_00, "USD")).not_to eq nil + expect(Money.new(1_00, "USD")).not_to be_nil end it "can be used to compare with an object that inherits from Money" do @@ -53,7 +53,7 @@ expect(Money.new(0, "EUR")).to eq 0 expect(Money.new(0, "USD")).to eq 0.0 expect(Money.new(0, "USD")).to eq BigDecimal(0) - expect(Money.new(1, "USD")).to_not eq 0 + expect(Money.new(1, "USD")).not_to eq 0 end it 'raises error for non-zero numerics' do @@ -200,19 +200,19 @@ end it 'compares with numeric 0' do - expect(Money.usd(1) < 0).to eq false - expect(Money.usd(1) > 0.0).to eq true - expect(Money.usd(0) >= 0.0).to eq true + expect(Money.usd(1) < 0).to be false + expect(Money.usd(1) > 0.0).to be true + expect(Money.usd(0) >= 0.0).to be true end it 'compares with zero Money in any currency' do - expect(Money.new(42, 'USD') > Money.zero('USD')).to eq(true) - expect(Money.new(42, 'USD') > Money.zero('CAD')).to eq(true) - expect(Money.new(42, 'CAD') > Money.zero('USD')).to eq(true) + expect(Money.new(42, 'USD') > Money.zero('USD')).to be(true) + expect(Money.new(42, 'USD') > Money.zero('CAD')).to be(true) + expect(Money.new(42, 'CAD') > Money.zero('USD')).to be(true) - expect(Money.zero('USD') > Money.new(42, 'USD')).to eq(false) - expect(Money.zero('CAD') > Money.new(42, 'USD')).to eq(false) - expect(Money.zero('USD') > Money.new(42, 'CAD')).to eq(false) + expect(Money.zero('USD') > Money.new(42, 'USD')).to be(false) + expect(Money.zero('CAD') > Money.new(42, 'USD')).to be(false) + expect(Money.zero('USD') > Money.new(42, 'CAD')).to be(false) expect(Money.zero('USD') == Money.zero('USD')).to be(true) expect(Money.zero('USD') == Money.zero('CAD')).to be(true) @@ -371,6 +371,7 @@ context 'ceiling rounding' do let(:rounding_mode) { BigDecimal::ROUND_CEILING } + it "obeys the rounding preference" do expect(Money.new(10) / 3).to eq Money.new(4) end @@ -378,6 +379,7 @@ context 'floor rounding' do let(:rounding_mode) { BigDecimal::ROUND_FLOOR } + it "obeys the rounding preference" do expect(Money.new(10) / 6).to eq Money.new(1) end @@ -385,6 +387,7 @@ context 'half up rounding' do let(:rounding_mode) { BigDecimal::ROUND_HALF_UP } + it "obeys the rounding preference" do expect(Money.new(10) / 4).to eq Money.new(3) end @@ -392,6 +395,7 @@ context 'half down rounding' do let(:rounding_mode) { BigDecimal::ROUND_HALF_DOWN } + it "obeys the rounding preference" do expect(Money.new(10) / 4).to eq Money.new(2) end @@ -735,7 +739,7 @@ expect(Money.new(0, "USD").nonzero?).to be_nil money = Money.new(1, "USD") - expect(money.nonzero?).to be_equal(money) + expect(money.nonzero?).to equal(money) end end @@ -781,7 +785,7 @@ it "treats multiplication as commutative" do expect { 2 * Money.new(2, 'USD') - }.to_not raise_error + }.not_to raise_error result = 2 * Money.new(2, 'USD') expect(result).to eq(Money.new(4, 'USD')) end @@ -813,9 +817,9 @@ end it 'compares with numeric 0' do - expect(0 < Money.usd(1)).to eq true - expect(0.0 > Money.usd(1)).to eq false - expect(0.0 >= Money.usd(0)).to eq true + expect(0 < Money.usd(1)).to be true + expect(0.0 > Money.usd(1)).to be false + expect(0.0 >= Money.usd(0)).to be true end it "raises errors for all numeric types, not just Integer" do diff --git a/spec/money/constructors_spec.rb b/spec/money/constructors_spec.rb index 75f9d4ec0f..7feca11bb5 100644 --- a/spec/money/constructors_spec.rb +++ b/spec/money/constructors_spec.rb @@ -14,6 +14,7 @@ describe "::zero" do subject { Money.zero } + it { is_expected.to eq Money.empty } it "instantiates a subclass when inheritance is used" do diff --git a/spec/money/formatting_rules_spec.rb b/spec/money/formatting_rules_spec.rb index fd8cd9bd07..d85c5ef2b9 100644 --- a/spec/money/formatting_rules_spec.rb +++ b/spec/money/formatting_rules_spec.rb @@ -3,13 +3,13 @@ RSpec.describe Money::FormattingRules do it 'does not modify frozen rules in place' do expect { - Money::FormattingRules.new(Money::Currency.new('USD'), { separator: '.' }.freeze) + described_class.new(Money::Currency.new('USD'), { separator: '.' }.freeze) }.not_to raise_error end it 'does not modify rules in place' do rules = { separator: '.' } - new_rules = Money::FormattingRules.new(Money::Currency.new('USD'), rules) + new_rules = described_class.new(Money::Currency.new('USD'), rules) expect(rules).to eq(separator: '.') expect(rules).not_to eq(new_rules) @@ -21,7 +21,7 @@ currency = Money::Currency.new('EUR') rules = { format: '%n%u', separator: '.', delimiter: ',' } - expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u') + expect(described_class.new(currency, rules)[:format]).to eq('%n%u') end it 'returns the translated format for the locale' do @@ -31,7 +31,7 @@ currency = Money::Currency.new('EUR') rules = { separator: '.', delimiter: ',' } - expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%n %u') + expect(I18n.with_locale(:fr) { described_class.new(currency, rules)[:format] }).to eq('%n %u') end end @@ -41,7 +41,7 @@ currency = Money::Currency.new('EUR') rules = { format: '%n%u', separator: '.', delimiter: ',' } - expect(Money::FormattingRules.new(currency, rules)[:format]).to eq('%n%u') + expect(described_class.new(currency, rules)[:format]).to eq('%n%u') end it 'returns the default format for the locale' do @@ -53,7 +53,7 @@ rules = { separator: '.', delimiter: ',' } allow(currency).to receive(:format).and_return("%u%n") - expect(I18n.with_locale(:fr) { Money::FormattingRules.new(currency, rules)[:format] }).to eq('%u%n') + expect(I18n.with_locale(:fr) { described_class.new(currency, rules)[:format] }).to eq('%u%n') end end end diff --git a/spec/money/formatting_spec.rb b/spec/money/formatting_spec.rb index cc706d15e5..0e62660614 100644 --- a/spec/money/formatting_spec.rb +++ b/spec/money/formatting_spec.rb @@ -8,11 +8,11 @@ context "without i18n" do subject(:money) { Money.empty("USD") } - it "should use ',' as the thousands separator" do + it "uses ',' as the thousands separator" do expect(money.thousands_separator).to eq ',' end - it "should use '.' as the decimal mark" do + it "uses '.' as the decimal mark" do expect(money.decimal_mark).to eq '.' end end @@ -30,18 +30,20 @@ context "with currency locale_backend", :locale_backend_currency do subject(:money) { Money.empty("USD") } - it "should use ',' as the thousands separator" do + it "uses ',' as the thousands separator" do expect(money.thousands_separator).to eq ',' end - it "should use '.' as the decimal mark" do + it "uses '.' as the decimal mark" do expect(money.decimal_mark).to eq '.' end end context "with i18n locale_backend", :locale_backend_i18n do context "with number.format.*" do - before :each do + subject(:money) { Money.empty("USD") } + + before do I18n.locale = :de I18n.backend.store_translations( :de, @@ -49,19 +51,19 @@ ) end - subject(:money) { Money.empty("USD") } - - it "should use '.' as the thousands separator" do + it "uses '.' as the thousands separator" do expect(money.thousands_separator).to eq '.' end - it "should use ',' as the decimal mark" do + it "uses ',' as the decimal mark" do expect(money.decimal_mark).to eq ',' end end context "with number.currency.format.*" do - before :each do + subject(:money) { Money.empty("USD") } + + before do I18n.locale = :de I18n.backend.store_translations( :de, @@ -69,19 +71,19 @@ ) end - subject(:money) { Money.empty("USD") } - - it "should use '.' as the thousands separator" do + it "uses '.' as the thousands separator" do expect(money.thousands_separator).to eq '.' end - it "should use ',' as the decimal mark" do + it "uses ',' as the decimal mark" do expect(money.decimal_mark).to eq ',' end end context "with number.currency.symbol.*" do - before :each do + subject(:money) { Money.empty("CAD") } + + before do I18n.locale = :de I18n.backend.store_translations( :de, @@ -89,19 +91,17 @@ ) end - subject(:money) { Money.empty("CAD") } - - it "should use 'CAD$' as the currency symbol" do + it "uses 'CAD$' as the currency symbol" do 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 + it "respects 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 "300.000 kr." 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 + it "respects 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" end end @@ -115,6 +115,8 @@ context "Locale :ja" do before { @_locale = I18n.locale; I18n.locale = :ja } + after { I18n.locale = @_locale } + it "formats Japanese currency in Japanese properly" do money = Money.new(1000, "JPY") expect(money.format).to eq "¥1,000" @@ -122,8 +124,6 @@ expect(money.format(symbol: false)).to eq "1,000" expect(money.format(format: "%u%n")).to eq "¥1,000" end - - after { I18n.locale = @_locale } end it "returns the monetary value as a string" do @@ -392,11 +392,11 @@ end describe ":south_asian_number_formatting delimiter" do - before(:each) do + before do Money::Currency.register(JSON.parse(INDIAN_BAR, symbolize_names: true)) end - after(:each) do + after do Money::Currency.unregister(JSON.parse(INDIAN_BAR, symbolize_names: true)) end @@ -447,11 +447,11 @@ end context "currency locale_backend i18n", :locale_backend_currency 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 + it "respects 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 "300,000 kr." 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 + it "respects 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" end end @@ -587,7 +587,7 @@ end describe ':delimiter_pattern option' do - it "should use delimiter pattern" do + it "uses delimiter pattern" do expect(Money.new(1_456_00, "EUR").format(delimiter_pattern: /(\d)(?=\d)/)).to eq "€1.4.5.6,00" end end @@ -628,12 +628,12 @@ end context "custom currencies with 4 decimal places" do - before :each do + before do Money::Currency.register(JSON.parse(BAR, symbolize_names: true)) Money::Currency.register(JSON.parse(EU4, symbolize_names: true)) end - after :each do + after do Money::Currency.unregister(JSON.parse(BAR, symbolize_names: true)) Money::Currency.unregister(JSON.parse(EU4, symbolize_names: true)) end @@ -719,7 +719,7 @@ expect(Money.new(1999_98, "USDC").format(disambiguate: true, symbol: false)).to eq("0.199998") end - it "should never return an ambiguous format with disambiguate: true" do + it "nevers return an ambiguous format with disambiguate: true" do formatted_results = {} # When we format the same amount in all known currencies, disambiguate should return diff --git a/spec/money_spec.rb b/spec/money_spec.rb index 8a75cbdb29..66f8f5e482 100644 --- a/spec/money_spec.rb +++ b/spec/money_spec.rb @@ -13,20 +13,22 @@ it 'sets the locale_backend to nil' do Money.locale_backend = nil - expect(Money.locale_backend).to eq(nil) + expect(Money.locale_backend).to be_nil end end describe ".new" do - let(:initializing_value) { 1 } subject(:money) { Money.new(initializing_value) } - it "should be an instance of `Money::Bank::VariableExchange`" do + let(:initializing_value) { 1 } + + it "is an instance of `Money::Bank::VariableExchange`" do expect(money.bank).to be Money::Bank::VariableExchange.instance end context 'given the initializing value is an integer' do let(:initializing_value) { Integer(1) } + it 'stores the integer as the number of cents' do expect(money.cents).to eq initializing_value end @@ -35,44 +37,50 @@ context 'given the initializing value is a float' do context 'and the value is 1.00' do let(:initializing_value) { 1.00 } + it { is_expected.to eq Money.new(1) } end context 'and the value is 1.01' do let(:initializing_value) { 1.01 } + it { is_expected.to eq Money.new(1) } end context 'and the value is 1.50' do let(:initializing_value) { 1.50 } + it { is_expected.to eq Money.new(2) } end end context 'given the initializing value is a rational' do let(:initializing_value) { Rational(1) } + it { is_expected.to eq Money.new(1) } end context 'given the initializing value is money' do let(:initializing_value) { Money.new(1_00, Money::Currency.new('NZD')) } + it { is_expected.to eq initializing_value } end context "given the initializing value doesn't respond to .to_d" do let(:initializing_value) { :"1" } + it { is_expected.to eq Money.new(1) } end context 'given a currency is not provided' do subject(:money) { Money.new(initializing_value) } - it "should have the default currency" do + it "has the default currency" do expect(money.currency).to eq Money.default_currency end context 'without a default' do - it 'should throw an NoCurrency Error' do + it 'throws an NoCurrency Error' do Money.default_currency = nil expect { money }.to raise_error(Money::Currency::NoCurrency) @@ -86,7 +94,7 @@ context 'and the currency is NZD' do let(:currency) { Money::Currency.new('NZD') } - it "should have NZD currency" do + it "has NZD currency" do expect(money.currency).to eq Money::Currency.new('NZD') end end @@ -94,7 +102,7 @@ context 'and the currency is nil' do let(:currency) { nil } - it "should have the default currency" do + it "has the default currency" do expect(money.currency).to eq Money.default_currency end end @@ -118,7 +126,7 @@ context 'given the initializing value is 1.50' do let(:initializing_value) { 1.50 } - it "should have the correct cents" do + it "has the correct cents" do expect(money.cents).to eq BigDecimal('1.50') end end @@ -223,7 +231,7 @@ context 'and the currency is nil' do let(:currency) { nil } - it "should have the default currency" do + it "has the default currency" do expect(Money.from_amount(1, currency).currency).to eq Money.default_currency end end @@ -816,7 +824,7 @@ it "does no exchange when the currencies are the same" do money = Money.new(100_00, "USD") - expect(money.bank).to_not receive(:exchange_with) + expect(money.bank).not_to receive(:exchange_with) expect(money.exchange_to("USD")).to eq money end end @@ -950,9 +958,10 @@ end describe "#round" do - let(:money) { Money.new(15.75, 'NZD') } subject(:rounded) { money.round } + let(:money) { Money.new(15.75, 'NZD') } + context "without infinite_precision" do it "returns a different money" do expect(rounded).not_to be money diff --git a/spec/rates_store/memory_spec.rb b/spec/rates_store/memory_spec.rb index ed5958bb8d..f1b223ddeb 100644 --- a/spec/rates_store/memory_spec.rb +++ b/spec/rates_store/memory_spec.rb @@ -5,8 +5,8 @@ describe '#add_rate and #get_rate' do it 'stores rate in memory' do - expect(subject.add_rate('USD', 'CAD', 0.9)).to eql 0.9 - expect(subject.get_rate('USD', 'CAD')).to eql 0.9 + expect(subject.add_rate('USD', 'CAD', 0.9)).to be 0.9 + expect(subject.get_rate('USD', 'CAD')).to be 0.9 end end @@ -28,7 +28,7 @@ end it 'is an Enumeator' do - expect(subject.each_rate).to be_kind_of(Enumerator) + expect(subject.each_rate).to be_a(Enumerator) result = subject.each_rate.each_with_object({}) { |(from, to, rate), m| m[[from, to].join] = rate } expect(result).to match({ 'USDCAD' => 0.9, 'CADUSD' => 1.1 }) end @@ -52,7 +52,7 @@ end describe '#marshal_dump' do - let(:subject) { Money::RatesStore::Memory.new(optional: true) } + let(:subject) { described_class.new(optional: true) } it 'can reload' do bank = Money::Bank::VariableExchange.new(subject) diff --git a/spec/support/default_currency.rb b/spec/support/default_currency.rb index bf3b7225a4..de97d02356 100644 --- a/spec/support/default_currency.rb +++ b/spec/support/default_currency.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.configure do |config| - config.before(:each) do + config.before do Money.default_currency = Money::Currency.new("USD") end end diff --git a/spec/support/i18n.rb b/spec/support/i18n.rb index a6f31900cf..b407f433f5 100644 --- a/spec/support/i18n.rb +++ b/spec/support/i18n.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.configure do |config| - config.before(:each) do + config.before do I18n.enforce_available_locales = false I18n.backend = I18n::Backend::Simple.new end diff --git a/spec/support/shared_examples/money_examples.rb b/spec/support/shared_examples/money_examples.rb index b18dd7934d..7730b671bc 100644 --- a/spec/support/shared_examples/money_examples.rb +++ b/spec/support/shared_examples/money_examples.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true RSpec.shared_examples 'instance with custom bank' do |operation, value| + subject { evaluated_value ? instance.send(operation, evaluated_value) : instance.send(operation) } + let(:custom_bank) { Money::Bank::VariableExchange.new } let(:instance) { Money.new(1, :usd, custom_bank) } let(:evaluated_value) { value.respond_to?(:call) ? value.call : value } - subject { evaluated_value ? instance.send(operation, evaluated_value) : instance.send(operation) } - it "returns custom bank from new instance" do new_money_instances = Array(subject).select { |el| el.is_a?(Money) }