diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -299,6 +299,7 @@ 'PhutilPregsprintfTestCase' => 'xsprintf/__tests__/PhutilPregsprintfTestCase.php', 'PhutilProcessGroupDaemon' => 'daemon/torture/PhutilProcessGroupDaemon.php', 'PhutilProseDiff' => 'utils/PhutilProseDiff.php', + 'PhutilProseDiffTestCase' => 'utils/__tests__/PhutilProseDiffTestCase.php', 'PhutilProseDifferenceEngine' => 'utils/PhutilProseDifferenceEngine.php', 'PhutilProtocolChannel' => 'channel/PhutilProtocolChannel.php', 'PhutilProxyException' => 'error/PhutilProxyException.php', @@ -862,6 +863,7 @@ 'PhutilPregsprintfTestCase' => 'PhutilTestCase', 'PhutilProcessGroupDaemon' => 'PhutilTortureTestDaemon', 'PhutilProseDiff' => 'Phobject', + 'PhutilProseDiffTestCase' => 'PhutilTestCase', 'PhutilProseDifferenceEngine' => 'Phobject', 'PhutilProtocolChannel' => 'PhutilChannelChannel', 'PhutilProxyException' => 'Exception', diff --git a/src/utils/PhutilProseDiff.php b/src/utils/PhutilProseDiff.php --- a/src/utils/PhutilProseDiff.php +++ b/src/utils/PhutilProseDiff.php @@ -63,10 +63,12 @@ $type = $part['type']; if ($last !== $type) { - $combined[] = array( - 'type' => $last, - 'text' => $last_text, - ); + if ($last !== null) { + $combined[] = array( + 'type' => $last, + 'text' => $last_text, + ); + } $last_text = null; $last = $type; } diff --git a/src/utils/__tests__/PhutilProseDiffTestCase.php b/src/utils/__tests__/PhutilProseDiffTestCase.php new file mode 100644 --- /dev/null +++ b/src/utils/__tests__/PhutilProseDiffTestCase.php @@ -0,0 +1,48 @@ +assertProseParts( + '', + '', + array(), + pht('Empty')); + + $this->assertProseParts( + "xxx\nyyy", + "xxx\nzzz\nyyy", + array( + "= xxx\n", + "+ zzz\n", + '= yyy', + ), + pht('Add Paragraph')); + + $this->assertProseParts( + "xxx\nzzz\nyyy", + "xxx\nyyy", + array( + "= xxx\n", + "- zzz\n", + '= yyy', + ), + pht('Remove Paragraph')); + } + + private function assertProseParts($old, $new, array $expect_parts, $label) { + $engine = new PhutilProseDifferenceEngine(); + $diff = $engine->getDiff($old, $new); + + $parts = $diff->getParts(); + + $actual = array(); + foreach ($parts as $part) { + $actual[] = $part['type'].' '.$part['text']; + } + + $this->assertEqual($expect_parts, $actual, $label); + } + + +}