Sha256: d2d57ae2d2c658e8f31d4dd3f804edbec0d44e4688e6cf6bee6c3cbd8448f4d2

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

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

RSpec.describe Reek::Smells::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

1 entries across 1 versions & 1 rubygems

Version Path
reek-4.4.2 spec/reek/smells/nil_check_spec.rb