lib/rr/scenario_definition.rb in rr-0.3.9 vs lib/rr/scenario_definition.rb in rr-0.3.10

- old
+ new

@@ -5,34 +5,35 @@ class ScenarioDefinition ORIGINAL_METHOD = Object.new attr_accessor :times_called, :argument_expectation, :times_matcher, - :double, :implementation, :after_call_value, :yields_value, :scenario + attr_reader :block_callback_strategy def initialize(space) @space = space @implementation = nil @argument_expectation = nil @times_matcher = nil @after_call_value = nil @yields_value = nil + returns_block_callback_strategy! end # Scenario#with sets the expectation that the Scenario will receive # the passed in arguments. # # Passing in a block sets the return value. # # mock(subject).method_name.with(1, 2) {:return_value} def with(*args, &returns) @argument_expectation = Expectations::ArgumentEqualityExpectation.new(*args) - returns(&returns) if returns + install_method_callback returns self end # Scenario#with_any_args sets the expectation that the Scenario can receive # any arguments. @@ -40,11 +41,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.with_any_args {:return_value} def with_any_args(&returns) @argument_expectation = Expectations::AnyArgumentExpectation.new - returns(&returns) if returns + install_method_callback returns self end # Scenario#with_no_args sets the expectation that the Scenario will receive # no arguments. @@ -52,11 +53,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.with_no_args {:return_value} def with_no_args(&returns) @argument_expectation = Expectations::ArgumentEqualityExpectation.new() - returns(&returns) if returns + install_method_callback returns self end # Scenario#never sets the expectation that the Scenario will never be # called. @@ -75,11 +76,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.once {:return_value} def once(&returns) @times_matcher = TimesCalledMatchers::IntegerMatcher.new(1) - returns(&returns) if returns + install_method_callback returns self end # Scenario#twice sets the expectation that the Scenario will be called # 2 times. @@ -87,11 +88,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.twice {:return_value} def twice(&returns) @times_matcher = TimesCalledMatchers::IntegerMatcher.new(2) - returns(&returns) if returns + install_method_callback returns self end # Scenario#at_least sets the expectation that the Scenario # will be called at least n times. @@ -100,11 +101,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.at_least(4) {:return_value} def at_least(number, &returns) @times_matcher = TimesCalledMatchers::AtLeastMatcher.new(number) - returns(&returns) if returns + install_method_callback returns self end # Scenario#at_most allows sets the expectation that the Scenario # will be called at most n times. @@ -113,11 +114,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.at_most(4) {:return_value} def at_most(number, &returns) @times_matcher = TimesCalledMatchers::AtMostMatcher.new(number) - returns(&returns) if returns + install_method_callback returns self end # Scenario#any_number_of_times sets an that the Scenario will be called # any number of times. This effectively removes the times called expectation @@ -126,11 +127,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.any_number_of_times def any_number_of_times(&returns) @times_matcher = TimesCalledMatchers::AnyTimesMatcher.new - returns(&returns) if returns + install_method_callback returns self end # Scenario#times creates an TimesCalledExpectation of the passed # in number. @@ -138,11 +139,11 @@ # Passing in a block sets the return value. # # mock(subject).method_name.times(4) {:return_value} def times(matcher_value, &returns) @times_matcher = TimesCalledMatchers::TimesCalledMatcher.create(matcher_value) - returns(&returns) if returns + install_method_callback returns self end # Scenario#ordered sets the Scenario to have an ordered # expectation. @@ -157,11 +158,11 @@ "For example, using instance_of does not allow ordered to be used. " << "probe the class's #new method instead." ) unless @scenario @ordered = true @space.ordered_scenarios << @scenario unless @space.ordered_scenarios.include?(@scenario) - returns(&returns) if returns + install_method_callback returns self end # Scenario#ordered? returns true when the Scenario is ordered. # @@ -179,11 +180,11 @@ # # mock(subject).method_name.yields(yield_arg1, yield_arg2) {return_value} # subject.method_name {|yield_arg1, yield_arg2|} def yields(*args, &returns) @yields_value = args - returns(&returns) if returns + install_method_callback returns self end # Scenario#after_call creates a callback that occurs after call # is called. The passed in block receives the return value of @@ -270,8 +271,26 @@ # The Arguments that this Scenario expects def expected_arguments return [] unless argument_expectation argument_expectation.expected_arguments + end + + def returns_block_callback_strategy! # :nodoc: + @block_callback_strategy = :returns + end + + def after_call_block_callback_strategy! # :nodoc: + @block_callback_strategy = :after_call + end + + protected + def install_method_callback(block) + return unless block + case @block_callback_strategy + when :returns; returns(&block) + when :after_call; after_call(&block) + else raise "Unknown block_callback_strategy: #{@block_callback_strategy.inspect}" + end end end end \ No newline at end of file