Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

+ Refactored lifecycle hooks into a module.

+ Refactored deprecated hook system into a module.

[git-p4: depot-paths = "//src/minitest/dev/": change = 7656]
  • Loading branch information...
commit fa9f8b8e750a8eb6c01a1ae28b88b58c81a2b093 1 parent cdc31af
@zenspider zenspider authored
Showing with 140 additions and 122 deletions.
  1. +140 −122 lib/minitest/unit.rb
View
262 lib/minitest/unit.rb
@@ -1022,12 +1022,151 @@ def windows? platform = RUBY_PLATFORM
end
##
+ # Provides before/after hooks for setup and teardown. These are
+ # meant for library writers, NOT for regular test authors. See
+ # #before_setup for an example.
+
+ module LifecycleHooks
+ ##
+ # Runs before every test, after setup. This hook is meant for
+ # libraries to extend minitest. It is not meant to be used by
+ # test developers.
+ #
+ # See #before_setup for an example.
+
+ def after_setup; end
+
+ ##
+ # Runs before every test, before setup. This hook is meant for
+ # libraries to extend minitest. It is not meant to be used by
+ # test developers.
+ #
+ # As a simplistic example:
+ #
+ # module MyMinitestPlugin
+ # def before_setup
+ # super
+ # # ... stuff to do before setup is run
+ # end
+ #
+ # def after_setup
+ # # ... stuff to do after setup is run
+ # super
+ # end
+ #
+ # def before_teardown
+ # super
+ # # ... stuff to do before teardown is run
+ # end
+ #
+ # def after_teardown
+ # # ... stuff to do after teardown is run
+ # super
+ # end
+ # end
+ #
+ # class MiniTest::Unit::TestCase
+ # include MyMinitestPlugin
+ # end
+
+ def before_setup; end
+
+ ##
+ # Runs after every test, before teardown. This hook is meant for
+ # libraries to extend minitest. It is not meant to be used by
+ # test developers.
+ #
+ # See #before_setup for an example.
+
+ def before_teardown; end
+
+ ##
+ # Runs after every test, after teardown. This hook is meant for
+ # libraries to extend minitest. It is not meant to be used by
+ # test developers.
+ #
+ # See #before_setup for an example.
+
+ def after_teardown; end
+ end
+
+ module Deprecated # :nodoc:
+
+ ##
+ # This entire module is deprecated and slated for removal on 2013-01-01.
+
+ module Hooks
+ ##
+ # Adds a block of code that will be executed before every
+ # TestCase is run.
+ #
+ # NOTE: This method is deprecated, use before/after_setup. It
+ # will be removed on 2013-01-01.
+
+ def self.add_setup_hook arg=nil, &block
+ warn "NOTE: MiniTest::Unit::TestCase.add_setup_hook is deprecated, use before/after_setup via a module (and call super!). It will be removed on 2013-01-01. Called from #{caller.first}"
+ hook = arg || block
+ @setup_hooks << hook
+ end
+
+ def self.setup_hooks # :nodoc:
+ if superclass.respond_to? :setup_hooks then
+ superclass.setup_hooks
+ else
+ []
+ end + @setup_hooks
+ end
+
+ def run_setup_hooks # :nodoc:
+ _run_hooks self.class.setup_hooks
+ end
+
+ def _run_hooks hooks # :nodoc:
+ hooks.each do |hook|
+ if hook.respond_to?(:arity) && hook.arity == 1
+ hook.call(self)
+ else
+ hook.call
+ end
+ end
+ end
+
+ ##
+ # Adds a block of code that will be executed after every
+ # TestCase is run.
+ #
+ # NOTE: This method is deprecated, use before/after_teardown. It
+ # will be removed on 2013-01-01.
+
+ def self.add_teardown_hook arg=nil, &block
+ warn "NOTE: MiniTest::Unit::TestCase#add_teardown_hook is deprecated, use before/after_teardown. It will be removed on 2013-01-01. Called from #{caller.first}"
+ hook = arg || block
+ @teardown_hooks << hook
+ end
+
+ def self.teardown_hooks # :nodoc:
+ if superclass.respond_to? :teardown_hooks then
+ superclass.teardown_hooks
+ else
+ []
+ end + @teardown_hooks
+ end
+
+ def run_teardown_hooks # :nodoc:
+ _run_hooks self.class.teardown_hooks.reverse
+ end
+ end
+ end
+
+ ##
# Subclass TestCase to create your own tests. Typically you'll want a
# TestCase subclass per implementation class.
#
# See MiniTest::Assertions
class TestCase
+ include LifecycleHooks
+ include Deprecated::Hooks
include Guard
extend Guard
@@ -1170,140 +1309,19 @@ def passed?
def setup; end
##
- # Runs before every test, after setup. This hook is meant for
- # libraries to extend minitest. It is not meant to be used by
- # test developers.
- #
- # See #before_setup for an example.
-
- def after_setup; end
-
- ##
- # Runs before every test, before setup. This hook is meant for
- # libraries to extend minitest. It is not meant to be used by
- # test developers.
- #
- # As a simplistic example:
- #
- # module MyMinitestPlugin
- # def before_setup
- # super
- # # ... stuff to do before setup is run
- # end
- #
- # def after_setup
- # # ... stuff to do after setup is run
- # super
- # end
- #
- # def before_teardown
- # super
- # # ... stuff to do before teardown is run
- # end
- #
- # def after_teardown
- # # ... stuff to do after teardown is run
- # super
- # end
- # end
- #
- # class MiniTest::Unit::TestCase
- # include MyMinitestPlugin
- # end
-
- def before_setup; end
-
- ##
# Runs after every test. Use this to clean up after each test
# run.
def teardown; end
- ##
- # Runs after every test, before teardown. This hook is meant for
- # libraries to extend minitest. It is not meant to be used by
- # test developers.
- #
- # See #before_setup for an example.
-
- def before_teardown; end
-
- ##
- # Runs after every test, after teardown. This hook is meant for
- # libraries to extend minitest. It is not meant to be used by
- # test developers.
- #
- # See #before_setup for an example.
-
- def after_teardown; end
-
def self.reset_setup_teardown_hooks # :nodoc:
+ # also deprecated... believe it.
@setup_hooks = []
@teardown_hooks = []
end
reset_setup_teardown_hooks
- ##
- # Adds a block of code that will be executed before every
- # TestCase is run.
- #
- # NOTE: This method is deprecated, use before/after_setup. It
- # will be removed on 2013-01-01.
-
- def self.add_setup_hook arg=nil, &block
- warn "NOTE: MiniTest::Unit::TestCase.add_setup_hook is deprecated, use before/after_setup via a module (and call super!). It will be removed on 2013-01-01. Called from #{caller.first}"
- hook = arg || block
- @setup_hooks << hook
- end
-
- def self.setup_hooks # :nodoc:
- if superclass.respond_to? :setup_hooks then
- superclass.setup_hooks
- else
- []
- end + @setup_hooks
- end
-
- def run_setup_hooks # :nodoc:
- _run_hooks self.class.setup_hooks
- end
-
- def _run_hooks hooks # :nodoc:
- hooks.each do |hook|
- if hook.respond_to?(:arity) && hook.arity == 1
- hook.call(self)
- else
- hook.call
- end
- end
- end
-
- ##
- # Adds a block of code that will be executed after every
- # TestCase is run.
- #
- # NOTE: This method is deprecated, use before/after_teardown. It
- # will be removed on 2013-01-01.
-
- def self.add_teardown_hook arg=nil, &block
- warn "NOTE: MiniTest::Unit::TestCase#add_teardown_hook is deprecated, use before/after_teardown. It will be removed on 2013-01-01. Called from #{caller.first}"
- hook = arg || block
- @teardown_hooks << hook
- end
-
- def self.teardown_hooks # :nodoc:
- if superclass.respond_to? :teardown_hooks then
- superclass.teardown_hooks
- else
- []
- end + @teardown_hooks
- end
-
- def run_teardown_hooks # :nodoc:
- _run_hooks self.class.teardown_hooks.reverse
- end
-
include MiniTest::Assertions
end # class TestCase
end # class Unit
Please sign in to comment.
Something went wrong with that request. Please try again.