Index: test/unit/mock_test.rb =================================================================== --- test/unit/mock_test.rb (revision 135) +++ test/unit/mock_test.rb (working copy) @@ -13,8 +13,16 @@ assert_nothing_raised(ExpectationError) do assert_equal 1, mock.method1 end - end + end + def test_should_set_single_expectation_with_terse_syntax + mock = Mock.new + mock.expects.method1.returns(1) + assert_nothing_raised(ExpectationError) do + assert_equal 1, mock.method1 + end + end + def test_should_build_and_store_expectations mock = Mock.new expectation = mock.expects(:method1) @@ -92,10 +100,17 @@ def test_should_pass_backtrace_into_expectation mock = Mock.new - backtrace = Object.new + backtrace = [] expectation = mock.expects(:method1, backtrace) assert_equal backtrace, expectation.backtrace end + + def test_should_pass_backtrace_into_expectation_with_terse_syntax + mock = Mock.new + backtrace = [] + expectation = mock.expects(backtrace).method1 + assert_equal backtrace, expectation.backtrace + end def test_should_pass_backtrace_into_stub mock = Mock.new @@ -103,6 +118,13 @@ stub = mock.stubs(:method1, backtrace) assert_equal backtrace, stub.backtrace end + + def test_should_pass_backtrace_into_stub_with_terse_syntax + mock = Mock.new + backtrace = [] + stub = mock.stubs(backtrace).method1 + assert_equal backtrace, stub.backtrace + end def test_should_create_and_add_stubs mock = Mock.new @@ -110,6 +132,13 @@ stub2 = mock.stubs(:method2) assert_equal [stub1, stub2].to_set, mock.expectations.to_set end + + def test_should_create_and_add_stubs_with_terse_syntax + 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_find_matching_expectation mock = Mock.new @@ -117,6 +146,13 @@ expectation2 = mock.expects(:my_method).with(:argument3, :argument4) assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4) end + + def test_should_find_matching_expectation_with_terse_syntax + mock = Mock.new + expectation1 = mock.expects.my_method(:argument1, :argument2) + expectation2 = mock.expects.my_method(:argument3, :argument4) + assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4) + end def test_should_find_most_recent_matching_expectation mock = Mock.new @@ -124,6 +160,13 @@ expectation2 = mock.expects(:my_method).with(:argument1, :argument2) assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2) end + + def test_should_find_most_recent_matching_expectation_with_terse_syntax + mock = Mock.new + expectation1 = mock.expects.my_method(:argument1, :argument2) + expectation2 = mock.expects.my_method(:argument1, :argument2) + assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2) + end def test_should_invoke_expectation_and_return_result mock = Mock.new @@ -131,6 +174,13 @@ result = mock.my_method assert_equal :result, result end + + def test_should_invoke_expectation_and_return_result_with_terse_syntax + mock = Mock.new + mock.expects.my_method {:result} + result = mock.my_method + assert_equal :result, result + end def test_should_not_raise_error_if_stubbing_everything mock = Mock.new(stub_everything = true) @@ -218,6 +268,15 @@ mock.method1() { |*parameters| yielded_parameters = parameters } assert_equal parameters_for_yield, yielded_parameters end + + def test_should_yield_supplied_parameters_to_block_with_terse_syntax + mock = Mock.new + parameters_for_yield = [1, 2, 3] + mock.expects.method1.yields(*parameters_for_yield) + yielded_parameters = nil + mock.method1() { |*parameters| yielded_parameters = parameters } + assert_equal parameters_for_yield, yielded_parameters + end def test_should_respond_to_expected_method mock = Mock.new Index: lib/mocha/terse_stub.rb =================================================================== --- lib/mocha/terse_stub.rb (revision 0) +++ lib/mocha/terse_stub.rb (revision 0) @@ -0,0 +1,16 @@ +module Mocha # :nodoc: + class TerseStub + def initialize(mock, backtrace) + @mock = mock + @backtrace = backtrace + end + + def method_missing(method_name, *args, &block) + if block + @mock.stubs(method_name, @backtrace).with(*args).returns(block) + else + @mock.stubs(method_name, @backtrace).with(*args) + end + end + end +end \ No newline at end of file Index: lib/mocha/mock.rb =================================================================== --- lib/mocha/mock.rb (revision 135) +++ lib/mocha/mock.rb (working copy) @@ -1,3 +1,5 @@ +require 'mocha/terse_mock' +require 'mocha/terse_stub' require 'mocha/expectation' require 'mocha/stub' require 'mocha/missing_expectation' @@ -49,7 +51,15 @@ # object.expects(:method2).returns(:result2) # # Aliased by \_\_expects\_\_ - def expects(method_name_or_hash, backtrace = nil) + def expects(*args) + if args.empty? || args.first.is_a?(Array) + return TerseMock.new(self, args.first) + else + return expects_with_method_name(*args) + end + end + + def expects_with_method_name(method_name_or_hash, 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)) @@ -80,7 +90,15 @@ # object.stubs(:method2).returns(:result2) # # Aliased by \_\_stubs\_\_ - def stubs(method_name_or_hash, backtrace = nil) + def stubs(*args) + if args.empty? || args.first.is_a?(Array) + return TerseStub.new(self, args.first) + else + return stubs_with_method_name(*args) + end + end + + def stubs_with_method_name(method_name_or_hash, 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)) @@ -88,7 +106,7 @@ else add_expectation(Stub.new(self, method_name_or_hash, backtrace)) end - end + end # :stopdoc: Index: lib/mocha/terse_mock.rb =================================================================== --- lib/mocha/terse_mock.rb (revision 0) +++ lib/mocha/terse_mock.rb (revision 0) @@ -0,0 +1,16 @@ +module Mocha # :nodoc: + class TerseMock + def initialize(mock, backtrace) + @mock = mock + @backtrace = backtrace + end + + def method_missing(method_name, *args, &block) + if block + @mock.expects(method_name, @backtrace).with(*args).returns(block) + else + @mock.expects(method_name, @backtrace).with(*args) + end + end + end +end \ No newline at end of file