/**
  * Proxy getting properties to real objects.
  *
  * @param string $name Property name.
  *
  * @throws \UnexpectedValueException If property unknown.
  * @return float
  */
 public function __get($name)
 {
     switch ($name) {
         case 'Diff_Timeout':
             $result = $this->diff->getTimeout();
             break;
         case 'Diff_EditCost':
             $result = $this->diff->getEditCost();
             break;
         case 'Match_Threshold':
             $result = $this->match->getThreshold();
             break;
         case 'Match_Distance':
             $result = $this->match->getDistance();
             break;
         case 'Match_MaxBits':
             $result = $this->match->getMaxBits();
             break;
         case 'Patch_DeleteThreshold':
             $result = $this->patch->getDeleteTreshold();
             break;
         case 'Patch_Margin':
             $result = $this->patch->getMargin();
             break;
         default:
             throw new \UnexpectedValueException('Unknown property: ' . $name);
     }
     return $result;
 }
Пример #2
0
 public function testMain()
 {
     // Perform a trivial diff.
     // Null case.
     $this->assertEquals(array(), $this->d->main("", "", false)->getChanges());
     // Equality.
     $this->assertEquals(array(array(Diff::EQUAL, "abc")), $this->d->main("abc", "abc", false)->getChanges());
     // Check '0' strings
     $this->assertEquals(array(array(Diff::EQUAL, "0"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "12"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "0"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "34"), array(Diff::INSERT, "X"), array(Diff::EQUAL, "0")), $this->d->main("0120340", "0X12X0X34X0", false)->getChanges());
     $this->assertEquals(array(array(Diff::EQUAL, "0"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "12"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "0"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "34"), array(Diff::DELETE, "X"), array(Diff::EQUAL, "0")), $this->d->main("0X12X0X34X0", "0120340", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "Apple"), array(Diff::INSERT, "Banana"), array(Diff::EQUAL, "s are a"), array(Diff::INSERT, "lso"), array(Diff::EQUAL, " fruit.")), $this->d->main("Apples are a fruit.", "Bananas are also fruit.", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "a"), array(Diff::INSERT, Utils::unicodeChr(0x680)), array(Diff::EQUAL, "x"), array(Diff::DELETE, "\t"), array(Diff::INSERT, "")), $this->d->main("ax\t", Utils::unicodeChr(0x680) . "x", false)->getChanges());
     // Overlaps.
     $this->assertEquals(array(array(Diff::DELETE, "1"), array(Diff::EQUAL, "a"), array(Diff::DELETE, "y"), array(Diff::EQUAL, "b"), array(Diff::DELETE, "2"), array(Diff::INSERT, "xab")), $this->d->main("1ayb2", "abxab", false)->getChanges());
     $this->assertEquals(array(array(Diff::INSERT, "xaxcx"), array(Diff::EQUAL, "abc"), array(Diff::DELETE, "y")), $this->d->main("abcy", "xaxcxabc", false)->getChanges());
     $this->assertEquals(array(array(Diff::DELETE, "ABCD"), array(Diff::EQUAL, "a"), array(Diff::DELETE, "="), array(Diff::INSERT, "-"), array(Diff::EQUAL, "bcd"), array(Diff::DELETE, "="), array(Diff::INSERT, "-"), array(Diff::EQUAL, "efghijklmnopqrs"), array(Diff::DELETE, "EFGHIJKLMNOefg")), $this->d->main("ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg", "a-bcd-efghijklmnopqrs", false)->getChanges());
     // Large equality.
     $this->assertEquals(array(array(Diff::INSERT, " "), array(Diff::EQUAL, "a"), array(Diff::INSERT, "nd"), array(Diff::EQUAL, " [[Pennsylvania]]"), array(Diff::DELETE, " and [[New")), $this->d->main("a [[Pennsylvania]] and [[New", " and [[Pennsylvania]]", false)->getChanges());
     // Timeout.
     // 100ms
     $this->d->setTimeout(0.1);
     $a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n";
     $b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n";
     // Increase the text lengths by 1024 times to ensure a timeout.
     for ($i = 0; $i < 10; $i++) {
         $a .= $a;
         $b .= $b;
     }
     $startTime = microtime(1);
     $this->d->main($a, $b);
     $endTime = microtime(1);
     // Test that we took at least the timeout period.
     $this->assertGreaterThanOrEqual($this->d->getTimeout(), $endTime - $startTime);
     // Test that we didn't take forever (be forgiving).
     // Theoretically this test could fail very occasionally if the
     // OS task swaps or locks up for a second at the wrong moment.
     // TODO must be $this->d->getTimeout() * 2, but it need some optimization of linesToCharsMunge()
     $this->assertLessThan($this->d->getTimeout() * 15, $endTime - $startTime);
     $this->d->setTimeout(0);
     // Test the linemode speedup.
     // Must be long to pass the 100 char cutoff.
     // Simple line-mode.
     $a = str_repeat("1234567890\n", 13);
     $b = str_repeat("abcdefghij\n", 13);
     $this->assertEquals($this->d->main($a, $b, false)->getChanges(), $this->d->main($a, $b, true)->getChanges());
     // Single line-mode.
     $a = str_repeat("1234567890", 13);
     $b = str_repeat("abcdefghij", 13);
     $this->assertEquals($this->d->main($a, $b, false)->getChanges(), $this->d->main($a, $b, true)->getChanges());
     function rebuildtexts($diffs)
     {
         // Construct the two texts which made up the diff originally.
         $text1 = "";
         $text2 = "";
         foreach ($diffs as $change) {
             if ($change[0] != Diff::INSERT) {
                 $text1 .= $change[1];
             }
             if ($change[0] != Diff::DELETE) {
                 $text2 .= $change[1];
             }
         }
         return array($text1, $text2);
     }
     // Overlap line-mode.
     $a = str_repeat("1234567890\n", 13);
     $b = "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n";
     $this->assertEquals(rebuildtexts($this->d->main($a, $b, false)->getChanges()), rebuildtexts($this->d->main($a, $b, true)->getChanges()));
     // Test null inputs.
     try {
         $this->d->main(null, null);
         $this->fail();
     } catch (\InvalidArgumentException $e) {
     }
 }