Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #50. #51

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion tests/Diff/Renderer/Text/TextRenderersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,3 @@ public function testUnifiedCli()
$this->assertStringEqualsFile('tests/resources/ab.diff', $result);
}
}

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