From 6af843b9cb99fb844bf866e71e9ee52be126080f Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 4 May 2012 21:46:01 +0000 Subject: [PATCH] Imported minitest 2.12.1 (r7323) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +- lib/minitest/README.txt | 50 ++- lib/minitest/autorun.rb | 1 + lib/minitest/benchmark.rb | 14 +- lib/minitest/mock.rb | 66 ++- lib/minitest/pride.rb | 32 +- lib/minitest/spec.rb | 54 ++- lib/minitest/unit.rb | 217 ++++++++-- test/minitest/metametameta.rb | 49 +++ test/minitest/test_minitest_benchmark.rb | 1 + test/minitest/test_minitest_mock.rb | 82 +++- test/minitest/test_minitest_spec.rb | 523 ++++++++++++++++++----- test/minitest/test_minitest_unit.rb | 311 ++++++++++---- 13 files changed, 1113 insertions(+), 294 deletions(-) create mode 100644 test/minitest/metametameta.rb diff --git a/ChangeLog b/ChangeLog index b80d6a0826..965906200a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat May 5 06:43:10 2012 Ryan Davis + + * lib/minitest/*: Imported minitest 2.12.1 (r7323) + * test/minitest/*: ditto + Sat May 5 01:47:33 2012 NARUSE, Yui * test/zlib/test_zlib.rb (test_inflate): add a test for Zlib.inflate. @@ -5261,7 +5266,7 @@ Fri Nov 18 13:03:38 2011 NAKAMURA Usaku * 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 +Fri Nov 18 08:00:41 2011 Ryan Davis * lib/minitest/*: Imported minitest 2.8.1 (r6750) * test/minitest/*: ditto diff --git a/lib/minitest/README.txt b/lib/minitest/README.txt index 25c4dae1a9..8c33c0aa52 100644 --- a/lib/minitest/README.txt +++ b/lib/minitest/README.txt @@ -14,7 +14,7 @@ TDD, BDD, mocking, and benchmarking. 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 @@ implementors that need a minimal set of methods to bootstrap a working test suite. For example, there is no magic involved for test-case discovery. - "Again, I can’t 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 @@ Given that you'd like to test the following class: 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 @@ Output is tab-delimited to make it easy to paste into a spreadsheet. def ask(question) method = question.tr(" ","_") + "?" - @meme.send(method) + @meme.__send__(method) end end @@ -242,6 +246,46 @@ fixture loading: 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 + execution provided by minitest-instrument in database +minitest-libnotify :: Test notifier for minitest via libnotify. +minitest-macruby :: Provides extensions to minitest for macruby UI testing. +minitest-matchers :: Adds support for RSpec-style matchers to minitest. +minitest-metadata :: Annotate tests with metadata (key-value). +minitest-mongoid :: Mongoid assertion matchers for MiniTest +minitest-must_not :: Provides must_not as an alias for wont in MiniTest +minitest-predicates :: Adds support for .predicate? methods +minitest-pry :: A minitest plugin to drop into pry on assertion failure. +minitest-rails :: MiniTest integration for Rails 3.1. +minitest-reporters :: Create customizable MiniTest output formats +minitest-rg :: redgreen minitest +minitest-spec-magic :: Minitest::Spec extensions for Rails and beyond +minitest-tags :: add tags for minitest +minitest-wscolor :: Yet another test colorizer. +minitest_owrapper :: Get tests results as a TestResult object. +minitest_should :: Shoulda style syntax for minitest test::unit. +minitest_tu_shim :: minitest_tu_shim bridges between test/unit and minitest. +mongoid-minitest :: MiniTest matchers for Mongoid. + == REQUIREMENTS: * Ruby 1.8, maybe even 1.6 or lower. No magic is involved. diff --git a/lib/minitest/autorun.rb b/lib/minitest/autorun.rb index 443f2f61d4..cb4a3a0e5d 100644 --- a/lib/minitest/autorun.rb +++ b/lib/minitest/autorun.rb @@ -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! diff --git a/lib/minitest/benchmark.rb b/lib/minitest/benchmark.rb index c6faa50770..02121db340 100644 --- a/lib/minitest/benchmark.rb +++ b/lib/minitest/benchmark.rb @@ -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! @@ -7,9 +8,7 @@ require 'minitest/unit' require 'minitest/spec' -class MiniTest::Unit - attr_accessor :runner - +class MiniTest::Unit # :nodoc: def run_benchmarks # :nodoc: _run_anything :benchmark end @@ -318,6 +317,15 @@ class MiniTest::Spec define_method "bench_#{name.gsub(/\W+/, '_')}", &block end + ## + # Specifies the ranges used for benchmarking for that class. + # + # bench_range do + # bench_exp(2, 16, 2) + # end + # + # See Unit::TestCase.bench_range for more details. + def self.bench_range &block return super unless block diff --git a/lib/minitest/mock.rb b/lib/minitest/mock.rb index 0d86bfb847..55b0095c0b 100644 --- a/lib/minitest/mock.rb +++ b/lib/minitest/mock.rb @@ -1,10 +1,12 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! # File a patch instead and assign it to Ryan Davis. ###################################################################### -class MockExpectationError < StandardError; end +class MockExpectationError < StandardError # :nodoc: +end # omg... worst bug ever. rdoc doesn't allow 1-liners ## # A simple and clean mock object framework. @@ -24,8 +26,8 @@ module MiniTest end def initialize # :nodoc: - @expected_calls = {} - @actual_calls = Hash.new {|h,k| h[k] = [] } + @expected_calls = Hash.new { |calls, name| calls[name] = [] } + @actual_calls = Hash.new { |calls, name| calls[name] = [] } end ## @@ -50,44 +52,70 @@ module MiniTest # @mock.verify # => raises MockExpectationError def expect(name, retval, args=[]) - @expected_calls[name] = { :retval => retval, :args => args } + raise ArgumentError, "args must be an array" unless Array === args + @expected_calls[name] << { :retval => retval, :args => args } self end + def call name, data + case data + when Hash then + "#{name}(#{data[:args].inspect[1..-2]}) => #{data[:retval].inspect}" + else + data.map { |d| call name, d }.join ", " + end + end + ## # Verify that all methods were called as expected. Raises # +MockExpectationError+ if the mock object was not called as # expected. def verify - @expected_calls.each_key do |name| - expected = @expected_calls[name] - msg1 = "expected #{name}, #{expected.inspect}" - msg2 = "#{msg1}, got #{@actual_calls[name].inspect}" + @expected_calls.each do |name, calls| + calls.each do |expected| + msg1 = "expected #{call name, expected}" + msg2 = "#{msg1}, got [#{call name, @actual_calls[name]}]" - raise MockExpectationError, msg2 if - @actual_calls.has_key? name and - not @actual_calls[name].include?(expected) + raise MockExpectationError, msg2 if + @actual_calls.has_key? name and + not @actual_calls[name].include?(expected) - raise MockExpectationError, msg1 unless - @actual_calls.has_key? name and @actual_calls[name].include?(expected) + raise MockExpectationError, msg1 unless + @actual_calls.has_key? name and @actual_calls[name].include?(expected) + end end true end def method_missing(sym, *args) # :nodoc: - expected = @expected_calls[sym] - - unless expected then + unless @expected_calls.has_key?(sym) then raise NoMethodError, "unmocked method %p, expected one of %p" % [sym, @expected_calls.keys.sort_by(&:to_s)] end - expected_args, retval = expected[:args], expected[:retval] + index = @actual_calls[sym].length + expected_call = @expected_calls[sym][index] - unless expected_args.size == args.size + unless expected_call then + raise MockExpectationError, "No more expects available for %p: %p" % + [sym, args] + end + + expected_args, retval = expected_call[:args], expected_call[:retval] + + if expected_args.size != args.size then raise ArgumentError, "mocked method %p expects %d arguments, got %d" % - [sym, expected[:args].size, args.size] + [sym, expected_args.size, args.size] + end + + fully_matched = expected_args.zip(args).all? { |mod, a| + mod === a or mod == a + } + + unless fully_matched then + raise MockExpectationError, "mocked method %p called with unexpected arguments %p" % + [sym, args] end @actual_calls[sym] << { diff --git a/lib/minitest/pride.rb b/lib/minitest/pride.rb index 9cf16fdffa..9de4e37a98 100644 --- a/lib/minitest/pride.rb +++ b/lib/minitest/pride.rb @@ -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 @@ require "minitest/unit" # 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 @@ class PrideIO # io.sync = true end + ## + # Wrap print to colorize the output. + def print o case o when "." then @@ -36,7 +45,7 @@ class PrideIO end end - def puts(*o) + def puts(*o) # :nodoc: o.map! { |s| s.sub(/Finished tests/) { @index = 0 @@ -49,6 +58,9 @@ class PrideIO super end + ## + # Color a string. + def pride string string = "*" if string == "." c = @colors[@index % @size] @@ -56,15 +68,20 @@ class PrideIO "#{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 @@ class PrideLOL < PrideIO # inspired by lolcat, but massively cleaned up super end + ## + # Make the string even more colorful. Damnit. + def pride string c = @colors[@index % @size] @index += 1 diff --git a/lib/minitest/spec.rb b/lib/minitest/spec.rb index ac28b24c4a..c0c0ae2f02 100644 --- a/lib/minitest/spec.rb +++ b/lib/minitest/spec.rb @@ -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! @@ -13,11 +14,14 @@ class Module # :nodoc: # warn "%-22p -> %p %p" % [meth, new_name, dont_flip] self.class_eval <<-EOM def #{new_name} *args - return MiniTest::Spec.current.#{meth}(*args, &self) if - Proc === self - return MiniTest::Spec.current.#{meth}(args.first, self) if - args.size == 1 unless #{!!dont_flip} - return MiniTest::Spec.current.#{meth}(self, *args) + case + when Proc === self then + MiniTest::Spec.current.#{meth}(*args, &self) + when #{!!dont_flip} then + MiniTest::Spec.current.#{meth}(self, *args) + else + MiniTest::Spec.current.#{meth}(args.first, self, *args[1..-1]) + end end EOM end @@ -177,6 +181,12 @@ class MiniTest::Spec < MiniTest::Unit::TestCase add_teardown_hook {|tc| tc.instance_eval(&block) } end + NAME_RE = if RUBY_VERSION >= "1.9" + Regexp.new("[^[[:word:]]]+") + else + /\W+/u + end + ## # Define an expectation with name +desc+. Name gets morphed to a # proper test method name. For some freakish reason, people who @@ -194,15 +204,22 @@ class MiniTest::Spec < MiniTest::Unit::TestCase @specs ||= 0 @specs += 1 - name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ] + name = "test_%04d_%s" % [ @specs, desc.gsub(NAME_RE, '_').downcase ] define_method name, &block self.children.each do |mod| mod.send :undef_method, name if mod.public_method_defined? name end + + name end + ## + # Essentially, define an accessor for +name+ with +block+. + # + # Why use let instead of def? I honestly don't know. + def self.let name, &block define_method name do @_memoized ||= {} @@ -210,6 +227,10 @@ class MiniTest::Spec < MiniTest::Unit::TestCase end end + ## + # Another lazy man's accessor generator. Made even more lazy by + # setting the name for you to +subject+. + def self.subject &block let :subject, &block end @@ -232,6 +253,14 @@ class MiniTest::Spec < MiniTest::Unit::TestCase end # :stopdoc: + def after_setup + run_setup_hooks + end + + def before_teardown + run_teardown_hooks + end + class << self attr_reader :desc alias :specify :it @@ -240,6 +269,9 @@ class MiniTest::Spec < MiniTest::Unit::TestCase # :startdoc: end +## +# It's where you hide your "assertions". + module MiniTest::Expectations ## # See MiniTest::Assertions#assert_empty. @@ -248,7 +280,7 @@ module MiniTest::Expectations # # :method: must_be_empty - infect_an_assertion :assert_empty, :must_be_empty + infect_an_assertion :assert_empty, :must_be_empty, :unary ## # See MiniTest::Assertions#assert_equal @@ -322,7 +354,7 @@ module MiniTest::Expectations # # :method: must_be_nil - infect_an_assertion :assert_nil, :must_be_nil + infect_an_assertion :assert_nil, :must_be_nil, :unary ## # See MiniTest::Assertions#assert_operator @@ -408,7 +440,7 @@ module MiniTest::Expectations # # :method: wont_be_empty - infect_an_assertion :refute_empty, :wont_be_empty + infect_an_assertion :refute_empty, :wont_be_empty, :unary ## # See MiniTest::Assertions#refute_equal @@ -483,7 +515,7 @@ module MiniTest::Expectations # # :method: wont_be_nil - infect_an_assertion :refute_nil, :wont_be_nil + infect_an_assertion :refute_nil, :wont_be_nil, :unary ## # See MiniTest::Assertions#refute_operator @@ -517,6 +549,6 @@ module MiniTest::Expectations infect_an_assertion :refute_same, :wont_be_same_as end -class Object +class Object # :nodoc: include MiniTest::Expectations end diff --git a/lib/minitest/unit.rb b/lib/minitest/unit.rb index 2c767b68fa..0a0e10816e 100644 --- a/lib/minitest/unit.rb +++ b/lib/minitest/unit.rb @@ -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,8 +62,13 @@ module MiniTest # 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 @@ -187,6 +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." msg = message(msg) { "Expected block to return true value" } assert yield, msg end @@ -225,7 +232,7 @@ module MiniTest 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 @@ module MiniTest # 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 @@ module MiniTest end ## - # Fails unless +exp+ is =~ +act+. + # Fails unless +matcher+ =~ +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 @@ module MiniTest assert obj.nil?, msg end - UNDEFINED = Object.new - def UNDEFINED.inspect; "UNDEFINED"; end - ## # For testing with binary operators. # @@ -313,8 +317,8 @@ module MiniTest yield end - x = assert_equal stdout, out, "In stdout" if stdout y = assert_equal stderr, err, "In stderr" if stderr + x = assert_equal stdout, out, "In stdout" if stdout (!stdout || x) && (!stderr || y) end @@ -334,10 +338,11 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest end ## - # Fails if +exp+ =~ +act+ + # Fails if +matcher+ =~ +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 @@ module MiniTest 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,6 +660,10 @@ module MiniTest attr_accessor :help # :nodoc: attr_accessor :verbose # :nodoc: attr_writer :options # :nodoc: + attr_accessor :last_error # :nodoc: + + ## + # Lazy accessor for options. def options @options ||= {} @@ -662,6 +671,7 @@ module MiniTest @@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 @@ module MiniTest # # 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 @@ module MiniTest # 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 @@ module MiniTest grep(/^run_/).map { |s| s.to_s }).uniq end + ## + # Return the IO for output. + def output self.class.output end @@ -756,6 +772,9 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest 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 @@ module MiniTest # exception +e+ def puke klass, meth, e + self.last_error = e e = case e when MiniTest::Skip then @skips += 1 @@ -859,9 +903,10 @@ module MiniTest @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 @@ module MiniTest 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 @@ -944,6 +989,52 @@ module MiniTest io.puts format % [test_count, assertion_count, failures, errors, skips] 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. @@ -951,6 +1042,9 @@ module MiniTest # See MiniTest::Assertions class TestCase + include Guard + extend Guard + attr_reader :__name__ # :nodoc: PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, @@ -963,16 +1057,21 @@ module MiniTest 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 @@ module MiniTest @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 @@ module MiniTest @@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 @@ module MiniTest 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 @@ -1074,11 +1181,33 @@ module MiniTest 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 @@ module MiniTest 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: diff --git a/test/minitest/metametameta.rb b/test/minitest/metametameta.rb new file mode 100644 index 0000000000..4352cfee82 --- /dev/null +++ b/test/minitest/metametameta.rb @@ -0,0 +1,49 @@ +# encoding: utf-8 +###################################################################### +# This file is imported from the minitest project. +# DO NOT make modifications in this repo. They _will_ be reverted! +# File a patch instead and assign it to Ryan Davis. +###################################################################### + +require 'tempfile' +require 'stringio' +require 'minitest/autorun' + +class MetaMetaMetaTestCase < MiniTest::Unit::TestCase + def assert_report expected = nil + expected ||= <<-EOM.gsub(/^ {6}/, '') + Run options: --seed 42 + + # Running tests: + + . + + Finished tests in 0.00 + + 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips + EOM + + output = @output.string.dup + output.sub!(/Finished tests in .*/, "Finished tests in 0.00") + output.sub!(/Loaded suite .*/, 'Loaded suite blah') + output.gsub!(/\[[^\]:]+:\d+\]/, '[FILE:LINE]') + output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in') + assert_equal(expected, output) + end + + def setup + super + srand 42 + 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 + super + MiniTest::Unit.output = $stdout + Object.send :remove_const, :ATestCase if defined? ATestCase + end +end diff --git a/test/minitest/test_minitest_benchmark.rb b/test/minitest/test_minitest_benchmark.rb index cdd7c3c640..fc1301e63b 100644 --- a/test/minitest/test_minitest_benchmark.rb +++ b/test/minitest/test_minitest_benchmark.rb @@ -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! diff --git a/test/minitest/test_minitest_mock.rb b/test/minitest/test_minitest_mock.rb index e8954621ec..02321e0f97 100644 --- a/test/minitest/test_minitest_mock.rb +++ b/test/minitest/test_minitest_mock.rb @@ -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! @@ -26,7 +27,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase def test_blow_up_if_not_called @mock.foo - util_verify_bad + util_verify_bad "expected meaning_of_life() => 42, got []" end def test_not_blow_up_if_everything_called @@ -46,7 +47,7 @@ class TestMiniTestMock < MiniTest::Unit::TestCase @mock.meaning_of_life @mock.expect(:bar, true) - util_verify_bad + util_verify_bad "expected bar() => true, got []" end def test_blow_up_on_wrong_number_of_arguments @@ -84,9 +85,20 @@ class TestMiniTestMock < MiniTest::Unit::TestCase @mock.meaning_of_life @mock.expect(:sum, 3, [1, 2]) - @mock.sum(2, 4) + e = assert_raises MockExpectationError do + @mock.sum(2, 4) + end - util_verify_bad + exp = "mocked method :sum called with unexpected arguments [2, 4]" + assert_equal exp, e.message + end + + def test_expect_with_non_array_args + e = assert_raises ArgumentError do + @mock.expect :blah, 3, false + end + + assert_equal "args must be an array", e.message end def test_respond_appropriately @@ -142,29 +154,59 @@ class TestMiniTestMock < MiniTest::Unit::TestCase 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 test_verify_shows_the_actual_arguments_in_the_message - mock = MiniTest::Mock.new - mock.expect :capitalized, true, ["a"] - mock.capitalized "b" e = assert_raises MockExpectationError do - mock.verify + mock.strict_expectation 1 end - a = {:retval=>true, :args=>["a"]} - b = {:retval=>true, :args=>["b"]} - - expected = "expected capitalized, #{a.inspect}, got [#{b.inspect}]" - assert_equal expected, e.message + exp = "mocked method :strict_expectation called with unexpected arguments [1]" + assert_equal exp, e.message end - def util_verify_bad - assert_raises MockExpectationError do + def test_method_missing_empty + mock = MiniTest::Mock.new + + mock.expect :a, nil + + mock.a + + e = assert_raises MockExpectationError do + mock.a + end + + assert_equal "No more expects available for :a: []", e.message + end + + def test_same_method_expects_are_verified_when_all_called + mock = MiniTest::Mock.new + mock.expect :foo, nil, [:bar] + mock.expect :foo, nil, [:baz] + + mock.foo :bar + mock.foo :baz + + assert mock.verify + end + + def test_same_method_expects_blow_up_when_not_all_called + mock = MiniTest::Mock.new + mock.expect :foo, nil, [:bar] + mock.expect :foo, nil, [:baz] + + mock.foo :bar + + e = assert_raises(MockExpectationError) { mock.verify } + + exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]" + + assert_equal exp, e.message + end + + def util_verify_bad exp + e = assert_raises MockExpectationError do @mock.verify end + + assert_equal exp, e.message end end diff --git a/test/minitest/test_minitest_spec.rb b/test/minitest/test_minitest_spec.rb index 27e3dc7628..68c4dec003 100644 --- a/test/minitest/test_minitest_spec.rb +++ b/test/minitest/test_minitest_spec.rb @@ -1,9 +1,11 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! # File a patch instead and assign it to Ryan Davis. ###################################################################### +# encoding: utf-8 require 'minitest/autorun' require 'stringio' @@ -13,6 +15,19 @@ class ExampleA; end class ExampleB < ExampleA; end describe MiniTest::Spec do + def assert_triggered expected = "blah", klass = MiniTest::Assertion + @assertion_count += 2 + + e = assert_raises(klass) do + yield + end + + msg = e.message.sub(/(---Backtrace---).*/m, '\1') + msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)') + + assert_equal expected, msg + end + before do @assertion_count = 4 end @@ -21,8 +36,78 @@ describe MiniTest::Spec do self._assertions.must_equal @assertion_count end - # TODO: figure out how the hell to write a test for this - # it "will skip if there is no block" + it "needs to be able to catch a MiniTest::Assertion exception" do + @assertion_count = 1 + + assert_triggered "Expected 1 to not be equal to 1." do + 1.wont_equal 1 + end + end + + it "needs to be sensible about must_include order" do + @assertion_count += 3 # must_include is 2 assertions + + [1, 2, 3].must_include(2).must_equal true + + assert_triggered "Expected [1, 2, 3] to include 5." do + [1, 2, 3].must_include 5 + end + + assert_triggered "msg.\nExpected [1, 2, 3] to include 5." do + [1, 2, 3].must_include 5, "msg" + end + end + + it "needs to be sensible about wont_include order" do + @assertion_count += 3 # wont_include is 2 assertions + + [1, 2, 3].wont_include(5).must_equal false + + assert_triggered "Expected [1, 2, 3] to not include 2." do + [1, 2, 3].wont_include 2 + end + + assert_triggered "msg.\nExpected [1, 2, 3] to not include 2." do + [1, 2, 3].wont_include 2, "msg" + end + end + + it "needs to catch an expected exception" do + @assertion_count = 2 + + proc { raise "blah" }.must_raise RuntimeError + proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion + end + + it "needs to catch an unexpected exception" do + @assertion_count -= 2 # no positive + + msg = <<-EOM.gsub(/^ {6}/, '').chomp + [RuntimeError] exception expected, not + Class: + Message: <\"MiniTest::Assertion\"> + ---Backtrace--- + EOM + + assert_triggered msg do + proc { raise MiniTest::Assertion }.must_raise RuntimeError + end + + assert_triggered "msg.\n#{msg}" do + proc { raise MiniTest::Assertion }.must_raise RuntimeError, "msg" + end + end + + it "needs to ensure silence" do + @assertion_count -= 1 # no msg + @assertion_count += 2 # assert_output is 2 assertions + + proc { }.must_be_silent.must_equal true + + assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do + proc { print "xxx" }.must_be_silent + end + end it "needs to have all methods named well" do @assertion_count = 2 @@ -60,159 +145,379 @@ describe MiniTest::Spec do wonts.must_equal expected_wonts end + it "needs to raise if an expected exception is not raised" do + @assertion_count -= 2 # no positive test + + assert_triggered "RuntimeError expected but nothing was raised." do + proc { 42 }.must_raise RuntimeError + end + + assert_triggered "msg.\nRuntimeError expected but nothing was raised." do + proc { 42 }.must_raise RuntimeError, "msg" + end + end + + it "needs to verify binary messages" do + 42.wont_be(:<, 24).must_equal false + + assert_triggered 'Expected 24 to not be < 42.' do + 24.wont_be :<, 42 + end + + assert_triggered "msg.\nExpected 24 to not be < 42." do + 24.wont_be :<, 42, "msg" + end + end + + it "needs to verify emptyness" do + @assertion_count += 3 # empty is 2 assertions + + [].must_be_empty.must_equal true + + assert_triggered "Expected [42] to be empty." do + [42].must_be_empty + end + + assert_triggered "msg.\nExpected [42] to be empty." do + [42].must_be_empty "msg" + end + end + it "needs to verify equality" do (6 * 7).must_equal(42).must_equal true - proc { (6 * 9).must_equal(42) }.must_raise MiniTest::Assertion + + assert_triggered "Expected: 42\n Actual: 54" do + (6 * 9).must_equal 42 + end + + assert_triggered "msg.\nExpected: 42\n Actual: 54" do + (6 * 9).must_equal 42, "msg" + end + end + + it "needs to verify floats outside a delta" do + @assertion_count += 1 # extra test + + 24.wont_be_close_to(42).must_equal false + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.001.' do + (6 * 7.0).wont_be_close_to 42 + end + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 1.0e-05.' do + (6 * 7.0).wont_be_close_to 42, 0.00001 + end + + assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be < 1.0e-05." do + (6 * 7.0).wont_be_close_to 42, 0.00001, "msg" + end + end + + it "needs to verify floats outside an epsilon" do + @assertion_count += 1 # extra test + + 24.wont_be_within_epsilon(42).must_equal false + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.042.' do + (6 * 7.0).wont_be_within_epsilon 42 + end + + assert_triggered 'Expected |42 - 42.0| (0.0) to not be < 0.00042.' do + (6 * 7.0).wont_be_within_epsilon 42, 0.00001 + end + + assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be < 0.00042." do + (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg" + end end it "needs to verify floats within a delta" do + @assertion_count += 1 # extra test + (6.0 * 7).must_be_close_to(42.0).must_equal true - proc { 42.002.must_be_close_to 42.0 }.must_raise MiniTest::Assertion + + assert_triggered 'Expected |0.0 - 0.01| (0.01) to be < 0.001.' do + (1.0 / 100).must_be_close_to 0.0 + end + + assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do + (1.0 / 1000).must_be_close_to 0.0, 0.000001 + end + + assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be < 1.0e-06." do + (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg" + end end - it "needs to verify types of objects" do - (6 * 7).must_be_instance_of(Fixnum).must_equal true - proc { (6 * 7).must_be_instance_of String }.must_raise MiniTest::Assertion - end + it "needs to verify floats within an epsilon" do + @assertion_count += 1 # extra test - it "needs to verify kinds of objects" do - @assertion_count = 6 + (6.0 * 7).must_be_within_epsilon(42.0).must_equal true - (6 * 7).must_be_kind_of(Fixnum).must_equal true - (6 * 7).must_be_kind_of(Numeric).must_equal true - proc { (6 * 7).must_be_kind_of String }.must_raise MiniTest::Assertion - end + assert_triggered 'Expected |0.0 - 0.01| (0.01) to be < 0.0.' do + (1.0 / 100).must_be_within_epsilon 0.0 + end - it "needs to verify regexp matches" do - @assertion_count = 6 + assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 0.0.' do + (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001 + end - "blah".must_match(/\w+/).must_equal true - proc { "blah".must_match(/\d+/) }.must_raise MiniTest::Assertion - end - - it "needs to verify nil" do - nil.must_be_nil.must_equal true - proc { 42.must_be_nil }.must_raise MiniTest::Assertion - end - - it "needs to verify using any binary operator" do - 41.must_be(:<, 42).must_equal true - proc { 42.must_be(:<, 41) }.must_raise MiniTest::Assertion - end - - it "needs to verify using any predicate" do - "".must_be(:empty?).must_equal true - proc { "blah".must_be(:empty?) }.must_raise MiniTest::Assertion - end - - it "needs to catch an expected exception" do - @assertion_count = 2 - - proc { raise "blah" }.must_raise RuntimeError - proc { raise MiniTest::Assertion }.must_raise MiniTest::Assertion - end - - it "needs to catch an unexpected exception" do - @assertion_count = 2 - - proc { - proc { raise MiniTest::Assertion }.must_raise(RuntimeError) - }.must_raise MiniTest::Assertion - end - - it "needs raise if an expected exception is not raised" do - @assertion_count = 2 - - proc { proc { 42 }.must_raise(RuntimeError) }.must_raise MiniTest::Assertion - end - - it "needs to be able to catch a MiniTest::Assertion exception" do - @assertion_count = 2 - - proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion - end - - it "needs to verify using respond_to" do - 42.must_respond_to(:+).must_equal true - proc { 42.must_respond_to(:clear) }.must_raise MiniTest::Assertion + assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be < 0.0." do + (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg" + end end it "needs to verify identity" do 1.must_be_same_as(1).must_equal true - proc { 1.must_be_same_as 2 }.must_raise MiniTest::Assertion - end - it "needs to verify throw" do - @assertion_count = 6 + assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do + 1.must_be_same_as 2 + end - proc { throw :blah }.must_throw(:blah).must_equal true - proc { proc { }.must_throw(:blah) }.must_raise MiniTest::Assertion - proc { proc { throw :xxx }.must_throw(:blah) }.must_raise MiniTest::Assertion + assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do + 1.must_be_same_as 2, "msg" + end end it "needs to verify inequality" do 42.wont_equal(6 * 9).must_equal false - proc { 1.wont_equal 1 }.must_raise MiniTest::Assertion + + assert_triggered "Expected 1 to not be equal to 1." do + 1.wont_equal 1 + end + + assert_triggered "msg.\nExpected 1 to not be equal to 1." do + 1.wont_equal 1, "msg" + end + end + + it "needs to verify instances of a class" do + 42.wont_be_instance_of(String).must_equal false + + assert_triggered 'Expected 42 to not be an instance of Fixnum.' do + 42.wont_be_instance_of Fixnum + end + + assert_triggered "msg.\nExpected 42 to not be an instance of Fixnum." do + 42.wont_be_instance_of Fixnum, "msg" + end + end + + it "needs to verify kinds of a class" do + 42.wont_be_kind_of(String).must_equal false + + assert_triggered 'Expected 42 to not be a kind of Integer.' do + 42.wont_be_kind_of Integer + end + + assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do + 42.wont_be_kind_of Integer, "msg" + end + end + + it "needs to verify kinds of objects" do + @assertion_count += 2 # extra test + + (6 * 7).must_be_kind_of(Fixnum).must_equal true + (6 * 7).must_be_kind_of(Numeric).must_equal true + + assert_triggered "Expected 42 to be a kind of String, not Fixnum." do + (6 * 7).must_be_kind_of String + end + + assert_triggered "msg.\nExpected 42 to be a kind of String, not Fixnum." do + (6 * 7).must_be_kind_of String, "msg" + end end it "needs to verify mismatch" do - @assertion_count = 6 + @assertion_count += 3 # match is 2 + "blah".wont_match(/\d+/).must_equal false - proc { "blah".wont_match(/\w+/) }.must_raise MiniTest::Assertion + + assert_triggered "Expected /\\w+/ to not match \"blah\"." do + "blah".wont_match(/\w+/) + end + + assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do + "blah".wont_match(/\w+/, "msg") + end end - it "needs to verify using any (negative) predicate" do - "blah".wont_be(:empty?).must_equal false - proc { "".wont_be(:empty?) }.must_raise MiniTest::Assertion + it "needs to verify nil" do + nil.must_be_nil.must_equal true + + assert_triggered "Expected 42 to be nil." do + 42.must_be_nil + end + + assert_triggered "msg.\nExpected 42 to be nil." do + 42.must_be_nil "msg" + end end - it "needs to verify non-nil" do - 42.wont_be_nil.must_equal false - proc { nil.wont_be_nil }.must_raise MiniTest::Assertion + it "needs to verify non-emptyness" do + @assertion_count += 3 # empty is 2 assertions + + ['some item'].wont_be_empty.must_equal false + + assert_triggered "Expected [] to not be empty." do + [].wont_be_empty + end + + assert_triggered "msg.\nExpected [] to not be empty." do + [].wont_be_empty "msg" + end end it "needs to verify non-identity" do 1.wont_be_same_as(2).must_equal false - proc { 1.wont_be_same_as 1 }.must_raise MiniTest::Assertion + + assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do + 1.wont_be_same_as 1 + end + + assert_triggered "msg.\nExpected 1 (oid=N) to not be the same as 1 (oid=N)." do + 1.wont_be_same_as 1, "msg" + end end - it "needs to verify output in stdout" do - proc { print "blah" }.must_output("blah").must_equal true + it "needs to verify non-nil" do + 42.wont_be_nil.must_equal false - proc { - proc { print "xxx" }.must_output("blah") - }.must_raise MiniTest::Assertion + assert_triggered "Expected nil to not be nil." do + nil.wont_be_nil + end + + assert_triggered "msg.\nExpected nil to not be nil." do + nil.wont_be_nil "msg" + end + end + + it "needs to verify objects not responding to a message" do + "".wont_respond_to(:woot!).must_equal false + + assert_triggered 'Expected "" to not respond to to_s.' do + "".wont_respond_to :to_s + end + + assert_triggered "msg.\nExpected \"\" to not respond to to_s." do + "".wont_respond_to :to_s, "msg" + end end it "needs to verify output in stderr" do + @assertion_count -= 1 # no msg + proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true - proc { + assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do proc { $stderr.print "xxx" }.must_output(nil, "blah") - }.must_raise MiniTest::Assertion + end end - it "needs to ensure silence" do - @assertion_count = 5 + it "needs to verify output in stdout" do + @assertion_count -= 1 # no msg - proc { }.must_be_silent.must_equal true + proc { print "blah" }.must_output("blah").must_equal true - proc { - proc { print "xxx" }.must_be_silent - }.must_raise MiniTest::Assertion + assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do + proc { print "xxx" }.must_output("blah") + end end - it "needs to be sensible about must_include order" do - @assertion_count = 6 - [1, 2, 3].must_include(2).must_equal true - proc { [1, 2, 3].must_include 5 }.must_raise MiniTest::Assertion + it "needs to verify regexp matches" do + @assertion_count += 3 # must_match is 2 assertions + + "blah".must_match(/\w+/).must_equal true + + assert_triggered "Expected /\\d+/ to match \"blah\"." do + "blah".must_match(/\d+/) + end + + assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do + "blah".must_match(/\d+/, "msg") + end end - it "needs to be sensible about wont_include order" do - @assertion_count = 6 - [1, 2, 3].wont_include(5).must_equal false - proc { [1, 2, 3].wont_include 2 }.must_raise MiniTest::Assertion + it "needs to verify throw" do + @assertion_count += 2 # 2 extra tests + + proc { throw :blah }.must_throw(:blah).must_equal true + + assert_triggered "Expected :blah to have been thrown." do + proc { }.must_throw :blah + end + + assert_triggered "Expected :blah to have been thrown, not :xxx." do + proc { throw :xxx }.must_throw :blah + end + + assert_triggered "msg.\nExpected :blah to have been thrown." do + proc { }.must_throw :blah, "msg" + end + + assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do + proc { throw :xxx }.must_throw :blah, "msg" + end end + + it "needs to verify types of objects" do + (6 * 7).must_be_instance_of(Fixnum).must_equal true + + exp = "Expected 42 to be an instance of String, not Fixnum." + + assert_triggered exp do + (6 * 7).must_be_instance_of String + end + + assert_triggered "msg.\n#{exp}" do + (6 * 7).must_be_instance_of String, "msg" + end + end + + it "needs to verify using any (negative) predicate" do + @assertion_count -= 1 # doesn't take a message + + "blah".wont_be(:empty?).must_equal false + + assert_triggered "Expected \"\" to not be empty?." do + "".wont_be :empty? + end + end + + it "needs to verify using any binary operator" do + @assertion_count -= 1 # no msg + + 41.must_be(:<, 42).must_equal true + + assert_triggered "Expected 42 to be < 41." do + 42.must_be(:<, 41) + end + end + + it "needs to verify using any predicate" do + @assertion_count -= 1 # no msg + + "".must_be(:empty?).must_equal true + + assert_triggered "Expected \"blah\" to be empty?." do + "blah".must_be :empty? + end + end + + it "needs to verify using respond_to" do + 42.must_respond_to(:+).must_equal true + + assert_triggered "Expected 42 (Fixnum) to respond to #clear." do + 42.must_respond_to :clear + end + + assert_triggered "msg.\nExpected 42 (Fixnum) to respond to #clear." do + 42.must_respond_to :clear, "msg" + end + end + end describe MiniTest::Spec, :let do @@ -381,17 +686,21 @@ class TestMeta < MiniTest::Unit::TestCase end def test_describe_first_structure - x = y = z = nil + x = x1 = x2 = y = z = nil x = describe "top-level thingy" do y = describe "first thingy" do end - it "top-level-it" do end + x1 = it "top-level-it" do end + x2 = it "не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world" 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} + test_methods = ['test_0001_top_level_it', 'test_0002_не_латинские_буквы_и_спецсимволы_いった_α_β_γ_δ_ε_hello_world'].sort + + assert_equal test_methods, [x1, x2] + assert_equal test_methods, + x.instance_methods.grep(/^test/).map {|o| o.to_s}.sort assert_equal [], y.instance_methods.grep(/^test/) assert_equal [], z.instance_methods.grep(/^test/) end diff --git a/test/minitest/test_minitest_unit.rb b/test/minitest/test_minitest_unit.rb index 7db7b3ce30..935b0e274a 100644 --- a/test/minitest/test_minitest_unit.rb +++ b/test/minitest/test_minitest_unit.rb @@ -1,18 +1,18 @@ +# encoding: utf-8 ###################################################################### # This file is imported from the minitest project. # DO NOT make modifications in this repo. They _will_ be reverted! # File a patch instead and assign it to Ryan Davis. ###################################################################### -require 'stringio' require 'pathname' -require 'minitest/autorun' +require 'test/minitest/metametameta' module MyModule; end class AnError < StandardError; include MyModule; end class ImmutableString < String; def inspect; super.freeze; end; end -class TestMiniTestUnit < MiniTest::Unit::TestCase +class TestMiniTestUnit < MetaMetaMetaTestCase pwd = Pathname.new(File.expand_path(Dir.pwd)) basedir = Pathname.new(File.expand_path("lib/minitest")) + 'mini' basedir = basedir.relative_path_from(pwd).to_s @@ -22,38 +22,6 @@ class TestMiniTestUnit < MiniTest::Unit::TestCase "#{MINITEST_BASE_DIR}/test.rb:139:in `run'", "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"] - def assert_report expected = nil - expected ||= "Run options: --seed 42 - -# Running tests: - -. - -Finished tests in 0.00 - -1 tests, 1 assertions, 0 failures, 0 errors, 0 skips -" - output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00") - output.sub!(/Loaded suite .*/, 'Loaded suite blah') - output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:') - output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]') - assert_equal(expected, output) - end - - def setup - srand 42 - 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 - Object.send :remove_const, :ATestCase if defined? ATestCase - end - def test_class_puke_with_assertion_failed exception = MiniTest::Assertion.new "Oh no!" exception.set_backtrace ["unhappy"] @@ -235,21 +203,23 @@ Finished tests in 0.00 @tu.run %w[--seed 42] - expected = "Run options: --seed 42 + expected = <<-EOM.gsub(/^ {6}/, '') + Run options: --seed 42 -# Running tests: + # Running tests: -E. + E. -Finished tests in 0.00 + Finished tests in 0.00 - 1) Error: -test_error(ATestCase): -RuntimeError: unhandled exception - FILE:LINE:in `test_error' + 1) Error: + test_error(ATestCase): + RuntimeError: unhandled exception + FILE:LINE:in `test_error' + + 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips + EOM -2 tests, 1 assertions, 0 failures, 1 errors, 0 skips -" assert_report expected end @@ -506,10 +476,82 @@ Finished tests in 0.00 end end + def test_before_setup + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :setup do + super() + call_order << :setup + end + + define_method :before_setup do + call_order << :before_setup + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:before_setup, :setup] + assert_equal expected, call_order + end + + def test_after_teardown + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :teardown do + super() + call_order << :teardown + end + + define_method :after_teardown do + call_order << :after_teardown + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:teardown, :after_teardown] + assert_equal expected, call_order + end + + def test_all_teardowns_are_guaranteed_to_run + call_order = [] + Class.new(MiniTest::Unit::TestCase) do + define_method :after_teardown do + super() + call_order << :after_teardown + raise + end + + define_method :teardown do + super() + call_order << :teardown + raise + end + + define_method :before_teardown do + super() + call_order << :before_teardown + raise + end + + def test_omg; assert true; end + end + + @tu.run %w[--seed 42] + + expected = [:before_teardown, :teardown, :after_teardown] + assert_equal expected, call_order + end + def test_setup_hooks call_order = [] - tc = Class.new(MiniTest::Unit::TestCase) do + tc = Class.new(MiniTest::Spec) do define_method :setup do super() call_order << :method @@ -546,7 +588,7 @@ Finished tests in 0.00 def test_teardown_hooks call_order = [] - tc = Class.new(MiniTest::Unit::TestCase) do + tc = Class.new(MiniTest::Spec) do define_method :teardown do super() call_order << :method @@ -583,7 +625,7 @@ Finished tests in 0.00 def test_setup_and_teardown_hooks_survive_inheritance call_order = [] - parent = Class.new(MiniTest::Unit::TestCase) do + parent = Class.new(MiniTest::Spec) do define_method :setup do super() call_order << :setup_method @@ -626,7 +668,11 @@ Finished tests in 0.00 end class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase + RUBY18 = ! defined? Encoding + def setup + super + MiniTest::Unit::TestCase.reset @tc = MiniTest::Unit::TestCase.new 'fake tc' @@ -659,15 +705,22 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_block - @tc.assert_block do - true + exp = ["NOTE: MiniTest::Unit::TestCase#assert_block is deprecated,", + "use assert. It will be removed on or after 2012-06-01.\n"].join " " + + assert_output "", exp do + @tc.assert_block do + true + end end end def test_assert_block_triggered - util_assert_triggered "blah.\nExpected block to return true value." do - @tc.assert_block "blah" do - false + assert_output do + util_assert_triggered "blah.\nExpected block to return true value." do + @tc.assert_block "blah" do + false + end end end end @@ -804,13 +857,13 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_in_delta_triggered - util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to be < 1.0e-06.' do + util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to be < 1.0e-06.' do @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001 end end def test_assert_in_epsilon - @assertion_count = 8 + @assertion_count = 10 @tc.assert_in_epsilon 10000, 9991 @tc.assert_in_epsilon 9991, 10000 @@ -821,14 +874,24 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_in_epsilon 9999.1, 10000, 0.0001 @tc.assert_in_epsilon 1.0, 1.0001, 0.0001 @tc.assert_in_epsilon 1.0001, 1.0, 0.0001 + + @tc.assert_in_epsilon(-1, -1) + @tc.assert_in_epsilon(-10000, -9991) end def test_assert_in_epsilon_triggered - util_assert_triggered 'Expected 10000 - 9990 (10) to be < 9.99.' do + util_assert_triggered 'Expected |10000 - 9990| (10) to be < 9.99.' do @tc.assert_in_epsilon 10000, 9990 end end + def test_assert_in_epsilon_triggered_negative_case + x = RUBY18 ? "0.1" : "0.10000000000000009" + util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be < 0.1." do + @tc.assert_in_epsilon(-1.1, -1, 0.1) + end + end + def test_assert_includes @assertion_count = 2 @@ -871,7 +934,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_match(/\w+/, "blah blah blah") end - def test_assert_match_object + def test_assert_match_matcher_object @assertion_count = 2 pattern = Object.new @@ -880,6 +943,15 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase @tc.assert_match pattern, 5 end + def test_assert_match_matchee_to_str + @assertion_count = 2 + + obj = Object.new + def obj.to_str; "blah" end + + @tc.assert_match "blah", obj + end + def test_assert_match_object_triggered @assertion_count = 2 @@ -956,7 +1028,7 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end def test_assert_output_triggered_both - util_assert_triggered util_msg("yay", "boo", "In stdout") do + util_assert_triggered util_msg("blah", "blah blah", "In stderr") do @tc.assert_output "yay", "blah" do print "boo" $stderr.print "blah blah" @@ -980,12 +1052,28 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end + def test_assert_predicate + @tc.assert_predicate "", :empty? + end + + def test_assert_predicate_triggered + util_assert_triggered 'Expected "blah" to be empty?.' do + @tc.assert_predicate "blah", :empty? + end + end + def test_assert_raises @tc.assert_raises RuntimeError do raise "blah" end end + def test_assert_raises_module + @tc.assert_raises MyModule do + raise AnError + end + end + ## # *sigh* This is quite an odd scenario, but it is from real (albeit # ugly) test code in ruby-core: @@ -1006,12 +1094,6 @@ class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase end end - def test_assert_raises_module - @tc.assert_raises MyModule do - raise AnError - end - end - def test_assert_raises_triggered_different e = assert_raises MiniTest::Assertion do @tc.assert_raises RuntimeError do @@ -1039,13 +1121,15 @@ FILE:LINE:in `test_assert_raises_triggered_different' end end - expected = "XXX -[RuntimeError] exception expected, not -Class: -Message: <\"icky\"> ----Backtrace--- -FILE:LINE:in `test_assert_raises_triggered_different_msg' ----------------" + expected = <<-EOM.gsub(/^ {6}/, '').chomp + XXX. + [RuntimeError] exception expected, not + Class: + Message: <\"icky\"> + ---Backtrace--- + FILE:LINE:in `test_assert_raises_triggered_different_msg' + --------------- + EOM actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE') actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0' @@ -1072,7 +1156,7 @@ FILE:LINE:in `test_assert_raises_triggered_different_msg' end end - expected = "XXX\nMiniTest::Assertion expected but nothing was raised." + expected = "XXX.\nMiniTest::Assertion expected but nothing was raised." assert_equal expected, e.message end @@ -1150,8 +1234,6 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_assert_silent_triggered_err - @assertion_count = 2 - util_assert_triggered util_msg("", "blah blah", "In stderr") do @tc.assert_silent do $stderr.print "blah blah" @@ -1160,6 +1242,8 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_assert_silent_triggered_out + @assertion_count = 2 + util_assert_triggered util_msg("", "blah blah", "In stdout") do @tc.assert_silent do print "blah blah" @@ -1211,10 +1295,15 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' methods = MiniTest::Assertions.public_instance_methods methods.map! { |m| m.to_s } if Symbol === methods.first - ignores = %w(assert_block assert_no_match assert_not_equal - assert_not_nil assert_not_same assert_nothing_raised - assert_nothing_thrown assert_output assert_raise - assert_raises assert_send assert_silent assert_throws) + # These don't have corresponding refutes _on purpose_. They're + # useless and will never be added, so don't bother. + ignores = %w[assert_block assert_output assert_raises assert_send + assert_silent assert_throws] + + # These are test/unit methods. I'm not actually sure why they're still here + ignores += %w[assert_no_match assert_not_equal assert_not_nil + assert_not_same assert_nothing_raised + assert_nothing_thrown assert_raise] asserts = methods.grep(/^assert/).sort - ignores refutes = methods.grep(/^refute/).sort - ignores @@ -1252,6 +1341,12 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end end + def test_expectation_with_a_message + util_assert_triggered "Expected: 2\n Actual: 1" do + 1.must_equal 2, '' + end + end + def test_flunk util_assert_triggered 'Epic Fail!' do @tc.flunk @@ -1317,7 +1412,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_refute_in_delta_triggered - util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to not be < 0.1.' do + util_assert_triggered 'Expected |0.0 - 0.001| (0.001) to not be < 0.1.' do @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1 end end @@ -1327,7 +1422,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end def test_refute_in_epsilon_triggered - util_assert_triggered 'Expected 10000 - 9991 (9) to not be < 10.0.' do + util_assert_triggered 'Expected |10000 - 9991| (9) to not be < 10.0.' do @tc.refute_in_epsilon 10000, 9991 fail end @@ -1375,7 +1470,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' @tc.refute_match(/\d+/, "blah blah blah") end - def test_refute_match_object + def test_refute_match_matcher_object @assertion_count = 2 @tc.refute_match Object.new, 5 # default #=~ returns false end @@ -1409,6 +1504,16 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' end end + def test_refute_predicate + @tc.refute_predicate "42", :empty? + end + + def test_refute_predicate_triggered + util_assert_triggered 'Expected "" to not be empty?.' do + @tc.refute_predicate "", :empty? + end + end + def test_refute_operator @tc.refute_operator 2, :<, 1 end @@ -1482,6 +1587,28 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' assert_equal expected, sample_test_case.test_methods end + def test_i_suck_and_my_tests_are_order_dependent_bang_sets_test_order_alpha + @assertion_count = 0 + + shitty_test_case = Class.new MiniTest::Unit::TestCase + + shitty_test_case.i_suck_and_my_tests_are_order_dependent! + + assert_equal :alpha, shitty_test_case.test_order + end + + def test_i_suck_and_my_tests_are_order_dependent_bang_does_not_warn + @assertion_count = 0 + + shitty_test_case = Class.new MiniTest::Unit::TestCase + + def shitty_test_case.test_order ; :lol end + + assert_silent do + shitty_test_case.i_suck_and_my_tests_are_order_dependent! + end + end + def util_assert_triggered expected, klass = MiniTest::Assertion e = assert_raises(klass) do yield @@ -1508,3 +1635,25 @@ FILE:LINE:in `test_assert_raises_triggered_subclass' MiniTest::Assertions.diff = old_diff end end + +class TestMiniTestGuard < MiniTest::Unit::TestCase + def test_mri_eh + assert self.class.mri? "ruby blah" + assert self.mri? "ruby blah" + end + + def test_jruby_eh + assert self.class.jruby? "java" + assert self.jruby? "java" + end + + def test_rubinius_eh + assert self.class.rubinius? "rbx" + assert self.rubinius? "rbx" + end + + def test_windows_eh + assert self.class.windows? "mswin" + assert self.windows? "mswin" + end +end