Skip to content
Permalink
Browse files

Reworked the \n vs \\n mu_pp_for_diff situation.

Don't munge the output if there is both \n and \\n (nerd mode), but
also munge single vs double differently to improve readability in
different ways.

I think this actually works well. The last one? Not quite right.

Fixes #791.

[git-p4: depot-paths = "//src/minitest/dev/": change = 12259]
  • Loading branch information...
zenspider committed Sep 20, 2019
1 parent 083c91f commit b0e07f4dd05d5369b913aa72f8d02609790c090f
Showing with 56 additions and 36 deletions.
  1. +18 −11 lib/minitest/assertions.rb
  2. +38 −25 test/minitest/test_minitest_assertions.rb
@@ -61,12 +61,15 @@ def diff exp, act
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 =
(expect.include?("\n") ||
butwas.include?("\n") ||
expect.size > 30 ||
butwas.size > 30 ||
expect == butwas) &&
(e1 ^ e2 ||
b1 ^ b2 ||
expect.size > 30 ||
butwas.size > 30 ||
expect == butwas) &&
Minitest::Assertions.diff

return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
@@ -133,14 +136,18 @@ def mu_pp_for_diff obj
str = mu_pp obj

# both '\n' & '\\n' (_after_ mu_pp (aka inspect))
nerd = (str.index(/(?<=\\|^)\\n/) &&
str.index(/(?<!\\|^)\\n/))
single = str.match?(/(?<!\\|^)\\n/)
double = str.match?(/(?<=\\|^)\\n/)

process =
if nerd then # nerd view:
lambda { |s| "#{s}#{ "\n" if s == "\\n" }" } # keep escapes, add nl for "\n"
else # normal view:
lambda { |s| s == "\\n" ? "\n" : "\\n\n" } # de-escape a bit, add nls
if single ^ double then
if single then
lambda { |s| s == "\\n" ? "\n" : s } # unescape
else
lambda { |s| s == "\\\\n" ? "\\n\n" : s } # unescape a bit, add nls
end
else
:itself # leave it alone
end

str.
@@ -302,19 +302,34 @@ def test_assert_equal_does_not_allow_lhs_nil_triggered
end
end

def test_assert_equal_string_bug791
exp = <<-'EOF'.gsub(/^ {10}/, "") # note single quotes
--- expected
+++ actual
@@ -1,2 +1 @@
-"\\n
-"
+"\\\"
EOF

exp = "Expected: \"\\\\n\"\n Actual: \"\\\\\""
assert_triggered exp do
@tc.assert_equal "\\n", "\\"
end
end

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

assert_triggered msg do
exp = "A\\n\nB"
exp = "A\\nB"
act = "A\n\\nB"

@tc.assert_equal exp, act
@@ -1219,11 +1234,9 @@ def test_diff_str_mixed
msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
--- expected
+++ actual
@@ -1,2 +1,2 @@
-"A\\n\n
-B"
+"A\n
+\\nB"
@@ -1 +1 @@
-"A\\n\nB"
+"A\n\\nB"
EOM

exp = "A\\n\nB"
@@ -1309,7 +1322,7 @@ def test_mu_pp_for_diff_str_bad_encoding

def test_mu_pp_for_diff_str_bad_encoding_both
str = "\666A\\n\nB".force_encoding Encoding::UTF_8
exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\n\nB\""
exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""

assert_mu_pp_for_diff exp, str, :raw
end
@@ -1323,23 +1336,23 @@ def test_mu_pp_for_diff_str_encoding

def test_mu_pp_for_diff_str_encoding_both
str = "A\\n\nB".b
exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\n\nB\""
exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""

assert_mu_pp_for_diff exp, str, :raw
end

def test_mu_pp_for_diff_str_nerd
assert_mu_pp_for_diff "A\\n\nB\\\\nC", "A\nB\\nC"
assert_mu_pp_for_diff "\\n\nB\\\\nC", "\nB\\nC"
assert_mu_pp_for_diff "\\n\nB\\\\n", "\nB\\n"
assert_mu_pp_for_diff "\\n\n\\\\n", "\n\\n"
assert_mu_pp_for_diff "\\\\n\\n\n", "\\n\n"
assert_mu_pp_for_diff "\\\\nB\\n\n", "\\nB\n"
assert_mu_pp_for_diff "\\\\nB\\n\nC", "\\nB\nC"
assert_mu_pp_for_diff "A\\\\n\\n\nB", "A\\n\nB"
assert_mu_pp_for_diff "A\\n\n\\\\nB", "A\n\\nB"
assert_mu_pp_for_diff "\\\\n\\n\n", "\\n\n"
assert_mu_pp_for_diff "\\n\n\\\\n", "\n\\n"
assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
end

def test_mu_pp_for_diff_str_normal

0 comments on commit b0e07f4

Please sign in to comment.
You can’t perform that action at this time.