Permalink
Browse files

More descriptive vars

  • Loading branch information...
1 parent 3df3c5b commit 8e45a8213fedfcf3840462ef0d61ef5bfc2f1339 @dcadenas committed Feb 17, 2010
@@ -9,8 +9,8 @@ class ProxyForInstance
def initialize(proxy_target, options = {})
@proxy_target = proxy_target
- options[:except] = [options[:except]].flatten.compact.map{|m| m.to_s}
- options[:only] = [options[:only]].flatten.compact.map{|m| m.to_s}
+ options[:except] = normalize_options(options[:except])
+ options[:only] = normalize_options(options[:only])
options = {:class => proxy_target.class}.merge(options)
@label = options[:label]
@class = options[:class]
@@ -28,8 +28,8 @@ def method_missing(method_name, *args, &block)
Thread.current["inside_missed_stub"] = true
result = begin
@proxy_target.send(method_name, *args, &block)
- rescue => e
- e
+ rescue => exception
+ exception
end
return result if !@only_methods.include?(method_name.to_s) && !@only_methods.empty? || @methods_to_skip.include?(method_name.to_s) || were_we_already_processing_a_missed_stub
@@ -38,9 +38,13 @@ def method_missing(method_name, *args, &block)
Thread.current["inside_missed_stub"] = false unless were_we_already_processing_a_missed_stub
end
- private
+ private
def raise_missed_stub_exception(method_name, args, result)
raise MissedStubException.new(@label || @proxy_target, method_name, args, result, Stubborn.suggester)
end
+
+ def normalize_options(options)
+ [options].flatten.compact.map{|method_name| method_name.to_s}
+ end
end
end
@@ -22,11 +22,10 @@ def new(*args, &block)
ProxyForInstance.new(new_instance, options)
end
- private
def redefine_const(const, value)
const_parts = const.name.split('::')
const_name = const_parts.pop
- parent_const = const_parts.inject(Object){|a, c| a.const_get(c) }
+ parent_const = const_parts.inject(Object){|constant_chain, constant| constant_chain.const_get(constant) }
parent_const.__send__(:remove_const, const_name) if parent_const.const_defined?(const_name)
parent_const.const_set(const_name, value)
@@ -3,18 +3,11 @@ module Suggesters
module RSpecSuggester
def self.suggestions(object_or_label, method_name, args, result_object)
object_label = object_or_label.respond_to?(:to_str) ? object_or_label : friendly_name(object_or_label)
- args = args.map{|a| friendly_name(a)}.join(", ")
- result = friendly_name(result_object)
+ args = args.map{|arg| friendly_name(arg)}.join(", ")
with = args.strip.empty? ? nil : ".with(#{args})"
- return_method = if result_object.is_a?(Exception)
- ".and_raise(#{result_object.class})"
- elsif result_object.nil?
- nil
- else
- ".and_return(#{result})"
- end
+ return_method = get_return_method(result_object)
suggestions = []
suggestions << "#{object_label}.stub!(:#{method_name})#{with}#{return_method}"
@@ -23,6 +16,17 @@ def self.suggestions(object_or_label, method_name, args, result_object)
end
private
+ def self.get_return_method(result_object)
+ result = friendly_name(result_object)
+ if result_object.is_a?(Exception)
+ ".and_raise(#{result_object.class})"
+ elsif result_object.nil?
+ nil
+ else
+ ".and_return(#{result})"
+ end
+ end
+
def self.friendly_name(object)
return "\"#{object}\"" if object.respond_to?(:to_str)
return object.inspect if object.respond_to?(:to_int) || object.is_a?(Hash) || object.nil? || object == true || object == false

0 comments on commit 8e45a82

Please sign in to comment.