Created
October 26, 2011 11:35
-
-
Save bfabry/1316094 to your computer and use it in GitHub Desktop.
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 'ruby_refactorer' | |
describe RubyRefactorer do | |
it "should be able to extract methods" do | |
code = <<CODE | |
def outer_method | |
x, y = 2, 3 | |
x + y | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:start_line => 2, | |
:end_line => 2, | |
:name => 'add').should == "\ | |
def add(x, y) | |
x + y | |
end" | |
end | |
it "should be able to extract methods with no arguments" do | |
code = <<CODE | |
puts "hello!" | |
22 + 34 | |
puts "goodbye!" | |
CODE | |
RubyRefactorer.extract_method(code, | |
:start_line => 1, | |
:end_line => 1, | |
:name => 'add').should == "\ | |
def add | |
22 + 34 | |
end" | |
end | |
it "should be able to reindent code" do | |
code = <<CODE | |
puts "hello!" | |
22 + 34 | |
puts "goodbye!" | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 1, | |
:end_line => 1, | |
:name => 'add').should == "\ | |
def add | |
22 + 34 | |
end" | |
end | |
it "should not put methods with parameters or subjects in the params" do | |
code = <<CODE | |
def method | |
x = 4; y=5 | |
puts x | |
SomeClass.new.do_method(y) | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 2, | |
:end_line => 3, | |
:name => 'dostuff').should == "\ | |
def dostuff(x, y) | |
puts x | |
SomeClass.new.do_method(y) | |
end" | |
end | |
it "should not consider globals and instance vars as params" do | |
code = <<CODE | |
def method | |
puts @x | |
SomeClass.new.do_method($y) | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 1, | |
:end_line => 2, | |
:name => 'dostuff').should == "\ | |
def dostuff | |
puts @x | |
SomeClass.new.do_method($y) | |
end" | |
end | |
it "should add objects that have methods called on them as params" do | |
code = <<CODE | |
def method | |
x = SomeClass.new | |
x.method_call('hello') | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 2, | |
:end_line => 2, | |
:name => 'dostuff').should == "\ | |
def dostuff(x) | |
x.method_call('hello') | |
end" | |
end | |
it "should not add terms defined within the extracted block" do | |
code = <<CODE | |
def method | |
x = SomeClass.new | |
x.method_call('hello') | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 1, | |
:end_line => 2, | |
:name => 'dostuff').should == "\ | |
def dostuff | |
x = SomeClass.new | |
x.method_call('hello') | |
end" | |
end | |
it "should not add terms not defined within the scope of the context" do | |
code = <<CODE | |
def method | |
puts get_name | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 1, | |
:end_line => 1, | |
:name => 'dostuff').should == "\ | |
def dostuff | |
puts get_name | |
end" | |
end | |
it "should add terms passed in as params to context" do | |
code = <<CODE | |
def method(x) | |
y = x + 10 | |
puts x + y | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 2, | |
:end_line => 2, | |
:name => 'dostuff').should == "\ | |
def dostuff(x, y) | |
puts x + y | |
end" | |
end | |
it "should add lvars defined arbitrary blocks deep" do | |
code = <<CODE | |
def method(some_array) | |
x = some_array.length | |
some_array.map do |item| | |
score = item.score | |
puts x + score | |
puts item.inspect | |
end | |
puts x | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 4, | |
:end_line => 5, | |
:name => 'nested').should == "\ | |
def nested(x, score, item) | |
puts x + score | |
puts item.inspect | |
end" | |
end | |
it "should not add vars defined in blocks closed above the extract" do | |
code = <<CODE | |
def method(some_array) | |
x = some_array.length | |
score = 10 | |
some_array.map do |item| | |
puts item | |
end | |
if x > 3 | |
if x + score > 15 | |
puts x + score + item.score | |
end | |
end | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 9, | |
:end_line => 9, | |
:name => 'nested').should == "\ | |
def nested(x, score) | |
puts x + score + item.score | |
end" | |
end | |
it "should not add vars defined after the extract" do | |
code = <<CODE | |
def method(some_array) | |
x = some_array.length | |
some_array.map do |item| | |
puts item | |
end | |
if x > 3 | |
if x + score > 15 | |
puts x + score + item.score | |
end | |
end | |
score = 10 | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 8, | |
:end_line => 8, | |
:name => 'nested').should == "\ | |
def nested(x) | |
puts x + score + item.score | |
end" | |
end | |
it "should be able to pass in blocks" do | |
code = <<CODE | |
def method(x, y, &block) | |
z = x * y | |
some_shit = SomeClass.new(x) | |
if some_shit.good_to_go? | |
puts some_shit.print(y) | |
SomeShitProcessor.new(some_shit).process(&block) | |
end | |
end | |
CODE | |
RubyRefactorer.extract_method(code, | |
:indent => 2, | |
:start_line => 5, | |
:end_line => 6, | |
:name => 'process_someshit').should == "\ | |
def process_someshit(some_shit, y, block) | |
puts some_shit.print(y) | |
SomeShitProcessor.new(some_shit).process(&block) | |
end" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment