示例#1
0
文件: Upstream.php 项目: nochso/diff
 public function format(Diff $diff)
 {
     $messages = new Multiline($diff->getMessages());
     $messages->prefix('#');
     $lines = new Multiline(array_merge($this->header, $messages->toArray()));
     $lines->setEol("\n");
     $inOld = false;
     $i = 0;
     $old = [];
     $diffLines = $diff->getDiffLines();
     foreach ($diffLines as $line) {
         if ($line->isSame()) {
             if ($inOld === false) {
                 $inOld = $i;
             }
         } elseif ($inOld !== false) {
             if ($i - $inOld > 5) {
                 $old[$inOld] = $i - 1;
             }
             $inOld = false;
         }
         ++$i;
     }
     $start = isset($old[0]) ? $old[0] : 0;
     $end = count($diffLines);
     if ($tmp = array_search($end, $old)) {
         $end = $tmp;
     }
     $newChunk = true;
     for ($i = $start; $i < $end; ++$i) {
         if (isset($old[$i])) {
             $lines->add('');
             $newChunk = true;
             $i = $old[$i];
         }
         if ($newChunk) {
             if ($this->showNonDiffLines === true) {
                 $lines->add('@@ @@');
             }
             $newChunk = false;
         }
         if ($diffLines[$i]->isAddition()) {
             $lines->add('+' . $diffLines[$i]->getText());
         } elseif ($diffLines[$i]->isRemoval()) {
             $lines->add('-' . $diffLines[$i]->getText());
         } elseif ($this->showNonDiffLines === true) {
             $lines->add(' ' . $diffLines[$i]->getText());
         }
     }
     $lines->add('');
     return (string) $lines;
 }
示例#2
0
 /**
  * @dataProvider contextProvider
  */
 public function testContext($maxContext, $from, $to, $expected)
 {
     $gh = new GithubMarkdown();
     $context = new ContextDiff();
     $context->setMaxContext($maxContext);
     $this->assertEquals($expected, $gh->format(Diff::create($from, $to, $context)));
 }
示例#3
0
文件: POSIXTest.php 项目: nochso/diff
 /**
  * @dataProvider cliEscaperProvider
  *
  * @param string $from
  * @param string $to
  * @param string $expected
  */
 public function testCliEscaper($from, $to, $expected)
 {
     $formatter = new POSIX();
     $formatter->setEscaper(new Escape\Cli());
     $output = $formatter->format(Diff::create($from, $to));
     $this->assertSame($expected, $output);
 }
示例#4
0
 public function testTypesOtherThanArrayAndStringCanBePassed()
 {
     $expected = "--- Original\n+++ New\n@@ @@\n-1\n+2\n";
     $diff = Diff::create(1, 2);
     $formatter = new Upstream();
     $this->assertEquals($expected, $formatter->format($diff));
 }
示例#5
0
文件: HTMLTest.php 项目: nochso/diff
 /**
  * @dataProvider formatWithoutLineNumberProvider
  *
  * @param string $from
  * @param string $to
  * @param string $expected
  */
 public function testFormatWithoutLineNumber($from, $to, $expected)
 {
     $diff = Diff::create($from, $to);
     $formatter = new HTML();
     $formatter->getPrintf()->disableLineNumber();
     $output = $formatter->format($diff);
     $this->assertSame($expected, $output);
 }
示例#6
0
文件: TextTest.php 项目: nochso/diff
 /**
  * @dataProvider contextProvider
  *
  * @param int    $maxContext
  * @param string $from
  * @param string $to
  * @param string $expected
  */
 public function testContext($maxContext, $from, $to, $expected)
 {
     $context = new ContextDiff();
     $context->setMaxContext($maxContext);
     $diff = Diff::create($from, $to, $context);
     $formatter = new Text();
     $this->assertSame($expected, $formatter->format($diff));
 }
示例#7
0
 /**
  * @dataProvider lineEndingProvider
  */
 public function testLineEndingWarning($from, $to, $expectedFromEol, $expectedToEol)
 {
     $formatter = new Upstream();
     $diff = $formatter->format(Diff::create($from, $to));
     // No warning
     if ($expectedFromEol === null) {
         $this->assertNotRegExp('/#Warning: Line ending changed from .+ to .+$/m', $diff);
         return;
     }
     $fromEolName = (new EOL($expectedFromEol))->getName();
     $toEolName = (new EOL($expectedToEol))->getName();
     $pattern = sprintf('/^#Warning: Line ending changed from %s to %s$/m', preg_quote($fromEolName), preg_quote($toEolName));
     $this->assertRegExp($pattern, $diff);
 }
示例#8
0
 public function setDiff($sourceBefore, $sourceAfter)
 {
     $this->diff = Diff::create($sourceBefore, $sourceAfter);
 }
示例#9
0
 private function showDiff(Document $document, $existingFilepath)
 {
     $before = file_get_contents($existingFilepath);
     $after = $document->getContent();
     $diff = Diff\Diff::create($before, $after);
     $this->stdio->out('Differences to existing file:');
     if (!count($diff->getDiffLines())) {
         $this->stdio->outln(' <<yellow>>None<<reset>>');
         $this->stdio->outln();
         return;
     }
     $this->stdio->outln();
     $this->stdio->outln();
     if ($this->stdio->getStdout()->isPosix()) {
         $t = new Diff\Format\Template\POSIX();
     } else {
         $t = new Diff\Format\Template\Text();
     }
     $this->stdio->outln($t->format($diff));
     $this->stdio->outln();
 }
示例#10
0
文件: Printf.php 项目: nochso/diff
 /**
  * Prepare this helper before first using it to format lines.
  *
  * @param \nochso\Diff\Diff $diff
  */
 public function prepare(Diff $diff)
 {
     $this->lineCountLength = strlen($diff->getMaxLineNumber());
 }
示例#11
0
 public function format(Diff $diff)
 {
     $this->lineCountLength = strlen($diff->getMaxLineNumber());
     return parent::format($diff);
 }
示例#12
0
 public function getMessages()
 {
     return $this->diff->getMessages();
 }