parent
3a27acc3c3
commit
d3450058a0
@ -0,0 +1,3 @@ |
||||
This line is the same. |
||||
This line is different in 1.txt |
||||
This line is the same. |
||||
@ -0,0 +1,3 @@ |
||||
This line is the same. |
||||
This line is different in 2.txt |
||||
This line is the same. |
||||
@ -0,0 +1,39 @@ |
||||
#!/usr/bin/php |
||||
<?php |
||||
/** |
||||
* Text_Diff example script. |
||||
* |
||||
* Take two files from the command line args and produce a unified |
||||
* diff of them. |
||||
* |
||||
* @package Text_Diff |
||||
*/ |
||||
|
||||
require_once 'Text/Diff.php'; |
||||
require_once 'Text/Diff/Renderer.php'; |
||||
require_once 'Text/Diff/Renderer/unified.php'; |
||||
|
||||
/* Make sure we have enough arguments. */ |
||||
if (count($argv) < 3) { |
||||
echo "Usage: diff.php <file1> <file2>\n\n"; |
||||
exit; |
||||
} |
||||
|
||||
/* Make sure both files exist. */ |
||||
if (!is_readable($argv[1])) { |
||||
echo "$argv[1] not found or not readable.\n\n"; |
||||
} |
||||
if (!is_readable($argv[2])) { |
||||
echo "$argv[2] not found or not readable.\n\n"; |
||||
} |
||||
|
||||
/* Load the lines of each file. */ |
||||
$lines1 = file($argv[1]); |
||||
$lines2 = file($argv[2]); |
||||
|
||||
/* Create the Diff object. */ |
||||
$diff = new Text_Diff('auto', array($lines1, $lines2)); |
||||
|
||||
/* Output the diff in unified format. */ |
||||
$renderer = new Text_Diff_Renderer_unified(); |
||||
echo $renderer->render($diff); |
||||
@ -0,0 +1,3 @@ |
||||
This line is the same. |
||||
This line is different in 1.txt |
||||
This line is the same. |
||||
@ -0,0 +1,3 @@ |
||||
This line is the same. |
||||
This line is different in 2.txt |
||||
This line is the same. |
||||
@ -0,0 +1,4 @@ |
||||
This line is the same. |
||||
This line is different in 1.txt |
||||
This line is the same. |
||||
This line is new in 3.txt |
||||
@ -0,0 +1,3 @@ |
||||
This line is the same. |
||||
This line is different in 4.txt |
||||
This line is the same. |
||||
@ -0,0 +1,5 @@ |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
Various Content. |
||||
More Content. |
||||
Testing diff and renderer. |
||||
@ -0,0 +1,7 @@ |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
Inserting a line. |
||||
Various Content. |
||||
Replacing content. |
||||
Testing similarities and renderer. |
||||
Append content. |
||||
@ -0,0 +1,11 @@ |
||||
*** 1.txt 2005-03-21 13:37:59.000000000 +0100
|
||||
--- 2.txt 2005-03-21 13:38:00.000000000 +0100
|
||||
***************
|
||||
*** 1,3 ****
|
||||
This line is the same.
|
||||
! This line is different in 1.txt
|
||||
This line is the same.
|
||||
--- 1,3 ----
|
||||
This line is the same.
|
||||
! This line is different in 2.txt
|
||||
This line is the same.
|
||||
@ -0,0 +1,25 @@ |
||||
--TEST-- |
||||
Text_Diff: Context renderer |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/context.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/1.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/2.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_context(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
*************** |
||||
*** 1,3 **** |
||||
This line is the same. |
||||
! This line is different in 1.txt |
||||
This line is the same. |
||||
--- 1,3 ---- |
||||
This line is the same. |
||||
! This line is different in 2.txt |
||||
This line is the same. |
||||
@ -0,0 +1,31 @@ |
||||
--TEST-- |
||||
Text_Diff: Context renderer 2 |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/context.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/5.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/6.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_context(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
*************** |
||||
*** 1,5 **** |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
Various Content. |
||||
! More Content. |
||||
! Testing diff and renderer. |
||||
--- 1,7 ---- |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
+ Inserting a line. |
||||
Various Content. |
||||
! Replacing content. |
||||
! Testing similarities and renderer. |
||||
! Append content. |
||||
@ -0,0 +1,62 @@ |
||||
--TEST-- |
||||
Text_Diff: Basic diff operation |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/1.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/2.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
echo strtolower(print_r($diff, true)); |
||||
?> |
||||
--EXPECT-- |
||||
text_diff object |
||||
( |
||||
[_edits] => array |
||||
( |
||||
[0] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
[1] => text_diff_op_change object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is different in 1.txt |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is different in 2.txt |
||||
) |
||||
|
||||
) |
||||
|
||||
[2] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
@ -0,0 +1,19 @@ |
||||
--TEST-- |
||||
Text_Diff: Inline renderer |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/inline.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/1.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/2.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_inline(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
This line is the same. |
||||
This line is different in <del>1.txt</del><ins>2.txt</ins> |
||||
This line is the same. |
||||
@ -0,0 +1,37 @@ |
||||
--TEST-- |
||||
Text_Diff: Inline renderer 2 |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/inline.php'; |
||||
|
||||
$lines1 = array( |
||||
"This is a test.\n", |
||||
"Adding random text to simulate files.\n", |
||||
"Various Content.\n", |
||||
"More Content.\n", |
||||
"Testing diff and renderer.\n" |
||||
); |
||||
$lines2 = array( |
||||
"This is a test.\n", |
||||
"Adding random text to simulate files.\n", |
||||
"Inserting a line.\n", |
||||
"Various Content.\n", |
||||
"Replacing content.\n", |
||||
"Testing similarities and renderer.\n", |
||||
"Append content.\n" |
||||
); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_inline(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
<ins>Inserting a line.</ins> |
||||
Various Content. |
||||
<del>More Content.</del><ins>Replacing content.</ins> |
||||
Testing <del>diff</del><ins>similarities</ins> and renderer.<ins> |
||||
Append content.</ins> |
||||
@ -0,0 +1,30 @@ |
||||
--TEST-- |
||||
Text_Diff: PEAR Bug #12740 (failed assertion) |
||||
--FILE-- |
||||
<?php |
||||
|
||||
require dirname(__FILE__) . '/../Diff.php'; |
||||
require dirname(__FILE__) . '/../Diff/Renderer/inline.php'; |
||||
|
||||
$a = <<<QQ |
||||
<li>The tax credit amounts to 30% of the cost of the system, with a |
||||
maximum of 2,000. This credit is separate from the 500 home improvement |
||||
credit.</li> |
||||
<h3>Fuel Cells<a |
||||
href="12341234213421341234123412341234123421341234213412342134213423" |
||||
class="anchor" title="Link to this section"><br /> |
||||
<li>Your fuel 123456789</li> |
||||
QQ; |
||||
|
||||
$b = <<<QQ |
||||
<li> of gas emissions by 2050</li> |
||||
<li>Raise car fuel economy to 50 mpg by 2017</li> |
||||
<li>Increase access to mass transit systems</li> |
||||
QQ; |
||||
|
||||
$diff = new Text_Diff(explode("\n", $b), explode("\n", $a)); |
||||
$renderer = new Text_Diff_Renderer_inline(); |
||||
$renderer->render($diff); |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
@ -0,0 +1,30 @@ |
||||
--TEST-- |
||||
Text_Diff: PEAR Bug #4879 (inline renderer hangs on numbers in input string) |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/inline.php'; |
||||
|
||||
$oldtext = <<<EOT |
||||
Common text |
||||
Bob had 1 apple, Alice had 2. |
||||
Bon appetit! |
||||
EOT; |
||||
|
||||
$newtext = <<< EOT |
||||
Common text |
||||
Bob had 10 apples, Alice had 1. |
||||
Bon appetit! |
||||
EOT; |
||||
|
||||
$oldpieces = explode ("\n", $oldtext); |
||||
$newpieces = explode ("\n", $newtext); |
||||
$diff = &new Text_Diff ($oldpieces, $newpieces); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_inline(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
Common text |
||||
Bob had <del>1 apple,</del><ins>10 apples,</ins> Alice had <del>2.</del><ins>1.</ins> |
||||
Bon appetit! |
||||
@ -0,0 +1,45 @@ |
||||
--TEST-- |
||||
Text_Diff: PEAR Bug #6251 (too much trailing context) |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/unified.php'; |
||||
|
||||
$oldtext = <<<EOT |
||||
|
||||
Original Text |
||||
|
||||
|
||||
|
||||
ss |
||||
ttt |
||||
EOT; |
||||
|
||||
$newtext = <<< EOT |
||||
|
||||
Modified Text |
||||
|
||||
|
||||
|
||||
ss |
||||
ttt |
||||
EOT; |
||||
|
||||
$oldpieces = explode ("\n", $oldtext); |
||||
$newpieces = explode ("\n", $newtext); |
||||
$diff = &new Text_Diff ($oldpieces, $newpieces); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_unified(array('leading_context_lines' => 3, 'trailing_context_lines' => 3)); |
||||
|
||||
// We need to use var_dump, as the test runner strips trailing empty lines. |
||||
var_dump($renderer->render($diff)); |
||||
?> |
||||
--EXPECT-- |
||||
string(54) "@@ -1,5 +1,5 @@ |
||||
|
||||
-Original Text |
||||
+Modified Text |
||||
|
||||
|
||||
|
||||
" |
||||
@ -0,0 +1,18 @@ |
||||
--TEST-- |
||||
Text_Diff: PEAR Bug #6428 (problem with single digits after space) |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/inline.php'; |
||||
|
||||
$from = array('Line 1', 'Another line'); |
||||
$to = array('Line 1', 'Another line'); |
||||
|
||||
$diff = &new Text_Diff ($from, $to); |
||||
$renderer = &new Text_Diff_Renderer_inline(); |
||||
|
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
Line <del>1</del><ins> 1</ins> |
||||
Another line |
||||
@ -0,0 +1,145 @@ |
||||
--TEST-- |
||||
Text_Diff: Text_Diff_Engine_string test. |
||||
--FILE-- |
||||
<?php |
||||
|
||||
require_once 'PEAR.php'; |
||||
require_once 'Text/Diff.php'; |
||||
|
||||
$unified = file_get_contents(dirname(__FILE__) . '/unified.patch'); |
||||
$diff_u = new Text_Diff('string', array($unified)); |
||||
echo strtolower(print_r($diff_u, true)); |
||||
|
||||
$unified2 = file_get_contents(dirname(__FILE__) . '/unified2.patch'); |
||||
$diff_u2 = new Text_Diff('string', array($unified2)); |
||||
var_export(is_a($diff_u2->getDiff(), 'PEAR_Error')); |
||||
echo "\n"; |
||||
$diff_u2 = new Text_Diff('string', array($unified2, 'unified')); |
||||
echo strtolower(print_r($diff_u2, true)); |
||||
|
||||
$context = file_get_contents(dirname(__FILE__) . '/context.patch'); |
||||
$diff_c = new Text_Diff('string', array($context)); |
||||
echo strtolower(print_r($diff_c, true)); |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
text_diff object |
||||
( |
||||
[_edits] => array |
||||
( |
||||
[0] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
[1] => text_diff_op_change object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is different in 1.txt |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is different in 2.txt |
||||
) |
||||
|
||||
) |
||||
|
||||
[2] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
true |
||||
text_diff object |
||||
( |
||||
[_edits] => array |
||||
( |
||||
[0] => text_diff_op_change object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => for the first time in u.s. history number of private contractors and troops are equal |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => number of private contractors and troops are equal for first time in u.s. history |
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
text_diff object |
||||
( |
||||
[_edits] => array |
||||
( |
||||
[0] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
[1] => text_diff_op_change object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is different in 1.txt |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is different in 2.txt |
||||
) |
||||
|
||||
) |
||||
|
||||
[2] => text_diff_op_copy object |
||||
( |
||||
[orig] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
[final] => array |
||||
( |
||||
[0] => this line is the same. |
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
|
||||
) |
||||
@ -0,0 +1,7 @@ |
||||
--- 1.txt 2005-03-21 13:37:59.000000000 +0100
|
||||
+++ 2.txt 2005-03-21 13:38:00.000000000 +0100
|
||||
@@ -1,3 +1,3 @@
|
||||
This line is the same.
|
||||
-This line is different in 1.txt
|
||||
+This line is different in 2.txt
|
||||
This line is the same.
|
||||
@ -0,0 +1,21 @@ |
||||
--TEST-- |
||||
Text_Diff: Unified renderer |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/unified.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/1.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/2.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_unified(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
@@ -1,3 +1,3 @@ |
||||
This line is the same. |
||||
-This line is different in 1.txt |
||||
+This line is different in 2.txt |
||||
This line is the same. |
||||
@ -0,0 +1,26 @@ |
||||
--TEST-- |
||||
Text_Diff: Unified renderer 2 |
||||
--FILE-- |
||||
<?php |
||||
include_once 'Text/Diff.php'; |
||||
include_once 'Text/Diff/Renderer/unified.php'; |
||||
|
||||
$lines1 = file(dirname(__FILE__) . '/5.txt'); |
||||
$lines2 = file(dirname(__FILE__) . '/6.txt'); |
||||
|
||||
$diff = &new Text_Diff($lines1, $lines2); |
||||
|
||||
$renderer = &new Text_Diff_Renderer_unified(); |
||||
echo $renderer->render($diff); |
||||
?> |
||||
--EXPECT-- |
||||
@@ -1,5 +1,7 @@ |
||||
This is a test. |
||||
Adding random text to simulate files. |
||||
+Inserting a line. |
||||
Various Content. |
||||
-More Content. |
||||
-Testing diff and renderer. |
||||
+Replacing content. |
||||
+Testing similarities and renderer. |
||||
+Append content. |
||||
Loading…
Reference in new issue