Skip to content

Commit

Permalink
Merge pull request #626 from freerange/tidy-up-minitest-modules
Browse files Browse the repository at this point in the history
Tidy up Minitest vs MiniTest references
  • Loading branch information
floehopper committed Nov 12, 2023
2 parents f086b7e + a13e1e4 commit e7134d5
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .yardopts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lib/mocha/expectation_error.rb
lib/mocha/stubbing_error.rb
lib/mocha/unexpected_invocation.rb
lib/mocha/integration/test_unit/adapter.rb
lib/mocha/integration/mini_test/adapter.rb
lib/mocha/integration/minitest/adapter.rb
-
RELEASE.md
COPYING.md
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* A Ruby library for [mocking](http://xunitpatterns.com/Mock%20Object.html) and [stubbing](http://xunitpatterns.com/Test%20Stub.html) - but deliberately not (yet) [faking](http://xunitpatterns.com/Fake%20Object.html) or [spying](http://xunitpatterns.com/Test%20Spy.html).
* A unified, simple and readable syntax for both full & partial mocking.
* Built-in support for MiniTest and Test::Unit.
* Built-in support for Minitest and Test::Unit.
* Supported by many other test frameworks.

### Intended Usage
Expand All @@ -18,7 +18,7 @@ Install the latest version of the gem with the following command...

$ gem install mocha

Note: If you are intending to use Mocha with Test::Unit or MiniTest, you should only setup Mocha *after* loading the relevant test library...
Note: If you are intending to use Mocha with Test::Unit or Minitest, you should only setup Mocha *after* loading the relevant test library...

##### Test::Unit

Expand All @@ -29,12 +29,12 @@ require 'test/unit'
require 'mocha/test_unit'
```

##### MiniTest
##### Minitest

```ruby
require 'rubygems'
gem 'mocha'
require 'minitest/unit'
require 'minitest/autorun'
require 'mocha/minitest'
```

Expand All @@ -53,14 +53,14 @@ require 'test/unit'
require 'mocha/test_unit'
```

##### MiniTest
##### Minitest

```ruby
# Gemfile
gem 'mocha'

# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
require 'minitest/unit'
require 'minitest/autorun'
require 'mocha/minitest'
```

Expand Down Expand Up @@ -103,9 +103,9 @@ end

If you're loading Mocha using Bundler within a Rails application, you should setup Mocha manually e.g. at the bottom of your `test_helper.rb`.

##### MiniTest
##### Minitest

Note that since Rails v4 (at least), `ActiveSupport::TestCase` has inherited from `Minitest::Test` or its earlier equivalents. Thus unless you are *explicitly* using Test::Unit, you are likely to be using MiniTest.
Note that since Rails v4 (at least), `ActiveSupport::TestCase` has inherited from `Minitest::Test` or its earlier equivalents. Thus unless you are *explicitly* using Test::Unit, you are likely to be using Minitest.

```ruby
# Gemfile in Rails app
Expand Down
18 changes: 9 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ namespace 'test' do # rubocop:disable Metrics/BlockLength
end

namespace 'integration' do
desc 'Run MiniTest integration tests (intended to be run in its own process)'
desc 'Run Minitest integration tests (intended to be run in its own process)'
Rake::TestTask.new('minitest') do |t|
t.libs << 'test'
t.test_files = FileList['test/integration/mini_test_test.rb']
t.test_files = FileList['test/integration/minitest_test.rb']
t.verbose = true
t.warning = true
end
Expand Down Expand Up @@ -93,18 +93,18 @@ end
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def benchmark_test_case(klass, iterations)
require 'benchmark'
require 'mocha/detection/mini_test'
require 'mocha/detection/minitest'

if defined?(MiniTest)
minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
if defined?(Minitest)
minitest_version = Gem::Version.new(Mocha::Detection::Minitest.version)
if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
Minitest.seed = 1
result = Benchmark.realtime { iterations.times { |_i| klass.run(MiniTest::CompositeReporter.new) } }
MiniTest::Runnable.runnables.delete(klass)
result = Benchmark.realtime { iterations.times { |_i| klass.run(Minitest::CompositeReporter.new) } }
Minitest::Runnable.runnables.delete(klass)
result
else
MiniTest::Unit.output = StringIO.new
Benchmark.realtime { iterations.times { |_i| MiniTest::Unit.new.run([klass]) } }
Minitest::Unit.output = StringIO.new
Benchmark.realtime { iterations.times { |_i| Minitest::Unit.new.run([klass]) } }
end
else
load 'test/unit/ui/console/testrunner.rb' unless defined?(Test::Unit::UI::Console::TestRunner)
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'mocha/class_methods'

module Mocha
# Methods added to +Test::Unit::TestCase+, +MiniTest::Unit::TestCase+ or equivalent.
# Methods added to +Test::Unit::TestCase+, +Minitest::Unit::TestCase+ or equivalent.
# The mock creation methods are {#mock}, {#stub} and {#stub_everything}, all of which return a #{Mock}
# which can be further modified by {Mock#responds_like} and {Mock#responds_like_instance_of} methods,
# both of which return a {Mock}, too, and can therefore, be chained to the original creation methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Mocha
module Detection
module MiniTest
module Minitest
def self.testcase
if defined?(::Minitest::Test)
::Minitest::Test
elsif defined?(::MiniTest::Unit::TestCase)
::MiniTest::Unit::TestCase
elsif defined?(::Minitest::Unit::TestCase)
::Minitest::Unit::TestCase
end
end

def self.version
if defined?(::MiniTest::Unit::VERSION)
::MiniTest::Unit::VERSION
if defined?(::Minitest::Unit::VERSION)
::Minitest::Unit::VERSION
elsif defined?(::Minitest::VERSION)
::Minitest::VERSION
else
Expand Down
4 changes: 2 additions & 2 deletions lib/mocha/detection/test_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Detection
module TestUnit
def self.testcase
if defined?(::Test::Unit::TestCase) &&
!(defined?(::MiniTest::Unit::TestCase) && (::Test::Unit::TestCase < ::MiniTest::Unit::TestCase)) &&
!(defined?(::MiniTest::Spec) && (::Test::Unit::TestCase < ::MiniTest::Spec))
!(defined?(::Minitest::Unit::TestCase) && (::Test::Unit::TestCase < ::Minitest::Unit::TestCase)) &&
!(defined?(::Minitest::Spec) && (::Test::Unit::TestCase < ::Minitest::Spec))
::Test::Unit::TestCase
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mocha/expectation_error_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Mocha
#
# This class should only be used by authors of test libraries and not by typical "users" of Mocha.
#
# For example, it is used by +Mocha::Integration::MiniTest::Adapter+ in order to have Mocha raise a +MiniTest::Assertion+ which can then be sensibly handled by +MiniTest::Unit::TestCase+.
# For example, it is used by +Mocha::Integration::Minitest::Adapter+ in order to have Mocha raise a +Minitest::Assertion+ which can then be sensibly handled by +Minitest::Unit::TestCase+.
#
# @see Mocha::Integration::MiniTest::Adapter
# @see Mocha::Integration::Minitest::Adapter
class ExpectationErrorFactory
class << self
# @!attribute exception_class
Expand Down
4 changes: 2 additions & 2 deletions lib/mocha/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ module Mocha
#
# This module is provided as part of the +Mocha::API+ module and is therefore part of the public API, but should only be used by authors of test libraries and not by typical "users" of Mocha.
#
# Integration with Test::Unit and MiniTest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as *part* of Mocha, but is supported by means of the methods in this module.
# Integration with Test::Unit and Minitest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as *part* of Mocha, but is supported by means of the methods in this module.
#
# See the code in the +Adapter+ modules for examples of how to use the methods in this module. +Mocha::ExpectationErrorFactory+ may be used if you want +Mocha+ to raise a different type of exception.
#
# @see Mocha::Integration::TestUnit::Adapter
# @see Mocha::Integration::MiniTest::Adapter
# @see Mocha::Integration::Minitest::Adapter
# @see Mocha::ExpectationErrorFactory
# @see Mocha::API
module Hooks
Expand Down
28 changes: 0 additions & 28 deletions lib/mocha/integration/mini_test.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/mocha/integration/minitest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'mocha/debug'
require 'mocha/detection/minitest'
require 'mocha/integration/minitest/adapter'

module Mocha
module Integration
module Minitest
def self.activate
target = Detection::Minitest.testcase
return false unless target

minitest_version = Gem::Version.new(Detection::Minitest.version)
Debug.puts "Detected Minitest version: #{minitest_version}"

unless Minitest::Adapter.applicable_to?(minitest_version)
raise 'Versions of minitest earlier than v3.3.0 are not supported.'
end

unless target < Minitest::Adapter
Debug.puts "Applying #{Minitest::Adapter.description}"
target.send(:include, Minitest::Adapter)
end

true
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

module Mocha
module Integration
module MiniTest
# Integrates Mocha into recent versions of MiniTest.
module Minitest
# Integrates Mocha into recent versions of Minitest.
#
# See the source code for an example of how to integrate Mocha into a test library.
module Adapter
include Mocha::API

# @private
def self.applicable_to?(mini_test_version)
Gem::Requirement.new('>= 3.3.0').satisfied_by?(mini_test_version)
def self.applicable_to?(minitest_version)
Gem::Requirement.new('>= 3.3.0').satisfied_by?(minitest_version)
end

# @private
def self.description
'adapter for MiniTest gem >= v3.3.0'
'adapter for Minitest gem >= v3.3.0'
end

# @private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Mocha
module Integration
module MiniTest
module Minitest
def self.translate(exception)
return exception unless exception.is_a?(::Mocha::ExpectationError)
translated_exception = ::MiniTest::Assertion.new(exception.message)
translated_exception = ::Minitest::Assertion.new(exception.message)
translated_exception.set_backtrace(exception.backtrace)
translated_exception
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mocha/minitest.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'mocha/ruby_version'
require 'mocha/integration/mini_test'
require 'mocha/integration/minitest'

unless Mocha::Integration::MiniTest.activate
raise "MiniTest must be loaded *before* `require 'mocha/minitest'`."
unless Mocha::Integration::Minitest.activate
raise "Minitest must be loaded *before* `require 'mocha/minitest'`."
end
2 changes: 1 addition & 1 deletion test/acceptance/acceptance_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'mocha/mockery'
require 'introspection'

if Mocha::Detection::MiniTest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
if Mocha::Detection::Minitest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
require 'mocha/minitest'
else
require 'mocha/test_unit'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
require 'mocha/minitest'
require 'integration/shared_tests'

class MiniTestTest < Mocha::TestCase
class MinitestTest < Mocha::TestCase
include SharedTests
end
10 changes: 5 additions & 5 deletions test/mini_test_result.rb → test/minitest_result_pre_v5.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'stringio'
require 'minitest/unit'
require 'mocha/detection/minitest'

class MiniTestResult
minitest_version = Gem::Version.new(::MiniTest::Unit::VERSION)
class MinitestResult
minitest_version = Gem::Version.new(Mocha::Detection::Minitest.version)
if Gem::Requirement.new('<= 4.6.1').satisfied_by?(minitest_version)
FAILURE_PATTERN = /(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n/m
ERROR_PATTERN = /(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n/m
Expand Down Expand Up @@ -75,11 +75,11 @@ def passed?
end

def failures
@runner.report.map { |puked| MiniTestResult.parse_failure(puked) }.compact
@runner.report.map { |puked| MinitestResult.parse_failure(puked) }.compact
end

def errors
@runner.report.map { |puked| MiniTestResult.parse_error(puked) }.compact
@runner.report.map { |puked| MinitestResult.parse_error(puked) }.compact
end

def failure_messages
Expand Down
12 changes: 3 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'parameter_matchers'))
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'acceptance'))

require 'mocha/detection/mini_test'
require 'mocha/detection/minitest'

begin
require 'minitest'
# rubocop:disable Lint/HandleExceptions
rescue LoadError
end
# rubocop:enable Lint/HandleExceptions
begin
require 'minitest/unit'
# rubocop:disable Lint/HandleExceptions
rescue LoadError
end
# rubocop:enable Lint/HandleExceptions

module Mocha; end

if (minitest_testcase = Mocha::Detection::MiniTest.testcase) && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
if (minitest_testcase = Mocha::Detection::Minitest.testcase) && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
begin
require 'minitest/autorun'
rescue LoadError
MiniTest::Unit.autorun
Minitest::Unit.autorun
end
# rubocop:disable Style/ClassAndModuleChildren
class Mocha::TestCase < minitest_testcase
Expand Down
12 changes: 6 additions & 6 deletions test/test_runner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'assertions'

require 'mocha/detection/mini_test'
require 'mocha/detection/minitest'

module TestRunner
def run_as_test(&block)
Expand All @@ -20,20 +20,20 @@ def run_as_tests(methods = {})

tests = methods.keys.select { |m| m.to_s[/^test/] }.map { |m| test_class.new(m) }

if Mocha::Detection::MiniTest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
if Mocha::Detection::Minitest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
minitest_version = Gem::Version.new(Mocha::Detection::Minitest.version)
if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
require File.expand_path('../minitest_result', __FILE__)
tests.each(&:run)
Minitest::Runnable.runnables.delete(test_class)
test_result = MinitestResult.new(tests)
elsif Gem::Requirement.new('> 0.0.0', '< 5.0.0').satisfied_by?(minitest_version)
require File.expand_path('../mini_test_result', __FILE__)
runner = MiniTest::Unit.new
require File.expand_path('../minitest_result_pre_v5', __FILE__)
runner = Minitest::Unit.new
tests.each do |test|
test.run(runner)
end
test_result = MiniTestResult.new(runner, tests)
test_result = MinitestResult.new(runner, tests)
end
else
require File.expand_path('../test_unit_result', __FILE__)
Expand Down

0 comments on commit e7134d5

Please sign in to comment.