Permalink
Browse files

Merge pull request #493 from pscheit/3.6

  • Loading branch information...
2 parents 6764f04 + cc354b0 commit 5d0ff52bdff9afd479e38fdc880adce453ce88e5 @sebastianbergmann committed Feb 9, 2012
Showing with 208 additions and 61 deletions.
  1. +83 −61 PHPUnit/Util/Diff.php
  2. +125 −0 Tests/Util/DiffTest.php
View
@@ -60,14 +60,93 @@
class PHPUnit_Util_Diff
{
/**
- * Returns the diff between two arrays or strings.
+ * Returns the diff between two arrays or strings as string.
*
* @param array|string $from
* @param array|string $to
* @return string
*/
public static function diff($from, $to)
{
+ $buffer= "--- Expected\n+++ Actual\n";
+ $diff = self::diffToArray($from,$to);
+
+ $inOld = FALSE;
+ $i = 0;
+ $old = array();
+
+ foreach ($diff as $line) {
+ if ($line[1] === 0 /* OLD */) {
+ if ($inOld === FALSE) {
+ $inOld = $i;
+ }
+ }
+
+ else if ($inOld !== FALSE) {
+ if (($i - $inOld) > 5) {
+ $old[$inOld] = $i - 1;
+ }
+
+ $inOld = FALSE;
+ }
+
+ ++$i;
+ }
+
+ $start = isset($old[0]) ? $old[0] : 0;
+ $end = count($diff);
+ $i = 0;
+
+ if ($tmp = array_search($end, $old)) {
+ $end = $tmp;
+ }
+
+ $newChunk = TRUE;
+
+ for ($i = $start; $i < $end; $i++) {
+ if (isset($old[$i])) {
+ $buffer .= "\n";
+ $newChunk = TRUE;
+ $i = $old[$i];
+ }
+
+ if ($newChunk) {
+ $buffer .= "@@ @@\n";
+ $newChunk = FALSE;
+ }
+
+ if ($diff[$i][1] === 1 /* ADDED */) {
+ $buffer .= '+' . $diff[$i][0] . "\n";
+ }
+
+ else if ($diff[$i][1] === 2 /* REMOVED */) {
+ $buffer .= '-' . $diff[$i][0] . "\n";
+ }
+
+ else {
+ $buffer .= ' ' . $diff[$i][0] . "\n";
+ }
+ }
+
+ return $buffer;
+ }
+
+ /**
+ * Returns the diff between two arrays or strings as array.
+ *
+ * every array-entry containts two elements:
+ * - [0] => string $token
+ * - [1] => 2|1|0
+ *
+ * - 2: REMOVED: $token was removed from $from
+ * - 1: ADDED: $token was added to $from
+ * - 0: OLD: $token is not changed in $to
+ *
+ * @param array|string $from
+ * @param array|string $to
+ * @return array
+ */
+ public static function diffToArray($from, $to) {
if (is_string($from)) {
$from = preg_split('(\r\n|\r|\n)', $from);
}
@@ -76,7 +155,7 @@ public static function diff($from, $to)
$to = preg_split('(\r\n|\r|\n)', $to);
}
- $buffer = "--- Expected\n+++ Actual\n";
+
$start = array();
$end = array();
$fromLength = count($from);
@@ -143,65 +222,8 @@ public static function diff($from, $to)
foreach ($end as $token) {
$diff[] = array($token, 0 /* OLD */);
}
-
- $inOld = FALSE;
- $i = 0;
- $old = array();
-
- foreach ($diff as $line) {
- if ($line[1] === 0 /* OLD */) {
- if ($inOld === FALSE) {
- $inOld = $i;
- }
- }
-
- else if ($inOld !== FALSE) {
- if (($i - $inOld) > 5) {
- $old[$inOld] = $i - 1;
- }
-
- $inOld = FALSE;
- }
-
- ++$i;
- }
-
- $start = isset($old[0]) ? $old[0] : 0;
- $end = count($diff);
- $i = 0;
-
- if ($tmp = array_search($end, $old)) {
- $end = $tmp;
- }
-
- $newChunk = TRUE;
-
- for ($i = $start; $i < $end; $i++) {
- if (isset($old[$i])) {
- $buffer .= "\n";
- $newChunk = TRUE;
- $i = $old[$i];
- }
-
- if ($newChunk) {
- $buffer .= "@@ @@\n";
- $newChunk = FALSE;
- }
-
- if ($diff[$i][1] === 1 /* ADDED */) {
- $buffer .= '+' . $diff[$i][0] . "\n";
- }
-
- else if ($diff[$i][1] === 2 /* REMOVED */) {
- $buffer .= '-' . $diff[$i][0] . "\n";
- }
-
- else {
- $buffer .= ' ' . $diff[$i][0] . "\n";
- }
- }
-
- return $buffer;
+
+ return $diff;
}
/**
@@ -55,6 +55,11 @@
*/
class Util_DiffTest extends PHPUnit_Framework_TestCase
{
+
+ const REMOVED = 2;
+ const ADDED = 1;
+ const OLD = 0;
+
/**
* @covers PHPUnit_Util_Diff::diff
*/
@@ -67,6 +72,21 @@ public function testComparisonErrorMessage()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorMessage_toArray()
+ {
+ $diff = array();
+ $diff[] = array('a', self::REMOVED);
+ $diff[] = array('b', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('a', 'b')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorStartSame()
@@ -78,6 +98,21 @@ public function testComparisonErrorStartSame()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorStartSame_toArray()
+ {
+ $diff = array();
+ $diff[] = array('ba', self::REMOVED);
+ $diff[] = array('bc', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('ba', 'bc')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorEndSame()
@@ -89,6 +124,21 @@ public function testComparisonErrorEndSame()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorEndSame_toArray()
+ {
+ $diff = array();
+ $diff[] = array('ab', self::REMOVED);
+ $diff[] = array('cb', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('ab', 'cb')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorStartAndEndSame()
@@ -100,6 +150,21 @@ public function testComparisonErrorStartAndEndSame()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorStartAndEndSame_toArray()
+ {
+ $diff = array();
+ $diff[] = array('abc', self::REMOVED);
+ $diff[] = array('adc', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('abc', 'adc')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorStartSameComplete()
@@ -111,6 +176,21 @@ public function testComparisonErrorStartSameComplete()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorStartSameComplete_toArray()
+ {
+ $diff = array();
+ $diff[] = array('ab', self::REMOVED);
+ $diff[] = array('abc', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('ab', 'abc')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorEndSameComplete()
@@ -122,6 +202,21 @@ public function testComparisonErrorEndSameComplete()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorEndSameComplete_toArray()
+ {
+ $diff = array();
+ $diff[] = array('bc', self::REMOVED);
+ $diff[] = array('abc', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('bc', 'abc')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorOverlapingMatches()
@@ -133,6 +228,21 @@ public function testComparisonErrorOverlapingMatches()
}
/**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorOverlapingMatches_toArray()
+ {
+ $diff = array();
+ $diff[] = array('abc', self::REMOVED);
+ $diff[] = array('abbc', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('abc', 'abbc')
+ );
+ }
+
+ /**
* @covers PHPUnit_Util_Diff::diff
*/
public function testComparisonErrorOverlapingMatches2()
@@ -142,4 +252,19 @@ public function testComparisonErrorOverlapingMatches2()
PHPUnit_Util_Diff::diff('abcdde', 'abcde')
);
}
+
+ /**
+ * @covers PHPUnit_Util_Diff::diffToArray
+ */
+ public function testComparisonErrorOverlapingMatches2_toArray()
+ {
+ $diff = array();
+ $diff[] = array('abcdde', self::REMOVED);
+ $diff[] = array('abcde', self::ADDED);
+
+ $this->assertEquals(
+ $diff,
+ PHPUnit_Util_Diff::diffToArray('abcdde', 'abcde')
+ );
+ }
}

0 comments on commit 5d0ff52

Please sign in to comment.