Skip to content

Commit

Permalink
Added failure scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Parsons committed Jul 13, 2012
1 parent f12410d commit 1d77ecd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
5 changes: 1 addition & 4 deletions features/rename_temp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ Feature: Rename temp
Then the tool should return an error code
Then the output should be:
"""
def name
fred = "fred"
"My name is " + fred
end
The starting extent was invalid.
"""

6 changes: 5 additions & 1 deletion features/support/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def run_refactoring(name, *args)
end

def last_output
@last_result.resulting_code
@last_result.to_s
end

def last_exit_status
Expand Down Expand Up @@ -52,6 +52,10 @@ def run_refactoring(name, *args)
run("raffle #{name} #{args.join(' ')}")
end

def last_output
all_output
end

alias :assert_file_content :check_exact_file_content
end
end
Expand Down
32 changes: 29 additions & 3 deletions lib/raffle/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@

module Raffle
class Recorder
def initialize
@errors = []
end

def output_sexp(sexp)
@output_sexp = sexp
self
end

def invalid_starting_exent(extent)
@errors << "The starting extent was invalid."
end

def result
SuccessfulResult.new(@output_sexp)
if @errors.empty?
SuccessfulResult.new(@output_sexp)
else
FailedResult.new(@errors)
end
end
end

class SuccessfulResult < Struct.new(:output_sexp)
def resulting_code
def to_s
rubify(output_sexp)
end

def report
$stdout.puts(result)
$stdout.puts(self)
end

def rubify(sexpr)
Expand All @@ -29,4 +41,18 @@ def exit_status
0
end
end

class FailedResult < Struct.new(:errors)
def to_s
errors.join("\n") + "\n"
end

def report
$stderr.puts(self)
end

def exit_status
1
end
end
end
5 changes: 4 additions & 1 deletion lib/raffle/refactorings/rename_temp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class RenameTemp

def call(starting_sexp, extent, extent_sexp, new_name, result)
original_name = name_of_ident_at_position(starting_sexp, extent.start)
return result.invalid_starting_exent(extent) if original_name.nil?

scope_sexp = containing_scope_for_position(starting_sexp, extent.start)
unless_scope_defines_variable_again = lambda do |new_scope|
not defining_block_parameter?(new_scope, original_name)
Expand All @@ -22,7 +24,8 @@ def call(starting_sexp, extent, extent_sexp, new_name, result)
private

def name_of_ident_at_position(starting_sexp, position)
sexp_for_position(starting_sexp, position)[1]
ident_candidate = sexp_for_position(starting_sexp, position)
ident_candidate[0] == :@ident ? ident_candidate[1] : nil
end

def sexp_for_position(starting_sexp, position)
Expand Down

0 comments on commit 1d77ecd

Please sign in to comment.