summaryrefslogtreecommitdiff
path: root/lib/minitest
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-01 05:12:55 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-01 05:12:55 +0000
commite4b16eff50b01b396838a2229c540f752fc975bc (patch)
tree0ad62807ed8aba23e1e58227fb9587dd46386f8c /lib/minitest
parentb204eabf0563bffc93d8a4b10e4a5e69c5eb2201 (diff)
MOSTLY Imported minitest 2.2.1 (r6277)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31887 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest')
-rw-r--r--lib/minitest/benchmark.rb14
-rw-r--r--lib/minitest/spec.rb91
-rw-r--r--lib/minitest/unit.rb135
3 files changed, 201 insertions, 39 deletions
diff --git a/lib/minitest/benchmark.rb b/lib/minitest/benchmark.rb
index 83f62917ca..77c0afafb7 100644
--- a/lib/minitest/benchmark.rb
+++ b/lib/minitest/benchmark.rb
@@ -111,10 +111,16 @@ class MiniTest::Unit
##
# Runs the given +work+ and asserts that the times gathered fit to
- # match a constant rate (eg, linear slope == 0) within a given error
- # +threshold+.
+ # match a constant rate (eg, linear slope == 0) within a given
+ # +threshold+. Note: because we're testing for a slope of 0, R^2
+ # is not a good determining factor for the fit, so the threshold
+ # is applied against the slope itself. As such, you probably want
+ # to tighten it from the default.
#
- # Fit is calculated by #fit_constant.
+ # See http://www.graphpad.com/curvefit/goodness_of_fit.htm for
+ # more details.
+ #
+ # Fit is calculated by #fit_linear.
#
# Ranges are specified by ::bench_range.
#
@@ -328,7 +334,7 @@ class MiniTest::Spec
# end
# end
- def self.bench_performance_linear name, threshold = 0.9, &work
+ def self.bench_performance_linear name, threshold = 0.99, &work
bench name do
assert_performance_linear threshold, &work
end
diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb
index 79f2102bca..4b16cd03ec 100644
--- a/lib/minitest/spec.rb
+++ b/lib/minitest/spec.rb
@@ -61,17 +61,41 @@ module Kernel
#
# TODO: find good tutorial url.
#
- # Defines a test class subclassing from either
- # MiniTest::Unit::TestCase or from the surrounding describe's class.
-
- def describe desc, &block
+ # Defines a test class subclassing from either MiniTest::Spec or
+ # from the surrounding describe's class. The surrounding class may
+ # subclass MiniTest::Spec manually in order to easily share code:
+ #
+ # class MySpec < MiniTest::Spec
+ # # ... shared code ...
+ # end
+ #
+ # class TestStuff < MySpec
+ # it "does stuff" do
+ # # shared code available here
+ # end
+ # describe "inner stuff" do
+ # it "still does stuff" do
+ # # ...and here
+ # end
+ # end
+ # end
+
+ def describe desc, &block # :doc:
stack = MiniTest::Spec.describe_stack
name = [stack.last, desc].compact.join("::")
- cls = Class.new(stack.last || MiniTest::Spec)
+ sclas = stack.last || if Class === self && self < MiniTest::Spec then
+ self
+ else
+ MiniTest::Spec.spec_type desc
+ end
+ cls = Class.new sclas
+
+ sclas.children << cls unless cls == MiniTest::Spec
# :stopdoc:
# omg this sucks
(class << cls; self; end).send(:define_method, :to_s) { name }
+ (class << cls; self; end).send(:define_method, :desc) { desc }
# :startdoc:
cls.nuke_test_methods!
@@ -84,21 +108,40 @@ module Kernel
private :describe
end
-class Module
- def classes type = Object # :nodoc:
- constants.map { |n| const_get n }.find_all { |c|
- c.class == Class and type > c
- } - [self]
- end
-end
-
##
# MiniTest::Spec -- The faster, better, less-magical spec framework!
#
# For a list of expectations, see Object.
-
class MiniTest::Spec < MiniTest::Unit::TestCase
+ ##
+ # Contains pairs of matchers and Spec classes to be used to
+ # calculate the superclass of a top-level describe. This allows for
+ # automatically customizable spec types.
+ #
+ # See: register_spec_type and spec_type
+
+ TYPES = [[//, MiniTest::Spec]]
+
+ ##
+ # Register a new type of spec that matches the spec's description. Eg:
+ #
+ # register_spec_plugin(/Controller$/, MiniTest::Spec::Rails)
+
+ def self.register_spec_type matcher, klass
+ TYPES.unshift [matcher, klass]
+ end
+
+ ##
+ # Figure out the spec class to use based on a spec's description. Eg:
+ #
+ # spec_type("BlahController") # => MiniTest::Spec::Rails
+
+ def self.spec_type desc
+ desc = desc.to_s
+ TYPES.find { |re, klass| re === desc }.last
+ end
+
@@describe_stack = []
def self.describe_stack # :nodoc:
@@describe_stack
@@ -108,6 +151,10 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
@@current_spec
end
+ def self.children
+ @children ||= []
+ end
+
def initialize name # :nodoc:
super
@@current_spec = self
@@ -119,12 +166,22 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
end
end
+ ##
+ # Spec users want setup/teardown to be inherited and NOTHING ELSE.
+ # It is almost like method reuse is lost on them.
+
def self.define_inheritable_method name, &block # :nodoc:
+ # regular super() warns
super_method = self.superclass.instance_method name
+ teardown = name.to_s == "teardown"
+ super_before = super_method && ! teardown
+ super_after = super_method && teardown
+
define_method name do
- super_method.bind(self).call if super_method # regular super() warns
+ super_method.bind(self).call if super_before
instance_eval(&block)
+ super_method.bind(self).call if super_after
end
end
@@ -171,8 +228,8 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
define_method name, &block
- classes(MiniTest::Spec).each do |mod|
- mod.send :undef_method, name if mod.respond_to? name
+ self.children.each do |mod|
+ mod.send :undef_method, name if mod.public_method_defined? name
end
end
end
diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb
index e3a069a3e9..41f127a208 100644
--- a/lib/minitest/unit.rb
+++ b/lib/minitest/unit.rb
@@ -5,6 +5,7 @@
######################################################################
require 'optparse'
+require 'rbconfig'
##
# Minimal (mostly drop-in) replacement for test-unit.
@@ -65,9 +66,88 @@ module MiniTest
module Assertions
+ WINDOZE = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
+
+ ##
+ # Returns the diff command to use in #diff. Tries to intelligently
+ # figure out what diff to use.
+
+ def self.diff
+ @diff = if WINDOZE
+ "diff.exe -u"
+ else
+ if system("gdiff", __FILE__, __FILE__)
+ "gdiff -u" # solaris and kin suck
+ elsif system("diff", __FILE__, __FILE__)
+ "diff -u"
+ else
+ nil
+ end
+ end unless defined? @diff
+
+ @diff
+ end
+
+ ##
+ # Set the diff command to use in #diff.
+
+ def self.diff= o
+ @diff = o
+ end
+
+ ##
+ # Returns a diff between +exp+ and +act+. If there is no known
+ # diff command or if it doesn't make sense to diff the output
+ # (single line, short output), then it simply returns a basic
+ # comparison between the two.
+
+ def diff exp, act
+ require "tempfile"
+
+ expect = mu_pp_for_diff exp
+ butwas = mu_pp_for_diff act
+ result = nil
+
+ need_to_diff =
+ MiniTest::Assertions.diff &&
+ (expect.include?("\n") ||
+ butwas.include?("\n") ||
+ expect.size > 30 ||
+ butwas.size > 30 ||
+ expect == butwas)
+
+ return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
+ need_to_diff
+
+ Tempfile.open("expect") do |a|
+ a.puts expect
+ a.rewind
+ Tempfile.open("butwas") do |b|
+ b.puts butwas
+ b.rewind
+
+ result = `#{MiniTest::Assertions.diff} #{a.path} #{b.path}`
+ result.sub!(/^\-\-\- .+/, "--- expected")
+ result.sub!(/^\+\+\+ .+/, "+++ actual")
+
+ if result.empty? then
+ klass = exp.class
+ result = [
+ "No visible difference.",
+ "You should look at your implementation of #{klass}#==.",
+ expect
+ ].join "\n"
+ end
+ end
+ end
+
+ result
+ end
+
##
- # mu_pp gives a human-readable version of +obj+. By default #inspect is
- # called. You can override this to use #pretty_print if you want.
+ # This returns a human-readable version of +obj+. By default
+ # #inspect is called. You can override this to use #pretty_print
+ # if you want.
def mu_pp obj
s = obj.inspect
@@ -75,6 +155,16 @@ module MiniTest
s
end
+ ##
+ # This returns a diff-able human-readable version of +obj+. This
+ # differs from the regular mu_pp because it expands escaped
+ # newlines and makes hex-values generic (like object_ids). This
+ # uses mu_pp to do the first pass and then cleans it up.
+
+ def mu_pp_for_diff obj # TODO: possibly rename
+ mu_pp(obj).gsub(/\\n/, "\n").gsub(/0x[a-f0-9]+/m, '0xXXXXXX')
+ end
+
def _assertions= n # :nodoc:
@_assertions = n
end
@@ -114,12 +204,19 @@ module MiniTest
end
##
- # Fails unless <tt>exp == act</tt>.
+ # Fails unless <tt>exp == act</tt> printing the difference between
+ # the two, if possible.
+ #
+ # If there is no visible difference but the assertion fails, you
+ # should suspect that your #== is buggy, or your inspect output is
+ # missing crucial details.
+ #
+ # For floats use assert_in_delta.
#
- # For floats use assert_in_delta
+ # See also: MiniTest::Assertions.diff
def assert_equal exp, act, msg = nil
- msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" }
+ msg = message(msg) { diff exp, act }
assert(exp == act, msg)
end
@@ -181,7 +278,7 @@ module MiniTest
def assert_match exp, act, msg = nil
msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" }
assert_respond_to act, :"=~"
- exp = /#{Regexp.escape exp}/ if String === exp && String === act
+ exp = Regexp.new Regexp.escape exp if String === exp and String === act
assert exp =~ act, msg
end
@@ -225,8 +322,8 @@ module MiniTest
# Fails unless the block raises one of +exp+
def assert_raises *exp
- msg = String === exp.last ? exp.pop : nil
- msg = msg.to_s + "\n" if msg
+ msg = "#{exp.pop}\n" if String === exp.last
+
should_raise = false
begin
yield
@@ -346,7 +443,14 @@ module MiniTest
# Returns details for exception +e+
def exception_details e, msg
- "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{MiniTest::filter_backtrace(e.backtrace).join("\n")}\n---------------"
+ [
+ "#{msg}",
+ "Class: <#{e.class}>",
+ "Message: <#{e.message.inspect}>",
+ "---Backtrace---",
+ "#{MiniTest::filter_backtrace(e.backtrace).join("\n")}",
+ "---------------",
+ ].join "\n"
end
##
@@ -362,14 +466,8 @@ module MiniTest
def message msg = nil, &default
proc {
- if msg then
- msg = msg.to_s unless String === msg
- msg += '.' unless msg.empty?
- msg += "\n#{default.call}."
- msg.strip
- else
- "#{default.call}."
- end
+ custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
+ "#{custom_message}#{default.call}."
}
end
@@ -521,7 +619,7 @@ module MiniTest
end
class Unit
- VERSION = "2.0.2" # :nodoc:
+ VERSION = "2.2.1" # :nodoc:
attr_accessor :report, :failures, :errors, :skips # :nodoc:
attr_accessor :test_count, :assertion_count # :nodoc:
@@ -698,6 +796,7 @@ module MiniTest
e = case e
when MiniTest::Skip then
@skips += 1
+ return "S" unless @verbose
"Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
when MiniTest::Assertion then
@failures += 1