diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-06 22:58:07 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-06 22:58:07 +0000 |
commit | 06925a095259693cb3850617e8e01cd1cb3b05d8 (patch) | |
tree | 640d5b75d0a68603802053eaeca67d3e19cc0990 /lib/minitest | |
parent | 74972dcc6f51c9eec9a8c7ffb39ba3f86346a33c (diff) |
Imported minitest 1.5.0 r5596
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26247 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest')
-rw-r--r-- | lib/minitest/spec.rb | 42 | ||||
-rw-r--r-- | lib/minitest/unit.rb | 64 |
2 files changed, 66 insertions, 40 deletions
diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb index 81bd08867a..e730e205e8 100644 --- a/lib/minitest/spec.rb +++ b/lib/minitest/spec.rb @@ -9,7 +9,23 @@ require 'minitest/unit' class Module - def infect_with_assertions pos_prefix, neg_prefix, skip_re, map = {} + def infect_an_assertion meth, new_name, dont_flip = false + # warn "%-22p -> %p %p" % [meth, new_name, dont_flip] + self.class_eval <<-EOM + def #{new_name} *args, &block + return MiniTest::Spec.current.#{meth}(*args, &self) if + Proc === self + return MiniTest::Spec.current.#{meth}(args.first, self) if + args.size == 1 unless #{!!dont_flip} + return MiniTest::Spec.current.#{meth}(self, *args) + end + EOM + end + + def infect_with_assertions(pos_prefix, neg_prefix, + skip_re, + dont_flip_re = /\c0/, + map = {}) MiniTest::Assertions.public_instance_methods(false).each do |meth| meth = meth.to_s @@ -25,14 +41,7 @@ class Module regexp, replacement = map.find { |re, _| new_name =~ re } new_name.sub! regexp, replacement if replacement - # warn "%-22p -> %p %p" % [meth, new_name, regexp] - self.class_eval <<-EOM - def #{new_name} *args, &block - return MiniTest::Spec.current.#{meth}(*args, &self) if Proc === self - return MiniTest::Spec.current.#{meth}(args.first, self) if args.size == 1 - return MiniTest::Spec.current.#{meth}(self, *args) - end - EOM + infect_an_assertion meth, new_name, new_name =~ dont_flip_re end end end @@ -40,6 +49,7 @@ end Object.infect_with_assertions(:must, :wont, /^(must|wont)$|wont_(throw)| must_(block|not?_|nothing|raise$)/x, + /(must|wont)_(include|respond_to)/, /(must_throw)s/ => '\1', /(?!not)_same/ => '_be_same_as', /_in_/ => '_be_within_', @@ -57,7 +67,9 @@ module Kernel def describe desc, &block stack = MiniTest::Spec.describe_stack name = desc.to_s.split(/\W+/).map { |s| s.capitalize }.join + "Spec" - cls = Object.class_eval "class #{name} < #{stack.last}; end; #{name}" + prev = stack.last + name = "#{prev == MiniTest::Spec ? nil : prev}::#{name}" + cls = Object.class_eval "class #{name} < #{prev}; end; #{name}" cls.nuke_test_methods! @@ -85,7 +97,7 @@ class MiniTest::Spec < MiniTest::Unit::TestCase def self.nuke_test_methods! self.public_instance_methods.grep(/^test_/).each do |name| - send :remove_method, name rescue nil + self.send :undef_method, name end end @@ -99,11 +111,19 @@ class MiniTest::Spec < MiniTest::Unit::TestCase end def self.before(type = :each, &block) + if type == :all + warn "change before :all to before :each" + type = :each + end raise "unsupported before type: #{type}" unless type == :each define_inheritable_method :setup, &block end def self.after(type = :each, &block) + if type == :all # REFACTOR + warn "change before :all to before :each" + type = :each + end raise "unsupported after type: #{type}" unless type == :each define_inheritable_method :teardown, &block end diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb index e936f39fc7..1570b7f457 100644 --- a/lib/minitest/unit.rb +++ b/lib/minitest/unit.rb @@ -95,25 +95,25 @@ module MiniTest end def assert_includes collection, obj, msg = nil - msg = message(msg) { "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}" } - flip = (obj.respond_to? :include?) && ! (collection.respond_to? :include?) # HACK for specs - obj, collection = collection, obj if flip + msg = message(msg) { + "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}" + } assert_respond_to collection, :include? assert collection.include?(obj), msg end def assert_instance_of cls, obj, msg = nil - msg = message(msg) { "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}" } - flip = (Module === obj) && ! (Module === cls) # HACK for specs - obj, cls = cls, obj if flip + msg = message(msg) { + "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}" + } + assert obj.instance_of?(cls), msg end def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of msg = message(msg) { "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" } - flip = (Module === obj) && ! (Module === cls) # HACK for specs - obj, cls = cls, obj if flip + assert obj.kind_of?(cls), msg end @@ -136,28 +136,29 @@ module MiniTest def assert_raises *exp msg = String === exp.last ? exp.pop : nil + msg = msg.to_s + "\n" if msg should_raise = false begin yield should_raise = true rescue Exception => e + details = "#{msg}#{mu_pp(exp)} exception expected, not" assert(exp.any? { |ex| ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class - }, exception_details(e, "#{mu_pp(exp)} exception expected, not")) + }, exception_details(e, details)) return e end exp = exp.first if exp.size == 1 - flunk "#{mu_pp(exp)} expected but nothing was raised." if should_raise + flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised." if + should_raise end def assert_respond_to obj, meth, msg = nil msg = message(msg) { "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}" } - flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs - obj, meth = meth, obj if flip assert obj.respond_to?(meth), msg end @@ -247,13 +248,17 @@ module MiniTest end def refute_equal exp, act, msg = nil - msg = message(msg) { "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}" } + msg = message(msg) { + "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}" + } refute exp == act, msg end def refute_in_delta exp, act, delta = 0.001, msg = nil n = (exp - act).abs - msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to not be < #{delta}" } + msg = message(msg) { + "Expected #{exp} - #{act} (#{n}) to not be < #{delta}" + } refute delta > n, msg end @@ -262,24 +267,22 @@ module MiniTest end def refute_includes collection, obj, msg = nil - msg = message(msg) { "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}" } - flip = (obj.respond_to? :include?) && ! (collection.respond_to? :include?) # HACK for specs - obj, collection = collection, obj if flip + msg = message(msg) { + "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}" + } assert_respond_to collection, :include? refute collection.include?(obj), msg end def refute_instance_of cls, obj, msg = nil - msg = message(msg) { "Expected #{mu_pp(obj)} to not be an instance of #{cls}" } - flip = (Module === obj) && ! (Module === cls) # HACK for specs - obj, cls = cls, obj if flip + msg = message(msg) { + "Expected #{mu_pp(obj)} to not be an instance of #{cls}" + } refute obj.instance_of?(cls), msg end def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" } - flip = (Module === obj) && ! (Module === cls) # HACK for specs - obj, cls = cls, obj if flip refute obj.kind_of?(cls), msg end @@ -296,19 +299,22 @@ module MiniTest end def refute_operator o1, op, o2, msg = nil - msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}" } + msg = message(msg) { + "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}" + } refute o1.__send__(op, o2), msg end def refute_respond_to obj, meth, msg = nil msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" } - flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs - obj, meth = meth, obj if flip + refute obj.respond_to?(meth), msg end def refute_same exp, act, msg = nil - msg = message(msg) { "Expected #{mu_pp(act)} to not be the same as #{mu_pp(exp)}" } + msg = message(msg) { + "Expected #{mu_pp(act)} to not be the same as #{mu_pp(exp)}" + } refute exp.equal?(act), msg end @@ -319,7 +325,7 @@ module MiniTest end class Unit - VERSION = "1.4.2" + VERSION = "1.5.0" attr_accessor :report, :failures, :errors, :skips attr_accessor :test_count, :assertion_count @@ -520,8 +526,8 @@ module MiniTest include MiniTest::Assertions end # class TestCase - end # class Test -end # module Mini + end # class Unit +end # module MiniTest if $DEBUG then # this helps me ferret out porting issues |