describes are nestable, via subclassing. before/after/def inherits, specs do not... and minor bug fixes

[git-p4: depot-paths = "//src/minitest/dev/": change = 4638]
This commit is contained in:
Ryan Davis 2009-02-23 15:31:58 -08:00
parent f921db180e
commit a11b03781c
3 changed files with 55 additions and 10 deletions

View File

@ -1,3 +1,14 @@
=== 1.3.2 / ??
* 1 minor enhancement:
* describes are nestable (via subclass). before/after/def inherits, specs don't.
* 2 bug fixes:
* Fixed location on must/wont.
* Switched to __name__ to avoid common ivar name.
=== 1.3.1 / 2009-01-20
* 1 minor enhancement:

View File

@ -49,15 +49,26 @@ end
module Kernel
def describe desc, &block
cls = Class.new(MiniTest::Spec)
Object.const_set desc.to_s.split(/\W+/).map { |s| s.capitalize }.join, cls
stack = MiniTest::Spec.describe_stack
cls = Class.new(stack.last)
klass_name = desc.to_s.split(/\W+/).map { |s| s.capitalize }.join + "Spec"
Object.const_set klass_name, cls
cls.nuke_test_methods!
stack.push cls
cls.class_eval(&block)
stack.pop
end
private :describe
end
class MiniTest::Spec < MiniTest::Unit::TestCase
@@describe_stack = [MiniTest::Spec]
def self.describe_stack
@@describe_stack
end
def self.current
@@current_spec
end
@ -67,14 +78,37 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
@@current_spec = self
end
def self.nuke_test_methods!
self.public_instance_methods.grep(/^test_/).each do |name|
send :remove_method, name rescue nil
end
end
def self.before(type = :each, &block)
raise "unsupported before type: #{type}" unless type == :each
define_method :setup, &block
sklass = self.superclass
define_method :setup do
# regular super() warns
super_setup = sklass.instance_method :setup
super_setup.bind(self).call if super_setup
instance_eval(&block)
end
end
def self.after(type = :each, &block)
raise "unsupported after type: #{type}" unless type == :each
define_method :teardown, &block
sklass = self.superclass
define_method :teardown do
# regular super() warns
super_teardown = sklass.instance_method :teardown
super_teardown.bind(self).call if super_teardown
super()
instance_eval(&block)
end
end
def self.it desc, &block

View File

@ -337,7 +337,7 @@ module MiniTest
def location e
last_before_assertion = ""
e.backtrace.reverse_each do |s|
break if s =~ /in .(assert|refute|flunk|pass|fail|raise)/
break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
last_before_assertion = s
end
last_before_assertion.sub(/:in .*$/, '')
@ -424,30 +424,30 @@ module MiniTest
end
class TestCase
attr_reader :name
attr_reader :__name__
def run runner
result = '.'
begin
@passed = nil
self.setup
self.__send__ self.name
self.__send__ self.__name__
@passed = true
rescue Exception => e
@passed = false
result = runner.puke(self.class, self.name, e)
result = runner.puke(self.class, self.__name__, e)
ensure
begin
self.teardown
rescue Exception => e
result = runner.puke(self.class, self.name, e)
result = runner.puke(self.class, self.__name__, e)
end
end
result
end
def initialize name
@name = name
@__name__ = name
@passed = nil
end