Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php final class DifferentialHunkTestCase extends PhutilTestCase { public function te..
Decoded Output download
<?php
final class DifferentialHunkTestCase extends PhutilTestCase {
public function testMakeChanges() {
$root = dirname(__FILE__).'/hunk/';
$hunk = new DifferentialHunk();
$hunk->setChanges(Filesystem::readFile($root.'basic.diff'));
$hunk->setOldOffset(1);
$hunk->setNewOffset(11);
$old = Filesystem::readFile($root.'old.txt');
$this->assertEqual($old, $hunk->makeOldFile());
$new = Filesystem::readFile($root.'new.txt');
$this->assertEqual($new, $hunk->makeNewFile());
$added = array(
12 => "1 quack
",
13 => "1 quack
",
16 => "5 drake
",
);
$this->assertEqual($added, $hunk->getAddedLines());
$hunk = new DifferentialHunk();
$hunk->setChanges(Filesystem::readFile($root.'newline.diff'));
$hunk->setOldOffset(1);
$hunk->setNewOffset(11);
$this->assertEqual("a
", $hunk->makeOldFile());
$this->assertEqual('a', $hunk->makeNewFile());
$this->assertEqual(array(11 => 'a'), $hunk->getAddedLines());
}
public function testMakeStructuredChanges1() {
$hunk = $this->loadHunk('fruit1.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple
",
),
2 => array(
'type' => ' ',
'text' => "cherry
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple
",
),
2 => array(
'type' => '+',
'text' => "banana
",
),
3 => array(
'type' => '+',
'text' => "plum
",
),
4 => array(
'type' => ' ',
'text' => "cherry
",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredChanges2() {
$hunk = $this->loadHunk('fruit2.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple
",
),
2 => array(
'type' => ' ',
'text' => "banana
",
),
3 => array(
'type' => '-',
'text' => "plum
",
),
4 => array(
'type' => ' ',
'text' => "cherry
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple
",
),
2 => array(
'type' => ' ',
'text' => "banana
",
),
3 => array(
'type' => ' ',
'text' => "cherry
",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineAdded() {
$hunk = $this->loadHunk('trailing_newline_added.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => '-',
'text' => 'quack',
),
4 => array(
'type' => '\',
'text' => " No newline at end of file
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => '+',
'text' => "quack
",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineRemoved() {
$hunk = $this->loadHunk('trailing_newline_removed.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => '-',
'text' => "quack
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => '+',
'text' => 'quack',
),
4 => array(
'type' => '\',
'text' => " No newline at end of file
",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineAbsent() {
$hunk = $this->loadHunk('trailing_newline_absent.diff');
$this->assertEqual(
array(
1 => array(
'type' => '-',
'text' => "quack
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => ' ',
'text' => 'quack',
),
4 => array(
'type' => '\',
'text' => " No newline at end of file
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => '+',
'text' => "meow
",
),
2 => array(
'type' => ' ',
'text' => "quack
",
),
3 => array(
'type' => ' ',
'text' => 'quack',
),
4 => array(
'type' => '\',
'text' => " No newline at end of file
",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredOffset() {
$hunk = $this->loadHunk('offset.diff');
$this->assertEqual(
array(
76 => array(
'type' => ' ',
'text' => " \$bits .= '0';
",
),
77 => array(
'type' => ' ',
'text' => " }
",
),
78 => array(
'type' => ' ',
'text' => " }
",
),
79 => array(
'type' => ' ',
'text' => " }
",
),
80 => array(
'type' => '-',
'text' => " \$this->bits = \$bits;
",
),
81 => array(
'type' => ' ',
'text' => " }
",
),
82 => array(
'type' => ' ',
'text' => " return \$this->bits;
",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
76 => array(
'type' => ' ',
'text' => " \$bits .= '0';
",
),
77 => array(
'type' => ' ',
'text' => " }
",
),
78 => array(
'type' => ' ',
'text' => " }
",
),
79 => array(
'type' => '+',
'text' => " break;
",
),
80 => array(
'type' => ' ',
'text' => " }
",
),
81 => array(
'type' => '+',
'text' => " \$this->bits = \$bytes;
",
),
82 => array(
'type' => ' ',
'text' => " }
",
),
83 => array(
'type' => ' ',
'text' => " return \$this->bits;
",
),
),
$hunk->getStructuredNewFile());
}
private function loadHunk($name) {
$root = dirname(__FILE__).'/hunk/';
$data = Filesystem::readFile($root.$name);
$parser = new ArcanistDiffParser();
$changes = $parser->parseDiff($data);
$viewer = PhabricatorUser::getOmnipotentUser();
$diff = DifferentialDiff::newFromRawChanges($viewer, $changes);
$changesets = $diff->getChangesets();
if (count($changesets) !== 1) {
throw new Exception(
pht(
'Expected exactly one changeset from "%s".',
$name));
}
$changeset = head($changesets);
$hunks = $changeset->getHunks();
if (count($hunks) !== 1) {
throw new Exception(
pht(
'Expected exactly one hunk from "%s".',
$name));
}
$hunk = head($hunks);
return $hunk;
}
}
?>
Did this file decode correctly?
Original Code
<?php
final class DifferentialHunkTestCase extends PhutilTestCase {
public function testMakeChanges() {
$root = dirname(__FILE__).'/hunk/';
$hunk = new DifferentialHunk();
$hunk->setChanges(Filesystem::readFile($root.'basic.diff'));
$hunk->setOldOffset(1);
$hunk->setNewOffset(11);
$old = Filesystem::readFile($root.'old.txt');
$this->assertEqual($old, $hunk->makeOldFile());
$new = Filesystem::readFile($root.'new.txt');
$this->assertEqual($new, $hunk->makeNewFile());
$added = array(
12 => "1 quack\n",
13 => "1 quack\n",
16 => "5 drake\n",
);
$this->assertEqual($added, $hunk->getAddedLines());
$hunk = new DifferentialHunk();
$hunk->setChanges(Filesystem::readFile($root.'newline.diff'));
$hunk->setOldOffset(1);
$hunk->setNewOffset(11);
$this->assertEqual("a\n", $hunk->makeOldFile());
$this->assertEqual('a', $hunk->makeNewFile());
$this->assertEqual(array(11 => 'a'), $hunk->getAddedLines());
}
public function testMakeStructuredChanges1() {
$hunk = $this->loadHunk('fruit1.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple\n",
),
2 => array(
'type' => ' ',
'text' => "cherry\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple\n",
),
2 => array(
'type' => '+',
'text' => "banana\n",
),
3 => array(
'type' => '+',
'text' => "plum\n",
),
4 => array(
'type' => ' ',
'text' => "cherry\n",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredChanges2() {
$hunk = $this->loadHunk('fruit2.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple\n",
),
2 => array(
'type' => ' ',
'text' => "banana\n",
),
3 => array(
'type' => '-',
'text' => "plum\n",
),
4 => array(
'type' => ' ',
'text' => "cherry\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "apple\n",
),
2 => array(
'type' => ' ',
'text' => "banana\n",
),
3 => array(
'type' => ' ',
'text' => "cherry\n",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineAdded() {
$hunk = $this->loadHunk('trailing_newline_added.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => '-',
'text' => 'quack',
),
4 => array(
'type' => '\\',
'text' => " No newline at end of file\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => '+',
'text' => "quack\n",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineRemoved() {
$hunk = $this->loadHunk('trailing_newline_removed.diff');
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => '-',
'text' => "quack\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => ' ',
'text' => "quack\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => '+',
'text' => 'quack',
),
4 => array(
'type' => '\\',
'text' => " No newline at end of file\n",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredNewlineAbsent() {
$hunk = $this->loadHunk('trailing_newline_absent.diff');
$this->assertEqual(
array(
1 => array(
'type' => '-',
'text' => "quack\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => ' ',
'text' => 'quack',
),
4 => array(
'type' => '\\',
'text' => " No newline at end of file\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
1 => array(
'type' => '+',
'text' => "meow\n",
),
2 => array(
'type' => ' ',
'text' => "quack\n",
),
3 => array(
'type' => ' ',
'text' => 'quack',
),
4 => array(
'type' => '\\',
'text' => " No newline at end of file\n",
),
),
$hunk->getStructuredNewFile());
}
public function testMakeStructuredOffset() {
$hunk = $this->loadHunk('offset.diff');
$this->assertEqual(
array(
76 => array(
'type' => ' ',
'text' => " \$bits .= '0';\n",
),
77 => array(
'type' => ' ',
'text' => " }\n",
),
78 => array(
'type' => ' ',
'text' => " }\n",
),
79 => array(
'type' => ' ',
'text' => " }\n",
),
80 => array(
'type' => '-',
'text' => " \$this->bits = \$bits;\n",
),
81 => array(
'type' => ' ',
'text' => " }\n",
),
82 => array(
'type' => ' ',
'text' => " return \$this->bits;\n",
),
),
$hunk->getStructuredOldFile());
$this->assertEqual(
array(
76 => array(
'type' => ' ',
'text' => " \$bits .= '0';\n",
),
77 => array(
'type' => ' ',
'text' => " }\n",
),
78 => array(
'type' => ' ',
'text' => " }\n",
),
79 => array(
'type' => '+',
'text' => " break;\n",
),
80 => array(
'type' => ' ',
'text' => " }\n",
),
81 => array(
'type' => '+',
'text' => " \$this->bits = \$bytes;\n",
),
82 => array(
'type' => ' ',
'text' => " }\n",
),
83 => array(
'type' => ' ',
'text' => " return \$this->bits;\n",
),
),
$hunk->getStructuredNewFile());
}
private function loadHunk($name) {
$root = dirname(__FILE__).'/hunk/';
$data = Filesystem::readFile($root.$name);
$parser = new ArcanistDiffParser();
$changes = $parser->parseDiff($data);
$viewer = PhabricatorUser::getOmnipotentUser();
$diff = DifferentialDiff::newFromRawChanges($viewer, $changes);
$changesets = $diff->getChangesets();
if (count($changesets) !== 1) {
throw new Exception(
pht(
'Expected exactly one changeset from "%s".',
$name));
}
$changeset = head($changesets);
$hunks = $changeset->getHunks();
if (count($hunks) !== 1) {
throw new Exception(
pht(
'Expected exactly one hunk from "%s".',
$name));
}
$hunk = head($hunks);
return $hunk;
}
}
Function Calls
None |
Stats
MD5 | ce1d7955a86c3e4e8ee094ff1393437c |
Eval Count | 0 |
Decode Time | 135 ms |