Sha256: 3cac3dc908a6f5040147ce6a966a29499c8619a3defb4650194ee8307aa7fef6

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), 'spec_helper')
require File.join(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))), 'lib', 'reek', 'smells', 'nil_check')
require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared')

include Reek
include Reek::Smells

describe NilCheck do

  context 'for methods' do

    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 caseNil 
        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

1 entries across 1 versions & 1 rubygems

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