Created
August 31, 2014 19:30
-
-
Save tenderlove/4efa525ead553f907359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'allocation_tracer' | |
class MyTest < Minitest::Test | |
def setup | |
super | |
@a = Object.new | |
@b = Object.new | |
@start = -GC.stat(:total_allocated_object) | |
end | |
def teardown | |
p @start + GC.stat(:total_allocated_object) | |
super | |
end | |
def test_same_ok | |
assert_same @a, @a | |
end | |
def test_same_fail | |
#assert_same @a, @b | |
end | |
end | |
__END__ | |
Before patch: | |
[aaron@higgins minitest (master)]$ ruby -Ilib test.rb | |
Run options: --seed 55775 | |
# Running: | |
0 | |
.5 | |
. | |
Finished in 0.002264s, 883.3922 runs/s, 441.6961 assertions/s | |
After patch: | |
[aaron@higgins minitest (master)]$ ruby -Ilib test.rb | |
Run options: --seed 23530 | |
# Running: | |
2 | |
.0 | |
. | |
Finished in 0.002271s, 880.6693 runs/s, 440.3347 assertions/s. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/minitest/assertions.rb b/lib/minitest/assertions.rb | |
index 9864d38..c9de55a 100644 | |
--- a/lib/minitest/assertions.rb | |
+++ b/lib/minitest/assertions.rb | |
@@ -125,7 +125,7 @@ module Minitest | |
msg ||= "Failed assertion, no message given." | |
self.assertions += 1 | |
unless test then | |
- msg = msg.call if Proc === msg | |
+ msg = msg.call if msg.respond_to? :call | |
raise Minitest::Assertion, msg | |
end | |
true | |
@@ -325,14 +325,18 @@ module Minitest | |
assert obj.respond_to?(meth), msg | |
end | |
+ class Omg < Struct.new :act, :exp, :tc | |
+ def call | |
+ data = [tc.mu_pp(act), act.object_id, tc.mu_pp(exp), exp.object_id] | |
+ "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data | |
+ end | |
+ end | |
+ | |
## | |
# Fails unless +exp+ and +act+ are #equal? | |
def assert_same exp, act, msg = nil | |
- msg = message(msg) { | |
- data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id] | |
- "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data | |
- } | |
+ msg = message(msg, '.'.freeze, Omg.new(act, exp, self)) | |
assert exp.equal?(act), msg | |
end | |
@@ -479,15 +483,19 @@ module Minitest | |
assert false, msg | |
end | |
+ class Msg < Struct.new :msg, :ending, :callable | |
+ def call | |
+ msg = msg.call.chomp(".") if Proc === msg | |
+ custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty? | |
+ "#{custom_message}#{callable.call}#{ending}" | |
+ end | |
+ end | |
+ | |
## | |
# Returns a proc that will output +msg+ along with the default message. | |
- def message msg = nil, ending = ".", &default | |
- proc { | |
- msg = msg.call.chomp(".") if Proc === msg | |
- custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty? | |
- "#{custom_message}#{default.call}#{ending}" | |
- } | |
+ def message msg = nil, ending = '.'.freeze, callable | |
+ Msg.new msg, ending, callable | |
end | |
## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment