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; }
/** * @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))); }
/** * @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); }
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)); }
/** * @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); }
/** * @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)); }
/** * @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); }
public function setDiff($sourceBefore, $sourceAfter) { $this->diff = Diff::create($sourceBefore, $sourceAfter); }
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(); }
/** * 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()); }
public function format(Diff $diff) { $this->lineCountLength = strlen($diff->getMaxLineNumber()); return parent::format($diff); }
public function getMessages() { return $this->diff->getMessages(); }