summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--lib/minitest/mock.rb2
-rw-r--r--lib/minitest/spec.rb37
-rw-r--r--lib/minitest/unit.rb280
-rw-r--r--test/minitest/metametameta.rb22
-rw-r--r--test/minitest/test_minitest_spec.rb27
-rw-r--r--test/minitest/test_minitest_unit.rb383
7 files changed, 336 insertions, 420 deletions
diff --git a/ChangeLog b/ChangeLog
index 3bdc066355..a60dfec052 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 21 09:32:41 2012 Ryan Davis <ryand-ruby@zenspider.com>
+
+ * lib/minitest/*: Imported minitest 3.3.0 (r7676)
+ * test/minitest/*: ditto
+
Tue Aug 21 09:05:32 2012 NAKAMURA Usaku <usa@ruby-lang.org>
* test/testunit/tests_for_parallel/ptest_forth.rb: added a test case
diff --git a/lib/minitest/mock.rb b/lib/minitest/mock.rb
index 38f15757eb..b741b8e742 100644
--- a/lib/minitest/mock.rb
+++ b/lib/minitest/mock.rb
@@ -57,7 +57,7 @@ module MiniTest
self
end
- def __call name, data
+ def __call name, data # :nodoc:
case data
when Hash then
"#{name}(#{data[:args].inspect[1..-2]}) => #{data[:retval].inspect}"
diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb
index 02c57ec7ef..1d85cd9f32 100644
--- a/lib/minitest/spec.rb
+++ b/lib/minitest/spec.rb
@@ -162,10 +162,11 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
#
# Equivalent to MiniTest::Unit::TestCase#setup.
- def self.before type = :each, &block
- raise "unsupported before type: #{type}" unless type == :each
-
- add_setup_hook {|tc| tc.instance_eval(&block) }
+ def self.before type = nil, &block
+ define_method :setup do
+ super()
+ self.instance_eval(&block)
+ end
end
##
@@ -175,10 +176,11 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
#
# Equivalent to MiniTest::Unit::TestCase#teardown.
- def self.after type = :each, &block
- raise "unsupported after type: #{type}" unless type == :each
-
- add_teardown_hook {|tc| tc.instance_eval(&block) }
+ def self.after type = nil, &block
+ define_method :teardown do
+ self.instance_eval(&block)
+ super()
+ end
end
##
@@ -247,14 +249,6 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
end
# :stopdoc:
- def after_setup
- run_setup_hooks
- end
-
- def before_teardown
- run_teardown_hooks
- end
-
class << self
attr_reader :desc
alias :specify :it
@@ -290,11 +284,11 @@ module MiniTest::Expectations
#
# n.must_be_close_to m [, delta]
#
- # :method: must_be_within_delta
+ # :method: must_be_close_to
infect_an_assertion :assert_in_delta, :must_be_close_to
- alias :must_be_within_delta :must_be_close_to
+ alias :must_be_within_delta :must_be_close_to # :nodoc:
##
# See MiniTest::Assertions#assert_in_epsilon
@@ -450,12 +444,11 @@ module MiniTest::Expectations
#
# n.wont_be_close_to m [, delta]
#
- # :method: wont_be_within_delta
+ # :method: wont_be_close_to
- infect_an_assertion :refute_in_delta, :wont_be_within_delta
+ infect_an_assertion :refute_in_delta, :wont_be_close_to
- alias :wont_be_close_to :wont_be_within_delta
- # FIX: reverse aliases
+ alias :wont_be_within_delta :wont_be_close_to # :nodoc:
##
# See MiniTest::Assertions#refute_in_epsilon
diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb
index 1b95742af3..ed9c4fc721 100644
--- a/lib/minitest/unit.rb
+++ b/lib/minitest/unit.rb
@@ -191,9 +191,12 @@ module MiniTest
##
# Fails unless the block returns a true value.
+ #
+ # NOTE: This method is deprecated, use assert. It will be removed
+ # on 2013-01-01."
def assert_block msg = nil
- warn "NOTE: MiniTest::Unit::TestCase#assert_block is deprecated, use assert. It will be removed on or after 2012-06-01. Called from #{caller.first}"
+ warn "NOTE: MiniTest::Unit::TestCase#assert_block is deprecated, use assert. It will be removed on 2013-01-01. Called from #{caller.first}"
msg = message(msg) { "Expected block to return true value" }
assert yield, msg
end
@@ -652,7 +655,7 @@ module MiniTest
end
class Unit # :nodoc:
- VERSION = "3.2.0" # :nodoc:
+ VERSION = "3.3.0" # :nodoc:
attr_accessor :report, :failures, :errors, :skips # :nodoc:
attr_accessor :test_count, :assertion_count # :nodoc:
@@ -674,8 +677,8 @@ module MiniTest
@@after_tests = []
##
- # A simple hook allowing you to run a block of code after the
- # tests are done. Eg:
+ # A simple hook allowing you to run a block of code after _all_ of
+ # the tests are done. Eg:
#
# MiniTest::Unit.after_tests { p $debugging_info }
@@ -1026,12 +1029,151 @@ module MiniTest
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
@@ -1077,6 +1219,7 @@ module MiniTest
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue Exception => e
+ @passed = false
result = runner.puke self.class, self.__name__, e
end
end
@@ -1167,148 +1310,31 @@ module MiniTest
end
##
- # Runs before every test. Use this to refactor test initialization.
+ # Runs before every test. Use this to set up before each test
+ # run.
def setup; end
##
- # Runs before every test after setup. Use this to refactor test
- # initialization.
-
- def after_setup; end
-
- ##
- # Runs before every setup. Use this to refactor test initialization.
-
- def before_setup; end
-
- ##
- # Runs after every test. Use this to refactor test cleanup.
+ # Runs after every test. Use this to clean up after each test
+ # run.
def teardown; end
- ##
- # Runs after every test before teardown. Use this to refactor test
- # initialization.
-
- def before_teardown; end
-
- ##
- # Runs after every teardown. Use this to refactor test cleanup.
-
- 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. Equivalent to +setup+, but usable multiple times and without
- # re-opening any classes.
- #
- # All of the setup hooks will run in order after the +setup+ method, if
- # one is defined.
- #
- # The argument can be any object that responds to #call or a block.
- # That means that this call,
- #
- # MiniTest::Unit::TestCase.add_setup_hook { puts "foo" }
- #
- # ... is equivalent to:
- #
- # module MyTestSetup
- # def self.call
- # puts "foo"
- # end
- # end
- #
- # MiniTest::Unit::TestCase.add_setup_hook MyTestSetup
- #
- # The blocks passed to +add_setup_hook+ take an optional parameter that
- # will be the TestCase instance that is executing the block.
-
- def self.add_setup_hook arg=nil, &block
- 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:
- self.class.setup_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. Equivalent to +teardown+, but usable multiple times and without
- # re-opening any classes.
- #
- # All of the teardown hooks will run in reverse order after the
- # +teardown+ method, if one is defined.
- #
- # The argument can be any object that responds to #call or a block.
- # That means that this call,
- #
- # MiniTest::Unit::TestCase.add_teardown_hook { puts "foo" }
- #
- # ... is equivalent to:
- #
- # module MyTestTeardown
- # def self.call
- # puts "foo"
- # end
- # end
- #
- # MiniTest::Unit::TestCase.add_teardown_hook MyTestTeardown
- #
- # The blocks passed to +add_teardown_hook+ take an optional parameter
- # that will be the TestCase instance that is executing the block.
-
- def self.add_teardown_hook arg=nil, &block
- 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:
- self.class.teardown_hooks.reverse.each do |hook|
- if hook.respond_to?(:arity) && hook.arity == 1
- hook.call(self)
- else
- hook.call
- end
- end
- end
-
include MiniTest::Assertions
end # class TestCase
end # class Unit
end # module MiniTest
-Minitest = MiniTest # because ugh... I typo this all the time
+Minitest = MiniTest # :nodoc: because ugh... I typo this all the time
if $DEBUG then
module Test # :nodoc:
diff --git a/test/minitest/metametameta.rb b/test/minitest/metametameta.rb
index 35a27d51b6..a4f7dfa1e6 100644
--- a/test/minitest/metametameta.rb
+++ b/test/minitest/metametameta.rb
@@ -9,25 +9,29 @@ require 'tempfile'
require 'stringio'
require 'minitest/autorun'
+class MiniTest::Unit::TestCase
+ def clean s
+ s.gsub(/^ {6}/, '')
+ end
+end
+
class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
- def assert_report expected = nil
- expected ||= <<-EOM.gsub(/^ {6}/, '')
- Run options: --seed 42
+ def assert_report expected, flags = %w[--seed 42]
+ header = clean <<-EOM
+ Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
# Running tests:
- .
-
- Finished tests in 0.00
-
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
EOM
+ @tu.run flags
+
output = @output.string.dup
output.sub!(/Finished tests in .*/, "Finished tests in 0.00")
output.sub!(/Loaded suite .*/, 'Loaded suite blah')
output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')
+ output.gsub!(/0x[A-Fa-f0-9]+/, '0xXXX')
if windows? then
output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, '[FILE:LINE]')
@@ -37,7 +41,7 @@ class MetaMetaMetaTestCase < MiniTest::Unit::TestCase
output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
end
- assert_equal(expected, output)
+ assert_equal header + expected, output
end
def setup
diff --git a/test/minitest/test_minitest_spec.rb b/test/minitest/test_minitest_spec.rb
index d267e8d7d0..56aa78d620 100644
--- a/test/minitest/test_minitest_spec.rb
+++ b/test/minitest/test_minitest_spec.rb
@@ -637,20 +637,20 @@ class TestMeta < MiniTest::Unit::TestCase
def test_structure
x, y, z, * = util_structure
- assert_equal "top-level thingy", x.to_s
- assert_equal "top-level thingy::inner thingy", y.to_s
+ assert_equal "top-level thingy", x.to_s
+ assert_equal "top-level thingy::inner thingy", y.to_s
assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
- assert_equal "top-level thingy", x.desc
- assert_equal "inner thingy", y.desc
+ assert_equal "top-level thingy", x.desc
+ assert_equal "inner thingy", y.desc
assert_equal "very inner thingy", z.desc
- top_methods = %w(test_0001_top-level-it)
- inner_methods1 = %w(test_0001_inner-it)
+ top_methods = %w(setup teardown test_0001_top-level-it)
+ inner_methods1 = %w(setup teardown test_0001_inner-it)
inner_methods2 = inner_methods1 +
%w(test_0002_anonymous test_0003_anonymous)
- assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
+ assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
end
@@ -658,13 +658,18 @@ class TestMeta < MiniTest::Unit::TestCase
def test_setup_teardown_behavior
_, _, z, before_list, after_list = util_structure
- tc = z.new(nil)
+ @tu = MiniTest::Unit.new
+ @output = StringIO.new("")
+ MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
+ MiniTest::Unit.output = @output
- tc.run_setup_hooks
- tc.run_teardown_hooks
+ tc = z.new :test_0002_anonymous
+ tc.run @tu
assert_equal [1, 2, 3], before_list
assert_equal [3, 2, 1], after_list
+ ensure
+ MiniTest::Unit.output = $stdout
end
def test_children
@@ -696,7 +701,7 @@ class TestMeta < MiniTest::Unit::TestCase
z = describe "second thingy" do end
end
- test_methods = ['test_0001_top level it', 'test_0002_не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world'].sort
+ test_methods = ['test_0001_top level it', 'test_0002_не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world'].sort
assert_equal test_methods, [x1, x2]
assert_equal test_methods,
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb
index b27f3fde33..28c6e93028 100644
--- a/test/minitest/test_minitest_unit.rb
+++ b/test/minitest/test_minitest_unit.rb
@@ -13,8 +13,8 @@ class AnError < StandardError; include MyModule; end
class ImmutableString < String; def inspect; super.freeze; end; end
class TestMiniTestUnit < MetaMetaMetaTestCase
- pwd = Pathname.new(File.expand_path(Dir.pwd))
- basedir = Pathname.new(File.expand_path("lib/minitest")) + 'mini'
+ pwd = Pathname.new File.expand_path Dir.pwd
+ basedir = Pathname.new(File.expand_path "lib/minitest") + 'mini'
basedir = basedir.relative_path_from(pwd).to_s
MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
@@ -152,12 +152,12 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
bt = util_expand_bt bt
ex = ["-e:1"]
- fu = MiniTest::filter_backtrace(bt)
+ fu = MiniTest::filter_backtrace bt
assert_equal ex, fu
end
def test_run_test
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
attr_reader :foo
def run_test name
@@ -171,25 +171,19 @@ class TestMiniTestUnit < MetaMetaMetaTestCase
end
end
- Object.const_set(:ATestCase, tc)
-
- @tu.run %w[--seed 42]
-
- expected = "Run options: --seed 42
-
-# Running tests:
+ expected = clean <<-EOM
+ .
-.
+ Finished tests in 0.00
-Finished tests in 0.00
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
+ EOM
-1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
-"
assert_report expected
end
def test_run_error
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -199,21 +193,13 @@ Finished tests in 0.00
end
end
- Object.const_set(:ATestCase, tc)
-
- @tu.run %w[--seed 42]
-
- expected = <<-EOM.gsub(/^ {6}/, '')
- Run options: --seed 42
-
- # Running tests:
-
+ expected = clean <<-EOM
E.
Finished tests in 0.00
1) Error:
- test_error(ATestCase):
+ test_error(#<Class:0xXXX>):
RuntimeError: unhandled exception
FILE:LINE:in `test_error'
@@ -224,7 +210,7 @@ Finished tests in 0.00
end
def test_run_error_teardown
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -234,30 +220,24 @@ Finished tests in 0.00
end
end
- Object.const_set(:ATestCase, tc)
-
- @tu.run %w[--seed 42]
-
- expected = "Run options: --seed 42
+ expected = clean <<-EOM
+ E
-# Running tests:
-
-E
+ Finished tests in 0.00
-Finished tests in 0.00
+ 1) Error:
+ test_something(#<Class:0xXXX>):
+ RuntimeError: unhandled exception
+ FILE:LINE:in `teardown'
- 1) Error:
-test_something(ATestCase):
-RuntimeError: unhandled exception
- FILE:LINE:in `teardown'
+ 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
+ EOM
-1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
-"
assert_report expected
end
def test_run_failing
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -267,29 +247,23 @@ RuntimeError: unhandled exception
end
end
- Object.const_set(:ATestCase, tc)
-
- @tu.run %w[--seed 42]
+ expected = clean <<-EOM
+ F.
- expected = "Run options: --seed 42
-
-# Running tests:
-
-F.
+ Finished tests in 0.00
-Finished tests in 0.00
+ 1) Failure:
+ test_failure(#<Class:0xXXX>) [FILE:LINE]:
+ Failed assertion, no message given.
- 1) Failure:
-test_failure(ATestCase) [FILE:LINE]:
-Failed assertion, no message given.
+ 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
+ EOM
-2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
-"
assert_report expected
end
def test_run_failing_filtered
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -299,39 +273,37 @@ Failed assertion, no message given.
end
end
- Object.const_set(:ATestCase, tc)
+ expected = clean <<-EOM
+ .
- @tu.run %w[--name /some|thing/ --seed 42]
-
- expected = "Run options: --name \"/some|thing/\" --seed 42
-
-# Running tests:
-
-.
+ Finished tests in 0.00
-Finished tests in 0.00
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
+ EOM
-1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
-"
- assert_report expected
+ assert_report expected, %w[--name /some|thing/ --seed 42]
end
def test_run_passing
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
end
- Object.const_set(:ATestCase, tc)
+ expected = clean <<-EOM
+ .
- @tu.run %w[--seed 42]
+ Finished tests in 0.00
- assert_report
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
+ EOM
+
+ assert_report expected
end
def test_run_skip
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -341,25 +313,19 @@ Finished tests in 0.00
end
end
- Object.const_set(:ATestCase, tc)
-
- @tu.run %w[--seed 42]
+ expected = clean <<-EOM
+ S.
- expected = "Run options: --seed 42
-
-# Running tests:
-
-S.
+ Finished tests in 0.00
-Finished tests in 0.00
+ 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
+ EOM
-2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
-"
assert_report expected
end
def test_run_skip_verbose
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
def test_something
assert true
end
@@ -369,27 +335,21 @@ Finished tests in 0.00
end
end
- Object.const_set(:ATestCase, tc)
+ expected = clean <<-EOM
+ #<Class:0xXXX>#test_skip = 0.00 s = S
+ #<Class:0xXXX>#test_something = 0.00 s = .
- @tu.run %w[--seed 42 --verbose]
-
- expected = "Run options: --seed 42 --verbose
-
-# Running tests:
-
-ATestCase#test_skip = 0.00 s = S
-ATestCase#test_something = 0.00 s = .
+ Finished tests in 0.00
-Finished tests in 0.00
+ 1) Skipped:
+ test_skip(#<Class:0xXXX>) [FILE:LINE]:
+ not yet
- 1) Skipped:
-test_skip(ATestCase) [FILE:LINE]:
-not yet
+ 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
+ EOM
-2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
-"
- assert_report expected
+ assert_report expected, %w[--seed 42 --verbose]
end
def test_default_runner_is_minitest_unit
@@ -397,18 +357,15 @@ not yet
end
def test_run_with_other_runner
-
- runner = Class.new(MiniTest::Unit) do
- # Run once before each suite
- def _run_suite(suite, type)
- begin
- suite.before_suite
- super(suite, type)
- end
+ MiniTest::Unit.runner = Class.new MiniTest::Unit do
+ def _run_suite suite, type
+ suite.before_suite # Run once before each suite
+ super suite, type
end
- end
+ end.new
- tc = Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
+ def self.name; "wacky!" end
def self.before_suite
MiniTest::Unit.output.puts "Running #{self.name} tests"
@@ -424,22 +381,15 @@ not yet
end
end
- Object.const_set(:ATestCase, tc)
- MiniTest::Unit.runner = runner.new
- @tu.run %w[--seed 42]
-
- # We should only see 'running ATestCase tests' once
- expected = "Run options: --seed 42
+ expected = clean <<-EOM
+ Running wacky! tests
+ ..
-# Running tests:
-
-Running ATestCase tests
-..
+ Finished tests in 0.00
-Finished tests in 0.00
+ 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
+ EOM
-2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
-"
assert_report expected
end
@@ -455,7 +405,6 @@ Finished tests in 0.00
end
yield
-
ensure
Class.class_eval do
alias inherited inherited_without_hacks
@@ -478,7 +427,7 @@ Finished tests in 0.00
def test_before_setup
call_order = []
- Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
define_method :setup do
super()
call_order << :setup
@@ -497,9 +446,31 @@ Finished tests in 0.00
assert_equal expected, call_order
end
+ def test_passed_eh_teardown_good
+ test_class = Class.new MiniTest::Unit::TestCase do
+ def teardown; assert true; end
+ def test_omg; assert true; end
+ end
+
+ test = test_class.new :test_omg
+ test.run @tu
+ assert test.passed?
+ end
+
+ def test_passed_eh_teardown_flunked
+ test_class = Class.new MiniTest::Unit::TestCase do
+ def teardown; flunk; end
+ def test_omg; assert true; end
+ end
+
+ test = test_class.new :test_omg
+ test.run @tu
+ refute test.passed?
+ end
+
def test_after_teardown
call_order = []
- Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
define_method :teardown do
super()
call_order << :teardown
@@ -520,7 +491,7 @@ Finished tests in 0.00
def test_all_teardowns_are_guaranteed_to_run
call_order = []
- Class.new(MiniTest::Unit::TestCase) do
+ Class.new MiniTest::Unit::TestCase do
define_method :after_teardown do
super()
call_order << :after_teardown
@@ -548,91 +519,15 @@ Finished tests in 0.00
assert_equal expected, call_order
end
- def test_setup_hooks
- call_order = []
-
- tc = Class.new(MiniTest::Spec) do
- define_method :setup do
- super()
- call_order << :method
- end
-
- define_method :test2 do
- call_order << :test2
- end
-
- define_method :test1 do
- call_order << :test1
- end
- end
-
- tc.add_setup_hook lambda { call_order << :proc }
-
- argument = nil
-
- tc.add_setup_hook do |arg|
- argument = arg
- call_order << :block
- end
-
- @tu.run %w[--seed 42]
-
- assert_kind_of tc, argument
-
- expected = [:method, :proc, :block, :test1,
- :method, :proc, :block, :test2]
-
- assert_equal expected, call_order
- end
-
- def test_teardown_hooks
+ def test_setup_and_teardown_survive_inheritance
call_order = []
- tc = Class.new(MiniTest::Spec) do
- define_method :teardown do
- super()
- call_order << :method
- end
-
- define_method :test2 do
- call_order << :test2
- end
-
- define_method :test1 do
- call_order << :test1
- end
- end
-
- tc.add_teardown_hook lambda { call_order << :proc }
-
- argument = nil
-
- tc.add_teardown_hook do |arg|
- argument = arg
- call_order << :block
- end
-
- @tu.run %w[--seed 42]
-
- assert_kind_of tc, argument
-
- expected = [:test1, :block, :proc, :method,
- :test2, :block, :proc, :method]
-
- assert_equal expected, call_order
- end
-
- def test_setup_and_teardown_hooks_survive_inheritance
- call_order = []
-
- parent = Class.new(MiniTest::Spec) do
- define_method :setup do
- super()
+ parent = Class.new MiniTest::Spec do
+ before do
call_order << :setup_method
end
- define_method :teardown do
- super()
+ after do
call_order << :teardown_method
end
@@ -641,19 +536,12 @@ Finished tests in 0.00
end
end
- parent.add_setup_hook { call_order << :setup_hook }
- parent.add_teardown_hook { call_order << :teardown_hook }
-
_ = Class.new parent
- parent.add_setup_hook { call_order << :setup_after }
- parent.add_teardown_hook { call_order << :teardown_after }
-
@tu.run %w[--seed 42]
# Once for the parent class, once for the child
- expected = [:setup_method, :setup_hook, :setup_after, :test,
- :teardown_after, :teardown_hook, :teardown_method] * 2
+ expected = [:setup_method, :test, :teardown_method] * 2
assert_equal expected, call_order
end
@@ -683,7 +571,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
def teardown
assert_equal(@assertion_count, @tc._assertions,
"expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions
- Object.send :remove_const, :ATestCase if defined? ATestCase
end
def test_assert
@@ -706,7 +593,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
def test_assert_block
exp = ["NOTE: MiniTest::Unit::TestCase#assert_block is deprecated,",
- "use assert. It will be removed on or after 2012-06-01."].join " "
+ "use assert. It will be removed on 2013-01-01."].join " "
out, err = capture_io do
@tc.assert_block do
@@ -1104,12 +991,14 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
end
- expected = "[RuntimeError] exception expected, not
-Class: <SyntaxError>
-Message: <\"icky\">
----Backtrace---
-FILE:LINE:in `test_assert_raises_triggered_different'
----------------"
+ expected = clean <<-EOM.chomp
+ [RuntimeError] exception expected, not
+ Class: <SyntaxError>
+ Message: <\"icky\">
+ ---Backtrace---
+ FILE:LINE:in `test_assert_raises_triggered_different'
+ ---------------
+ EOM
actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
@@ -1124,7 +1013,7 @@ FILE:LINE:in `test_assert_raises_triggered_different'
end
end
- expected = <<-EOM.gsub(/^ {6}/, '').chomp
+ expected = clean <<-EOM
XXX.
[RuntimeError] exception expected, not
Class: <SyntaxError>
@@ -1137,7 +1026,7 @@ FILE:LINE:in `test_assert_raises_triggered_different'
actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
- assert_equal expected, actual
+ assert_equal expected.chomp, actual
end
def test_assert_raises_triggered_none
@@ -1171,12 +1060,14 @@ FILE:LINE:in `test_assert_raises_triggered_different'
end
end
- expected = "[StandardError] exception expected, not
-Class: <AnError>
-Message: <\"AnError\">
----Backtrace---
-FILE:LINE:in `test_assert_raises_triggered_subclass'
----------------"
+ expected = clean <<-EOM.chomp
+ [StandardError] exception expected, not
+ Class: <AnError>
+ Message: <\"AnError\">
+ ---Backtrace---
+ FILE:LINE:in `test_assert_raises_triggered_subclass'
+ ---------------
+ EOM
actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
@@ -1255,14 +1146,14 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
end
def test_assert_throws
- @tc.assert_throws(:blah) do
+ @tc.assert_throws :blah do
throw :blah
end
end
def test_assert_throws_different
util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do
- @tc.assert_throws(:blah) do
+ @tc.assert_throws :blah do
throw :not_blah
end
end
@@ -1270,7 +1161,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_assert_throws_unthrown
util_assert_triggered 'Expected :blah to have been thrown.' do
- @tc.assert_throws(:blah) do
+ @tc.assert_throws :blah do
# do nothing
end
end
@@ -1315,21 +1206,13 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes
end
- def test_class_inherited
- @assertion_count = 0
-
- Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
-
- assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
- end
-
def test_class_test_suites
@assertion_count = 0
- Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
+ tc = Class.new(MiniTest::Unit::TestCase)
assert_equal 1, MiniTest::Unit::TestCase.test_suites.size
- assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
+ assert_equal [tc], MiniTest::Unit::TestCase.test_suites
end
def test_expectation
@@ -1565,7 +1448,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_test_methods_random
@assertion_count = 0
- sample_test_case = Class.new(MiniTest::Unit::TestCase) do
+ sample_test_case = Class.new MiniTest::Unit::TestCase do
def test_test1; assert "does not matter" end
def test_test2; assert "does not matter" end
def test_test3; assert "does not matter" end
@@ -1579,7 +1462,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_test_methods_sorted
@assertion_count = 0
- sample_test_case = Class.new(MiniTest::Unit::TestCase) do
+ sample_test_case = Class.new MiniTest::Unit::TestCase do
def self.test_order; :sorted end
def test_test3; assert "does not matter" end
def test_test2; assert "does not matter" end
@@ -1613,7 +1496,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
end
def util_assert_triggered expected, klass = MiniTest::Assertion
- e = assert_raises(klass) do
+ e = assert_raises klass do
yield
end