assertEquals() Line Ending Differences Are Obscure #503

Closed
ghost opened this Issue Feb 15, 2012 · 5 comments

Comments

Projects
None yet
4 participants
@ghost

ghost commented Feb 15, 2012

When using assertEquals("Windows EOL", "Unix EOL") with multi-line text, assertEquals() properly displays a failure, but does not show why in the difference report.

1) Namespace\ClassName::testCreateResponse
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 Parameter: parameter value
 Context: context value
 URL: http://localhost/test?key=value

/path/to/test/file.php:55
Collaborator

edorian commented Mar 24, 2012

The best I could come up with for this is:

diff --git a/PHPUnit/Util/Diff.php b/PHPUnit/Util/Diff.php
index f9040bf..df09fde 100644
--- a/PHPUnit/Util/Diff.php
+++ b/PHPUnit/Util/Diff.php
@@ -148,6 +148,9 @@ class PHPUnit_Util_Diff
      */
     public static function diffToArray($from, $to)
     {
+        preg_match_all('(\r\n|\r|\n)', $from, $fromMatches);
+        preg_match_all('(\r\n|\r|\n)', $to, $toMatches);
+
         if (is_string($from)) {
             $from = preg_split('(\r\n|\r|\n)', $from);
         }
@@ -189,6 +192,14 @@ class PHPUnit_Util_Diff
         $diff = array();
         $line = 0;

+        if(isset($fromMatches[0]) 
+            && $toMatches[0] 
+            && count($fromMatches[0]) === count($toMatches[0]) 
+            && $fromMatches[0] !== $toMatches[0]
+        ) {
+            $diff[] = array('#Warning: Strings contain different line endings!', 0);
+        }
+
         foreach ($start as $token) {
             $diff[] = array($token, 0 /* OLD */);
         }

It would produce:

1) Issue503Test::testCompareDifferentLineEnding
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
 #Warning: Strings contain different line endings!
 foo

Is that good enough for you @kherge?

Do you like it @sebastianbergmann ? I couldn't come up with nicer reporting without rewriting more than half of the diff algo (and possibly breaking lots of things).

This now only produces the Warning if the number of lines is the same so "Foo\n" vs "Bar\r\n\r\n\r\n" still gets reported properly without that warning.

AFAICS, this detects whether different line-endings are used and reports that fact in case the diff is empty. Looks good to me, then.

edorian closed this in 351eae1 Mar 26, 2012

@ghost

ghost commented Mar 26, 2012

Thank you @edorian!

@greglamb greglamb pushed a commit to greglamb/phpunit that referenced this issue Apr 19, 2013

@edorian edorian Closes #503: Improved the error message in cases where two equal stri…
…ngs with different line endings where compared
042fb6a
Contributor

keradus commented Oct 24, 2016 edited

For anyone who would have the issue in the future.
A little helper approach that will also display a debug statement showing the difference:
FriendsOfPHP/PHP-CS-Fixer@d60e963

even better: GeckoPackages/GeckoPHPUnit#18

output is not pretty, but at least sth is displayed so it's possible to find where the wrong character is.

Contributor

jmauerhan commented Oct 24, 2016

As a die-hard windows user, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment