Permalink
Browse files

+ Spec#before/after now do a simple define_method and call super. DUR.

+ Removed after_setup/before_teardown + run_X_hooks from Spec.
(removed another 84 lines of code/test)

[git-p4: depot-paths = "//src/minitest/dev/": change = 7654]
  • Loading branch information...
1 parent 2613629 commit 864e417eda34cf1a69661e9e750fb9c74a253727 @zenspider zenspider committed Jul 19, 2012
Showing with 31 additions and 115 deletions.
  1. +10 −16 lib/minitest/spec.rb
  2. +16 −11 test/minitest/test_minitest_spec.rb
  3. +5 −88 test/minitest/test_minitest_unit.rb
View
@@ -155,10 +155,11 @@ def self.nuke_test_methods! # :nodoc:
#
# 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
##
@@ -168,10 +169,11 @@ def self.before type = :each, &block
#
# 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
##
@@ -240,14 +242,6 @@ def self.to_s # :nodoc:
end
# :stopdoc:
- def after_setup
- run_setup_hooks
- end
-
- def before_teardown
- run_teardown_hooks
- end
-
class << self
attr_reader :desc
alias :specify :it
@@ -630,34 +630,39 @@ def test_spec_type
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
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
@@ -689,7 +694,7 @@ def test_describe_first_structure
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,
@@ -512,91 +512,15 @@ def test_omg; assert true; end
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
- 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
+ def test_setup_and_teardown_survive_inheritance
call_order = []
parent = Class.new MiniTest::Spec do
- define_method :setup do
- super()
+ before do
call_order << :setup_method
end
- define_method :teardown do
- super()
+ after do
call_order << :teardown_method
end
@@ -605,19 +529,12 @@ def test_setup_and_teardown_hooks_survive_inheritance
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
@@ -669,7 +586,7 @@ def test_assert__triggered_message
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

4 comments on commit 864e417

When you switched before/after to use super to call into setup/teardown, did you intend to remove the ability for a test group to have multiple before/after blocks?

Owner

zenspider replied Nov 14, 2012

I never intended to have that ability in the first place, so... yes. I guess.

Yeah, you could say that it isn't best practice, but we were using a macro to insert a VCR cassette before and eject it afterwards. We upgraded minitest and minitest-rails when fixing other bugs, and it bit us when superseding before blocks were used. The fix wasn't hard, but I just wanted to check with you in case you were open to restoring the functionality. I could put together a pull request, but it would probably mean returning to the less straightforward add_x_hook implementation. I'm fine either way. Thanks for your hard work.

Owner

zenspider replied Nov 19, 2012

Just push one set to a superclass and you're set.

Please sign in to comment.