1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

MOSTLY Imported minitest 2.2.1 (r6277)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31887 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ryan 2011-06-01 05:12:55 +00:00
parent b204eabf05
commit e4b16eff50
8 changed files with 524 additions and 57 deletions

View file

@ -1,3 +1,10 @@
Wed Jun 1 14:07:57 2011 Ryan Davis <ryand-ruby@zenspider.com>
* lib/minitest/*: MOSTLY Imported minitest 2.2.1 (r6277)... One
feature wouldn't run and I don't know how to fix it. I need
sora_h's help to get it running happy with test/unit.
* test/minitest/*: ditto
Wed Jun 1 12:35:50 2011 Ryan Davis <ryand-ruby@zenspider.com>
* lib/rubygems*: Import rubygems 1.8.5 (released @ 137c80f)

View file

@ -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

View file

@ -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.
# 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
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

View file

@ -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/
##
# 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.
# 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
##
# 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.
#
# For floats use assert_in_delta
# 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.
#
# 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

View file

@ -50,6 +50,21 @@ class TestMiniTestBenchmark < MiniTest::Unit::TestCase
assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
end
def test_fit_constant_clean
x = (1..5).to_a
y = [5.0, 5.0, 5.0, 5.0, 5.0]
assert_fit :linear, x, y, nil, 5.0, 0
end
def test_fit_constant_noisy
x = (1..5).to_a
y = [1.0, 1.2, 1.0, 0.8, 1.0]
# verified in numbers and R
assert_fit :linear, x, y, nil, 1.12, -0.04
end
def test_fit_linear_clean
# y = m * x + b where m = 2.2, b = 3.1
x = (1..5).to_a
@ -96,7 +111,7 @@ class TestMiniTestBenchmark < MiniTest::Unit::TestCase
def assert_fit msg, x, y, fit, exp_a, exp_b
a, b, rr = send "fit_#{msg}", x, y
assert_operator rr, :>=, fit
assert_operator rr, :>=, fit if fit
assert_in_delta exp_a, a
assert_in_delta exp_b, b
end

View file

@ -89,10 +89,10 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
def test_assign_per_mock_return_values
a = MiniTest::Mock.new
b = MiniTest::Mock.new
a.expect(:foo, :a)
b.expect(:foo, :b)
assert_equal :a, a.foo
assert_equal :b, b.foo
end

View file

@ -5,6 +5,7 @@
######################################################################
require 'minitest/spec'
require 'stringio'
MiniTest::Unit.autorun
@ -202,34 +203,112 @@ describe MiniTest::Spec do
end
class TestMeta < MiniTest::Unit::TestCase
def test_structure
def test_setup
srand 42
MiniTest::Unit::TestCase.reset
end
def util_structure
x = y = z = nil
before_list = []
after_list = []
x = describe "top-level thingy" do
before {}
after {}
before { before_list << 1 }
after { after_list << 1 }
it "top-level-it" do end
y = describe "inner thingy" do
before {}
before { before_list << 2 }
after { after_list << 2 }
it "inner-it" do end
z = describe "very inner thingy" do
before {}
before { before_list << 3 }
after { after_list << 3 }
it "inner-it" do end
end
end
end
return x, y, z, before_list, after_list
end
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::inner thingy::very inner thingy", z.to_s
assert_equal "top-level thingy", x.desc
assert_equal "inner thingy", y.desc
assert_equal "very inner thingy", z.desc
top_methods = %w(setup teardown test_0001_top_level_it)
inner_methods = %w(setup test_0001_inner_it)
inner_methods = %w(setup teardown test_0001_inner_it)
assert_equal top_methods, x.instance_methods(false).sort.map {|o| o.to_s }
assert_equal inner_methods, y.instance_methods(false).sort.map {|o| o.to_s }
assert_equal inner_methods, z.instance_methods(false).sort.map {|o| o.to_s }
end
def test_setup_teardown_behavior
_, _, z, before_list, after_list = util_structure
tc = z.new(nil)
tc.setup
tc.teardown
assert_equal [1, 2, 3], before_list
assert_equal [3, 2, 1], after_list
end
def test_children
MiniTest::Spec.children.clear
x = y = z = nil
x = describe "top-level thingy" do
y = describe "first thingy" do end
it "top-level-it" do end
z = describe "second thingy" do end
end
assert_equal [x], MiniTest::Spec.children
assert_equal [y, z], x.children
assert_equal [], y.children
assert_equal [], z.children
end
def test_describe_first_structure
x = y = z = nil
x = describe "top-level thingy" do
y = describe "first thingy" do end
it "top-level-it" do end
z = describe "second thingy" do end
end
assert_equal ['test_0001_top_level_it'],
x.instance_methods.grep(/^test/).map {|o| o.to_s}
assert_equal [], y.instance_methods.grep(/^test/)
assert_equal [], z.instance_methods.grep(/^test/)
end
def test_structure_subclasses
z = nil
x = Class.new MiniTest::Spec do
def xyz; end
end
y = Class.new x do
z = describe("inner") {}
end
assert_respond_to x.new(nil), "xyz"
assert_respond_to y.new(nil), "xyz"
assert_respond_to z.new(nil), "xyz"
end
end

View file

@ -51,6 +51,7 @@ Finished tests in 0.00
def teardown
MiniTest::Unit.output = $stdout
# HACK for ruby-trunk: MiniTest::Unit.runner = nil
Object.send :remove_const, :ATestCase if defined? ATestCase
end
@ -254,7 +255,7 @@ RuntimeError: unhandled exception
assert_report expected
end
def test_run_failing # TODO: add error test
def test_run_failing
tc = Class.new(MiniTest::Unit::TestCase) do
def test_something
assert true
@ -349,6 +350,36 @@ Finished tests in 0.00
S.
Finished tests in 0.00
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
def test_something
assert true
end
def test_skip
skip "not yet"
end
end
Object.const_set(:ATestCase, tc)
@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
1) Skipped:
@ -360,6 +391,60 @@ not yet
assert_report expected
end
def test_default_runner_is_minitest_unit
skip "ruby-trunk won't run with runner code :("
assert_instance_of MiniTest::Unit, MiniTest::Unit.runner
end
def test_run_with_other_runner
skip "ruby-trunk won't run with runner code :("
runner = Class.new(MiniTest::Unit) do
# Run once before each suite
def _run_suite(suite, type)
begin
suite.before_suite
super(suite, type)
end
end
end
tc = Class.new(MiniTest::Unit::TestCase) do
def self.before_suite
MiniTest::Unit.output.puts "Running #{self.name} tests"
@@foo = 1
end
def test_something
assert_equal 1, @@foo
end
def test_something_else
assert_equal 1, @@foo
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
# Running tests:
Running ATestCase tests
..
Finished tests in 0.00
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
"
assert_report expected
end
def util_expand_bt bt
if RUBY_VERSION =~ /^1\.9/ then
bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
@ -434,12 +519,115 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
@tc.assert_equal 1, 1
end
def test_assert_equal_different
util_assert_triggered "Expected 1, not 2." do
def test_assert_equal_different_diff_deactivated
without_diff do
util_assert_triggered util_msg("haha" * 10, "blah" * 10) do
o1 = "haha" * 10
o2 = "blah" * 10
@tc.assert_equal o1, o2
end
end
end
def test_assert_equal_different_hex
c = Class.new do
def initialize s; @name = s; end
end
o1 = c.new "a"
o2 = c.new "b"
msg = "--- expected
+++ actual
@@ -1 +1 @@
-#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
+#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
.".gsub(/^ +/, "")
util_assert_triggered msg do
@tc.assert_equal o1, o2
end
end
def test_assert_equal_different_hex_invisible
o1 = Object.new
o2 = Object.new
msg = "No visible difference.
You should look at your implementation of Object#==.
#<Object:0xXXXXXX>.".gsub(/^ +/, "")
util_assert_triggered msg do
@tc.assert_equal o1, o2
end
end
def test_assert_equal_different_long
msg = "--- expected
+++ actual
@@ -1 +1 @@
-\"hahahahahahahahahahahahahahahahahahahaha\"
+\"blahblahblahblahblahblahblahblahblahblah\"
.".gsub(/^ +/, "")
util_assert_triggered msg do
o1 = "haha" * 10
o2 = "blah" * 10
@tc.assert_equal o1, o2
end
end
def test_assert_equal_different_long_invisible
msg = "No visible difference.
You should look at your implementation of String#==.
\"blahblahblahblahblahblahblahblahblahblah\".".gsub(/^ +/, "")
util_assert_triggered msg do
o1 = "blah" * 10
o2 = "blah" * 10
def o1.== o
false
end
@tc.assert_equal o1, o2
end
end
def test_assert_equal_different_long_msg
msg = "message.
--- expected
+++ actual
@@ -1 +1 @@
-\"hahahahahahahahahahahahahahahahahahahaha\"
+\"blahblahblahblahblahblahblahblahblahblah\"
.".gsub(/^ +/, "")
util_assert_triggered msg do
o1 = "haha" * 10
o2 = "blah" * 10
@tc.assert_equal o1, o2, "message"
end
end
def test_assert_equal_different_short
util_assert_triggered util_msg(1, 2) do
@tc.assert_equal 1, 2
end
end
def test_assert_equal_different_short_msg
util_assert_triggered util_msg(1, 2, "message") do
@tc.assert_equal 1, 2, "message"
end
end
def test_assert_equal_different_short_multiline
msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n."
util_assert_triggered msg do
@tc.assert_equal "a\nb", "a\nc"
end
end
def test_assert_in_delta
@tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
end
@ -590,7 +778,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_both
util_assert_triggered "In stdout.\nExpected \"yay\", not \"boo\"." do
util_assert_triggered util_msg("yay", "boo", "In stdout") do
@tc.assert_output "yay", "blah" do
print "boo"
$stderr.print "blah blah"
@ -599,7 +787,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_err
util_assert_triggered "In stderr.\nExpected \"blah\", not \"blah blah\"." do
util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
@tc.assert_output nil, "blah" do
$stderr.print "blah blah"
end
@ -607,7 +795,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
end
def test_assert_output_triggered_out
util_assert_triggered "In stdout.\nExpected \"blah\", not \"blah blah\"." do
util_assert_triggered util_msg("blah", "blah blah", "In stdout") do
@tc.assert_output "blah" do
print "blah blah"
end
@ -786,7 +974,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_assert_silent_triggered_err
@assertion_count = 2
util_assert_triggered "In stderr.\nExpected \"\", not \"blah blah\"." do
util_assert_triggered util_msg("", "blah blah", "In stderr") do
@tc.assert_silent do
$stderr.print "blah blah"
end
@ -794,7 +982,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
end
def test_assert_silent_triggered_out
util_assert_triggered "In stdout.\nExpected \"\", not \"blah blah\"." do
util_assert_triggered util_msg("", "blah blah", "In stdout") do
@tc.assert_silent do
print "blah blah"
end
@ -889,8 +1077,9 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
def test_message
@assertion_count = 0
assert_equal "blah2.", @tc.message { "blah2" }.call
assert_equal "blah2.", @tc.message("") { "blah2" }.call
assert_equal "blah2.", @tc.message { "blah2" }.call
assert_equal "blah2.", @tc.message("") { "blah2" }.call
assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call
assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
end
@ -1101,4 +1290,19 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
assert_equal expected, msg
end
def util_msg exp, act, msg = nil
s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}."
s = "#{msg}.\n#{s}" if msg
s
end
def without_diff
old_diff = MiniTest::Assertions.diff
MiniTest::Assertions.diff = nil
yield
ensure
MiniTest::Assertions.diff = old_diff
end
end