lib/caricature/expectation.rb in caricature-0.7.2 vs lib/caricature/expectation.rb in caricature-0.7.5

- old
+ new

@@ -33,170 +33,212 @@ end # contains the syntax for building up an expectation # This is shared between the +ExpecationBuilder+ and the +Expectation+ module ExpectationSyntax + # tell the expection which arguments it needs to respond to # there is a magic argument here +any+ which configures # the expectation to respond to any arguments - def with(*args, &b) - @any_args = args.first.is_a?(Symbol) and args.first == :any - @args = args - @callback = b unless b.nil? + def with(*ags, &b) + collected[:any_args] = ags.first.is_a?(Symbol) and ags.first == :any + collected[:args] = ags + collected[:callback] = b unless b.nil? self end # tell the expectation it nees to return this value or the value returned by the block # you provide to this method. def return(value=nil, &b) - @return_value = value - @return_callback = b if b + collected[:return_value] = value + collected[:return_callback] = b if b self end # Sets up arguments for the block that is being passed into the isolated method call - def pass_block(*args, &b) - @any_block_args = args.first.is_a?(Symbol) and args.first == :any - @block_args = args - @block_callback = b unless b.nil? + def pass_block(*ags, &b) + collected[:any_block_args] = ags.first.is_a?(Symbol) and args.first == :any + collected[:block_args] = ags + collected[:block_callback] = b unless b.nil? self end # tell the expectation it needs to raise an error with the specified arguments alias_method :actual_raise, :raise def raise(*args) - @error_args = args + collected[:error_args] = args self end - + # tell the expecation it needs to call the super before the expectation exectution def super_before(&b) - @super = :before - @block = b if b + collected[:super] = :before + collected[:block] = b if b self end - + # tell the expectation it needs to call the super after the expecation execution def super_after(&b) - @super = :after - @block = b if b + collected[:super] = :after + collected[:block] = b if b self end # indicates whether this expectation should match with any arguments # or only for the specified arguments def any_args? - @any_args + collected[:any_args] end + + + private + + def collected + @collected ||= {} + end end # A description of an expectation. # An expectation has the power to call the proxy method or completely ignore it class Expectation include ExpectationSyntax - # gets the method_name to which this expectation needs to listen to - attr_reader :method_name - - # the arguments that this expectation needs to be constrained by - attr_reader :args - # the error_args that this expectation will raise an error with - attr_reader :error_args + def error_args + collected[:error_args] + end # the value that this expecation will return when executed - attr_reader :return_value + def return_value + collected[:return_value] + end # indicator for the mode to call the super +:before+, +:after+ and +nil+ - attr_reader :super - + def super + collected[:super] + end + # contains the callback if one is given - attr_reader :callback + def callback + collected[:callback] + end # contains the callback that is used to return the value when this expectation # is executed - attr_reader :return_callback + def return_callback + collected[:return_callback] + end # contains the arguments that will be passed on to the block - attr_reader :block_args + def block_args + collected[:block_args] + end # The block that will be used as value provider for the block in the method - attr_reader :block_callback + def block_callback + collected[:block_callback] + end # the block that will be used - attr_accessor :block - - + def block + collected[:block] + end + + # sets the block callback + def block=(val) + collected[:block] = val + end + + # gets the method_name to which this expectation needs to listen to + def method_name + collected[:method_name] + end + + # the arguments that this expectation needs to be constrained by + def args + collected[:args] + end + # Initializes a new instance of an expectation - def initialize(*args) - @method_name, @args, @error_args, @return_value, @return_callback, - @super, @callback, @block_args, @block_callback, @block = *args - @any_args = true + def initialize(options={}) + collected[:any_args] = true + collected.merge!(options) end - + # indicates whether this expecation will raise an event. def has_error_args? - !@error_args.nil? + !collected[:error_args].nil? end # indicates whether this expectation will return a value. def has_return_value? - !@return_value.nil? + !collected[:return_value].nil? end # call the super before the expectation def super_before? - @super == :before + collected[:super] == :before end # indicates whether super needs to be called somewhere def call_super? - !@super.nil? + !collected[:super].nil? end # indicates whether this expecation has a callback it needs to execute def has_callback? - !@callback.nil? + !collected[:callback].nil? end - + # indicates whether this expectation has a block as value provider for the method call block def has_block_callback? - !@block_callback.nil? + !collected[:block_callback].nil? end # a flag to indicate it has a return value callback def has_return_callback? - !@return_callback.nil? + !collected[:return_callback].nil? end # executes this expectation with its configuration def execute(*margs,&b) - ags = any_args? ? (margs.empty? ? :any : margs) : args - actual_raise *@error_args if has_error_args? - callback.call(*ags) if has_callback? + ags = any_args? ? :any : (margs.empty? ? collected[:args] : margs) + do_raise_error + do_callback(ags) + do_block_callback(&b) + do_event_raise if respond_to?(:events) - if b - if has_block_callback? - b.call(*@block_callback.call) - else - b.call(*@block_args) - end - end - - return @return_callback.call(*margs) if has_return_callback? + return collected[:return_callback].call(*margs) if has_return_callback? return return_value if has_return_value? nil end - def to_s + def to_s #:nodoc: "<Caricature::Expecation, method_name: #{method_name}, args: #{args}, error args: #{error_args}>" end - - def inspect - to_s + alias :inspect :to_s + + + private + + def do_raise_error #:nodoc: + actual_raise *collected[:error_args] if has_error_args? end + + def do_callback(ags) #:nodoc: + callback.call(*ags) if has_callback? + end + + def do_block_callback(&b) #:nodoc: + if b + ags = has_block_callback? ? collected[:block_callback].call : collected[:block_args] + b.call(*ags) + end + end + + end # Constructs the expecation object. # Used as a boundary between the definition and usage of the expectation class ExpectationBuilder @@ -205,17 +247,17 @@ # initialises a new instance of the expectation builder # this builder is passed into the block to allow only certain # operations in the block. def initialize(method_name) - @method_name, @args, @any_args = method_name, [], true + @collected = { :method_name => method_name, :args => [], :any_args => true } end # build up the expectation with the provided arguments def build - Expectation.new @method_name, @args, @error_args, @return_value, @return_callback, @super, @callback, @block_args, @block_callback, @block + Expectation.new collected end end end \ No newline at end of file