lib/caricature/expectation.rb in caricature-0.6.3 vs lib/caricature/expectation.rb in caricature-0.7.0

- old
+ new

@@ -36,13 +36,14 @@ # 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) + def with(*args, &b) @any_args = args.first.is_a?(Symbol) and args.first == :any - @args = args + @args = args + @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. @@ -93,16 +94,19 @@ # the value that this expecation will return when executed attr_reader :return_value # indicator for the mode to call the super +:before+, +:after+ and +nil+ - attr_reader :super + attr_reader :super + + # contains the callback if one is given + attr_reader :callback # Initializes a new instance of an expectation - def initialize(method_name, args, error_args, return_value, super_mode) - @method_name, @args, @error_args, @return_value, @super = - method_name, args, error_args, return_value, super_mode + def initialize(method_name, args, error_args, return_value, super_mode, callback) + @method_name, @args, @error_args, @return_value, @super, @callback = + method_name, args, error_args, return_value, super_mode, callback @any_args = true end # indicates whether this expecation will raise an event. def has_error_args? @@ -120,17 +124,23 @@ end # indicates whether super needs to be called somewhere def call_super? !@super.nil? + end + + # indicates whether this expecation has a callback it needs to execute + def has_callback? + !@callback.nil? end # executes this expectation with its configuration def execute(*margs) ags = any_args? ? (margs.empty? ? :any : margs) : args - actual_raise *@error_args if has_error_args? - return return_value if has_return_value? + actual_raise *@error_args if has_error_args? + callback.call(*ags) if has_callback? + return return_value if has_return_value? nil end def to_s "<Caricature::Expecation, method_name: #{method_name}, args: #{args}, error args: #{error_args}>" @@ -149,18 +159,18 @@ # 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, @return_value, @super, @block, @error_args, @args, @any_args = - method_name, nil, nil, nil, nil, [], true + @method_name, @return_value, @super, @block, @error_args, @args, @any_args, @callback = + method_name, nil, nil, nil, nil, [], true, nil end # build up the expectation with the provided arguments def build - Expectation.new @method_name, @args, @error_args, @return_value, @super + Expectation.new @method_name, @args, @error_args, @return_value, @super, @callback end end end \ No newline at end of file