diff --git a/lib/jblond/Diff/SequenceMatcher.php b/lib/jblond/Diff/SequenceMatcher.php index 2a90cd75..3ee19977 100644 --- a/lib/jblond/Diff/SequenceMatcher.php +++ b/lib/jblond/Diff/SequenceMatcher.php @@ -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']), @@ -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, @@ -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; } diff --git a/tests/Diff/SequenceMatcherTest.php b/tests/Diff/SequenceMatcherTest.php index f3cb0c00..f443741e 100644 --- a/tests/Diff/SequenceMatcherTest.php +++ b/tests/Diff/SequenceMatcherTest.php @@ -20,23 +20,47 @@ 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( @@ -44,15 +68,24 @@ public function testGetGroupedOpCodes() " 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() ); }