Skip to content

Commit

Permalink
Replaced code_checks by the new rspec matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrutherford committed Mar 28, 2009
1 parent 082e553 commit 7b0c5c1
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lib/reek/rake_task.rb
lib/reek/report.rb
lib/reek/smell_warning.rb
lib/reek/source.rb
lib/reek/spec.rb
lib/reek/sexp_formatter.rb
lib/reek/singleton_method_context.rb
lib/reek/smells/control_couple.rb
Expand All @@ -39,7 +40,6 @@ spec/integration/integration_spec.rb
spec/integration/reek_source_spec.rb
spec/integration/script_spec.rb
spec/reek/class_context_spec.rb
spec/reek/code_checks.rb
spec/reek/code_context_spec.rb
spec/reek/code_parser_spec.rb
spec/reek/config_spec.rb
Expand Down
3 changes: 3 additions & 0 deletions lib/reek/exceptions.reek
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ LargeClass:
LongMethod:
exclude:
- Reek::SexpFormatter#self.format
UtilityFunction:
exclude:
- Reek::Spec
2 changes: 1 addition & 1 deletion lib/reek/method_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(outer, exp, record = true)
def count_statements(num)
@num_statements += num
end

def depends_on_instance?
@depends_on_self || is_overriding_method?(@name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/large_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.contexts # :nodoc:
def self.default_config
super.adopt(
MAX_ALLOWED_METHODS_KEY => 25,
EXCLUDE_KEY => ['Array', 'Hash', 'Module']
EXCLUDE_KEY => ['Array', 'Hash', 'Module', 'String']
)
end

Expand Down
76 changes: 76 additions & 0 deletions lib/reek/spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Reek
module Spec
class ShouldReek
def matches?(actual)
@source = actual.to_source
@source.smelly?
end
def failure_message_for_should
"Expected source to reek, but it didn't"
end
def failure_message_for_should_not
"Expected no smells, but got the following:\n#{@source.report}"
end
end

def reek
ShouldReek.new
end

class ShouldReekOf
def initialize(klass, patterns)
@klass = klass
@patterns = patterns
end
def matches?(actual)
@source = actual.to_source
@source.has_smell?(@klass, @patterns)
end
def failure_message_for_should
"Expected source to reek of #{@klass}, but it didn't"
end
def failure_message_for_should_not
"Expected source not to reek of #{@klass}, but got:\n#{@source.report}"
end
end

def reek_of(klass, *patterns)
ShouldReekOf.new(klass, patterns)
end

class ShouldReekOnlyOf
def initialize(klass, patterns)
@klass = klass
@patterns = patterns
end
def matches?(actual)
@source = actual.to_source
@source.report.length == 1 and @source.has_smell?(@klass, @patterns)
end
def failure_message_for_should
"Expected source to reek only of #{@klass}, but got:\n#{@source.report}"
end
def failure_message_for_should_not
"Expected source not to reek only of #{@klass}, but it did"
end
end

def reek_only_of(klass, *patterns)
ShouldReekOnlyOf.new(klass, patterns)
end
end
end

class String
def to_source
Source.from_s(self)
end
end

module Reek
class Source
def to_source
self
end
end
end
20 changes: 13 additions & 7 deletions spec/reek/class_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'

require 'spec/reek/code_checks'
require 'reek/class_context'
require 'reek/smells/feature_envy'

include CodeChecks
include Reek

describe ClassContext do
check 'should report Long Parameter List',
'class Inner; def simple(arga, argb, argc, argd) f(3);true end end', [[/Inner/, /simple/, /4 parameters/]]
it 'should report Long Parameter List' do
ruby = 'class Inner; def simple(arga, argb, argc, argd) f(3);true end end'
ruby.should reek_of(:LongParameterList, /Inner/, /simple/, /4 parameters/)
end

it 'should report two different methods' do
src = <<EOEX
class Fred
def simple(arga, argb, argc, argd) f(3);true end
def simply(arga, argb, argc, argd) f(3);false end
end
EOEX
check 'should report two different methods', src, [[/Fred/, /simple/], [/Fred/, /simply/]]
src.should reek_of(:LongParameterList, /Fred/, /simple/)
src.should reek_of(:LongParameterList, /Fred/, /simply/)
end

it 'should report many different methods' do
src = <<EOEX
class Fred
def textile_bq(tag, atts, cite, content) f(3);end
Expand All @@ -27,8 +31,10 @@ def textile_fn_(tag, num, atts, cite, content) f(3);end
def textile_popup_help(name, windowW, windowH) f(3);end
end
EOEX
check 'should report many different methods', src,
[[/Fred/, /textile_bq/], [/Fred/, /textile_fn_/], [/Fred/, /textile_p/]]
src.should reek_of(:LongParameterList, /Fred/, /textile_bq/)
src.should reek_of(:LongParameterList, /Fred/, /textile_fn_/)
src.should reek_of(:LongParameterList, /Fred/, /textile_p/)
end
end

describe Class do
Expand Down
20 changes: 0 additions & 20 deletions spec/reek/code_checks.rb

This file was deleted.

21 changes: 13 additions & 8 deletions spec/reek/code_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

require 'reek/code_parser'
require 'reek/report'
require 'spec/reek/code_checks'

include CodeChecks
include Reek

describe CodeParser, "with no method definitions" do
check 'should report no problems for empty source code', '', []
check 'should report no problems for empty class', 'class Fred; end', []
it 'should report no problems for empty source code' do
''.should_not reek
end
it 'should report no problems for empty class' do
'class Fred; end'.should_not reek
end
end

describe CodeParser, 'with a global method definition' do
check 'should report no problems for simple method',
'def Outermost::fred() true; end', []
it 'should report no problems for simple method' do
'def Outermost::fred() true; end'.should_not reek
end
end

describe CodeParser, 'when given a C extension' do
Expand All @@ -28,12 +31,14 @@
end

describe CodeParser, 'when a yield is the receiver' do
source = 'def values(*args)
it 'should report no problems' do
source = 'def values(*args)
@to_sql += case
when block_given? then " #{yield.to_sql}"
else " values (#{args.to_sql})"
end
self
end'
check 'should report no problems', source, []
source.should_not reek
end
end
7 changes: 3 additions & 4 deletions spec/reek/if_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'

require 'spec/reek/code_checks'
require 'reek/if_context'

include CodeChecks
include Reek

describe IfContext do
check 'should find a class within top-level code',
'unless jim; class Array; end; end', []
it 'should find a class within top-level code' do
'unless jim; class Array; end; end'.should_not reek
end

it 'should find class within top-level code' do
stopctx = StopContext.new
Expand Down
17 changes: 9 additions & 8 deletions spec/reek/module_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'spec/reek/code_checks'
require 'reek/module_context'

include CodeChecks
include Reek

describe ModuleContext do
check 'should report module name for smell in method',
'module Fred; def simple(x) true; end; end', [[/x/, /simple/, /Fred/]]
it 'should report module name for smell in method' do
'module Fred; def simple(x) true; end; end'.should reek_of(:UncommunicativeName, /x/, /simple/, /Fred/)
end

check 'should not report module with empty class',
'module Fred; class Jim; end; end', []
it 'should not report module with empty class' do
'module Fred; class Jim; end; end'.should_not reek
end

it 'should handle module with empty class' do
stop = StopContext.new
Expand All @@ -19,8 +19,9 @@
end

describe ModuleContext do
check 'should recognise global constant',
'module ::Global class Inside; end; end', []
it 'should recognise global constant' do
'module ::Global class Inside; end; end'.should_not reek
end

it 'should not find missing global constant' do
element = ModuleContext.create(StopContext.new, [:module, [:colon3, :Global], nil])
Expand Down
25 changes: 15 additions & 10 deletions spec/reek/smells/control_couple_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
require File.dirname(__FILE__) + '/../../spec_helper.rb'

require 'spec/reek/code_checks'
require 'reek/smells/control_couple'

include CodeChecks
include Reek::Smells

describe ControlCouple do
check 'should report a ternary check on a parameter',
'def simple(arga) arga ? @ivar : 3 end', [[/arga/]]
check 'should not report a ternary check on an ivar',
'def simple(arga) @ivar ? arga : 3 end', []
check 'should not report a ternary check on a lvar',
'def simple(arga) lvar = 27; lvar ? arga : @ivar end', []
check 'should spot a couple inside a block',
'def blocks(arg) @text.map { |blk| arg ? blk : "#{blk}" } end', [[/arg/]]
it 'should report a ternary check on a parameter' do
'def simple(arga) arga ? @ivar : 3 end'.should reek_of(:ControlCouple, /arga/)
end

it 'should not report a ternary check on an ivar' do
'def simple(arga) @ivar ? arga : 3 end'.should_not reek
end

it 'should not report a ternary check on a lvar' do
'def simple(arga) lvar = 27; lvar ? arga : @ivar end'.should_not reek
end

it 'should spot a couple inside a block' do
'def blocks(arg) @text.map { |blk| arg ? blk : "#{blk}" } end'.should reek_of(:ControlCouple, /arg/)
end
end
44 changes: 28 additions & 16 deletions spec/reek/smells/duplication_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
require 'spec/reek/code_checks'
require 'reek/smells/duplication'

include CodeChecks
include Reek::Smells

describe Duplication, "repeated method calls" do
check 'should report repeated call',
'def double_thing() @other.thing + @other.thing end', [[/@other.thing/]]
check 'should report repeated call to lvar',
'def double_thing() other[@thing] + other[@thing] end', [[/other\[@thing\]/]]
check 'should report call parameters',
'def double_thing() @other.thing(2,3) + @other.thing(2,3) end', [[/@other.thing\(2, 3\)/]]
check 'should report nested calls',
'def double_thing() @other.thing.foo + @other.thing.foo end', [[/@other.thing[^\.]/], [/@other.thing.foo/]]
check 'should ignore calls to new',
'def double_thing() @other.new + @other.new end', []
it 'should report repeated call' do
'def double_thing() @other.thing + @other.thing end'.should reek_only_of(:Duplication, /@other.thing/)
end

it 'should report repeated call to lvar' do
'def double_thing() other[@thing] + other[@thing] end'.should reek_only_of(:Duplication, /other\[@thing\]/)
end

it 'should report call parameters' do
'def double_thing() @other.thing(2,3) + @other.thing(2,3) end'.should reek_only_of(:Duplication, /@other.thing\(2, 3\)/)
end

it 'should report nested calls' do
ruby = Source.from_s('def double_thing() @other.thing.foo + @other.thing.foo end')
ruby.should reek_of(:Duplication, /@other.thing[^\.]/)
ruby.should reek_of(:Duplication, /@other.thing.foo/)
end

it 'should ignore calls to new' do
'def double_thing() @other.new + @other.new end'.should_not reek
end
end

describe Duplication, "non-repeated method calls" do
check 'should not report similar calls',
'def equals(other) other.thing == self.thing end', []
check 'should respect call parameters',
'def double_thing() @other.thing(3) + @other.thing(2) end', []
it 'should not report similar calls' do
'def equals(other) other.thing == self.thing end'.should_not reek
end

it 'should respect call parameters' do
'def double_thing() @other.thing(3) + @other.thing(2) end'.should_not reek
end
end

require 'ostruct'
Expand Down

0 comments on commit 7b0c5c1

Please sign in to comment.