Skip to content

Commit

Permalink
Rspec matchers now work for any object [troessner#33 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrutherford committed Apr 9, 2009
1 parent 66430af commit 0cbce97
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
6 changes: 5 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
== 1.0.2 (pending)
== 1.0.2 (in the works)

=== Minor enhancements
* Now the "should reek" matchers apply to any object (#33)

=== Fixes
* Now counts attr assignments ([]= etc) in feature envy calculations
* Doesn't attempt to find *.reek files when reading code from stdin

== 1.0.1 2009-04-06

Expand Down
23 changes: 22 additions & 1 deletion lib/reek/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ module Reek
#
class Source

#
# Factory method: creates a +Source+ from obj.
# The code is not parsed until +report+ is called.
#
def self.from_object(obj)
return ObjectSource.new(obj, obj.to_s)
end

#
# Factory method: creates a +Source+ object by reading Ruby code from
# the +IO+ stream. The stream is consumed upto end-of-file, but the
Expand Down Expand Up @@ -64,6 +72,10 @@ def initialize(code, desc, dir = nil) # :nodoc:
@cf = @cf.load_local(dir) if dir
end

def check(parser) # :nodoc:
parser.check_source(@source)
end

#
# Returns a +Report+ listing the smells found in this source. The first
# call to +report+ parses the source code and constructs a list of
Expand All @@ -72,7 +84,7 @@ def initialize(code, desc, dir = nil) # :nodoc:
def report
unless @report
@report = Report.new
CodeParser.new(@report, @cf.smell_listeners).check_source(@source)
check(CodeParser.new(@report, @cf.smell_listeners))
end
@report
end
Expand Down Expand Up @@ -110,4 +122,13 @@ def report
ReportList.new(@sources)
end
end

#
# Represents an in-memory object that will be checked for smells.
#
class ObjectSource < Source
def check(parser) # :nodoc:
parser.check_object(@source)
end
end
end
6 changes: 6 additions & 0 deletions lib/reek/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def reek_only_of(smell_class, *patterns)
end
end

class Object
def to_source
Reek::Source.from_object(self)
end
end

class File
def to_source
Reek::Source.from_f(self)
Expand Down
8 changes: 2 additions & 6 deletions spec/reek/code_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
end

describe CodeParser, 'when given a C extension' do
before(:each) do
@cchk = CodeParser.new(Report.new, SmellConfig.new.smell_listeners)
end

it 'should ignore :cfunc' do
@cchk.check_object(Enumerable)
it 'ignores :cfunc' do
Enumerable.should_not reek
end
end

Expand Down
9 changes: 1 addition & 8 deletions spec/reek/smells/large_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class BigOne
end

it 'should not report short class' do
pending('to do')
class ShortClass
def method1() @var1; end
def method2() @var2; end
Expand All @@ -35,13 +34,7 @@ def method6() @var6; end
end

it 'should report large class' do
@cchk.check_object(BigOne)
@rpt.length.should == 1
end

it 'should report class name' do
@cchk.check_object(BigOne)
@rpt[0].report.should match(/BigOne/)
BigOne.should reek_only_of(:LargeClass, /BigOne/)
end

describe 'when exceptions are listed' do
Expand Down
9 changes: 1 addition & 8 deletions spec/reek/smells/long_parameter_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@
end

describe 'in a class' do
before(:each) do
@rpt = Report.new
@cchk = CodeParser.new(@rpt, SmellConfig.new.smell_listeners)
end

class InnerTest
def xyzero(arga,argb) f(3);true end
def abc(argx,yep,zero,argm) f(3);false end
end

it 'should only report long param list' do
@cchk.check_object(InnerTest)
@rpt.length.should == 1
@rpt[0].report.should match(/Long Parameter List/)
InnerTest.should reek_only_of(:LongParameterList, /abc/)
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/reek/smells/utility_function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
class Fred
attr_writer :xyz
end
@cchk.check_object(Fred)
@rpt.should be_empty
Fred.should_not reek
end

it 'should count usages of self'do
Expand All @@ -43,8 +42,7 @@ def thing(ff); @kids = 0; end
class Son < Father
def thing(ff); ff; end
end
@cchk.check_object(Son)
@rpt.should be_empty
Son.should_not reek
end

it 'should not report class method' do
Expand Down

0 comments on commit 0cbce97

Please sign in to comment.