diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-09 04:09:25 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-09 04:09:25 +0000 |
commit | a213c2ed9c84f0f64f0664e820227034d078f9f9 (patch) | |
tree | dbc503f2118fdb51f0c45ebf36ead1b799286468 /lib/minitest | |
parent | 2dece928e085d25e8326455b9c0f508a2d70d02d (diff) | |
download | ruby-a213c2ed9c84f0f64f0664e820227034d078f9f9.tar.gz |
Imported minitest 3.0.0 (r7435) w/ fixes for rubygems. 10955 tests, 2253343 assertions, 1 failures, 1 errors, 28 skips minus drb tests on x86_64-darwin11.3.0 and reviewed by drbrain
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest')
-rw-r--r-- | lib/minitest/README.txt | 18 | ||||
-rw-r--r-- | lib/minitest/mock.rb | 38 | ||||
-rw-r--r-- | lib/minitest/unit.rb | 12 |
3 files changed, 59 insertions, 9 deletions
diff --git a/lib/minitest/README.txt b/lib/minitest/README.txt index 8c33c0aa52..9029354921 100644 --- a/lib/minitest/README.txt +++ b/lib/minitest/README.txt @@ -33,8 +33,8 @@ algorithms in a repeatable manner. Now you can assert that your newb co-worker doesn't replace your linear algorithm with an exponential one! -minitest/mock by Steven Baker, is a beautifully tiny mock object -framework. +minitest/mock by Steven Baker, is a beautifully tiny mock (and stub) +object framework. minitest/pride shows pride in testing and adds coloring to your test output. I guess it is an example of how to write IO pipes too. :P @@ -54,7 +54,7 @@ discovery. * minitest/autorun - the easy and explicit way to run all your tests. * minitest/unit - a very fast, simple, and clean test system. * minitest/spec - a very fast, simple, and clean spec system. -* minitest/mock - a simple and clean mock system. +* minitest/mock - a simple and clean mock/stub system. * minitest/benchmark - an awesome way to assert your algorithm's performance. * minitest/pride - show your pride in testing! * Incredibly small and fast runner, but no bells and whistles. @@ -194,6 +194,18 @@ Output is tab-delimited to make it easy to paste into a spreadsheet. end end +=== Stubs + + def test_stale_eh + obj_under_test = Something.new + + refute obj_under_test.stale? + + Time.stub :now, Time.at(0) do # stub goes away once the block is done + assert obj_under_test.stale? + end + end + === Customizable Test Runner Types: MiniTest::Unit.runner=(runner) provides an easy way of creating custom diff --git a/lib/minitest/mock.rb b/lib/minitest/mock.rb index 55b0095c0b..ec36d775cc 100644 --- a/lib/minitest/mock.rb +++ b/lib/minitest/mock.rb @@ -132,3 +132,41 @@ module MiniTest end end end + +class Object # :nodoc: + + ## + # Add a temporary stubbed method replacing +name+ for the duration + # of the +block+. If +val_or_callable+ responds to #call, then it + # returns the result of calling it, otherwise returns the value + # as-is. Cleans up the stub at the end of the +block+. + # + # def test_stale_eh + # obj_under_test = Something.new + # refute obj_under_test.stale? + # + # Time.stub :now, Time.at(0) do + # assert obj_under_test.stale? + # end + # end + + def stub name, val_or_callable, &block + new_name = "__minitest_stub__#{name}" + + metaclass = class << self; self; end + metaclass.send :alias_method, new_name, name + metaclass.send :define_method, name do |*args| + if val_or_callable.respond_to? :call then + val_or_callable.call(*args) + else + val_or_callable + end + end + + yield + ensure + metaclass.send :undef_method, name + metaclass.send :alias_method, name, new_name + metaclass.send :undef_method, new_name + end +end diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb index 9d9439c552..50c1fac94b 100644 --- a/lib/minitest/unit.rb +++ b/lib/minitest/unit.rb @@ -193,7 +193,7 @@ module MiniTest # Fails unless the block returns a true value. def assert_block msg = nil - warn "NOTE: MiniTest::Unit::TestCase#assert_block is deprecated, use assert. It will be removed on or after 2012-06-01." + warn "NOTE: MiniTest::Unit::TestCase#assert_block is deprecated, use assert. It will be removed on or after 2012-06-01. Called from #{caller.first}" msg = message(msg) { "Expected block to return true value" } assert yield, msg end @@ -281,8 +281,8 @@ module MiniTest def assert_match matcher, obj, msg = nil msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" } - assert_respond_to obj, :"=~" - matcher = Regexp.new Regexp.escape matcher if String === matcher and obj.respond_to?(:to_str) + assert_respond_to matcher, :"=~" + matcher = Regexp.new Regexp.escape matcher if String === matcher assert matcher =~ obj, msg end @@ -582,8 +582,8 @@ module MiniTest def refute_match matcher, obj, msg = nil msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"} - assert_respond_to obj, :"=~" - matcher = Regexp.new Regexp.escape matcher if String === matcher and obj.respond_to?(:to_str) + assert_respond_to matcher, :"=~" + matcher = Regexp.new Regexp.escape matcher if String === matcher refute matcher =~ obj, msg end @@ -652,7 +652,7 @@ module MiniTest end class Unit # :nodoc: - VERSION = "2.12.1" # :nodoc: + VERSION = "3.0.0" # :nodoc: attr_accessor :report, :failures, :errors, :skips # :nodoc: attr_accessor :test_count, :assertion_count # :nodoc: |