summaryrefslogtreecommitdiff
path: root/test/minitest
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-08-23 21:47:25 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-08-23 21:47:25 +0000
commit2c43b9664b29f76c73ac8bae5400b79d0a5313e0 (patch)
tree6e9b4a581ac460c38d22de2750939fa815a98295 /test/minitest
parent885f5fa2b0c1ba20a26759e65fefeece48cb56db (diff)
Imported minitest 2.5.0 (r6557)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/minitest')
-rw-r--r--test/minitest/test_minitest_mock.rb62
-rw-r--r--test/minitest/test_minitest_spec.rb60
-rw-r--r--test/minitest/test_minitest_unit.rb151
3 files changed, 255 insertions, 18 deletions
diff --git a/test/minitest/test_minitest_mock.rb b/test/minitest/test_minitest_mock.rb
index 311f5fa7fb..b6e801c3aa 100644
--- a/test/minitest/test_minitest_mock.rb
+++ b/test/minitest/test_minitest_mock.rb
@@ -49,20 +49,34 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
util_verify_bad
end
- def test_not_verify_if_unexpected_method_is_called
- assert_raises NoMethodError do
- @mock.unexpected
- end
- end
-
def test_blow_up_on_wrong_number_of_arguments
@mock.foo
@mock.meaning_of_life
@mock.expect(:sum, 3, [1, 2])
- assert_raises ArgumentError do
+ e = assert_raises ArgumentError do
@mock.sum
end
+
+ assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
+ end
+
+ def test_return_mock_does_not_raise
+ retval = MiniTest::Mock.new
+ mock = MiniTest::Mock.new
+ mock.expect(:foo, retval)
+ mock.foo
+
+ assert mock.verify
+ end
+
+ def test_mock_args_does_not_raise
+ arg = MiniTest::Mock.new
+ mock = MiniTest::Mock.new
+ mock.expect(:foo, nil, [arg])
+ mock.foo(arg)
+
+ assert mock.verify
end
def test_blow_up_on_wrong_arguments
@@ -81,18 +95,22 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
end
def test_no_method_error_on_unexpected_methods
- assert_raises NoMethodError do
+ e = assert_raises NoMethodError do
@mock.bar
end
+
+ expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
+
+ assert_equal expected, e.message
end
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
@@ -104,6 +122,30 @@ class TestMiniTestMock < MiniTest::Unit::TestCase
assert !MiniTest::Mock.new.respond_to?(:foo)
end
+ def test_mock_is_a_blank_slate
+ @mock.expect :kind_of?, true, [Fixnum]
+ @mock.expect :==, true, [1]
+
+ assert @mock.kind_of?(Fixnum), "didn't mock :kind_of\?"
+ assert @mock == 1, "didn't mock :=="
+ end
+
+ def test_verify_allows_called_args_to_be_loosely_specified
+ mock = MiniTest::Mock.new
+ mock.expect :loose_expectation, true, [Integer]
+ mock.loose_expectation 1
+
+ assert mock.verify
+ end
+
+ def test_verify_raises_with_strict_args
+ mock = MiniTest::Mock.new
+ mock.expect :strict_expectation, true, [2]
+ mock.strict_expectation 1
+
+ util_verify_bad
+ end
+
def util_verify_bad
assert_raises MockExpectationError do
@mock.verify
diff --git a/test/minitest/test_minitest_spec.rb b/test/minitest/test_minitest_spec.rb
index 942156abdb..26dd4ec46a 100644
--- a/test/minitest/test_minitest_spec.rb
+++ b/test/minitest/test_minitest_spec.rb
@@ -4,11 +4,9 @@
# File a patch instead and assign it to Ryan Davis.
######################################################################
-require 'minitest/spec'
+require 'minitest/autorun'
require 'stringio'
-MiniTest::Unit.autorun
-
describe MiniTest::Spec do
before do
@assertion_count = 4
@@ -202,6 +200,53 @@ describe MiniTest::Spec do
end
end
+describe MiniTest::Spec, :let do
+ i_suck_and_my_tests_are_order_dependent!
+
+ def _count
+ $let_count ||= 0
+ end
+
+ let :count do
+ $let_count += 1
+ $let_count
+ end
+
+ it "is evaluated once per example" do
+ _count.must_equal 0
+
+ count.must_equal 1
+ count.must_equal 1
+
+ _count.must_equal 1
+ end
+
+ it "is REALLY evaluated once per example" do
+ _count.must_equal 1
+
+ count.must_equal 2
+ count.must_equal 2
+
+ _count.must_equal 2
+ end
+end
+
+describe MiniTest::Spec, :subject do
+ attr_reader :subject_evaluation_count
+
+ subject do
+ @subject_evaluation_count ||= 0
+ @subject_evaluation_count += 1
+ @subject_evaluation_count
+ end
+
+ it "is evaluated once per example" do
+ subject.must_equal 1
+ subject.must_equal 1
+ subject_evaluation_count.must_equal 1
+ end
+end
+
class TestMeta < MiniTest::Unit::TestCase
def test_setup
srand 42
@@ -245,8 +290,8 @@ class TestMeta < MiniTest::Unit::TestCase
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 teardown test_0001_inner_it)
+ top_methods = %w(test_0001_top_level_it)
+ inner_methods = %w(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 }
@@ -257,8 +302,9 @@ class TestMeta < MiniTest::Unit::TestCase
_, _, z, before_list, after_list = util_structure
tc = z.new(nil)
- tc.setup
- tc.teardown
+
+ tc.run_setup_hooks
+ tc.run_teardown_hooks
assert_equal [1, 2, 3], before_list
assert_equal [3, 2, 1], after_list
diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb
index b56fb0dc18..c921ca749e 100644
--- a/test/minitest/test_minitest_unit.rb
+++ b/test/minitest/test_minitest_unit.rb
@@ -12,6 +12,7 @@ MiniTest::Unit.autorun
module MyModule; end
class AnError < StandardError; include MyModule; end
+class ImmutableString < String; def inspect; super.freeze; end; end
class TestMiniTestUnit < MiniTest::Unit::TestCase
pwd = Pathname.new(File.expand_path(Dir.pwd))
@@ -46,12 +47,12 @@ Finished tests in 0.00
MiniTest::Unit::TestCase.reset
@tu = MiniTest::Unit.new
@output = StringIO.new("")
+ MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
MiniTest::Unit.output = @output
end
def teardown
MiniTest::Unit.output = $stdout
- MiniTest::Unit.runner = nil
Object.send :remove_const, :ATestCase if defined? ATestCase
end
@@ -442,6 +443,149 @@ Finished tests in 0.00
assert_report expected
end
+ def with_overridden_include
+ Class.class_eval do
+ def inherited_with_hacks klass
+ throw :inherited_hook
+ end
+
+ alias inherited_without_hacks inherited
+ alias inherited inherited_with_hacks
+ alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros
+ end
+
+ yield
+
+ ensure
+ Class.class_eval do
+ alias inherited inherited_without_hacks
+
+ undef_method :inherited_with_hacks
+ undef_method :inherited_without_hacks
+ end
+
+ refute_respond_to Class, :inherited_with_hacks
+ refute_respond_to Class, :inherited_without_hacks
+ end
+
+ def test_inherited_hook_plays_nice_with_others
+ with_overridden_include do
+ assert_throws :inherited_hook do
+ Class.new MiniTest::Unit::TestCase
+ end
+ end
+ end
+
+ def test_setup_hooks
+ call_order = []
+
+ tc = Class.new(MiniTest::Unit::TestCase) 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::Unit::TestCase) 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
+ call_order = []
+
+ parent = Class.new(MiniTest::Unit::TestCase) do
+ define_method :setup do
+ super()
+ call_order << :setup_method
+ end
+
+ define_method :teardown do
+ super()
+ call_order << :teardown_method
+ end
+
+ define_method :test_something do
+ call_order << :test
+ 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
+
+ assert_equal expected, call_order
+ end
+
def util_expand_bt bt
if RUBY_VERSION =~ /^1\.9/ then
bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
@@ -1084,6 +1228,11 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
@tc.pass
end
+ def test_prints
+ printer = Class.new { extend MiniTest::Assertions }
+ @tc.assert_equal '"test"', printer.mu_pp(ImmutableString.new 'test')
+ end
+
def test_refute
@assertion_count = 2