ruby-changes:23490
From: ryan <ko1@a...>
Date: Sat, 5 May 2012 06:46:36 +0900 (JST)
Subject: [ruby-changes:23490] ryan:r35541 (trunk): Imported minitest 2.12.1 (r7323)
ryan 2012-05-05 06:46:01 +0900 (Sat, 05 May 2012) New Revision: 35541 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35541 Log: Imported minitest 2.12.1 (r7323) Added files: trunk/test/minitest/metametameta.rb Modified files: trunk/ChangeLog trunk/lib/minitest/README.txt trunk/lib/minitest/autorun.rb trunk/lib/minitest/benchmark.rb trunk/lib/minitest/mock.rb trunk/lib/minitest/pride.rb trunk/lib/minitest/spec.rb trunk/lib/minitest/unit.rb trunk/test/minitest/test_minitest_benchmark.rb trunk/test/minitest/test_minitest_mock.rb trunk/test/minitest/test_minitest_spec.rb trunk/test/minitest/test_minitest_unit.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35540) +++ ChangeLog (revision 35541) @@ -1,3 +1,8 @@ +Sat May 5 06:43:10 2012 Ryan Davis <ryand-ruby@z...> + + * lib/minitest/*: Imported minitest 2.12.1 (r7323) + * test/minitest/*: ditto + Sat May 5 01:47:33 2012 NARUSE, Yui <naruse@r...> * test/zlib/test_zlib.rb (test_inflate): add a test for Zlib.inflate. @@ -5261,7 +5266,7 @@ * win32/win32.c (fcntl): on F_DUPFD, determine the inheritance of the new handle by O_NOINHERIT flag of original fd. -Fri Nov 18 08:00:41 2011 Ryan Davis <ryan@l...> +Fri Nov 18 08:00:41 2011 Ryan Davis <ryand-ruby@z...> * lib/minitest/*: Imported minitest 2.8.1 (r6750) * test/minitest/*: ditto Index: lib/minitest/autorun.rb =================================================================== --- lib/minitest/autorun.rb (revision 35540) +++ lib/minitest/autorun.rb (revision 35541) @@ -1,3 +1,4 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! Index: lib/minitest/unit.rb =================================================================== --- lib/minitest/unit.rb (revision 35540) +++ lib/minitest/unit.rb (revision 35541) @@ -1,3 +1,4 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! @@ -61,9 +62,14 @@ # printed if the assertion fails. module Assertions + UNDEFINED = Object.new # :nodoc: - WINDOZE = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + def UNDEFINED.inspect # :nodoc: + "UNDEFINED" # again with the rdoc bugs... :( + end + WINDOZE = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ # :nodoc: + ## # Returns the diff command to use in #diff. Tries to intelligently # figure out what diff to use. @@ -187,6 +193,7 @@ # 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." msg = message(msg) { "Expected block to return true value" } assert yield, msg end @@ -225,7 +232,7 @@ def assert_in_delta exp, act, delta = 0.001, msg = nil n = (exp - act).abs - msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to be < #{delta}" } + msg = message(msg) { "Expected |#{exp} - #{act}| (#{n}) to be < #{delta}"} assert delta >= n, msg end @@ -234,7 +241,7 @@ # error less than +epsilon+. def assert_in_epsilon a, b, epsilon = 0.001, msg = nil - assert_in_delta a, b, [a, b].min * epsilon, msg + assert_in_delta a, b, [a.abs, b.abs].min * epsilon, msg end ## @@ -270,13 +277,13 @@ end ## - # Fails unless +exp+ is <tt>=~</tt> +act+. + # Fails unless +matcher+ <tt>=~</tt> +obj+. - 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.new Regexp.escape exp if String === exp and String === act - assert exp =~ act, msg + def assert_match matcher, obj, msg = nil + msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" } + assert_respond_to matcher, :"=~" + matcher = Regexp.new Regexp.escape matcher if String === matcher + assert matcher =~ obj, msg end ## @@ -287,9 +294,6 @@ assert obj.nil?, msg end - UNDEFINED = Object.new - def UNDEFINED.inspect; "UNDEFINED"; end - ## # For testing with binary operators. # @@ -313,8 +317,8 @@ yield end + y = assert_equal stderr, err, "In stderr" if stderr x = assert_equal stdout, out, "In stdout" if stdout - y = assert_equal stderr, err, "In stderr" if stderr (!stdout || x) && (!stderr || y) end @@ -334,10 +338,11 @@ end ## - # Fails unless the block raises one of +exp+ + # Fails unless the block raises one of +exp+. Returns the + # exception matched so you can check the message, attributes, etc. def assert_raises *exp - msg = "#{exp.pop}\n" if String === exp.last + msg = "#{exp.pop}.\n" if String === exp.last should_raise = false begin @@ -355,7 +360,7 @@ details = "#{msg}#{mu_pp(exp)} exception expected, not" assert(exp.any? { |ex| ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class - }, proc{exception_details(e, details)}) + }, exception_details(e, details)) return e end @@ -523,14 +528,14 @@ end ## - # For comparing Floats. Fails if +exp+ is within +delta+ of +act+ + # For comparing Floats. Fails if +exp+ is within +delta+ of +act+. # # refute_in_delta Math::PI, (22.0 / 7.0) 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}" + "Expected |#{exp} - #{act}| (#{n}) to not be < #{delta}" } refute delta > n, msg end @@ -544,7 +549,7 @@ end ## - # Fails if +collection+ includes +obj+ + # Fails if +collection+ includes +obj+. def refute_includes collection, obj, msg = nil msg = message(msg) { @@ -555,7 +560,7 @@ end ## - # Fails if +obj+ is an instance of +cls+ + # Fails if +obj+ is an instance of +cls+. def refute_instance_of cls, obj, msg = nil msg = message(msg) { @@ -565,7 +570,7 @@ end ## - # Fails if +obj+ is a kind of +cls+ + # Fails if +obj+ is a kind of +cls+. 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}" } @@ -573,13 +578,13 @@ end ## - # Fails if +exp+ <tt>=~</tt> +act+ + # Fails if +matcher+ <tt>=~</tt> +obj+. - def refute_match exp, act, msg = nil - msg = message(msg) { "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}" } - assert_respond_to act, :"=~" - exp = (/#{Regexp.escape exp}/) if String === exp and String === act - refute exp =~ act, msg + def refute_match matcher, obj, msg = nil + msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"} + assert_respond_to matcher, :"=~" + matcher = Regexp.new Regexp.escape matcher if String === matcher + refute matcher =~ obj, msg end ## @@ -646,8 +651,8 @@ end end - class Unit - VERSION = "2.8.1" # :nodoc: + class Unit # :nodoc: + VERSION = "2.12.1" # :nodoc: attr_accessor :report, :failures, :errors, :skips # :nodoc: attr_accessor :test_count, :assertion_count # :nodoc: @@ -655,13 +660,18 @@ attr_accessor :help # :nodoc: attr_accessor :verbose # :nodoc: attr_writer :options # :nodoc: + attr_accessor :last_error # :nodoc: + ## + # Lazy accessor for options. + def options @options ||= {} end @@installed_at_exit ||= false @@out = $stdout + @@after_tests = [] ## # A simple hook allowing you to run a block of code after the @@ -669,8 +679,8 @@ # # MiniTest::Unit.after_tests { p $debugging_info } - def self.after_tests - at_exit { at_exit { yield } } + def self.after_tests &block + @@after_tests << block end ## @@ -686,7 +696,10 @@ # to run (at_exit stacks). exit_code = nil - at_exit { exit false if exit_code && exit_code != 0 } + at_exit { + @@after_tests.reverse_each(&:call) + exit false if exit_code && exit_code != 0 + } exit_code = MiniTest::Unit.new.run ARGV } unless @@installed_at_exit @@ -744,6 +757,9 @@ grep(/^run_/).map { |s| s.to_s }).uniq end + ## + # Return the IO for output. + def output self.class.output end @@ -756,6 +772,9 @@ output.print(*a) end + ## + # Runner for a given +type+ (eg, test vs bench). + def _run_anything type suites = TestCase.send "#{type}_suites" return if suites.empty? @@ -793,10 +812,16 @@ status end + ## + # Runs all the +suites+ for a given +type+. + def _run_suites suites, type suites.map { |suite| _run_suite suite, type } end + ## + # Run a single +suite+ for a given +type+. + def _run_suite suite, type header = "#{type}_suite_header" puts send(header, suite) if respond_to? header @@ -811,9 +836,12 @@ print "#{suite}##{method} = " if @verbose @start_time = Time.now + self.last_error = nil result = inst.run self time = Time.now - @start_time + record suite, method, inst._assertions, time, last_error + print "%.2f s = " % time if @verbose print result puts if @verbose @@ -824,6 +852,21 @@ return assertions.size, assertions.inject(0) { |sum, n| sum + n } end + ## + # Record the result of a single run. Makes it very easy to gather + # information. Eg: + # + # class StatisticsRecorder < MiniTest::Unit + # def record suite, method, assertions, time, error + # # ... record the results somewhere ... + # end + # end + # + # MiniTest::Unit.runner = StatisticsRecorder.new + + def record suite, method, assertions, time, error + end + def location e # :nodoc: last_before_assertion = "" e.backtrace.reverse_each do |s| @@ -838,6 +881,7 @@ # exception +e+ def puke klass, meth, e + self.last_error = e e = case e when MiniTest::Skip then @skips += 1 @@ -859,9 +903,10 @@ @report = [] @errors = @failures = @skips = 0 @verbose = false + self.last_error = nil end - def process_args args = [] + def process_args args = [] # :nodoc: options = {} orig_args = args.dup @@ -882,7 +927,7 @@ options[:verbose] = true end - opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a| + opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a| options[:filter] = a end @@ -945,12 +990,61 @@ end ## + # Provides a simple set of guards that you can use in your tests + # to skip execution if it is not applicable. These methods are + # mixed into TestCase as both instance and class methods so you + # can use them inside or outside of the test methods. + # + # def test_something_for_mri + # skip "bug 1234" if jruby? + # # ... + # end + # + # if windows? then + # # ... lots of test methods ... + # end + + module Guard + + ## + # Is this running on jruby? + + def jruby? platform = RUBY_PLATFORM + "java" == platform + end + + ## + # Is this running on mri? + + def mri? platform = RUBY_DESCRIPTION + /^ruby/ =~ platform + end + + ## + # Is this running on rubinius? + + def rubinius? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE + "rbx" == platform + end + + ## + # Is this running on windows? + + def windows? platform = RUBY_PLATFORM + /mswin|mingw/ =~ platform + end + end + + ## # Subclass TestCase to create your own tests. Typically you'll want a # TestCase subclass per implementation class. # # See MiniTest::Assertions class TestCase + include Guard + extend Guard + attr_reader :__name__ # :nodoc: PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, @@ -963,16 +1057,21 @@ def run runner trap "INFO" do + runner.report.each_with_index do |msg, i| + warn "\n%3d) %s" % [i + 1, msg] + end + warn '' time = runner.start_time ? Time.now - runner.start_time : 0 - warn "%s#%s %.2fs" % [self.class, self.__name__, time] + warn "Current Test: %s#%s %.2fs" % [self.class, self.__name__, time] runner.status $stderr end if SUPPORTS_INFO_SIGNAL result = "" begin @passed = nil + self.before_setup self.setup - self.run_setup_hooks + self.after_setup self.run_test self.__name__ result = "." unless io? @passed = true @@ -982,13 +1081,14 @@ @passed = false result = runner.puke self.class, self.__name__, e ensure - begin - self.run_teardown_hooks - self.teardown - rescue *PASSTHROUGH_EXCEPTIONS - raise - rescue Exception => e - result = runner.puke self.class, self.__name__, e + %w{ before_teardown teardown after_teardown }.each do |hook| + begin + self.send hook + rescue *PASSTHROUGH_EXCEPTIONS + raise + rescue Exception => e + result = runner.puke self.class, self.__name__, e + end end trap 'INFO', 'DEFAULT' if SUPPORTS_INFO_SIGNAL end @@ -1008,11 +1108,17 @@ @@current end + ## + # Return the output IO object + def io @__io__ = true MiniTest::Unit.output end + ## + # Have we hooked up the IO yet? + def io? @__io__ end @@ -1030,6 +1136,7 @@ def self.i_suck_and_my_tests_are_order_dependent! class << self + undef_method :test_order if method_defined? :test_order define_method :test_order do :alpha end end end @@ -1075,10 +1182,32 @@ def setup; end ## + # Runs before every test after setup. Use this to refactor test + # initialization. + + def after_setup; end + + ## + # Runs before every setup. Use this to refactor test initialization. + + def before_setup; end + + ## # Runs after every test. Use this to refactor test cleanup. def teardown; end + ## + # Runs after every test before teardown. Use this to refactor test + # initialization. + + def before_teardown; end + + ## + # Runs after every teardown. Use this to refactor test cleanup. + + def after_teardown; end + def self.reset_setup_teardown_hooks # :nodoc: @setup_hooks = [] @teardown_hooks = [] @@ -1189,6 +1318,8 @@ end # class Unit end # module MiniTest +Minitest = MiniTest # because ugh... I typo this all the time + if $DEBUG then module Test # :nodoc: module Unit # :nodoc: Index: lib/minitest/pride.rb =================================================================== --- lib/minitest/pride.rb (revision 35540) +++ lib/minitest/pride.rb (revision 35541) @@ -1,3 +1,4 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! @@ -10,12 +11,17 @@ # Show your testing pride! class PrideIO + + # Start an escape sequence ESC = "\e[" + + # End the escape sequence NND = "#{ESC}0m" + # The IO we're going to pipe through. attr_reader :io - def initialize io + def initialize io # :nodoc: @io = io # stolen from /System/Library/Perl/5.10.0/Term/ANSIColor.pm # also reference http://en.wikipedia.org/wiki/ANSI_escape_code @@ -25,6 +31,9 @@ # io.sync = true end + ## + # Wrap print to colorize the output. + def print o case o when "." then @@ -36,7 +45,7 @@ end end - def puts(*o) + def puts(*o) # :nodoc: o.map! { |s| s.sub(/Finished tests/) { @index = 0 @@ -49,6 +58,9 @@ super end + ## + # Color a string. + def pride string string = "*" if string == "." c = @colors[@index % @size] @@ -56,15 +68,20 @@ "#{ESC}#{c}m#{string}#{NND}" end - def method_missing msg, *args + def method_missing msg, *args # :nodoc: io.send(msg, *args) end end -class PrideLOL < PrideIO # inspired by lolcat, but massively cleaned up - PI_3 = Math::PI / 3 +## +# If you thought the PrideIO was colorful... +# +# (Inspired by lolcat, but with clean math) - def initialize io +class PrideLOL < PrideIO + PI_3 = Math::PI / 3 # :nodoc: + + def initialize io # :nodoc: # walk red, green, and blue around a circle separated by equal thirds. # # To visualize, type this into wolfram-alpha: @@ -88,6 +105,9 @@ super end + ## + # Make the string even more colorful. Damnit. + def pride string c = @colors[@index % @size] @index += 1 Index: lib/minitest/README.txt =================================================================== --- lib/minitest/README.txt (revision 35540) +++ lib/minitest/README.txt (revision 35541) @@ -14,7 +14,7 @@ paired up and we cracked open the code for a few test frameworks... - I MUST say that mintiest is *very* readable / understandable + I MUST say that minitest is *very* readable / understandable compared to the 'other two' options we looked at. Nicely done and thank you for helping us keep our mental sanity." @@ -44,7 +44,7 @@ test suite. For example, there is no magic involved for test-case discovery. - "Again, I can praise enough the idea of a testing/specing + "Again, I can't praise enough the idea of a testing/specing framework that I can actually read in full in one sitting!" -- Piotr Szotkowski @@ -117,6 +117,10 @@ end end +For matchers support check out: + +https://github.com/zenspider/minitest-matchers + === Benchmarks Add benchmarks to your regular unit tests. If the unit tests fail, the @@ -167,7 +171,7 @@ def ask(question) method = question.tr(" ","_") + "?" - @meme.send(method) + @meme.__send__(method) end end @@ -242,6 +246,46 @@ MiniTest::Unit.runner = MiniTestWithTransactions::Unit.new +== Known Extensions: + +minitest-capistrano :: Assertions and expectations for testing Capistrano recipes +minitest-capybara :: Capybara matchers support for minitest unit and spec +minitest-chef-handler :: Run Minitest suites as Chef report handlers +minitest-ci :: CI reporter plugin for MiniTest. +minitest-colorize :: Colorize MiniTest output and show failing tests instantly. +minitest-context :: Defines contexts for code reuse in MiniTest + specs that share common expectations. +minitest-debugger :: Wraps assert so failed assertions drop into + the ruby debugger. +minitest-display :: Patches MiniTest to allow for an easily configurable output. +minitest-emoji :: Print out emoji for your test passes, fails, and skips. +minitest-excludes :: Clean API for excluding certain tests you + don't want to run under certain conditions. +minitest-firemock :: Makes your MiniTest mocks more resilient. +minitest-growl :: Test notifier for minitest via growl. +minitest-instrument :: Instrument ActiveSupport::Notifications when + test method is executed +minitest-instrument-db :: Store information about speed of test + e (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/