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', 'feature_envy') require File.join(File.dirname(File.expand_path(__FILE__)), 'smell_detector_shared') include Reek include Reek::Smells describe FeatureEnvy do it 'should not report use of self' do 'def simple() self.to_s + self.to_i end'.should_not reek end it 'should not report vcall with no argument' do 'def simple() func; end'.should_not reek end it 'should not report vcall with argument' do 'def simple(arga) func(17); end'.should_not reek end it 'should not report single use' do 'def no_envy(arga) arga.barg(@item) end'.should_not reek end it 'should not report return value' do 'def no_envy(arga) arga.barg(@item); arga end'.should_not reek end context 'with 2 calls to a parameter' do it 'reports the smell' do 'def envy(arga) arga.b(arga) + arga.c(@fred) end'.should reek_only_of(:FeatureEnvy, /arga/) end end context 'when an envious receiver exists' do before :each do @source_name = 'green as a cucumber' @receiver = 'blah' @ctx = mock('method_context', :null_object => true) @ctx.should_receive(:envious_receivers).and_return({s(:lvar, @receiver) => 4}) @detector = FeatureEnvy.new(@source_name) @detector.examine_context(@ctx) warning = @detector.smells_found.to_a[0] # SMELL: too cumbersome! @yaml = warning.to_yaml end it 'reports the source' do @yaml.should match(/source:\s*#{@source_name}/) end it 'reports the class' do @yaml.should match(/\sclass:\s*LowCohesion/) end it 'reports the subclass' do @yaml.should match(/subclass:\s*FeatureEnvy/) end it 'reports the envious receiver' do @yaml.should match(/receiver:[\s]*#{@receiver}/) end it 'reports the number of references' do @yaml.should match(/references:\s*4/) end end it 'should report highest affinity' do src = <