Skip to content

Commit

Permalink
More detailed unit tests for LargeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrutherford committed Jun 10, 2009
1 parent 016f62a commit 6e40598
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/reek/code_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def process_module(exp)
end

def process_class(exp)
push(ClassContext.create(@element, exp)) do
scope = ClassContext.create(@element, exp)
push(scope) do
process_default(exp) unless @element.is_struct?
check_smells(:class)
end
scope
end

def process_defn(exp)
Expand Down
110 changes: 93 additions & 17 deletions spec/reek/smells/large_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
include Reek
include Reek::Smells

describe LargeClass do
describe LargeClass, 'checking Class objects' do

it 'should report large class' do
class BigOne
Expand All @@ -18,9 +18,6 @@ class BigOne
end
BigOne.should reek_only_of(:LargeClass, /BigOne/)
end
end

describe LargeClass do

it 'should not report short class' do
class ShortClass
Expand All @@ -33,6 +30,26 @@ def method6() @var6; end
end
ShortClass.should_not reek
end

describe LargeClass, 'counting instance variables' do
it 'warns about class with 10 ivars' do
class ManyIvars
def method
@vara = @varb = @varc = @vard = @vare
@varf = @varg = @varh = @vari = @varj
end
end
ManyIvars.should reek_of(:LargeClass, /10/)
end

it 'ignores class with only a couple of ivars' do
LargeClass.should_not reek_of(:LargeClass)
end

it 'ignores fq class with only a couple of ivars' do
Reek::Smells::LargeClass.should_not reek_of(:LargeClass)
end
end
end

describe LargeClass, 'when exceptions are listed' do
Expand Down Expand Up @@ -66,22 +83,81 @@ def method6() @var6; end
end
end

describe LargeClass, 'counting instance variables' do
it 'warns about class with 10 ivars' do
class ManyIvars
def method
@vara = @varb = @varc = @vard = @vare
@varf = @varg = @varh = @vari = @varj
end
end
ManyIvars.should reek_of(:LargeClass, /10/)
describe LargeClass, 'checking source code' do

def process_class(src)
source = Source.from_s(src)
CodeParser.new(nil, {}).process_class(source.generate_syntax_tree)
end

it 'ignores class with only a couple of ivars' do
LargeClass.should_not reek_of(:LargeClass)
describe 'counting instance variables' do
it 'should not report empty class' do
process_class('class Empty;end').variable_names.length.should == 0
end

it 'should count ivars in one method' do
process_class('class Empty;def ivars() @aa=@ab=@ac=@ad; end;end').variable_names.length.should == 4
end

it 'should count ivars in 2 methods' do
process_class('class Empty;def iv1() @aa=@ab; end;def iv2() @aa=@ac; end;end').variable_names.length.should == 3
end

it 'should not report 9 ivars' do
'class Empty;def ivars() @aa=@ab=@ac=@ad=@ae=@af=@ag=@ah=@ai; end;end'.should_not reek
end

it 'should report 10 ivars' do
'class Empty;def ivars() @aa=@ab=@ac=@ad=@ae=@af=@ag=@ah=@ai=@aj; end;end'.should reek_of(:LargeClass)
end

it 'should not report 10 ivars in 2 extensions' do
src = <<EOS
class Full;def ivars1() @aa=@ab=@ac=@ad=@ae; end;end
class Full;def ivars2() @af=@ag=@ah=@ai=@aj; end;end
EOS
src.should_not reek
end
end

it 'ignores fq class with only a couple of ivars' do
Reek::Smells::LargeClass.should_not reek_of(:LargeClass)
describe 'counting methods' do
it 'should not report empty class' do
process_class('class Empty;end').num_methods.should == 0
end

it 'should count 1 method' do
process_class('class Empty;def ivars() @aa=@ab; end;end').num_methods.should == 1
end

it 'should count 2 methods' do
process_class('class Empty;def meth1() @aa=@ab;end;def meth2() @aa=@ab;end;end').num_methods.should == 2
end

it 'should not report 25 methods' do
src = <<EOS
class Full
def me01()3 end;def me02()3 end;def me03()3 end;def me04()3 end;def me05()3 end
def me11()3 end;def me12()3 end;def me13()3 end;def me14()3 end;def me15()3 end
def me21()3 end;def me22()3 end;def me23()3 end;def me24()3 end;def me25()3 end
def me31()3 end;def me32()3 end;def me33()3 end;def me34()3 end;def me35()3 end
def me41()3 end;def me42()3 end;def me43()3 end;def me44()3 end;def me45()3 end
end
EOS
src.should_not reek
end

it 'should report 26 methods' do
src = <<EOS
class Full
def me01()3 end;def me02()3 end;def me03()3 end;def me04()3 end;def me05()3 end
def me11()3 end;def me12()3 end;def me13()3 end;def me14()3 end;def me15()3 end
def me21()3 end;def me22()3 end;def me23()3 end;def me24()3 end;def me25()3 end
def me31()3 end;def me32()3 end;def me33()3 end;def me34()3 end;def me35()3 end
def me41()3 end;def me42()3 end;def me43()3 end;def me44()3 end;def me45()3 end
def me51()3 end
end
EOS
src.should reek_of(:LargeClass)
end
end
end

0 comments on commit 6e40598

Please sign in to comment.