Changeset View
Changeset View
Standalone View
Standalone View
src/difference/__tests__/ArcanistDiffUtilsTestCase.php
| <?php | <?php | ||||
| /** | /** | ||||
| * Test cases for @{class:ArcanistDiffUtils}. | * Test cases for @{class:ArcanistDiffUtils}. | ||||
| * | |||||
| * @group testcase | |||||
| */ | */ | ||||
| final class ArcanistDiffUtilsTestCase extends ArcanistTestCase { | final class ArcanistDiffUtilsTestCase extends ArcanistTestCase { | ||||
| public function testLevenshtein() { | public function testLevenshtein() { | ||||
| $tests = array( | $tests = array( | ||||
| array( | array( | ||||
| 'a', | 'a', | ||||
| 'b', | 'b', | ||||
| 'x' | 'x', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'kalrmr(array($b))', | 'kalrmr(array($b))', | ||||
| 'array($b)', | 'array($b)', | ||||
| 'dddddddssssssssds' | 'dddddddssssssssds', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'array($b)', | 'array($b)', | ||||
| 'kalrmr(array($b))', | 'kalrmr(array($b))', | ||||
| 'iiiiiiissssssssis' | 'iiiiiiissssssssis', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'zkalrmr(array($b))z', | 'zkalrmr(array($b))z', | ||||
| 'xarray($b)x', | 'xarray($b)x', | ||||
| 'dddddddxsssssssssdx' | 'dddddddxsssssssssdx', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'xarray($b)x', | 'xarray($b)x', | ||||
| 'zkalrmr(array($b))z', | 'zkalrmr(array($b))z', | ||||
| 'iiiiiiixsssssssssix' | 'iiiiiiixsssssssssix', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'sssssssss' | 'sssssssss', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'abcdefghijkl', | 'abcdefghijkl', | ||||
| 'sssssssssiii' | 'sssssssssiii', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'abcdefghijkl', | 'abcdefghijkl', | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'sssssssssddd' | 'sssssssssddd', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'xyzabcdefghi', | 'xyzabcdefghi', | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'dddsssssssss' | 'dddsssssssss', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'abcdefghi', | 'abcdefghi', | ||||
| 'xyzabcdefghi', | 'xyzabcdefghi', | ||||
| 'iiisssssssss' | 'iiisssssssss', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'abcdefg', | 'abcdefg', | ||||
| 'abxdxfg', | 'abxdxfg', | ||||
| 'ssxsxss' | 'ssxsxss', | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'private function a($a, $b) {', | 'private function a($a, $b) {', | ||||
| 'public function and($b, $c) {', | 'public function and($b, $c) {', | ||||
| 'siixsdddxsssssssssssiissxsssxsss' | 'siixsdddxsssssssssssiissxsssxsss', | ||||
| ), | ), | ||||
| array( | array( | ||||
| // This is a test that we correctly detect shared prefixes and suffixes | // This is a test that we correctly detect shared prefixes and suffixes | ||||
| // and don't trigger "give up, too long" mode if there's a small text | // and don't trigger "give up, too long" mode if there's a small text | ||||
| // change in an ocean of similar text. | // change in an ocean of similar text. | ||||
| ' if ('. | ' if ('. | ||||
| Show All 38 Lines | public function testLevenshtein() { | ||||
| } | } | ||||
| } | } | ||||
| public function testGenerateUTF8IntralineDiff() { | public function testGenerateUTF8IntralineDiff() { | ||||
| // Both Strings Empty. | // Both Strings Empty. | ||||
| $left = ''; | $left = ''; | ||||
| $right = ''; | $right = ''; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 0)), | array(array(0, 0)), | ||||
| array(array(0, 0)) | array(array(0, 0)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // Left String Empty. | // Left String Empty. | ||||
| $left = ''; | $left = ''; | ||||
| $right = "Grumpy\xE2\x98\x83at"; | $right = "Grumpy\xE2\x98\x83at"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 0)), | array(array(0, 0)), | ||||
| array(array(0, 11)) | array(array(0, 11)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // Right String Empty. | // Right String Empty. | ||||
| $left = "Grumpy\xE2\x98\x83at"; | $left = "Grumpy\xE2\x98\x83at"; | ||||
| $right = ''; | $right = ''; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 11)), | array(array(0, 11)), | ||||
| array(array(0, 0)) | array(array(0, 0)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // Both Strings Same | // Both Strings Same | ||||
| $left = "Grumpy\xE2\x98\x83at"; | $left = "Grumpy\xE2\x98\x83at"; | ||||
| $right = "Grumpy\xE2\x98\x83at"; | $right = "Grumpy\xE2\x98\x83at"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 11)), | array(array(0, 11)), | ||||
| array(array(0, 11)) | array(array(0, 11)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // Both Strings are different. | // Both Strings are different. | ||||
| $left = "Grumpy\xE2\x98\x83at"; | $left = "Grumpy\xE2\x98\x83at"; | ||||
| $right = 'Smiling Dog'; | $right = 'Smiling Dog'; | ||||
| $result = array( | $result = array( | ||||
| array(array(1, 11)), | array(array(1, 11)), | ||||
| array(array(1, 11)) | array(array(1, 11)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // String with one difference in the middle. | // String with one difference in the middle. | ||||
| $left = 'GrumpyCat'; | $left = 'GrumpyCat'; | ||||
| $right = "Grumpy\xE2\x98\x83at"; | $right = "Grumpy\xE2\x98\x83at"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 6), array(1, 1), array(0, 2)), | array(array(0, 6), array(1, 1), array(0, 2)), | ||||
| array(array(0, 6), array(1, 3), array(0, 2)) | array(array(0, 6), array(1, 3), array(0, 2)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // Differences in middle, not connected to each other. | // Differences in middle, not connected to each other. | ||||
| $left = 'GrumpyCat'; | $left = 'GrumpyCat'; | ||||
| $right = "Grumpy\xE2\x98\x83a\xE2\x98\x83t"; | $right = "Grumpy\xE2\x98\x83a\xE2\x98\x83t"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 6), array(1, 2), array(0, 1)), | array(array(0, 6), array(1, 2), array(0, 1)), | ||||
| array(array(0, 6), array(1, 7), array(0, 1)) | array(array(0, 6), array(1, 7), array(0, 1)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // String with difference at the beginning. | // String with difference at the beginning. | ||||
| $left = "GrumpyC\xE2\x98\x83t"; | $left = "GrumpyC\xE2\x98\x83t"; | ||||
| $right = "DrumpyC\xE2\x98\x83t"; | $right = "DrumpyC\xE2\x98\x83t"; | ||||
| $result = array( | $result = array( | ||||
| array(array(1, 1), array(0, 10)), | array(array(1, 1), array(0, 10)), | ||||
| array(array(1, 1), array(0, 10)) | array(array(1, 1), array(0, 10)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // String with difference at the end. | // String with difference at the end. | ||||
| $left = "GrumpyC\xE2\x98\x83t"; | $left = "GrumpyC\xE2\x98\x83t"; | ||||
| $right = "GrumpyC\xE2\x98\x83P"; | $right = "GrumpyC\xE2\x98\x83P"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 10), array(1, 1)), | array(array(0, 10), array(1, 1)), | ||||
| array(array(0, 10), array(1, 1)) | array(array(0, 10), array(1, 1)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // String with differences at the beginning and end. | // String with differences at the beginning and end. | ||||
| $left = "GrumpyC\xE2\x98\x83t"; | $left = "GrumpyC\xE2\x98\x83t"; | ||||
| $right = "DrumpyC\xE2\x98\x83P"; | $right = "DrumpyC\xE2\x98\x83P"; | ||||
| $result = array( | $result = array( | ||||
| array(array(1, 1), array(0, 9), array(1, 1)), | array(array(1, 1), array(0, 9), array(1, 1)), | ||||
| array(array(1, 1), array(0, 9), array(1, 1)) | array(array(1, 1), array(0, 9), array(1, 1)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| // This is a unicode combining character, "COMBINING DOUBLE TILDE". | // This is a unicode combining character, "COMBINING DOUBLE TILDE". | ||||
| $cc = "\xCD\xA0"; | $cc = "\xCD\xA0"; | ||||
| $left = 'Senor'; | $left = 'Senor'; | ||||
| $right = "Sen{$cc}or"; | $right = "Sen{$cc}or"; | ||||
| $result = array( | $result = array( | ||||
| array(array(0, 2), array(1, 1), array(0, 2)), | array(array(0, 2), array(1, 1), array(0, 2)), | ||||
| array(array(0, 2), array(1, 3), array(0, 2)) | array(array(0, 2), array(1, 3), array(0, 2)), | ||||
| ); | ); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| $result, | $result, | ||||
| ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ArcanistDiffUtils::generateIntralineDiff($left, $right)); | ||||
| } | } | ||||
| } | } | ||||