spec/reek/smells/utility_function_spec.rb in reek-3.0.4 vs spec/reek/smells/utility_function_spec.rb in reek-3.1

- old
+ new

@@ -1,13 +1,14 @@ require_relative '../../spec_helper' require_relative '../../../lib/reek/smells/utility_function' +require_relative '../../../lib/reek/examiner' require_relative 'smell_detector_shared' RSpec.describe Reek::Smells::UtilityFunction do describe 'a detector' do before(:each) do - @source_name = 'dummy_source' + @source_name = 'string' @detector = build(:smell_detector, smell_type: :UtilityFunction, source: @source_name) end @@ -18,13 +19,11 @@ src = <<-EOS def simple(arga) arga.b.c end EOS - source = Reek::Source::SourceCode.from(src) - mctx = Reek::TreeWalker.new.process_def(source.syntax_tree) - @warning = @detector.examine_context(mctx)[0] # SMELL: too cumbersome! + @warning = Reek::Examiner.new(src, ['UtilityFunction']).smells[0] end it_should_behave_like 'common fields set correctly' it 'reports the line number of the method' do @@ -72,12 +71,57 @@ it 'should not report UtilityFunction also when using multiple arguments' do src = <<-EOS class C def m1(a) a.to_s; end def m2(a) a.to_s; end - module_function :m1, m2 + module_function :m1, :m2 end EOS + expect(src).not_to reek_of(:UtilityFunction) + end + + it 'does not report module functions defined by earlier modifier' do + src = <<-EOF + module M + module_function + def simple(a) a.to_s; end + end + EOF + expect(src).not_to reek_of(:UtilityFunction) + end + + it 'reports functions preceded by canceled modifier' do + src = <<-EOF + module M + module_function + public + def simple(a) a.to_s; end + end + EOF + expect(src).to reek_of(:UtilityFunction) + end + + it 'does not report when module_function is called in separate scope' do + src = <<-EOF + class C + def m(a) a.to_s; end + begin + module_function :m + end + end + EOF + expect(src).not_to reek_of(:UtilityFunction) + end + + it 'does not report when module_function modifier is called in separate scope' do + src = <<-EOF + class C + begin + module_function + end + def m(a) a.to_s; end + end + EOF expect(src).not_to reek_of(:UtilityFunction) end end end