Created
October 4, 2011 19:39
-
-
Save arwagner/1262573 to your computer and use it in GitHub Desktop.
more fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'reek2' | |
describe "smell detection" do | |
context "attribute" do | |
it "detects when there are 2 attr_readers in one class" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
attr_reader :bar1 | |
attr_reader :bar2 | |
end | |
RUBY | |
smelly_classes = Reek2.parse(code_str) do |code| | |
code.classes.where do |klass| | |
klass.calls_to(:attr_reader).count > 1 | |
end | |
end | |
smelly_classes.should == ['Foo'] | |
end | |
end | |
context "class variable" do | |
it "detects when there is a class variable in a class" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
@@bar = [] | |
end | |
RUBY | |
smelly_classes = Reek2.parse(code_str) do |code| | |
code.classes.where(class_variable_declarations.count >= 1) | |
end | |
smelly_classes.should == ['Foo'] | |
end | |
end | |
context "boolean parameter" do | |
it "detects when a method parameter defaults to true or false" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
def bar(meh=true) | |
end | |
endo | |
RUBY | |
smelly_methods = Reek2.parse(code_str) do |code| | |
code.methods.where(parameters.any? do |param| | |
[true,false].include param.default_value | |
end) | |
end | |
smelly_methods.should == ["bar"] | |
end | |
end | |
context "data clump" do | |
it "detects when more than one method takes the same set of parameters" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
def bar(baz,bah) | |
end | |
def meh(baz,bah) | |
end | |
end | |
RUBY | |
smelly_classes = Reek2.parse(code_str) do |code| | |
code.classes.where do |klass| | |
klass.methods.where do |meth| | |
klass.methods.where do |meth2| | |
meth.parameters == meth2.parameters | |
end.count > 1 | |
end.count > 1 | |
end | |
end | |
smelly_classes.should == ["Foo"] | |
end | |
end | |
context "duplication" do | |
it "detects when a method calls the same method more than once" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
def bar | |
puts | |
puts | |
end | |
end | |
RUBY | |
smelly_methods = Reek2.parse(code_str) do |code| | |
code.methods.where do |method| | |
method.method_calls.where do |call| | |
method.method_calls.where do |call2| | |
call2 == call | |
end.count > 1 | |
end.count > 1 | |
end | |
end | |
smelly_methods.should == ["bar"] | |
end | |
end | |
context "large class" do | |
it "detects when a class has more than one method" do | |
pending | |
code_str = <<RUBY | |
class Foo | |
def foo | |
end | |
def bar | |
end | |
end | |
RUBY | |
smelly_classes = Reek2.parse(code_str) do |code| | |
code.classes.where(methods.count > 1) | |
end | |
smelly_classes.should == ['Foo'] | |
end | |
end | |
context "long method" do | |
it "detects methods more than 1 statement long" do | |
pending | |
code_str = <<RUBY | |
def foo | |
statement(1) | |
statement(2) | |
end | |
RUBY | |
smelly_methods = Reek2.parse(code_str) do |code| | |
code.methods.where(statements.count > 1) | |
end | |
smelly_methods.should == ["foo"] | |
end | |
end | |
context "long parameter list" do | |
it "detects methods with more than 1 parameter in the parameter list" do | |
pending | |
code_str = <<RUBY | |
def foo(bar,baz) | |
end | |
RUBY | |
smelly_methods = Reek2.parse(code_str) do |code| | |
code.methods.where(parameters.count > 1) | |
end | |
smelly_methods.should == ["foo"] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment