Skip to content

Commit

Permalink
Fix #50.
Browse files Browse the repository at this point in the history
First or last x lines of text are shown even when there's no difference
between the two compared texts.
  • Loading branch information
DigiLive authored and DigiLive committed Jul 17, 2020
1 parent 65f2aa4 commit 47d6288
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib/jblond/Diff/SequenceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public function getGroupedOpCodes(): array

if ($this->options['trimEqual']) {
if ($opCodes['0']['0'] == 'equal') {
// Remove sequences at the start which are out of context.
// Remove sequences at the start of the text, but keep the context lines.
$opCodes['0'] = [
$opCodes['0']['0'],
max($opCodes['0']['1'], $opCodes['0']['2'] - $this->options['context']),
Expand All @@ -568,7 +568,7 @@ public function getGroupedOpCodes(): array
$lastItem = count($opCodes) - 1;
if ($opCodes[$lastItem]['0'] == 'equal') {
[$tag, $item1, $item2, $item3, $item4] = $opCodes[$lastItem];
// Remove sequences at the end which are out of context.
// Remove sequences at the end of the text, but keep the context lines.
$opCodes[$lastItem] = [
$tag,
$item1,
Expand Down Expand Up @@ -607,8 +607,8 @@ public function getGroupedOpCodes(): array
];
}

if ($this->options['trimEqual'] || (!empty($group) && !(count($group) == 1 && $group[0][0] == 'equal'))) {
//Do not add the last sequences. They're out of context.
if (!$this->options['trimEqual'] || (!empty($group) && !(count($group) == 1 && $group[0][0] == 'equal'))) {
// Add the last sequences when !trimEqual || When there are no differences between both versions.
$groups[] = $group;
}

Expand Down
53 changes: 43 additions & 10 deletions tests/Diff/SequenceMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,72 @@ public function __construct($name = null, array $data = [], $dataName = '')
parent::__construct($name, $data, $dataName);
}

public function testGetGroupedOpCodes()
public function testGetGroupedOpCodesDefault()
{
// Test with default options.
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345');
$sequenceMatcher = new SequenceMatcher(
'54321ABXDE12345',
'54321ABxDE12345'
);

$this->assertEquals(
[[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
[
[
['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]
]
],
$sequenceMatcher->getGroupedOpCodes()
);
}

public function testGetGroupedOpCodesTrimEqualFalse()
{
// Test with trimEqual disabled.
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['trimEqual' => false]);
// First and last context lines of the sequences are included.
$sequenceMatcher = new SequenceMatcher(
'54321ABXDE12345',
'54321ABxDE12345',
['trimEqual' => false]
);

$this->assertEquals(
[[['equal', 0, 3, 0, 3]], [['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
[
[['equal', 0, 3, 0, 3]],
[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]],
[['equal', 12, 15, 12, 15]],
],
$sequenceMatcher->getGroupedOpCodes()
);
}

// Test with ignoreWhitespace enabled.
public function testGetGroupedOpCodesIgnoreWhitespaceTrue()
{
// Test with ignoreWhitespace enabled. Both sequences are considered to be the same.
// Note: The sequenceMatcher evaluates the string character by character. Option ignoreWhitespace will ignore
// if the difference if the character is a tab in one sequence and a space in the other.
$sequenceMatcher = new SequenceMatcher(
"\t54321ABXDE12345 ",
" 54321ABXDE12345\t",
['ignoreWhitespace' => true]
);

$this->assertEquals(
[[['equal', 14, 17, 14, 17]]],
[],
$sequenceMatcher->getGroupedOpCodes()
);
}

public function testGetGroupedOpCodesIgnoreCaseTrue()
{
// Test with ignoreCase enabled. Both sequences are considered to be the same.
$sequenceMatcher = new SequenceMatcher(
'54321ABXDE12345',
'54321ABxDE12345',
['ignoreCase' => true]
);

// Test with ignoreCase enabled.
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['ignoreCase' => true]);
$this->assertEquals(
[[['equal', 12, 15, 12, 15]]],
[],
$sequenceMatcher->getGroupedOpCodes()
);
}
Expand Down

0 comments on commit 47d6288

Please sign in to comment.