From 1d77ecd9635220f2eb5f215e6239d918b598f3db Mon Sep 17 00:00:00 2001 From: Chris Parsons Date: Fri, 13 Jul 2012 21:40:24 +0100 Subject: [PATCH] Added failure scenario --- features/rename_temp.feature | 5 +--- features/support/driver.rb | 6 ++++- lib/raffle/recorder.rb | 32 +++++++++++++++++++++++--- lib/raffle/refactorings/rename_temp.rb | 5 +++- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/features/rename_temp.feature b/features/rename_temp.feature index 7d09458..6050142 100644 --- a/features/rename_temp.feature +++ b/features/rename_temp.feature @@ -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. """ diff --git a/features/support/driver.rb b/features/support/driver.rb index 9c0bdda..88995ab 100644 --- a/features/support/driver.rb +++ b/features/support/driver.rb @@ -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 @@ -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 diff --git a/lib/raffle/recorder.rb b/lib/raffle/recorder.rb index d07eecf..f40c4c8 100644 --- a/lib/raffle/recorder.rb +++ b/lib/raffle/recorder.rb @@ -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) @@ -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 diff --git a/lib/raffle/refactorings/rename_temp.rb b/lib/raffle/refactorings/rename_temp.rb index cd152b1..92f13b4 100644 --- a/lib/raffle/refactorings/rename_temp.rb +++ b/lib/raffle/refactorings/rename_temp.rb @@ -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) @@ -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)