Sha256: 06ab637527ded1315c6778257ad6d8aeb74c72cd94e18d90d1736dd155e173f8

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require_relative '../../../rubocop_helper'
require 'rubocop/cop/money/zero_money'

RSpec.describe RuboCop::Cop::Money::ZeroMoney do
  subject(:cop) { described_class.new(config) }

  let(:config) { RuboCop::Config.new }

  context 'with default configuration' do
    it 'registers an offense and corrects Money.zero without currency' do
      expect_offense(<<~RUBY)
        Money.zero
        ^^^^^^^^^^ Money.zero is removed, use `Money.new(0, Money::NULL_CURRENCY)`.
      RUBY

      expect_correction(<<~RUBY)
        Money.new(0, Money::NULL_CURRENCY)
      RUBY
    end

    it 'registers an offense and corrects Money.zero with currency' do
      expect_offense(<<~RUBY)
        Money.zero('CAD')
        ^^^^^^^^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'CAD')`.
      RUBY

      expect_correction(<<~RUBY)
        Money.new(0, 'CAD')
      RUBY
    end

    it 'does not register an offense when using Money.new with a currency' do
      expect_no_offenses(<<~RUBY)
        Money.new(0, 'CAD')
      RUBY
    end
  end

  context 'with ReplacementCurrency configuration' do
    let(:config) do
      RuboCop::Config.new(
        'Money/ZeroMoney' => {
          'ReplacementCurrency' => 'CAD'
        }
      )
    end

    it 'registers an offense and corrects Money.zero without currency' do
      expect_offense(<<~RUBY)
        Money.zero
        ^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'CAD')`.
      RUBY

      expect_correction(<<~RUBY)
        Money.new(0, 'CAD')
      RUBY
    end

    it 'registers an offense and corrects Money.zero with currency' do
      expect_offense(<<~RUBY)
        Money.zero('EUR')
        ^^^^^^^^^^^^^^^^^ Money.zero is removed, use `Money.new(0, 'EUR')`.
      RUBY

      expect_correction(<<~RUBY)
        Money.new(0, 'EUR')
      RUBY
    end

    it 'does not register an offense when using Money.new with a currency' do
      expect_no_offenses(<<~RUBY)
        Money.new(0, 'EUR')
      RUBY
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
shopify-money-3.0.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-2.2.2 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-2.2.1 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-2.2.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-2.0.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.3.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.2.1 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.2.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.1.2 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.1.1 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.1.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.0.2.pre spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.0.1.pre spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-1.0.0.pre spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-0.16.0 spec/rubocop/cop/money/zero_money_spec.rb
shopify-money-0.15.0 spec/rubocop/cop/money/zero_money_spec.rb