bourne-1.5.0/0000755000004100000410000000000012202014316013026 5ustar www-datawww-databourne-1.5.0/.travis.yml0000644000004100000410000000017412202014316015141 0ustar www-datawww-datarvm: - 2.0.0 - 1.9.3 - 1.9.2 - 1.8.7 - rbx-18mode - rbx-19mode matrix: allow_failures: - rvm: rbx-19mode bourne-1.5.0/test/0000755000004100000410000000000012202014316014005 5ustar www-datawww-databourne-1.5.0/test/test_runner.rb0000644000004100000410000000256412202014316016711 0ustar www-datawww-datarequire 'test/unit/testcase' if defined?(MiniTest) require 'mocha/integration/mini_test' require File.expand_path('../mini_test_result', __FILE__) else require 'test/unit/testresult' end module TestRunner def run_as_test(test_result = nil, &block) test_class = Class.new(Test::Unit::TestCase) do define_method(:test_me, &block) end test = test_class.new(:test_me) if defined?(Test::Unit::TestResult) test_result ||= Test::Unit::TestResult.new test.run(test_result) {} class << test_result attr_reader :failures, :errors def failure_messages failures.map { |failure| failure.message } end def failure_message_lines failure_messages.map { |message| message.split("\n") }.flatten end def error_messages errors.map { |error| error.message } end end else runner = MiniTest::Unit.new test.run(runner) test_result = MiniTestResult.new(runner, test) end test_result end def assert_passed(test_result) flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0 flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0 end def assert_failed(test_result) flunk "Test passed unexpectedly" if test_result.passed? end end bourne-1.5.0/test/matcher_helpers.rb0000644000004100000410000000022412202014316017475 0ustar www-datawww-dataclass Test::Unit::TestCase def assert_matcher_accepts(matcher, instance) assert matcher.matches?(instance), matcher.failure_message end end bourne-1.5.0/test/execution_point.rb0000644000004100000410000000123112202014316017543 0ustar www-datawww-dataclass ExecutionPoint attr_reader :backtrace def self.current new(caller) end def initialize(backtrace) @backtrace = backtrace end def file_name return "unknown" unless @backtrace && @backtrace.first /\A(.*?):\d+/.match(@backtrace.first)[1] end def line_number return "unknown" unless @backtrace && @backtrace.first Integer(/\A.*?:(\d+)/.match(@backtrace.first)[1]) end def ==(other) return false unless other.is_a?(ExecutionPoint) (file_name == other.file_name) and (line_number == other.line_number) end def to_s "file: #{file_name}; line: #{line_number}" end def inspect to_s end end bourne-1.5.0/test/test_helper.rb0000644000004100000410000000141012202014316016644 0ustar www-datawww-dataunless defined?(STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS) STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS = Object.public_instance_methods end $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")) $:.unshift File.expand_path(File.join(File.dirname(__FILE__))) $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit')) $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'parameter_matchers')) $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'acceptance')) if ENV['MOCHA_OPTIONS'] == 'use_test_unit_gem' require 'rubygems' gem 'test-unit' end require 'test/unit' require 'mocha/setup' if defined?(MiniTest) FailedAssertion = MiniTest::Assertion else FailedAssertion = Test::Unit::AssertionFailedError end bourne-1.5.0/test/acceptance/0000755000004100000410000000000012202014316016073 5ustar www-datawww-databourne-1.5.0/test/acceptance/mocha_example_test.rb0000644000004100000410000000513212202014316022262 0ustar www-datawww-datarequire File.expand_path('../../test_helper', __FILE__) require 'mocha/setup' class MochaExampleTest < Test::Unit::TestCase class Rover def initialize(left_track, right_track, steps_per_metre, steps_per_degree) @left_track, @right_track, @steps_per_metre, @steps_per_degree = left_track, right_track, steps_per_metre, steps_per_degree end def forward(metres) @left_track.step(metres * @steps_per_metre) @right_track.step(metres * @steps_per_metre) wait end def backward(metres) forward(-metres) end def left(degrees) @left_track.step(-degrees * @steps_per_degree) @right_track.step(+degrees * @steps_per_degree) wait end def right(degrees) left(-degrees) end def wait while (@left_track.moving? or @right_track.moving?); end end end def test_should_step_both_tracks_forward_ten_steps left_track = mock('left_track') right_track = mock('right_track') steps_per_metre = 5 rover = Rover.new(left_track, right_track, steps_per_metre, nil) left_track.expects(:step).with(10) right_track.expects(:step).with(10) left_track.stubs(:moving?).returns(false) right_track.stubs(:moving?).returns(false) rover.forward(2) end def test_should_step_both_tracks_backward_ten_steps left_track = mock('left_track') right_track = mock('right_track') steps_per_metre = 5 rover = Rover.new(left_track, right_track, steps_per_metre, nil) left_track.expects(:step).with(-10) right_track.expects(:step).with(-10) left_track.stubs(:moving?).returns(false) right_track.stubs(:moving?).returns(false) rover.backward(2) end def test_should_step_left_track_forwards_five_steps_and_right_track_backwards_five_steps left_track = mock('left_track') right_track = mock('right_track') steps_per_degree = 5.0 / 90.0 rover = Rover.new(left_track, right_track, nil, steps_per_degree) left_track.expects(:step).with(+5) right_track.expects(:step).with(-5) left_track.stubs(:moving?).returns(false) right_track.stubs(:moving?).returns(false) rover.right(90) end def test_should_step_left_track_backwards_five_steps_and_right_track_forwards_five_steps left_track = mock('left_track') right_track = mock('right_track') steps_per_degree = 5.0 / 90.0 rover = Rover.new(left_track, right_track, nil, steps_per_degree) left_track.expects(:step).with(-5) right_track.expects(:step).with(+5) left_track.stubs(:moving?).returns(false) right_track.stubs(:moving?).returns(false) rover.left(90) end end bourne-1.5.0/test/acceptance/stubba_example_test.rb0000644000004100000410000000436312202014316022460 0ustar www-datawww-datarequire File.expand_path('../../test_helper', __FILE__) require 'mocha/setup' class Widget def model 'original_model' end class << self def find(options) [] end def create(attributes) Widget.new end end end module Thingy def self.wotsit :hoojamaflip end end class StubbaExampleTest < Test::Unit::TestCase def test_should_stub_instance_method widget = Widget.new widget.expects(:model).returns('different_model') assert_equal 'different_model', widget.model end def test_should_stub_module_method should_stub_module_method end def test_should_stub_module_method_again should_stub_module_method end def test_should_stub_class_method should_stub_class_method end def test_should_stub_class_method_again should_stub_class_method end def test_should_stub_instance_method_on_any_instance_of_a_class should_stub_instance_method_on_any_instance_of_a_class end def test_should_stub_instance_method_on_any_instance_of_a_class_again should_stub_instance_method_on_any_instance_of_a_class end def test_should_stub_two_different_class_methods should_stub_two_different_class_methods end def test_should_stub_two_different_class_methods_again should_stub_two_different_class_methods end private def should_stub_module_method Thingy.expects(:wotsit).returns(:dooda) assert_equal :dooda, Thingy.wotsit end def should_stub_class_method widgets = [Widget.new] Widget.expects(:find).with(:all).returns(widgets) assert_equal widgets, Widget.find(:all) end def should_stub_two_different_class_methods found_widgets = [Widget.new] created_widget = Widget.new Widget.expects(:find).with(:all).returns(found_widgets) Widget.expects(:create).with(:model => 'wombat').returns(created_widget) assert_equal found_widgets, Widget.find(:all) assert_equal created_widget, Widget.create(:model => 'wombat') end def should_stub_instance_method_on_any_instance_of_a_class Widget.any_instance.expects(:model).at_least_once.returns('another_model') widget_1 = Widget.new widget_2 = Widget.new assert_equal 'another_model', widget_1.model assert_equal 'another_model', widget_2.model end end bourne-1.5.0/test/acceptance/acceptance_test_helper.rb0000644000004100000410000000126512202014316023110 0ustar www-datawww-datarequire File.expand_path('../../test_helper', __FILE__) require 'test_runner' require 'mocha/configuration' module AcceptanceTest class FakeLogger attr_reader :warnings def initialize @warnings = [] end def warn(message) @warnings << message end end attr_reader :logger include TestRunner def setup_acceptance_test Mocha::Configuration.reset_configuration @logger = FakeLogger.new mockery = Mocha::Mockery.instance @original_logger = mockery.logger mockery.logger = @logger end def teardown_acceptance_test Mocha::Configuration.reset_configuration Mocha::Mockery.instance.logger = @original_logger end end bourne-1.5.0/test/acceptance/spy_test.rb0000644000004100000410000001154612202014316020301 0ustar www-datawww-datarequire File.join(File.dirname(__FILE__), "acceptance_test_helper") require 'bourne' require 'matcher_helpers' module SpyTestMethods def setup setup_acceptance_test end def teardown teardown_acceptance_test end def test_should_accept_wildcard_stub_call_without_arguments instance = new_instance instance.stubs(:magic) instance.magic assert_received(instance, :magic) assert_matcher_accepts have_received(:magic), instance end def test_should_accept_wildcard_stub_call_with_arguments instance = new_instance instance.stubs(:magic) instance.magic(:argument) assert_received(instance, :magic) assert_matcher_accepts have_received(:magic), instance end def test_should_not_accept_wildcard_stub_without_call instance = new_instance instance.stubs(:magic) assert_fails { assert_received(instance, :magic) } assert_fails { assert_matcher_accepts have_received(:magic), instance } end def test_should_not_accept_call_without_arguments instance = new_instance instance.stubs(:magic) instance.magic assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } } assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance } end def test_should_not_accept_call_with_different_arguments instance = new_instance instance.stubs(:magic) instance.magic(2) assert_fails { assert_received(instance, :magic) {|expect| expect.with(1) } } assert_fails { assert_matcher_accepts have_received(:magic).with(1), instance } end def test_should_accept_call_with_correct_arguments instance = new_instance instance.stubs(:magic) instance.magic(1) assert_received(instance, :magic) {|expect| expect.with(1) } assert_matcher_accepts have_received(:magic).with(1), instance end def test_should_accept_call_with_wildcard_arguments instance = new_instance instance.stubs(:magic) instance.magic('hello') assert_received(instance, :magic) {|expect| expect.with(is_a(String)) } assert_matcher_accepts have_received(:magic).with(is_a(String)), instance end def test_should_reject_call_on_different_mock instance = new_instance other = new_instance instance.stubs(:magic) other.stubs(:magic) other.magic('hello') assert_fails { assert_received(instance, :magic) {|expect| expect.with(is_a(String)) } } assert_fails { assert_matcher_accepts have_received(:magic).with(is_a(String)), instance } end def test_should_accept_correct_number_of_calls instance = new_instance instance.stubs(:magic) 2.times { instance.magic } assert_received(instance, :magic) {|expect| expect.twice } assert_matcher_accepts have_received(:magic).twice, instance end def test_should_not_allow_should_not begin have_received(:magic).does_not_match?(new_instance) rescue Mocha::API::InvalidHaveReceived => exception assert_match "should_not have_received(:magic) is invalid, please use should have_received(:magic).never", exception.message, "Test failed, but with the wrong message" return end flunk("Expected to fail") end def test_should_warn_for_unstubbed_methods_with_expectations new_instance.stubs(:unknown) assert_fails(/unstubbed, expected exactly once/) { assert_matcher_accepts have_received(:unknown), new_instance } end def test_should_reject_not_enough_calls instance = new_instance instance.stubs(:magic) instance.magic message = /expected exactly twice/ assert_fails(message) { assert_received(instance, :magic) {|expect| expect.twice } } assert_fails(message) { assert_matcher_accepts have_received(:magic).twice, instance } end def test_should_reject_too_many_calls instance = new_instance instance.stubs(:magic) 2.times { instance.magic } message = /expected exactly once/ assert_fails(message) { assert_received(instance, :magic) {|expect| expect.once } } assert_fails(message) { assert_matcher_accepts have_received(:magic).once, instance } end def assert_fails(message=/not yet invoked/) begin yield rescue FailedAssertion => exception assert_match message, exception.message, "Test failed, but with the wrong message" return end flunk("Expected to fail") end end class PartialSpyTest < Test::Unit::TestCase include AcceptanceTest include SpyTestMethods def new_instance Object.new end end class PureSpyTest < Test::Unit::TestCase include AcceptanceTest include SpyTestMethods def new_instance stub end end class StubEverythingSpyTest < Test::Unit::TestCase include AcceptanceTest def setup setup_acceptance_test end def teardown teardown_acceptance_test end def test_should_match_invocations_with_no_explicit_stubbing instance = stub_everything instance.surprise! assert_received(instance, :surprise!) end end bourne-1.5.0/test/simple_counter.rb0000644000004100000410000000017512202014316017365 0ustar www-datawww-dataclass SimpleCounter attr_reader :count def initialize @count = 0 end def increment @count += 1 end end bourne-1.5.0/test/unit/0000755000004100000410000000000012202014316014764 5ustar www-datawww-databourne-1.5.0/test/unit/mock_test.rb0000644000004100000410000002542212202014316017306 0ustar www-datawww-data# to update: # - copy new code from https://raw.github.com/freerange/mocha/master/test/unit/mock_test.rb # - keep: require 'bourne/mock' # - keep: FakeExpectation test require File.expand_path('../../test_helper', __FILE__) require 'bourne/mock' require 'mocha/expectation_error_factory' require 'set' require 'simple_counter' class MockTest < Test::Unit::TestCase include Mocha def test_should_set_single_expectation mock = build_mock mock.expects(:method1).returns(1) assert_nothing_raised(ExpectationErrorFactory.exception_class) do assert_equal 1, mock.method1 end end def test_should_build_and_store_expectations mock = build_mock expectation = mock.expects(:method1) assert_not_nil expectation assert_equal [expectation], mock.__expectations__.to_a end def test_should_not_stub_everything_by_default mock = build_mock assert_equal false, mock.everything_stubbed end def test_should_stub_everything mock = build_mock mock.stub_everything assert_equal true, mock.everything_stubbed end def test_should_be_able_to_extend_mock_object_with_module mock = build_mock assert_nothing_raised(ExpectationErrorFactory.exception_class) { mock.extend(Module.new) } end def test_should_be_equal mock = build_mock assert_equal true, mock.eql?(mock) end if RUBY_VERSION < '1.9' OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || ["method_missing", "singleton_method_undefined", "initialize"].include?(m)} else OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || [:object_id, :method_missing, :singleton_method_undefined, :initialize, :String, :singleton_method_added].include?(m) } end def test_should_be_able_to_mock_standard_object_methods mock = build_mock OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) } OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) } assert mock.__verified__? end def test_should_be_able_to_stub_standard_object_methods mock = build_mock OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) } OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) } end def test_should_create_and_add_expectations mock = build_mock expectation1 = mock.expects(:method1) expectation2 = mock.expects(:method2) assert_equal [expectation1, expectation2].to_set, mock.__expectations__.to_set end def test_should_pass_backtrace_into_expectation mock = build_mock backtrace = Object.new expectation = mock.expects(:method1, backtrace) assert_equal backtrace, expectation.backtrace end def test_should_pass_backtrace_into_stub mock = build_mock backtrace = Object.new stub = mock.stubs(:method1, backtrace) assert_equal backtrace, stub.backtrace end def test_should_create_and_add_stubs mock = build_mock stub1 = mock.stubs(:method1) stub2 = mock.stubs(:method2) assert_equal [stub1, stub2].to_set, mock.__expectations__.to_set end def test_should_invoke_expectation_and_return_result mock = build_mock mock.expects(:my_method).returns(:result) result = mock.my_method assert_equal :result, result end def test_should_not_raise_error_if_stubbing_everything mock = build_mock mock.stub_everything result = nil assert_nothing_raised(ExpectationErrorFactory.exception_class) do result = mock.unexpected_method end assert_nil result end def test_should_raise_assertion_error_for_unexpected_method_call mock = build_mock error = assert_raise(ExpectationErrorFactory.exception_class) do mock.unexpected_method_called(:my_method, :argument1, :argument2) end assert_match(/unexpected invocation/, error.message) assert_match(/my_method/, error.message) assert_match(/argument1/, error.message) assert_match(/argument2/, error.message) end def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied mock = build_mock mock.expects(:method1) mock.expects(:method2) mock.method1 assert !mock.__verified__? end def test_should_increment_assertion_counter_for_every_verified_expectation mock = build_mock mock.expects(:method1) mock.method1 mock.expects(:method2) mock.method2 assertion_counter = SimpleCounter.new mock.__verified__?(assertion_counter) assert_equal 2, assertion_counter.count end def test_should_yield_supplied_parameters_to_block mock = build_mock 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_set_up_multiple_expectations_with_return_values mock = build_mock mock.expects(:method1 => :result1, :method2 => :result2) assert_equal :result1, mock.method1 assert_equal :result2, mock.method2 end def test_should_set_up_multiple_stubs_with_return_values mock = build_mock mock.stubs(:method1 => :result1, :method2 => :result2) assert_equal :result1, mock.method1 assert_equal :result2, mock.method2 end def test_should_keep_returning_specified_value_for_stubs mock = build_mock mock.stubs(:method1).returns(1) assert_equal 1, mock.method1 assert_equal 1, mock.method1 end def test_should_keep_returning_specified_value_for_expects mock = build_mock mock.expects(:method1).times(2).returns(1) assert_equal 1, mock.method1 assert_equal 1, mock.method1 end def test_should_match_most_recent_call_to_expects mock = build_mock mock.expects(:method1).returns(0) mock.expects(:method1).returns(1) assert_equal 1, mock.method1 end def test_should_match_most_recent_call_to_stubs mock = build_mock mock.stubs(:method1).returns(0) mock.stubs(:method1).returns(1) assert_equal 1, mock.method1 end def test_should_match_most_recent_call_to_stubs_or_expects mock = build_mock mock.stubs(:method1).returns(0) mock.expects(:method1).returns(1) assert_equal 1, mock.method1 end def test_should_match_most_recent_call_to_expects_or_stubs mock = build_mock mock.expects(:method1).returns(0) mock.stubs(:method1).returns(1) assert_equal 1, mock.method1 end def test_should_respond_to_expected_method mock = build_mock mock.expects(:method1) assert_equal true, mock.respond_to?(:method1) end def test_should_respond_to_expected_method_as_string mock = build_mock mock.expects(:method1) assert_equal true, mock.respond_to?('method1') end def test_should_not_respond_to_unexpected_method mock = build_mock assert_equal false, mock.respond_to?(:method1) end def test_should_respond_to_methods_which_the_responder_does_responds_to instance = Class.new do define_method(:respond_to?) { |symbol| true } end.new mock = build_mock mock.responds_like(instance) assert_equal true, mock.respond_to?(:invoked_method) end def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to instance = Class.new do define_method(:respond_to?) { |symbol| false } end.new mock = build_mock mock.responds_like(instance) assert_equal false, mock.respond_to?(:invoked_method) end def test_should_respond_to_methods_which_the_responder_instance_does_responds_to klass = Class.new do define_method(:respond_to?) { |symbol| true } end mock = build_mock mock.responds_like_instance_of(klass) assert_equal true, mock.respond_to?(:invoked_method) end def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to klass = Class.new do define_method(:respond_to?) { |symbol| false } end mock = build_mock mock.responds_like_instance_of(klass) assert_equal false, mock.respond_to?(:invoked_method) end def test_respond_like_should_return_itself_to_allow_method_chaining mock = build_mock assert_same mock.responds_like(Object.new), mock end def test_respond_like_instance_of_should_return_itself_to_allow_method_chaining mock = build_mock assert_same mock.responds_like_instance_of(Object), mock end def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder mock = build_mock mock.stubs(:invoked_method) assert_nothing_raised(NoMethodError) { mock.invoked_method } end def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method instance = Class.new do define_method(:respond_to?) { |symbol| true } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method) assert_nothing_raised(NoMethodError) { mock.invoked_method } end def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method instance = Class.new do define_method(:respond_to?) { |symbol| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method) assert_raises(NoMethodError) { mock.invoked_method } end def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder instance = Class.new do define_method(:respond_to?) { |symbol| false } define_method(:mocha_inspect) { 'mocha_inspect' } end.new mock = build_mock mock.responds_like(instance) mock.stubs(:invoked_method) begin mock.invoked_method rescue NoMethodError => e assert_match(/which responds like mocha_inspect/, e.message) end end def test_should_handle_respond_to_with_private_methods_param_without_error mock = build_mock assert_nothing_raised { mock.respond_to?(:object_id, false) } end def test_should_respond_to_any_method_if_stubbing_everything mock = build_mock mock.stub_everything assert mock.respond_to?(:abc) assert mock.respond_to?(:xyz) end def test_should_remove_expectation_for_unstubbed_method mock = build_mock mock.expects(:method1) mock.unstub(:method1) e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method1 } assert_match(/unexpected invocation/, e.message) end class FakeExpectation attr_reader :args def invoke end def match?(*args) true end def invocations_allowed? true end end def test_should_record_invocations method = :a_method args = [1, 2] mock = Mock.new(method) expectation = FakeExpectation.new mock.__expectations__.add expectation mock.send(method, *args) end private def build_mock Mock.new(nil) end end bourne-1.5.0/test/unit/mockery_test.rb0000644000004100000410000001205512202014316020024 0ustar www-datawww-datarequire File.expand_path('../../test_helper', __FILE__) require 'bourne/mockery' require 'mocha/state_machine' require 'bourne/invocation' class MockeryTest < Test::Unit::TestCase include Mocha def test_should_build_instance_of_mockery mockery = Mockery.instance assert_not_nil mockery assert_kind_of Mockery, mockery end def test_should_cache_instance_of_mockery mockery_1 = Mockery.instance mockery_2 = Mockery.instance assert_same mockery_1, mockery_2 end def test_should_expire_mockery_instance_cache mockery_1 = Mockery.instance Mockery.reset_instance mockery_2 = Mockery.instance assert_not_same mockery_1, mockery_2 end def test_should_raise_expectation_error_because_not_all_expectations_are_satisfied mockery = Mockery.new mock_1 = mockery.named_mock('mock-1') { expects(:method_1) } mock_2 = mockery.named_mock('mock-2') { expects(:method_2) } 1.times { mock_1.method_1 } 0.times { mock_2.method_2 } assert_raises(ExpectationErrorFactory.exception_class) { mockery.verify } end def test_should_reset_list_of_mocks_on_teardown mockery = Mockery.new mock = mockery.unnamed_mock { expects(:my_method) } mockery.teardown assert_nothing_raised(ExpectationErrorFactory.exception_class) { mockery.verify } end def test_should_build_instance_of_stubba_on_instantiation mockery = Mockery.new assert_not_nil mockery.stubba assert_kind_of Central, mockery.stubba end def test_should_build_new_instance_of_stubba_on_teardown mockery = Mockery.new stubba_1 = mockery.stubba mockery.teardown stubba_2 = mockery.stubba assert_not_same stubba_1, stubba_2 end def test_should_build_and_store_new_state_machine mockery = Mockery.new mockery.new_state_machine('state-machine-name') assert_equal 1, mockery.state_machines.length assert_kind_of StateMachine, mockery.state_machines[0] end def test_should_reset_list_of_state_machines_on_teardown mockery = Mockery.new mockery.new_state_machine('state-machine-name') mockery.teardown assert_equal 0, mockery.state_machines.length end class FakeMethod def stub; end def unstub; end def matches?(other); true; end end def test_should_unstub_all_methods_on_teardown mockery = Mockery.new stubba = mockery.stubba stubba.stub(FakeMethod.new) mockery.teardown assert stubba.stubba_methods.empty? end def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name mockery = Mockery.new mock = mockery.unnamed_mock assert_match Regexp.new("^#$"), mock.mocha_inspect end def test_should_display_object_id_for_inspect_if_mock_has_no_name mockery = Mockery.new mock = mockery.unnamed_mock assert_match Regexp.new("^#$"), mock.inspect end def test_should_display_name_for_mocha_inspect_if_mock_has_string_name mockery = Mockery.new mock = mockery.named_mock('named_mock') assert_equal "#", mock.mocha_inspect end def test_should_display_name_for_mocha_inspect_if_mock_has_symbol_name mockery = Mockery.new mock = mockery.named_mock(:named_mock) assert_equal "#", mock.mocha_inspect end def test_should_display_name_for_inspect_if_mock_has_string_name mockery = Mockery.new mock = mockery.named_mock('named_mock') assert_equal "#", mock.inspect end def test_should_display_name_for_inspect_if_mock_has_symbol_name mockery = Mockery.new mock = mockery.named_mock(:named_mock) assert_equal "#", mock.inspect end def test_should_display_impersonated_object_for_mocha_inspect mockery = Mockery.new instance = Object.new mock = mockery.mock_impersonating(instance) assert_equal "#{instance.mocha_inspect}", mock.mocha_inspect end def test_should_display_impersonated_object_for_inspect mockery = Mockery.new instance = Object.new mock = mockery.mock_impersonating(instance) assert_equal "#{instance.mocha_inspect}", mock.inspect end class FakeClass; end def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_mocha_inspect mockery = Mockery.new mock = mockery.mock_impersonating_any_instance_of(FakeClass) assert_equal "#", mock.mocha_inspect end def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_inspect mockery = Mockery.new mock = mockery.mock_impersonating_any_instance_of(FakeClass) assert_equal "#", mock.inspect end def test_should_record_invocation mock = 'a mock' method = :call_me args = [1, 2] mockery = Mockery.new mockery.invocation(mock, method, args) assert_equal 1, mockery.invocations.size invocation = mockery.invocations.first assert_equal mock, invocation.mock assert_equal method, invocation.method_name assert_equal args, invocation.arguments end end bourne-1.5.0/test/unit/assert_received_test.rb0000644000004100000410000000644312202014316021526 0ustar www-datawww-datarequire File.join(File.dirname(__FILE__), "..", "test_helper") require 'test_runner' require 'bourne/api' require 'bourne/mockery' require 'mocha/api' class AssertReceivedTest < Test::Unit::TestCase include Mocha include TestRunner include Mocha::API def teardown Mockery.reset_instance end class FakeMock def initialize(name) @name = name end def inspect @name end def mocha self end end def test_passes_if_invocation_exists method = :a_method mock = FakeMock.new('a mock') Mockery.instance.invocation(mock, method, []) assert_passes do assert_received(mock, method) end end def test_fails_if_invocation_doesnt_exist method = :a_method mock = FakeMock.new('a mock') assert_fails do assert_received(mock, method) end end def test_fails_if_invocation_exists_with_different_arguments method = :a_method mock = FakeMock.new('a mock') Mockery.instance.invocation(mock, method, [2, 1]) assert_fails do assert_received(mock, method) {|expect| expect.with(1, 2) } end end def test_passes_if_invocation_exists_with_wildcard_arguments method = :a_method mock = FakeMock.new('a mock') Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_received(mock, method) {|expect| expect.with(is_a(String)) } end end def test_passes_if_invocation_exists_with_exact_arguments method = :a_method mock = FakeMock.new('a mock') Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_received(mock, method) {|expect| expect.with('hello') } end end def test_fails_if_invocation_exists_only_on_other_mock method = :a_method mock = FakeMock.new('a mock') other = 'another mock' Mockery.instance.invocation(other, method, ['hello']) assert_fails do assert_received(mock, method) end end def test_passes_if_invocation_exists_for_impersonating_mock method = :a_method object = Object.new mock = FakeMock.new('a mock') class << object attr_accessor :mocha end object.mocha = mock Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_received(object, method) {|expect| expect.with('hello') } end end def test_passes_if_invocation_count_correct method = :a_method mock = FakeMock.new('a mock') 2.times { Mockery.instance.invocation(mock, method, []) } assert_passes do assert_received(mock, method) {|expect| expect.twice } end end def test_fails_if_invocation_count_too_low method = :a_method mock = FakeMock.new('a mock') Mockery.instance.invocation(mock, method, []) assert_fails do assert_received(mock, method) {|expect| expect.twice } end end def test_fails_if_invocation_count_too_high method = :a_method mock = FakeMock.new('a mock') 2.times { Mockery.instance.invocation(mock, method, []) } assert_fails do assert_received(mock, method) {|expect| expect.once } end end def assert_passes(&block) assert ! fails?(&block) end def assert_fails(&block) assert fails?(&block) end def fails? begin yield false rescue FailedAssertion true end end end bourne-1.5.0/test/unit/invocation_test.rb0000644000004100000410000000070412202014316020522 0ustar www-datawww-datarequire File.join(File.dirname(__FILE__), "..", "test_helper") require 'bourne/invocation' class InvocationTest < Test::Unit::TestCase include Mocha def test_has_mock_method_name_and_args mock = 'a mock' method = :call_me args = [1, 2] invocation = Invocation.new(mock, method, args) assert_equal mock, invocation.mock assert_equal method, invocation.method_name assert_equal args, invocation.arguments end end bourne-1.5.0/test/unit/have_received_test.rb0000644000004100000410000001005712202014316021144 0ustar www-datawww-datarequire File.join(File.dirname(__FILE__), "..", "test_helper") require 'test_runner' require 'bourne/api' require 'bourne/mockery' require 'mocha/api' require 'matcher_helpers' module HaveReceivedTestMethods include Mocha def teardown Mockery.reset_instance end def test_passes_if_invocation_exists method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, []) assert_passes do assert_matcher_accepts have_received(method), mock end end def test_fails_if_invocation_doesnt_exist method = :a_method mock = new_mock('a mock') assert_fails do assert_matcher_accepts have_received(method), mock end end def test_fails_if_invocation_exists_with_different_arguments method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, [2, 1]) assert_fails do assert_matcher_accepts have_received(method).with(1, 2), mock end end def test_passes_if_invocation_exists_with_wildcard_arguments method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_matcher_accepts have_received(method).with(is_a(String)), mock end end def test_passes_if_invocation_exists_with_exact_arguments method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_matcher_accepts have_received(method).with('hello'), mock end end def test_fails_if_invocation_exists_only_on_other_mock method = :a_method mock = new_mock('a mock') other = 'another mock' Mockery.instance.invocation(other, method, ['hello']) assert_fails do assert_matcher_accepts have_received(method), mock end end def test_passes_if_invocation_exists_for_impersonating_mock method = :a_method object = Object.new mock = new_mock('a mock') class << object attr_accessor :mocha end object.mocha = mock Mockery.instance.invocation(mock, method, ['hello']) assert_passes do assert_matcher_accepts have_received(method).with('hello'), object end end def test_passes_if_invocation_count_correct method = :a_method mock = new_mock('a mock') 2.times { Mockery.instance.invocation(mock, method, []) } assert_passes do assert_matcher_accepts have_received(method).twice, mock end end def test_fails_if_invocation_count_incorrect method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, []) assert_fails do assert_matcher_accepts have_received(method).twice, mock end end def test_fails_if_invocation_count_too_low method = :a_method mock = new_mock('a mock') Mockery.instance.invocation(mock, method, []) assert_fails do assert_matcher_accepts have_received(method).twice, mock end end def test_fails_if_invocation_count_too_high method = :a_method mock = new_mock('a mock') 2.times { Mockery.instance.invocation(mock, method, []) } assert_fails do assert_matcher_accepts have_received(method).once, mock end end def assert_passes(&block) assert ! fails?(&block) end def assert_fails(&block) assert fails?(&block) end def fails? begin yield false rescue FailedAssertion true end end end class PartialHaveReceivedTest < Test::Unit::TestCase include TestRunner include Mocha::API include HaveReceivedTestMethods class FakeMock def initialize(name) @name = name end def inspect @name end def mocha self end end def new_mock(*args) FakeMock.new(*args) end end class PureHaveReceivedTest < Test::Unit::TestCase include TestRunner include Mocha::API include HaveReceivedTestMethods class FakeMock def initialize(name) @name = name end def inspect @name end def mocha self end end def new_mock(*args) Mocha::Mock.new(*args) end end bourne-1.5.0/test/mini_test_result.rb0000644000004100000410000000365612202014316017735 0ustar www-datawww-datarequire 'stringio' require 'test/unit/testcase' require 'minitest/unit' class MiniTestResult FAILURE_PATTERN = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m ERROR_PATTERN = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n}m def self.parse_failure(raw) matches = FAILURE_PATTERN.match(raw) return nil unless matches Failure.new(matches[2], matches[3], [matches[4]], matches[5]) end def self.parse_error(raw) matches = ERROR_PATTERN.match(raw) return nil unless matches backtrace = raw.gsub(ERROR_PATTERN, '').split("\n").map(&:strip) Error.new(matches[2], matches[3], matches[4], backtrace) end class Failure attr_reader :method, :test_case, :location, :message def initialize(method, test_case, location, message) @method, @test_case, @location, @message = method, test_case, location, message end end class Error class Exception attr_reader :message, :backtrace def initialize(message, location) @message, @backtrace = message, location end end attr_reader :method, :test_case, :exception def initialize(method, test_case, message, backtrace) @method, @test_case, @exception = method, test_case, Exception.new(message, backtrace) end end def initialize(runner, test) @runner, @test = runner, test end def failure_count @runner.failures end def assertion_count @test._assertions end def error_count @runner.errors end def passed? @test.passed? end def failures @runner.report.map { |puked| MiniTestResult.parse_failure(puked) }.compact end def errors @runner.report.map { |puked| MiniTestResult.parse_error(puked) }.compact end def failure_messages failures.map(&:message) end def failure_message_lines failure_messages.map { |message| message.split("\n") }.flatten end def error_messages errors.map { |e| e.exception.message } end end bourne-1.5.0/test/method_definer.rb0000644000004100000410000000113612202014316017307 0ustar www-datawww-datarequire 'metaclass' module Mocha module ObjectMethods def define_instance_method(method_symbol, &block) __metaclass__.send(:define_method, method_symbol, block) end def replace_instance_method(method_symbol, &block) raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless self.respond_to?(method_symbol) define_instance_method(method_symbol, &block) end def define_instance_accessor(*symbols) symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) } end end end class Object include Mocha::ObjectMethods end bourne-1.5.0/LICENSE0000644000004100000410000000207012202014316014032 0ustar www-datawww-dataCopyright (c) 2010-2013 Joe Ferris and thoughtbot, inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. bourne-1.5.0/README.md0000644000004100000410000000444612202014316014315 0ustar www-datawww-dataBourne [![Build Status](https://secure.travis-ci.org/thoughtbot/bourne.png?branch=master)](http://travis-ci.org/thoughtbot/bourne) ====== Bourne extends mocha to allow detailed tracking and querying of stub and mock invocations. It allows test spies using the have_received rspec matcher and assert_received for Test::Unit. Bourne was extracted from jferris-mocha, a fork of mocha that adds test spies. Test Spies ---------- Test spies are a form of test double that preserves the normal four-phase unit test order and allows separation of stubbing and verification. Using a test spy is like using a mocked expectation except that there are two steps: 1. Stub out a method for which you want to verify invocations 2. Use an assertion or matcher to verify that the stub was invoked correctly Examples -------- RSpec: mock.should have_received(:to_s) Radio.should have_received(:new).with(1041) radio.should have_received(:volume).with(11).twice radio.should have_received(:off).never You also want to configure RSpec to use mocha for mocking: RSpec.configure do |config| config.mock_with :mocha end Test::Unit: assert_received(mock, :to_s) assert_received(Radio, :new) {|expect| expect.with(1041) } assert_received(radio, :volume) {|expect| expect.with(11).twice } assert_received(radio, :off) {|expect| expect.never } See Mocha::API for more information. Install ------- gem install bourne More Information ---------------- * [RDoc](http://rdoc.info/projects/thoughtbot/bourne) * [Issues](http://github.com/thoughtbot/bourne/issues) * [Mocha mailing list](http://groups.google.com/group/mocha-developer) Credits ------- Bourne was written by Joe Ferris. Mocha was written by James Mead. Several of the test examples and helpers used in the Bourne test suite were copied directly from Mocha. Thanks to thoughtbot for inspiration, ideas, and funding. Thanks to James for writing mocha. ![thoughtbot](http://thoughtbot.com/images/tm/logo.png) Thank you to all [the contributors](https://github.com/thoughtbot/bourne/contributors)! The names and logos for thoughtbot are trademarks of thoughtbot, inc. License ------- Bourne is Copyright © 2010-2013 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file. bourne-1.5.0/Rakefile0000644000004100000410000000342712202014316014501 0ustar www-datawww-datarequire 'bundler/setup' require 'bundler/gem_tasks' require 'rake/testtask' desc "Run all tests" task 'default' => ['test:units', 'test:acceptance', 'test:performance'] namespace 'test' do unit_tests = FileList['test/unit/**/*_test.rb'] acceptance_tests = FileList['test/acceptance/*_test.rb'] desc "Run unit tests" Rake::TestTask.new('units') do |t| t.libs << 'test' t.test_files = unit_tests end desc "Run acceptance tests" Rake::TestTask.new('acceptance') do |t| t.libs << 'test' t.test_files = acceptance_tests end desc "Run performance tests" task 'performance' do require File.join(File.dirname(__FILE__), 'test', 'acceptance', 'stubba_example_test') require File.join(File.dirname(__FILE__), 'test', 'acceptance', 'mocha_example_test') iterations = 1000 puts "\nBenchmarking with #{iterations} iterations..." [MochaExampleTest, StubbaExampleTest].each do |test_case| puts "#{test_case}: #{benchmark_test_case(test_case, iterations)} seconds." end end end def benchmark_test_case(klass, iterations) require 'benchmark' if defined?(MiniTest) MiniTest::Unit.output = StringIO.new Benchmark.realtime { iterations.times { |i| MiniTest::Unit.new.run([klass]) } } else load 'test/unit/ui/console/testrunner.rb' unless defined?(Test::Unit::UI::Console::TestRunner) unless $silent_option begin load 'test/unit/ui/console/outputlevel.rb' unless defined?(Test::Unit::UI::Console::OutputLevel::SILENT) $silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT } rescue LoadError $silent_option = Test::Unit::UI::SILENT end end Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, $silent_option) } } end end bourne-1.5.0/bourne.gemspec0000644000004100000410000000202712202014316015666 0ustar www-datawww-data# -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) require "bourne/version" Gem::Specification.new do |s| s.name = 'bourne' s.version = Bourne::VERSION.dup s.platform = Gem::Platform::RUBY s.authors = ["Joe Ferris"] s.email = 'jferris@thoughtbot.com' s.homepage = 'http://github.com/thoughtbot/bourne' s.summary = 'Adds test spies to mocha.' s.description = %q{Extends mocha to allow detailed tracking and querying of stub and mock invocations. Allows test spies using the have_received rspec matcher and assert_received for Test::Unit. Extracted from the jferris-mocha fork.} s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] # Follow instructions in test/unit/mock_test.rb to update. s.add_dependency('mocha', '>= 0.13.2', '< 0.15') s.add_development_dependency('rake') end bourne-1.5.0/checksums.yaml.gz0000444000004100000410000000065212202014316016317 0ustar www-datawww-dataH#×o=zNS~"ynQޣO~Eڀ<:I8 }@7ڌM`ե }{ЎB'2\IzUHQ`CKM17@96g30o3 nVVs̶[ׂPAvͤU1T *JP)U*xaK|]^]jE>י.Ai¡LW K՟^'r.ʌ:&5~"$m']ޓ&mmk8mUgRH9ZJS 8u$&Td+g7ff᪔D|π`\bourne-1.5.0/NEWS.md0000644000004100000410000000060712202014316014127 0ustar www-datawww-data1.3.2 (2013-02-15) * Compatible with Mocha 0.13.2 1.3.0 (2012-11-30) * Compatible with Mocha 0.13.0 1.2.1 (2012-11-26) * Compatible with Mocha 0.12.7 1.1.2 (2012-03-23) * Mention when a method is unstubbed * Compatible with Mocha 0.10.5 1.1.1 (2012-03-04) * Exactly the same as 1.1.0, but built with 1.8.7 so people with 1.8.7 can install the gem. 1.1.0 (2012-02-24) * 1.9 compatible bourne-1.5.0/metadata.yml0000644000004100000410000000614512202014316015337 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: bourne version: !ruby/object:Gem::Version version: 1.5.0 platform: ruby authors: - Joe Ferris autorequire: bindir: bin cert_chain: [] date: 2013-06-27 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: mocha requirement: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: 0.13.2 - - < - !ruby/object:Gem::Version version: '0.15' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: 0.13.2 - - < - !ruby/object:Gem::Version version: '0.15' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' description: ! "Extends mocha to allow detailed tracking and querying of\n stub and mock invocations. Allows test spies using the have_received rspec\n matcher and assert_received for Test::Unit. Extracted from the\n jferris-mocha fork." email: jferris@thoughtbot.com executables: [] extensions: [] extra_rdoc_files: [] files: - .gitignore - .travis.yml - Gemfile - LICENSE - NEWS.md - README.md - Rakefile - bourne.gemspec - lib/bourne.rb - lib/bourne/api.rb - lib/bourne/invocation.rb - lib/bourne/mock.rb - lib/bourne/mockery.rb - lib/bourne/version.rb - test/acceptance/acceptance_test_helper.rb - test/acceptance/mocha_example_test.rb - test/acceptance/spy_test.rb - test/acceptance/stubba_example_test.rb - test/execution_point.rb - test/matcher_helpers.rb - test/method_definer.rb - test/mini_test_result.rb - test/simple_counter.rb - test/test_helper.rb - test/test_runner.rb - test/unit/assert_received_test.rb - test/unit/have_received_test.rb - test/unit/invocation_test.rb - test/unit/mock_test.rb - test/unit/mockery_test.rb homepage: http://github.com/thoughtbot/bourne licenses: [] metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: Adds test spies to mocha. test_files: - test/acceptance/acceptance_test_helper.rb - test/acceptance/mocha_example_test.rb - test/acceptance/spy_test.rb - test/acceptance/stubba_example_test.rb - test/execution_point.rb - test/matcher_helpers.rb - test/method_definer.rb - test/mini_test_result.rb - test/simple_counter.rb - test/test_helper.rb - test/test_runner.rb - test/unit/assert_received_test.rb - test/unit/have_received_test.rb - test/unit/invocation_test.rb - test/unit/mock_test.rb - test/unit/mockery_test.rb bourne-1.5.0/Gemfile0000644000004100000410000000004712202014316014322 0ustar www-datawww-datasource 'https://rubygems.org' gemspec bourne-1.5.0/.gitignore0000644000004100000410000000006712202014316015021 0ustar www-datawww-datapkg *.swp .swo *~ tags rdoc *.gem Gemfile.lock .bundle bourne-1.5.0/lib/0000755000004100000410000000000012202014316013574 5ustar www-datawww-databourne-1.5.0/lib/bourne/0000755000004100000410000000000012202014316015066 5ustar www-datawww-databourne-1.5.0/lib/bourne/api.rb0000644000004100000410000000555012202014316016171 0ustar www-datawww-datarequire 'mocha/api' require 'bourne/mockery' module Mocha # :nodoc: module API # Asserts that the given mock received the given method. # # Examples: # # assert_received(mock, :to_s) # assert_received(Radio, :new) {|expect| expect.with(1041) } # assert_received(radio, :volume) {|expect| expect.with(11).twice } def assert_received(mock, expected_method_name) matcher = have_received(expected_method_name) yield(matcher) if block_given? assert matcher.matches?(mock), matcher.failure_message end class InvalidHaveReceived < StandardError end class HaveReceived #:nodoc: def initialize(expected_method_name) @expected_method_name = expected_method_name @expectations = [] end def method_missing(method, *args, &block) @expectations << [method, args, block] self end def matches?(mock) if mock.respond_to?(:mocha) @mock = mock.mocha else @mock = mock end @expectation = Expectation.new(@mock, @expected_method_name) @expectations.each do |method, args, block| @expectation.send(method, *args, &block) end invocation_count.times { @expectation.invoke } @expectation.verified? end def does_not_match?(mock) raise InvalidHaveReceived.new("should_not have_received(:#{@expected_method_name}) is invalid, please use" + " should have_received(:#{@expected_method_name}).never") end def failure_message message = "" if matching_stubs.empty? message << "unstubbed, " end message << @expectation.mocha_inspect end private def invocation_count matching_invocations.size end def matching_invocations invocations.select do |invocation| @expectation.match?(invocation.method_name, *invocation.arguments) end end def invocations Mockery.instance.invocations.select do |invocation| invocation.mock.equal?(@mock) || invocation.mock.mocha.equal?(@mock) end end def matching_stubs Mockery.instance.stubba.stubba_methods.select do |method| matching_stub?(method) end end def matching_stub?(method) method.mock.equal?(@mock) && method.method == @expected_method_name end end # :call-seq: # should have_received(method).with(arguments).times(times) # # Ensures that the given mock received the given method. # # Examples: # # mock.should have_received(:to_s) # Radio.should have_received(:new).with(1041) # radio.should have_received(:volume).with(11).twice def have_received(expected_method_name) HaveReceived.new(expected_method_name) end end end bourne-1.5.0/lib/bourne/version.rb0000644000004100000410000000005512202014316017100 0ustar www-datawww-datamodule Bourne VERSION = '1.5.0'.freeze end bourne-1.5.0/lib/bourne/mockery.rb0000644000004100000410000000104112202014316017060 0ustar www-datawww-datarequire 'mocha/mockery' require 'bourne/mock' require 'bourne/invocation' module Mocha # Used internally by Bourne extensions to Mocha when recording invocations of # stubbed and mocked methods. You shouldn't need to interact with this module # through normal testing, but you can access the full list of invocations # directly if necessary. class Mockery def invocation(mock, method_name, args) invocations << Invocation.new(mock, method_name, args) end def invocations @invocations ||= [] end end end bourne-1.5.0/lib/bourne/mock.rb0000644000004100000410000000062012202014316016342 0ustar www-datawww-datarequire 'mocha/mock' module Mocha # :nodoc: # Extends #method_missing on Mocha::Mock to record Invocations. class Mock # :nodoc: alias_method :method_missing_without_invocation, :method_missing def method_missing(symbol, *arguments, &block) Mockery.instance.invocation(self, symbol, arguments) method_missing_without_invocation(symbol, *arguments, &block) end end end bourne-1.5.0/lib/bourne/invocation.rb0000644000004100000410000000073512202014316017571 0ustar www-datawww-datamodule Mocha # :nodoc: # Used internally by Bourne extensions to Mocha. Represents a single # invocation of a stubbed or mocked method. The mock, method name, and # arguments are recorded and can be used to determine how a method was # invoked. class Invocation attr_reader :mock, :method_name, :arguments def initialize(mock, method_name, arguments) @mock = mock @method_name = method_name @arguments = arguments end end end bourne-1.5.0/lib/bourne.rb0000644000004100000410000000005112202014316015407 0ustar www-datawww-datarequire 'mocha/api' require 'bourne/api'