Skip to content

Commit 15c5b9f

Browse files
authored
Merge pull request #51 from JBlond/issue-50
Fix #50.
2 parents 65f2aa4 + af5b2c5 commit 15c5b9f

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

lib/jblond/Diff/SequenceMatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public function getGroupedOpCodes(): array
555555

556556
if ($this->options['trimEqual']) {
557557
if ($opCodes['0']['0'] == 'equal') {
558-
// Remove sequences at the start which are out of context.
558+
// Remove sequences at the start of the text, but keep the context lines.
559559
$opCodes['0'] = [
560560
$opCodes['0']['0'],
561561
max($opCodes['0']['1'], $opCodes['0']['2'] - $this->options['context']),
@@ -568,7 +568,7 @@ public function getGroupedOpCodes(): array
568568
$lastItem = count($opCodes) - 1;
569569
if ($opCodes[$lastItem]['0'] == 'equal') {
570570
[$tag, $item1, $item2, $item3, $item4] = $opCodes[$lastItem];
571-
// Remove sequences at the end which are out of context.
571+
// Remove sequences at the end of the text, but keep the context lines.
572572
$opCodes[$lastItem] = [
573573
$tag,
574574
$item1,
@@ -607,8 +607,8 @@ public function getGroupedOpCodes(): array
607607
];
608608
}
609609

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

tests/Diff/Renderer/Text/TextRenderersTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,3 @@ public function testUnifiedCli()
9595
$this->assertStringEqualsFile('tests/resources/ab.diff', $result);
9696
}
9797
}
98-

tests/Diff/SequenceMatcherTest.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,72 @@ public function __construct($name = null, array $data = [], $dataName = '')
2020
parent::__construct($name, $data, $dataName);
2121
}
2222

23-
public function testGetGroupedOpCodes()
23+
public function testGetGroupedOpCodesDefault()
2424
{
2525
// Test with default options.
26-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345');
26+
$sequenceMatcher = new SequenceMatcher(
27+
'54321ABXDE12345',
28+
'54321ABxDE12345'
29+
);
30+
2731
$this->assertEquals(
28-
[[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
32+
[
33+
[
34+
['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]
35+
]
36+
],
2937
$sequenceMatcher->getGroupedOpCodes()
3038
);
39+
}
3140

41+
public function testGetGroupedOpCodesTrimEqualFalse()
42+
{
3243
// Test with trimEqual disabled.
33-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['trimEqual' => false]);
44+
// First and last context lines of the sequences are included.
45+
$sequenceMatcher = new SequenceMatcher(
46+
'54321ABXDE12345',
47+
'54321ABxDE12345',
48+
['trimEqual' => false]
49+
);
50+
3451
$this->assertEquals(
35-
[[['equal', 0, 3, 0, 3]], [['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]]],
52+
[
53+
[['equal', 0, 3, 0, 3]],
54+
[['equal', 4, 7, 4, 7], ['replace', 7, 8, 7, 8], ['equal', 8, 11, 8, 11]],
55+
[['equal', 12, 15, 12, 15]],
56+
],
3657
$sequenceMatcher->getGroupedOpCodes()
3758
);
59+
}
3860

39-
// Test with ignoreWhitespace enabled.
61+
public function testGetGroupedOpCodesIgnoreWhitespaceTrue()
62+
{
63+
// Test with ignoreWhitespace enabled. Both sequences are considered to be the same.
4064
// Note: The sequenceMatcher evaluates the string character by character. Option ignoreWhitespace will ignore
4165
// if the difference if the character is a tab in one sequence and a space in the other.
4266
$sequenceMatcher = new SequenceMatcher(
4367
"\t54321ABXDE12345 ",
4468
" 54321ABXDE12345\t",
4569
['ignoreWhitespace' => true]
4670
);
71+
4772
$this->assertEquals(
48-
[[['equal', 14, 17, 14, 17]]],
73+
[],
4974
$sequenceMatcher->getGroupedOpCodes()
5075
);
76+
}
77+
78+
public function testGetGroupedOpCodesIgnoreCaseTrue()
79+
{
80+
// Test with ignoreCase enabled. Both sequences are considered to be the same.
81+
$sequenceMatcher = new SequenceMatcher(
82+
'54321ABXDE12345',
83+
'54321ABxDE12345',
84+
['ignoreCase' => true]
85+
);
5186

52-
// Test with ignoreCase enabled.
53-
$sequenceMatcher = new SequenceMatcher('54321ABXDE12345', '54321ABxDE12345', ['ignoreCase' => true]);
5487
$this->assertEquals(
55-
[[['equal', 12, 15, 12, 15]]],
88+
[],
5689
$sequenceMatcher->getGroupedOpCodes()
5790
);
5891
}

0 commit comments

Comments
 (0)