[前][次][番号順一覧][スレッド一覧]

ruby-changes:19840

From: ryan <ko1@a...>
Date: Wed, 1 Jun 2011 14:13:05 +0900 (JST)
Subject: [ruby-changes:19840] ryan:r31887 (trunk): MOSTLY Imported minitest 2.2.1 (r6277)

ryan	2011-06-01 14:12:55 +0900 (Wed, 01 Jun 2011)

  New Revision: 31887

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31887

  Log:
    MOSTLY Imported minitest 2.2.1 (r6277)

  Modified files:
    trunk/ChangeLog
    trunk/lib/minitest/benchmark.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 31886)
+++ ChangeLog	(revision 31887)
@@ -1,3 +1,10 @@
+Wed Jun  1 14:07:57 2011  Ryan Davis  <ryand-ruby@z...>
+
+	* lib/minitest/*: MOSTLY Imported minitest 2.2.1 (r6277)... One
+	  feature wouldn't run and I don't know how to fix it. I need
+	  sora_h's help to get it running happy with test/unit.
+	* test/minitest/*: ditto
+
 Wed Jun  1 12:35:50 2011  Ryan Davis  <ryand-ruby@z...>
 
 	* lib/rubygems*: Import rubygems 1.8.5 (released @ 137c80f)
Index: lib/minitest/unit.rb
===================================================================
--- lib/minitest/unit.rb	(revision 31886)
+++ lib/minitest/unit.rb	(revision 31887)
@@ -5,6 +5,7 @@
 ######################################################################
 
 require 'optparse'
+require 'rbconfig'
 
 ##
 # Minimal (mostly drop-in) replacement for test-unit.
@@ -65,16 +66,105 @@
 
   module Assertions
 
+    WINDOZE = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
+
     ##
-    # mu_pp gives a human-readable version of +obj+.  By default #inspect is
-    # called.  You can override this to use #pretty_print if you want.
+    # Returns the diff command to use in #diff. Tries to intelligently
+    # figure out what diff to use.
 
+    def self.diff
+      @diff = if WINDOZE
+                "diff.exe -u"
+              else
+                if system("gdiff", __FILE__, __FILE__)
+                  "gdiff -u" # solaris and kin suck
+                elsif system("diff", __FILE__, __FILE__)
+                  "diff -u"
+                else
+                  nil
+                end
+              end unless defined? @diff
+
+      @diff
+    end
+
+    ##
+    # Set the diff command to use in #diff.
+
+    def self.diff= o
+      @diff = o
+    end
+
+    ##
+    # Returns a diff between +exp+ and +act+. If there is no known
+    # diff command or if it doesn't make sense to diff the output
+    # (single line, short output), then it simply returns a basic
+    # comparison between the two.
+
+    def diff exp, act
+      require "tempfile"
+
+      expect = mu_pp_for_diff exp
+      butwas = mu_pp_for_diff act
+      result = nil
+
+      need_to_diff =
+        MiniTest::Assertions.diff &&
+        (expect.include?("\n")    ||
+         butwas.include?("\n")    ||
+         expect.size > 30         ||
+         butwas.size > 30         ||
+         expect == butwas)
+
+      return "Expected: #{mu_pp exp}\n  Actual: #{mu_pp act}" unless
+        need_to_diff
+
+      Tempfile.open("expect") do |a|
+        a.puts expect
+        a.rewind
+        Tempfile.open("butwas") do |b|
+          b.puts butwas
+          b.rewind
+
+          result = `#{MiniTest::Assertions.diff} #{a.path} #{b.path}`
+          result.sub!(/^\-\-\- .+/, "--- expected")
+          result.sub!(/^\+\+\+ .+/, "+++ actual")
+
+          if result.empty? then
+            klass = exp.class
+            result = [
+                      "No visible difference.",
+                      "You should look at your implementation of #{klass}#==.",
+                      expect
+                     ].join "\n"
+          end
+        end
+      end
+
+      result
+    end
+
+    ##
+    # This returns a human-readable version of +obj+. By default
+    # #inspect is called. You can override this to use #pretty_print
+    # if you want.
+
     def mu_pp obj
       s = obj.inspect
       s = s.force_encoding Encoding.default_external if defined? Encoding
       s
     end
 
+    ##
+    # This returns a diff-able human-readable version of +obj+. This
+    # differs from the regular mu_pp because it expands escaped
+    # newlines and makes hex-values generic (like object_ids). This
+    # uses mu_pp to do the first pass and then cleans it up.
+
+    def mu_pp_for_diff obj # TODO: possibly rename
+      mu_pp(obj).gsub(/\\n/, "\n").gsub(/0x[a-f0-9]+/m, '0xXXXXXX')
+    end
+
     def _assertions= n # :nodoc:
       @_assertions = n
     end
@@ -114,12 +204,19 @@
     end
 
     ##
-    # Fails unless <tt>exp == act</tt>.
+    # Fails unless <tt>exp == act</tt> printing the difference between
+    # the two, if possible.
     #
-    # For floats use assert_in_delta
+    # If there is no visible difference but the assertion fails, you
+    # should suspect that your #== is buggy, or your inspect output is
+    # missing crucial details.
+    #
+    # For floats use assert_in_delta.
+    #
+    # See also: MiniTest::Assertions.diff
 
     def assert_equal exp, act, msg = nil
-      msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" }
+      msg = message(msg) { diff exp, act }
       assert(exp == act, msg)
     end
 
@@ -181,7 +278,7 @@
     def assert_match exp, act, msg = nil
       msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" }
       assert_respond_to act, :"=~"
-      exp = /#{Regexp.escape exp}/ if String === exp && String === act
+      exp = Regexp.new Regexp.escape exp if String === exp and String === act
       assert exp =~ act, msg
     end
 
@@ -225,8 +322,8 @@
     # Fails unless the block raises one of +exp+
 
     def assert_raises *exp
-      msg = String === exp.last ? exp.pop : nil
-      msg = msg.to_s + "\n" if msg
+      msg = "#{exp.pop}\n" if String === exp.last
+
       should_raise = false
       begin
         yield
@@ -346,7 +443,14 @@
     # Returns details for exception +e+
 
     def exception_details e, msg
-      "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{MiniTest::filter_backtrace(e.backtrace).join("\n")}\n---------------"
+      [
+       "#{msg}",
+       "Class: <#{e.class}>",
+       "Message: <#{e.message.inspect}>",
+       "---Backtrace---",
+       "#{MiniTest::filter_backtrace(e.backtrace).join("\n")}",
+       "---------------",
+      ].join "\n"
     end
 
     ##
@@ -362,14 +466,8 @@
 
     def message msg = nil, &default
       proc {
-        if msg then
-          msg = msg.to_s unless String === msg
-          msg += '.' unless msg.empty?
-          msg += "\n#{default.call}."
-          msg.strip
-        else
-          "#{default.call}."
-        end
+        custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
+        "#{custom_message}#{default.call}."
       }
     end
 
@@ -521,7 +619,7 @@
   end
 
   class Unit
-    VERSION = "2.0.2" # :nodoc:
+    VERSION = "2.2.1" # :nodoc:
 
     attr_accessor :report, :failures, :errors, :skips # :nodoc:
     attr_accessor :test_count, :assertion_count       # :nodoc:
@@ -698,6 +796,7 @@
       e = case e
           when MiniTest::Skip then
             @skips += 1
+            return "S" unless @verbose
             "Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
           when MiniTest::Assertion then
             @failures += 1
Index: lib/minitest/benchmark.rb
===================================================================
--- lib/minitest/benchmark.rb	(revision 31886)
+++ lib/minitest/benchmark.rb	(revision 31887)
@@ -111,11 +111,17 @@
 
     ##
     # Runs the given +work+ and asserts that the times gathered fit to
-    # match a constant rate (eg, linear slope == 0) within a given error
-    # +threshold+.
+    # match a constant rate (eg, linear slope == 0) within a given
+    # +threshold+. Note: because we're testing for a slope of 0, R^2
+    # is not a good determining factor for the fit, so the threshold
+    # is applied against the slope itself. As such, you probably want
+    # to tighten it from the default.
     #
-    # Fit is calculated by #fit_constant.
+    # See http://www.graphpad.com/curvefit/goodness_of_fit.htm for
+    # more details.
     #
+    # Fit is calculated by #fit_linear.
+    #
     # Ranges are specified by ::bench_range.
     #
     # Eg:
@@ -328,7 +334,7 @@
   #     end
   #   end
 
-  def self.bench_performance_linear name, threshold = 0.9, &work
+  def self.bench_performance_linear name, threshold = 0.99, &work
     bench name do
       assert_performance_linear threshold, &work
     end
Index: lib/minitest/spec.rb
===================================================================
--- lib/minitest/spec.rb	(revision 31886)
+++ lib/minitest/spec.rb	(revision 31887)
@@ -61,17 +61,41 @@
   #
   # TODO: find good tutorial url.
   #
-  # Defines a test class subclassing from either
-  # MiniTest::Unit::TestCase or from the surrounding describe's class.
+  # Defines a test class subclassing from either MiniTest::Spec or
+  # from the surrounding describe's class. The surrounding class may
+  # subclass MiniTest::Spec manually in order to easily share code:
+  #
+  #     class MySpec < MiniTest::Spec
+  #       # ... shared code ...
+  #     end
+  #
+  #     class TestStuff < MySpec
+  #       it "does stuff" do
+  #         # shared code available here
+  #       end
+  #       describe "inner stuff" do
+  #         it "still does stuff" do
+  #           # ...and here
+  #         end
+  #       end
+  #     end
 
-  def describe desc, &block
+  def describe desc, &block # :doc:
     stack = MiniTest::Spec.describe_stack
     name  = [stack.last, desc].compact.join("::")
-    cls   = Class.new(stack.last || MiniTest::Spec)
+    sclas = stack.last || if Class === self && self < MiniTest::Spec then
+                            self
+                          else
+                            MiniTest::Spec.spec_type desc
+                          end
+    cls   = Class.new sclas
 
+    sclas.children << cls unless cls == MiniTest::Spec
+
     # :stopdoc:
     # omg this sucks
     (class << cls; self; end).send(:define_method, :to_s) { name }
+    (class << cls; self; end).send(:define_method, :desc) { desc }
     # :startdoc:
 
     cls.nuke_test_methods!
@@ -84,21 +108,40 @@
   private :describe
 end
 
-class Module
-  def classes type = Object # :nodoc:
-    constants.map { |n| const_get n }.find_all { |c|
-      c.class == Class and type > c
-    } - [self]
-  end
-end
-
 ##
 # MiniTest::Spec -- The faster, better, less-magical spec framework!
 #
 # For a list of expectations, see Object.
 
+class MiniTest::Spec < MiniTest::Unit::TestCase
+  ##
+  # Contains pairs of matchers and Spec classes to be used to
+  # calculate the superclass of a top-level describe. This allows for
+  # automatically customizable spec types.
+  #
+  # See: register_spec_type and spec_type
 
-class MiniTest::Spec < MiniTest::Unit::TestCase
+  TYPES = [[//, MiniTest::Spec]]
+
+  ##
+  # Register a new type of spec that matches the spec's description. Eg:
+  #
+  #     register_spec_plugin(/Controller$/, MiniTest::Spec::Rails)
+
+  def self.register_spec_type matcher, klass
+    TYPES.unshift [matcher, klass]
+  end
+
+  ##
+  # Figure out the spec class to use based on a spec's description. Eg:
+  #
+  #     spec_type("BlahController") # => MiniTest::Spec::Rails
+
+  def self.spec_type desc
+    desc = desc.to_s
+    TYPES.find { |re, klass| re === desc }.last
+  end
+
   @@describe_stack = []
   def self.describe_stack # :nodoc:
     @@describe_stack
@@ -108,6 +151,10 @@
     @@current_spec
   end
 
+  def self.children
+    @children ||= []
+  end
+
   def initialize name # :nodoc:
     super
     @@current_spec = self
@@ -119,12 +166,22 @@
     end
   end
 
+  ##
+  # Spec users want setup/teardown to be inherited and NOTHING ELSE.
+  # It is almost like method reuse is lost on them.
+
   def self.define_inheritable_method name, &block # :nodoc:
+    # regular super() warns
     super_method = self.superclass.instance_method name
 
+    teardown     = name.to_s == "teardown"
+    super_before = super_method && ! teardown
+    super_after  = super_method && teardown
+
     define_method name do
-      super_method.bind(self).call if super_method # regular super() warns
+      super_method.bind(self).call if super_before
       instance_eval(&block)
+      super_method.bind(self).call if super_after
     end
   end
 
@@ -171,8 +228,8 @@
 
     define_method name, &block
 
-    classes(MiniTest::Spec).each do |mod|
-      mod.send :undef_method, name if mod.respond_to? name
+    self.children.each do |mod|
+      mod.send :undef_method, name if mod.public_method_defined? name
     end
   end
 end
Index: test/minitest/test_minitest_mock.rb
===================================================================
--- test/minitest/test_minitest_mock.rb	(revision 31886)
+++ test/minitest/test_minitest_mock.rb	(revision 31887)
@@ -89,10 +89,10 @@
   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
Index: test/minitest/test_minitest_benchmark.rb
===================================================================
--- test/minitest/test_minitest_benchmark.rb	(revision 31886)
+++ test/minitest/test_minitest_benchmark.rb	(revision 31887)
@@ -50,6 +50,21 @@
     assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
   end
 
+  def test_fit_constant_clean
+    x = (1..5).to_a
+    y = [5.0, 5.0, 5.0, 5.0, 5.0]
+
+    assert_fit :linear, x, y, nil, 5.0, 0
+  end
+
+  def test_fit_constant_noisy
+    x = (1..5).to_a
+    y = [1.0, 1.2, 1.0, 0.8, 1.0]
+
+    # verified in numbers and R
+    assert_fit :linear, x, y, nil, 1.12, -0.04
+  end
+
   def test_fit_linear_clean
     # y = m * x + b where m = 2.2, b = 3.1
     x = (1..5).to_a
@@ -96,7 +111,7 @@
   def assert_fit msg, x, y, fit, exp_a, exp_b
     a, b, rr = send "fit_#{msg}", x, y
 
-    assert_operator rr, :>=, fit
+    assert_operator rr, :>=, fit if fit
     assert_in_delta exp_a, a
     assert_in_delta exp_b, b
   end
Index: test/minitest/test_minitest_spec.rb
===================================================================
--- test/minitest/test_minitest_spec.rb	(revision 31886)
+++ test/minitest/test_minitest_spec.rb	(revision 31887)
@@ -5,6 +5,7 @@
 ######################################################################
 
 require 'minitest/spec'
+require 'stringio'
 
 MiniTest::Unit.autorun
 
@@ -202,34 +203,112 @@
 end
 
 class TestMeta < MiniTest::Unit::TestCase
-  def test_structure
+  def test_setup
+    srand 42
+    MiniTest::Unit::TestCase.reset
+  end
+
+  def util_structure
     x = y = z = nil
+    before_list = []
+    after_list  = []
     x = describe "top-level thingy" do
-      before {}
-      after  {}
+      before { before_list << 1 }
+      after  { after_list  << 1 }
 
       it "top-level-it" do end
 
       y = describe "inner thingy" do
-        before {}
+        before { before_list << 2 }
+        after  { after_list  << 2 }
         it "inner-it" do end
 
         z = describe "very inner thingy" do
-          before {}
+          before { before_list << 3 }
+          after  { after_list  << 3 }
           it "inner-it" do end
         end
       end
     end
 
+    return x, y, z, before_list, after_list
+  end
+
+  def test_structure
+    x, y, z, * = util_structure
+
     assert_equal "top-level thingy", x.to_s
     assert_equal "top-level thingy::inner thingy", y.to_s
     assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
 
+    assert_equal "top-level thingy", x.desc
+    assert_equal "inner thingy", y.desc
+    assert_equal "very inner thingy", z.desc
+
     top_methods = %w(setup teardown test_0001_top_level_it)
-    inner_methods = %w(setup test_0001_inner_it)
+    inner_methods = %w(setup teardown test_0001_inner_it)
 
     assert_equal top_methods,   x.instance_methods(false).sort.map {|o| o.to_s }
     assert_equal inner_methods, y.instance_methods(false).sort.map {|o| o.to_s }
     assert_equal inner_methods, z.instance_methods(false).sort.map {|o| o.to_s }
   end
+
+  def test_setup_teardown_behavior
+    _, _, z, before_list, after_list = util_structure
+
+    tc = z.new(nil)
+    tc.setup
+    tc.teardown
+
+    assert_equal [1, 2, 3], before_list
+    assert_equal [3, 2, 1], after_list
+  end
+
+  def test_children
+    MiniTest::Spec.children.clear
+
+    x = y = z = nil
+    x = describe "top-level thingy" do
+      y = describe "first thingy" do end
+
+      it "top-level-it" do end
+
+      z = describe "second thingy" do end
+    end
+
+    assert_equal [x], MiniTest::Spec.children
+    assert_equal [y, z], x.children
+    assert_equal [], y.children
+    assert_equal [], z.children
+  end
+
+  def test_describe_first_structure
+    x = y = z = nil
+    x = describe "top-level thingy" do
+      y = describe "first thingy" do end
+
+      it "top-level-it" do end
+
+      z = describe "second thingy" do end
+    end
+
+    assert_equal ['test_0001_top_level_it'],
+      x.instance_methods.grep(/^test/).map {|o| o.to_s}
+    assert_equal [], y.instance_methods.grep(/^test/)
+    assert_equal [], z.instance_methods.grep(/^test/)
+  end
+
+  def test_structure_subclasses
+    z = nil
+    x = Class.new MiniTest::Spec do
+      def xyz; end
+    end
+    y = Class.new x do
+      z = describe("inner") {}
+    end
+
+    assert_respond_to x.new(nil), "xyz"
+    assert_respond_to y.new(nil), "xyz"
+    assert_respond_to z.new(nil), "xyz"
+  end
 end
Index: test/minitest/test_minitest_unit.rb
===================================================================
--- test/minitest/test_minitest_unit.rb	(revision 31886)
+++ test/minitest/test_minitest_unit.rb	(revision 31887)
@@ -51,6 +51,7 @@
 
   def teardown
     MiniTest::Unit.output = $stdout
+    # HACK for ruby-trunk: MiniTest::Unit.runner = nil
     Object.send :remove_const, :ATestCase if defined? ATestCase
   end
 
@@ -254,7 +255,7 @@
     assert_report expected
   end
 
-  def test_run_failing # TODO: add error test
+  def test_run_failing
     tc = Class.new(MiniTest::Unit::TestCase) do
       def test_something
         assert true
@@ -351,6 +352,36 @@
 
 Finished tests in 0.00
 
+2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
+"
+    assert_report expected
+  end
+
+  def test_run_skip_verbose
+    tc = Class.new(MiniTest::Unit::TestCase) do
+      def test_something
+        assert true
+      end
+
+      def test_skip
+        skip "not yet"
+      end
+    end
+
+    Object.const_set(:ATestCase, tc)
+
+    @tu.run %w[--seed 42 --verbose]
+
+    expected = "Run options: --seed 42 --verbose
+
+# Running tests:
+
+ATestCase#test_skip = 0.00 s = S
+ATestCase#test_something = 0.00 s = .
+
+
+Finished tests in 0.00
+
   1) Skipped:
 test_skip(ATestCase) [FILE:LINE]:
 not yet
@@ -360,6 +391,60 @@
     assert_report expected
   end
 
+  def test_default_runner_is_minitest_unit
+    skip "ruby-trunk won't run with runner code :("
+
+    assert_instance_of MiniTest::Unit, MiniTest::Unit.runner
+  end
+
+  def test_run_with_other_runner
+    skip "ruby-trunk won't run with runner code :("
+
+    runner = Class.new(MiniTest::Unit) do
+      # Run once before each suite
+      def _run_suite(suite, type)
+        begin
+          suite.before_suite
+          super(suite, type)
+        end
+      end
+    end
+
+    tc = Class.new(MiniTest::Unit::TestCase) do
+
+      def self.before_suite
+        MiniTest::Unit.output.puts "Running #{self.name} tests"
+        @@foo = 1
+      end
+
+      def test_something
+        assert_equal 1, @@foo
+      end
+
+      def test_something_else
+        assert_equal 1, @@foo
+      end
+    end
+
+    Object.const_set(:ATestCase, tc)
+    MiniTest::Unit.runner = runner.new
+    @tu.run %w[--seed 42]
+
+    # We should only see 'running ATestCase tests' once
+    expected = "Run options: --seed 42
+
+# Running tests:
+
+Running ATestCase tests
+..
+
+Finished tests in 0.00
+
+2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
+"
+    assert_report expected
+  end
+
   def util_expand_bt bt
     if RUBY_VERSION =~ /^1\.9/ then
       bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
@@ -434,12 +519,115 @@
     @tc.assert_equal 1, 1
   end
 
-  def test_assert_equal_different
-    util_assert_triggered "Expected 1, not 2." do
+  def test_assert_equal_different_diff_deactivated
+    without_diff do
+      util_assert_triggered util_msg("haha" * 10, "blah" * 10) do
+        o1 = "haha" * 10
+        o2 = "blah" * 10
+
+        @tc.assert_equal o1, o2
+      end
+    end
+  end
+
+  def test_assert_equal_different_hex
+    c = Class.new do
+      def initialize s; @name = s; end
+    end
+
+    o1 = c.new "a"
+    o2 = c.new "b"
+    msg = "--- expected
+           +++ actual
+           @@ -1 +1 @@
+           -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
+           +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
+           .".gsub(/^ +/, "")
+
+    util_assert_triggered msg do
+      @tc.assert_equal o1, o2
+    end
+  end
+
+  def test_assert_equal_different_hex_invisible
+    o1 = Object.new
+    o2 = Object.new
+
+    msg = "No visible difference.
+           You should look at your implementation of Object#==.
+           #<Object:0xXXXXXX>.".gsub(/^ +/, "")
+
+    util_assert_triggered msg do
+      @tc.assert_equal o1, o2
+    end
+  end
+
+  def test_assert_equal_different_long
+    msg = "--- expected
+           +++ actual
+           @@ -1 +1 @@
+           -\"hahahahahahahahahahahahahahahahahahahaha\"
+           +\"blahblahblahblahblahblahblahblahblahblah\"
+           .".gsub(/^ +/, "")
+
+    util_assert_triggered msg do
+      o1 = "haha" * 10
+      o2 = "blah" * 10
+
+      @tc.assert_equal o1, o2
+    end
+  end
+
+  def test_assert_equal_different_long_invisible
+    msg = "No visible difference.
+           You should look at your implementation of String#==.
+           \"blahblahblahblahblahblahblahblahblahblah\".".gsub(/^ +/, "")
+
+    util_assert_triggered msg do
+      o1 = "blah" * 10
+      o2 = "blah" * 10
+      def o1.== o
+        false
+      end
+      @tc.assert_equal o1, o2
+    end
+  end
+
+  def test_assert_equal_different_long_msg
+    msg = "message.
+           --- expected
+           +++ actual
+           @@ -1 +1 @@
+           -\"hahahahahahahahahahahahahahahahahahahaha\"
+           +\"blahblahblahblahblahblahblahblahblahblah\"
+           .".gsub(/^ +/, "")
+
+    util_assert_triggered msg do
+      o1 = "haha" * 10
+      o2 = "blah" * 10
+      @tc.assert_equal o1, o2, "message"
+    end
+  end
+
+  def test_assert_equal_different_short
+    util_assert_triggered util_msg(1, 2) do
       @tc.assert_equal 1, 2
     end
   end
 
+  def test_assert_equal_different_short_msg
+    util_assert_triggered util_msg(1, 2, "message") do
+      @tc.assert_equal 1, 2, "message"
+    end
+  end
+
+  def test_assert_equal_different_short_multiline
+    msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n."
+    util_assert_triggered msg do
+      @tc.assert_equal "a\nb", "a\nc"
+    end
+  end
+
   def test_assert_in_delta
     @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
   end
@@ -590,7 +778,7 @@
   end
 
   def test_assert_output_triggered_both
-    util_assert_triggered "In stdout.\nExpected \"yay\", not \"boo\"." do
+    util_assert_triggered util_msg("yay", "boo", "In stdout") do
       @tc.assert_output "yay", "blah" do
         print "boo"
         $stderr.print "blah blah"
@@ -599,7 +787,7 @@
   end
 
   def test_assert_output_triggered_err
-    util_assert_triggered "In stderr.\nExpected \"blah\", not \"blah blah\"." do
+    util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
       @tc.assert_output nil, "blah" do
         $stderr.print "blah blah"
       end
@@ -607,7 +795,7 @@
   end
 
   def test_assert_output_triggered_out
-    util_assert_triggered "In stdout.\nExpected \"blah\", not \"blah blah\"." do
+    util_assert_triggered util_msg("blah", "blah blah", "In stdout") do
       @tc.assert_output "blah" do
         print "blah blah"
       end
@@ -786,7 +974,7 @@
   def test_assert_silent_triggered_err
     @assertion_count = 2
 
-    util_assert_triggered "In stderr.\nExpected \"\", not \"blah blah\"." do
+    util_assert_triggered util_msg("", "blah blah", "In stderr") do
       @tc.assert_silent do
         $stderr.print "blah blah"
       end
@@ -794,7 +982,7 @@
   end
 
   def test_assert_silent_triggered_out
-    util_assert_triggered "In stdout.\nExpected \"\", not \"blah blah\"." do
+    util_assert_triggered util_msg("", "blah blah", "In stdout") do
       @tc.assert_silent do
         print "blah blah"
       end
@@ -889,8 +1077,9 @@
   def test_message
     @assertion_count = 0
 
-    assert_equal "blah2.",         @tc.message { "blah2" }.call
-    assert_equal "blah2.",         @tc.message("") { "blah2" }.call
+    assert_equal "blah2.",         @tc.message          { "blah2" }.call
+    assert_equal "blah2.",         @tc.message("")      { "blah2" }.call
+    assert_equal "blah1.\nblah2.", @tc.message(:blah1)  { "blah2" }.call
     assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
   end
 
@@ -1101,4 +1290,19 @@
 
     assert_equal expected, msg
   end
+
+  def util_msg exp, act, msg = nil
+    s = "Expected: #{exp.inspect}\n  Actual: #{act.inspect}."
+    s = "#{msg}.\n#{s}" if msg
+    s
+  end
+
+  def without_diff
+    old_diff = MiniTest::Assertions.diff
+    MiniTest::Assertions.diff = nil
+
+    yield
+  ensure
+    MiniTest::Assertions.diff = old_diff
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]