This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Moved changes from jferris-mocha into a separate repo that monkey-pat…

…ches mocha
  • Loading branch information...
0 parents commit f61e7891d8b7480b7bb241d8d686fee965d568f9 @jferris jferris committed Jun 9, 2010
@@ -0,0 +1,7 @@
+pkg
+doc
+templates
+agiledox.txt
+*.swp
+.swo
+tags
@@ -0,0 +1,46 @@
+require 'rake/rdoctask'
+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'
+ require 'test/unit/ui/console/testrunner'
+ begin
+ require 'test/unit/ui/console/outputlevel'
+ silent_option = { :output_level => Test::Unit::UI::Console::OutputLevel::SILENT }
+ rescue LoadError
+ silent_option = Test::Unit::UI::SILENT
+ end
+ time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, silent_option) } }
+end
+
@@ -0,0 +1,2 @@
+require 'mocha'
+require 'bourne/api'
@@ -0,0 +1,82 @@
+require '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 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
+ @expectation.invocation_count = invocation_count
+ @expectation.verified?
+ end
+
+ def failure_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)
+ end
+ 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
@@ -0,0 +1,21 @@
+require 'mocha/expectation'
+
+module Mocha # :nodoc:
+ class Expectation
+ attr_accessor :invocation_count
+
+ def invoke_with_args(args, &block)
+ Mockery.instance.invocation(@mock, method_name, args)
+ invoke_without_args(&block)
+ end
+
+ alias_method :invoke_without_args, :invoke
+ alias_method :invoke, :invoke_with_args
+
+ private
+
+ def method_name
+ @method_matcher.expected_method_name
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module Mocha
+ class Invocation # :nodoc:
+ attr_reader :mock, :method_name, :arguments
+ def initialize(mock, method_name, arguments)
+ @mock = mock
+ @method_name = method_name
+ @arguments = arguments
+ end
+ end
+end
@@ -0,0 +1,21 @@
+require 'mocha/mock'
+require 'bourne/expectation'
+
+module Mocha # :nodoc:
+ class Mock
+ def method_missing(symbol, *arguments, &block)
+ if @responder and not @responder.respond_to?(symbol)
+ raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
+ end
+ if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments)
+ matching_expectation_allowing_invocation.invoke(arguments, &block)
+ else
+ if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
+ message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
+ message << Mockery.instance.mocha_inspect
+ raise ExpectationError.new(message, caller)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,15 @@
+require 'mocha/mockery'
+require 'bourne/mock'
+require 'bourne/invocation'
+
+module Mocha
+ class Mockery
+ def invocation(mock, method_name, args)
+ invocations << Invocation.new(mock, method_name, args)
+ end
+
+ def invocations
+ @invocations ||= []
+ end
+ end
+end
@@ -0,0 +1,38 @@
+require 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
@@ -0,0 +1,98 @@
+require File.expand_path('../../test_helper', __FILE__)
+require 'mocha'
+
+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
Oops, something went wrong.

0 comments on commit f61e789

Please sign in to comment.