commit a4ce857800ad009f2b72de29d37a06b274870dfa Author: Steve Purcell AuthorDate: Wed Oct 31 16:04:45 2007 +0100 Commit: Steve Purcell CommitDate: Wed Oct 31 16:04:45 2007 +0100 Add support for pseudo-call syntax diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index 08e0199..704a117 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -12,6 +12,7 @@ module Mocha # :nodoc: class Mock # :call-seq: expects(method_name) -> expectation + # expects.method_name(*args) -> expectation # expects(method_names) -> last expectation # # Adds an expectation that a method identified by +method_name+ symbol must be called exactly once with any parameters. @@ -34,18 +35,26 @@ module Mocha # :nodoc: # object.expects(:method1).returns(:result1) # object.expects(:method2).returns(:result2) # + # Additionally, a pseudo-call syntax is provided for convenience, such that the following + # lines are equivalent: + # object.expects.method1("some", :values) + # object.expects(:method1).with("some", :values) + # # Aliased by \_\_expects\_\_ - def expects(method_name_or_hash, backtrace = nil) + def expects(method_name_or_hash = nil, backtrace = nil) if method_name_or_hash.is_a?(Hash) then method_name_or_hash.each do |method_name, return_value| add_expectation(Expectation.new(self, method_name, backtrace).returns(return_value)) end + elsif method_name_or_hash.nil? + PseudoCall.new(self, Expectation) else add_expectation(Expectation.new(self, method_name_or_hash, backtrace)) end end # :call-seq: stubs(method_name) -> expectation + # stubs.method_name(*args) -> expectation # stubs(method_names) -> last expectation # # Adds an expectation that a method identified by +method_name+ symbol may be called any number of times with any parameters. @@ -65,12 +74,19 @@ module Mocha # :nodoc: # object.stubs(:method1).returns(:result1) # object.stubs(:method2).returns(:result2) # + # Additionally, a pseudo-call syntax is provided for convenience, such that the following + # lines are equivalent: + # object.stubs.method1("some", :values) + # object.stubs(:method1).with("some", :values) + # # Aliased by \_\_stubs\_\_ - def stubs(method_name_or_hash, backtrace = nil) + def stubs(method_name_or_hash = nil, backtrace = nil) if method_name_or_hash.is_a?(Hash) then method_name_or_hash.each do |method_name, return_value| add_expectation(Stub.new(self, method_name, backtrace).returns(return_value)) end + elsif method_name_or_hash.nil? + PseudoCall.new(self, Stub) else add_expectation(Stub.new(self, method_name_or_hash, backtrace)) end @@ -193,4 +209,17 @@ module Mocha # :nodoc: end + class PseudoCall + # Turn this class into a blank slate + instance_methods.each { |m| undef_method m unless m =~ /^__/ } + + def initialize(mock, expectation_class) + @mock = mock + @expectation_class = expectation_class + end + + def method_missing(method_name, *args) + @mock.add_expectation(@expectation_class.new(@mock, method_name, nil)) + end + end end diff --git a/test/unit/mock_test.rb b/test/unit/mock_test.rb index a1710a5..ddc0e82 100644 --- a/test/unit/mock_test.rb +++ b/test/unit/mock_test.rb @@ -313,4 +313,26 @@ class MockTest < Test::Unit::TestCase assert_match(/which responds like mocha_inspect/, e.message) end end -end \ No newline at end of file + + def test_should_create_and_store_expectations_given_as_pseudo_calls + mock = Mock.new + expectation1 = mock.expects.method1 + expectation2 = mock.expects.method2 + assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set + end + + def test_should_create_and_add_stubs_given_as_pseudo_calls + mock = Mock.new + stub1 = mock.stubs.method1 + stub2 = mock.stubs.method2 + assert_equal [stub1, stub2].to_set, mock.expectations.to_set + end + + def test_should_treat_pseudo_call_parameters_as_matcher_parameters + mock = Mock.new + expectation = mock.expects.method1(1, "two", :three) + stub = mock.expects.method2(:four) + assert expectation.match?(:method1, 1, "two", :three) + assert stub.match?(:method2, :four) + end +end