Skip to content

Commit

Permalink
+ Refactored and pulled Assertions#things_to_diff out of #diff. (Burd…
Browse files Browse the repository at this point in the history
…etteLamar)

[git-p4: depot-paths = "//src/minitest/dev/": change = 12351]
  • Loading branch information
zenspider committed Oct 20, 2019
1 parent 9d8064c commit e761784
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions lib/minitest/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,16 @@ def self.diff= o
# 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.
#
# See +things_to_diff+ for more info.

def diff exp, act
expect = mu_pp_for_diff exp
butwas = mu_pp_for_diff act
result = nil

e1, e2 = expect.include?("\n"), expect.include?("\\n")
b1, b2 = butwas.include?("\n"), butwas.include?("\\n")

need_to_diff =
(e1 ^ e2 ||
b1 ^ b2 ||
expect.size > 30 ||
butwas.size > 30 ||
expect == butwas) &&
Minitest::Assertions.diff
expect, butwas = things_to_diff(exp, act)

return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
need_to_diff
expect

Tempfile.open("expect") do |a|
a.puts expect
Expand Down Expand Up @@ -102,6 +93,34 @@ def diff exp, act
result
end

##
# Returns things to diff [expect, butwas], or [nil, nil] if nothing to diff.
#
# Criterion:
#
# 1. Strings include newlines or escaped newlines, but not both.
# 2. or: String lengths are > 30 characters.
# 3. or: Strings are equal to each other (but maybe different encodings?).
# 4. and: we found a diff executable.

def things_to_diff exp, act
expect = mu_pp_for_diff exp
butwas = mu_pp_for_diff act

e1, e2 = expect.include?("\n"), expect.include?("\\n")
b1, b2 = butwas.include?("\n"), butwas.include?("\\n")

need_to_diff =
(e1 ^ e2 ||
b1 ^ b2 ||
expect.size > 30 ||
butwas.size > 30 ||
expect == butwas) &&
Minitest::Assertions.diff

need_to_diff && [expect, butwas]
end

##
# This returns a human-readable version of +obj+. By default
# #inspect is called. You can override this to use #pretty_inspect
Expand Down

0 comments on commit e761784

Please sign in to comment.