Sha256: 7c2016ac2c3da8e6ca6b02713286173e01e78f21ae37f371873b53c6dfa6358a

Contents?: true

Size: 1.98 KB

Versions: 28

Compression:

Stored size: 1.98 KB

Contents

require_relative '../../spec_helper'
require_lib 'reek/smell_detectors/nil_check'

RSpec.describe Reek::SmellDetectors::NilCheck do
  it 'reports the right values' do
    src = <<-EOS
      def alfa(bravo)
        bravo.nil?
      end
    EOS

    expect(src).to reek_of(:NilCheck,
                           lines:   [2],
                           context: 'alfa',
                           message: 'performs a nil-check',
                           source:  'string')
  end

  it 'does count all occurences' do
    src = <<-EOS
      def alfa(bravo, charlie)
        bravo.nil?
        charlie.nil?
      end
    EOS

    expect(src).to reek_of(:NilCheck,
                           lines:   [2, 3],
                           context: 'alfa')
  end

  it 'reports nothing when scope includes no nil checks' do
    src = 'def alfa; end'
    expect(src).not_to reek_of(:NilCheck)
  end

  it 'reports when scope uses == nil' do
    src = <<-EOS
      def alfa(bravo)
        bravo == nil
      end
    EOS

    expect(src).to reek_of(:NilCheck)
  end

  it 'reports when scope uses === nil' do
    src = <<-EOS
      def alfa(bravo)
        bravo === nil
      end
    EOS

    expect(src).to reek_of(:NilCheck)
  end

  it 'reports when scope uses nil ==' do
    src = <<-EOS
      def alfa(bravo)
        nil == bravo
      end
    EOS

    expect(src).to reek_of(:NilCheck)
  end

  it 'reports when scope uses a case-clause checking nil' do
    src = <<-EOS
      def alfa(bravo)
        case bravo
        when nil then puts "Nil"
        end
      end
    EOS

    expect(src).to reek_of(:NilCheck)
  end

  it 'reports when scope uses &.' do
    src = <<-EOS
      def alfa(bravo)
        bravo&.charlie
      end
    EOS

    expect(src).to reek_of(:NilCheck)
  end

  it 'reports all lines when scope uses multiple nilchecks' do
    src = <<-EOS
      def alfa(bravo)
        bravo.nil?
        @charlie === nil
        delta&.echo
      end
    EOS

    expect(src).to reek_of(:NilCheck, lines: [2, 3, 4])
  end
end

Version data entries

28 entries across 26 versions & 2 rubygems

Version Path
reek-4.6.0 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.6 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.5 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.4 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.3 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.2 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.1 spec/reek/smell_detectors/nil_check_spec.rb
reek-4.5.0 spec/reek/smell_detectors/nil_check_spec.rb