From ea47ff937695df32b6f713f19702378e5d47f6d0 Mon Sep 17 00:00:00 2001 From: Kevin Rutherford Date: Sun, 5 Jul 2009 20:48:54 +0100 Subject: [PATCH] Added copy operation for smell detectors --- lib/reek/smells/long_method.rb | 11 +++++++--- lib/reek/smells/smell_detector.rb | 6 ++++++ spec/reek/smells/smell_detector_spec.rb | 28 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/reek/smells/long_method.rb b/lib/reek/smells/long_method.rb index 81426b6d6..01ce3604f 100644 --- a/lib/reek/smells/long_method.rb +++ b/lib/reek/smells/long_method.rb @@ -23,9 +23,14 @@ def self.default_config ) end + attr_reader :max_statements + def initialize(config = LongMethod.default_config) - super - @max_statements = config[MAX_ALLOWED_STATEMENTS_KEY] + super(config) + end + + def max_statements + @config['max_statements'] end # @@ -34,7 +39,7 @@ def initialize(config = LongMethod.default_config) # def examine_context(method) num = method.num_statements - return false if num <= @max_statements + return false if num <= max_statements found(method, "has approx #{num} statements") end end diff --git a/lib/reek/smells/smell_detector.rb b/lib/reek/smells/smell_detector.rb index 327770475..247e824d0 100644 --- a/lib/reek/smells/smell_detector.rb +++ b/lib/reek/smells/smell_detector.rb @@ -45,6 +45,7 @@ def self.listen(hooks, config) end def initialize(config) + @config = config @enabled = config[ENABLED_KEY] @exceptions = config[EXCLUDE_KEY] @smells_found = [] @@ -66,9 +67,14 @@ def configure(config) end def configure_with(config) + @config.adopt!(config) @enabled = config[ENABLED_KEY] # if config.has_key?(ENABLED_KEY) end + def copy + self.class.new(@config.deep_copy) + end + def examine(context) before = @smells_found.size examine_context(context) if @enabled and !exception?(context) diff --git a/spec/reek/smells/smell_detector_spec.rb b/spec/reek/smells/smell_detector_spec.rb index c7946dfbe..dd8f5fa3a 100644 --- a/spec/reek/smells/smell_detector_spec.rb +++ b/spec/reek/smells/smell_detector_spec.rb @@ -7,6 +7,34 @@ include Reek include Reek::Smells +describe SmellDetector, 'configuration' do + before:each do + @detector = LongMethod.new + end + + it 'adopts new max_statements value' do + @detector.configure_with('max_statements' => 25) + @detector.max_statements.should == 25 + end +end + +describe SmellDetector, 'when copied' do + before :each do + @detector = LongMethod.new + @copy = @detector.copy + end + + it 'should have the same state' do + @copy.max_statements.should == @detector.max_statements + end + + it 'should change independently of its parent' do + default_max = @detector.max_statements + @copy.configure_with('max_statements' => 25) + @detector.max_statements.should == default_max + end +end + describe SmellDetector, 'when masked' do before(:each) do @detector = Duplication.new