Sha256: 147537809c8c8b154c62892c89674b10ae77f9d1fd6e2ea5e88468629b1a7541

Contents?: true

Size: 1.89 KB

Versions: 26

Compression:

Stored size: 1.89 KB

Contents

require_relative '../../spec_helper'
require_lib 'reek/context/code_context'
require_lib 'reek/smells/nil_check'
require_relative 'smell_detector_shared'

RSpec.describe Reek::Smells::NilCheck do
  context 'for methods' do
    it 'reports the correct line number' do
      src = <<-EOS
      def nilcheck foo
        foo.nil?
      end
      EOS
      ctx = Reek::Context::CodeContext.new(nil, Reek::Source::SourceCode.from(src).syntax_tree)
      detector = build(:smell_detector, smell_type: :NilCheck)
      smells = detector.inspect(ctx)
      expect(smells[0].lines).to eq [2]
    end

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

    it 'reports when scope uses multiple nil? methods' do
      src = <<-EOS
      def chk_multi_nil(para)
        para.nil?
        puts "Hello"
        \"\".nil?
      end
      EOS
      expect(src).to reek_of(:NilCheck)
    end

    it 'reports twice when scope uses == nil and === nil' do
      src = <<-EOS
      def chk_eq_nil(para)
        para == nil
        para === nil
      end
      EOS
      expect(src).to reek_of(:NilCheck)
    end

    it 'reports when scope uses nil ==' do
      expect('def chk_eq_nil_rev(para); nil == para; end').to reek_of(:NilCheck)
    end

    it 'reports when scope uses multiple case-clauses checking nil' do
      src = <<-EOS
      def case_nil
        case @inst_var
        when nil then puts "Nil"
        end
        puts "Hello"
        case @inst_var2
        when 1 then puts 1
        when nil then puts nil.inspect
        end
      end
      EOS
      expect(src).to reek_of(:NilCheck)
    end

    it 'reports a when clause that checks nil and other values' do
      src = <<-EOS
      def case_nil
        case @inst_var
        when nil, false then puts "Hello"
        end
      end
      EOS
      expect(src).to reek_of(:NilCheck)
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
reek-4.2.3 spec/reek/smells/nil_check_spec.rb
reek-4.2.2 spec/reek/smells/nil_check_spec.rb
reek-4.2.1 spec/reek/smells/nil_check_spec.rb
reek-4.2.0 spec/reek/smells/nil_check_spec.rb
reek-4.1.1 spec/reek/smells/nil_check_spec.rb
reek-4.1.0 spec/reek/smells/nil_check_spec.rb
reek-4.0.5 spec/reek/smells/nil_check_spec.rb
reek-4.0.4 spec/reek/smells/nil_check_spec.rb
reek-4.0.3 spec/reek/smells/nil_check_spec.rb
reek-4.0.2 spec/reek/smells/nil_check_spec.rb
reek-4.0.1 spec/reek/smells/nil_check_spec.rb
reek-4.0.0 spec/reek/smells/nil_check_spec.rb
reek-4.0.0.pre1 spec/reek/smells/nil_check_spec.rb
reek-3.11 spec/reek/smells/nil_check_spec.rb
reek-3.10.2 spec/reek/smells/nil_check_spec.rb
reek-3.10.1 spec/reek/smells/nil_check_spec.rb
reek-3.10.0 spec/reek/smells/nil_check_spec.rb
reek-3.9.1 spec/reek/smells/nil_check_spec.rb
reek-3.9.0 spec/reek/smells/nil_check_spec.rb
reek-3.8.3 spec/reek/smells/nil_check_spec.rb