Skip to content

Commit

Permalink
+ Extended Assertions#mu_pp to encoding validity output for strings t…
Browse files Browse the repository at this point in the history
…o improve diffs.

+ Extended Assertions#mu_pp to output encoding and validity if invalid to improve diffs.
+ Extended Assertions#mu_pp_for_diff to make escaped newlines more obvious in diffs.

[git-p4: depot-paths = "//src/minitest/dev/": change = 11903]
  • Loading branch information
zenspider committed Mar 28, 2019
1 parent e6bc448 commit 1f2b132
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
20 changes: 14 additions & 6 deletions lib/minitest/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,39 @@ def diff exp, act

##
# This returns a human-readable version of +obj+. By default
# #inspect is called. You can override this to use #pretty_print
# #inspect is called. You can override this to use #pretty_inspect
# if you want.
#
# See Minitest::Test.make_my_diffs_pretty!

def mu_pp obj
s = obj.inspect

if defined? Encoding then
s = s.encode Encoding.default_external

if String === obj && obj.encoding != Encoding.default_external then
s = "# encoding: #{obj.encoding}\n#{s}"
if String === obj && (obj.encoding != Encoding.default_external ||
!obj.valid_encoding?) then
enc = "# encoding: #{obj.encoding}"
val = "# valid: #{obj.valid_encoding?}"
s = "#{enc}\n#{val}\n#{s}"
end
end

s
end

##
# This returns a diff-able human-readable version of +obj+. This
# differs from the regular mu_pp because it expands escaped
# This returns a diff-able more 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
mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
mu_pp(obj).
gsub(/\\n/, "\n"). # escaped newlines -> newlines
gsub(/\\\n/, "\\n\n"). # escaped slash+newline -> escaped newline+newline
gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX") # anonymize hex values
end

##
Expand Down
63 changes: 61 additions & 2 deletions test/minitest/test_minitest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,11 @@ def test_assert_equal_string_encodings
msg = <<-EOM.gsub(/^ {10}/, "")
--- expected
+++ actual
@@ -1 +1,2 @@
@@ -1,3 +1,3 @@
-# encoding: UTF-8
-# valid: false
+# encoding: ASCII-8BIT
+# valid: true
"bad-utf8-\\xF1.txt"
EOM

Expand All @@ -931,9 +934,11 @@ def test_assert_equal_string_encodings_both_different
msg = <<-EOM.gsub(/^ {10}/, "")
--- expected
+++ actual
@@ -1,2 +1,2 @@
@@ -1,3 +1,3 @@
-# encoding: US-ASCII
-# valid: false
+# encoding: ASCII-8BIT
+# valid: true
"bad-utf8-\\xF1.txt"
EOM

Expand Down Expand Up @@ -1055,6 +1060,60 @@ def test_assert_equal_different_short_multiline
end
end

def test_assert_equal_unescape_newlines
msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
--- expected
+++ actual
@@ -1,2 +1,2 @@
-"hello
+"hello\n
world"
EOM

assert_triggered msg do
act = 'hello\nworld'
exp = "hello\nworld"

@tc.assert_equal exp, act
end
end

def assert_mu_pp exp, input
@tc.assert_equal exp, mu_pp(input)
end

def test_mu_pp
@assertion_count += 4

assert_mu_pp 42.inspect, 42
assert_mu_pp %w[a b c].inspect, %w[a b c]
assert_mu_pp "\"hello world\"", "hello world"
assert_mu_pp "\"hello\\nworld\"", "hello\nworld"
assert_mu_pp "\"hello\\\\nworld\"", 'hello\nworld' # notice single quotes
end

def assert_mu_pp_for_diff exp, input
@tc.assert_equal exp, mu_pp_for_diff(input)
end

def test_mu_pp_for_diff
@assertion_count += 3

assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
assert_mu_pp_for_diff "\"hello world\"", "hello world"
assert_mu_pp_for_diff "\"hello\nworld\"", "hello\nworld"

exp = "# encoding: ASCII-8BIT\n# valid: true\n\"hello\nworld\""
assert_mu_pp_for_diff exp, "hello\nworld".b
end

def test_mu_pp_for_diff_single_quotes_counter
@assertion_count += 1

assert_mu_pp_for_diff "\"hello\\n\nworld\"", 'hello\nworld'
assert_mu_pp_for_diff "\"hello\nworld\"", "hello\nworld"
end

def test_assert_equal_does_not_allow_lhs_nil
if Minitest::VERSION =~ /^6/ then
warn "Time to strip the MT5 test"
Expand Down

0 comments on commit 1f2b132

Please sign in to comment.