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

Now working with Ruby 1.9.x (with Minitest 2.5.x)

  • Loading branch information...
1 parent 5768256 commit 50e94e9b9b6bfab37657445a4952a949810bd4cb @sikachu sikachu committed Feb 24, 2012
View
@@ -35,12 +35,20 @@ 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
+
+ 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
- time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, silent_option) } }
end
View
@@ -13,10 +13,11 @@ def method_missing(symbol, *arguments, &block)
matching_expectation_allowing_invocation.invoke(arguments, &block)
else
if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
+ matching_expectation.invoke(arguments, &block) if matching_expectation
message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
- message << Mockery.instance.mocha_inspect
+ message << @mockery.mocha_inspect
raise ExpectationError.new(message, caller)
- else
+ else
target = if self.respond_to? :mocha
self.mocha
else
@@ -14,109 +14,110 @@ def teardown
def test_should_accept_wildcard_stub_call_without_arguments
instance = new_instance
- instance.stubs(:to_s)
- instance.to_s
- assert_received(instance, :to_s)
- assert_matcher_accepts have_received(:to_s), instance
+ instance.stubs(:magic)
+ instance.magic
+ assert_received(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(:to_s)
- instance.to_s(:argument)
- assert_received(instance, :to_s)
- assert_matcher_accepts have_received(:to_s), 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(:to_s)
- assert_fails { assert_received(instance, :to_s) }
- assert_fails { assert_matcher_accepts have_received(:to_s), 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(:to_s)
- instance.to_s
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
- assert_fails { assert_matcher_accepts have_received(:to_s).with(1), 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(:to_s)
- instance.to_s(2)
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
- assert_fails { assert_matcher_accepts have_received(:to_s).with(1), 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(:to_s)
- instance.to_s(1)
- assert_received(instance, :to_s) {|expect| expect.with(1) }
- assert_matcher_accepts have_received(:to_s).with(1), 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(:to_s)
- instance.to_s('hello')
- assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) }
- assert_matcher_accepts have_received(:to_s).with(is_a(String)), 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(:to_s)
- other.stubs(:to_s)
- other.to_s('hello')
- assert_fails { assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) } }
- assert_fails { assert_matcher_accepts have_received(:to_s).with(is_a(String)), 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(:to_s)
- 2.times { instance.to_s }
- assert_received(instance, :to_s) {|expect| expect.twice }
- assert_matcher_accepts have_received(:to_s).twice, 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(:to_s).does_not_match?(new_instance)
+ have_received(:magic).does_not_match?(new_instance)
rescue Mocha::API::InvalidHaveReceived => exception
- assert_match "should_not have_received(:to_s) is invalid, please use should have_received(:to_s).never", exception.message, "Test failed, but with the wrong message"
+ 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_reject_not_enough_calls
instance = new_instance
- instance.stubs(:to_s)
- instance.to_s
+ instance.stubs(:magic)
+ instance.magic
message = /expected exactly twice/
- assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.twice } }
- assert_fails(message) { assert_matcher_accepts have_received(:to_s).twice, instance }
+ 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(:to_s)
- 2.times { instance.to_s }
+ instance.stubs(:magic)
+ 2.times { instance.magic }
message = /expected exactly once/
- assert_fails(message) { assert_received(instance, :to_s) {|expect| expect.once } }
- assert_fails(message) { assert_matcher_accepts have_received(:to_s).once, instance }
+ 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 Test::Unit::AssertionFailedError => exception
+ rescue FailedAssertion => exception
assert_match message, exception.message, "Test failed, but with the wrong message"
return
end
View
@@ -0,0 +1,83 @@
+require '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
View
@@ -15,3 +15,9 @@
require 'test/unit'
require 'mocha'
+
+if defined?(MiniTest)
+ FailedAssertion = MiniTest::Assertion
+else
+ FailedAssertion = Test::Unit::AssertionFailedError
+end
View
@@ -22,6 +22,9 @@ class << test_result
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
@@ -44,4 +47,5 @@ def assert_failed(test_result)
flunk "Test passed unexpectedly" if test_result.passed?
end
-end
+end
+
@@ -137,7 +137,7 @@ def fails?
begin
yield
false
- rescue Test::Unit::AssertionFailedError
+ rescue FailedAssertion
true
end
end
@@ -131,7 +131,7 @@ def fails?
begin
yield
false
- rescue Test::Unit::AssertionFailedError
+ rescue FailedAssertion
true
end
end
Oops, something went wrong.

0 comments on commit 50e94e9

Please sign in to comment.