Sha256: 01ceaf7a1fa82855144bdf4bc196f47ac531d8f10c5eead6040c88abfc37464d

Contents?: true

Size: 1.76 KB

Versions: 7

Compression:

Stored size: 1.76 KB

Contents

require 'spec_helper'
require 'reek/smells/nil_check'
require 'reek/smells/smell_detector_shared'

include Reek
include Reek::Smells

describe NilCheck do

  context 'for methods' do

    it 'reports the correct line number' do
      src = <<-EOS
      def nilcheck foo
        foo.nil?
      end
      EOS
      ctx = CodeContext.new(nil, src.to_reek_source.syntax_tree)
      detector = NilCheck.new('source_name')
      smells = detector.examine_context(ctx)
      smells[0].lines.should eq [2]
    end

    it 'should report nothing when scope includes no nil checks' do
      'def no_nils; end'.should_not smell_of(NilCheck)
    end

    it 'should report when scope uses multiple nil? methods' do
      src = <<-eos
      def chk_multi_nil(para)
        para.nil?
        puts "Hello"
        \"\".nil?
      end
      eos
      src.should smell_of(NilCheck,
                          {NilCheck => nil}, {NilCheck => nil})
    end

    it 'should report twice when scope uses == nil and === nil' do
      src= <<-eos
      def chk_eq_nil(para)
        para == nil
        para === nil
      end
      eos
      src.should smell_of(NilCheck,
                          {NilCheck => nil}, {NilCheck => nil})
    end

    it 'should report when scope uses nil ==' do
      'def chk_eq_nil_rev(para); nil == para; end'.should smell_of(NilCheck)
    end

    it 'should report 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
      src.should smell_of(NilCheck,
                          {NilCheck => nil}, {NilCheck => nil})
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reek-1.3.8 spec/reek/smells/nil_check_spec.rb
reek-1.3.7 spec/reek/smells/nil_check_spec.rb
reek-1.3.6 spec/reek/smells/nil_check_spec.rb
reek-1.3.5 spec/reek/smells/nil_check_spec.rb
reek-1.3.4 spec/reek/smells/nil_check_spec.rb
reek-1.3.3 spec/reek/smells/nil_check_spec.rb
reek-1.3.2 spec/reek/smells/nil_check_spec.rb