[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/parser/__tests__/ -> DifferentialHunkParserTestCase.php (source)

   1  <?php
   2  
   3  final class DifferentialHunkParserTestCase extends PhabricatorTestCase {
   4  
   5    private function createComment() {
   6      $comment = new DifferentialInlineComment();
   7      return $comment;
   8    }
   9  
  10    private function createHunk(
  11      $old_offset,
  12      $old_len,
  13      $new_offset,
  14      $new_len,
  15      $changes) {
  16  
  17      $hunk = id(new DifferentialHunkModern())
  18        ->setOldOffset($old_offset)
  19        ->setOldLen($old_len)
  20        ->setNewOffset($new_offset)
  21        ->setNewLen($new_len)
  22        ->setChanges($changes);
  23  
  24      return $hunk;
  25    }
  26  
  27    // Returns a change that consists of a single hunk, starting at line 1.
  28    private function createSingleChange($old_lines, $new_lines, $changes) {
  29      return array(
  30        0 => $this->createHunk(1, $old_lines, 1, $new_lines, $changes),
  31      );
  32    }
  33  
  34    private function createHunksFromFile($name) {
  35      $data = Filesystem::readFile(dirname(__FILE__).'/data/'.$name);
  36  
  37      $parser = new ArcanistDiffParser();
  38      $changes = $parser->parseDiff($data);
  39      if (count($changes) !== 1) {
  40        throw new Exception("Expected 1 changeset for '{$name}'!");
  41      }
  42  
  43      $diff = DifferentialDiff::newFromRawChanges(
  44        PhabricatorUser::getOmnipotentUser(),
  45        $changes);
  46      return head($diff->getChangesets())->getHunks();
  47    }
  48  
  49    public function testOneLineOldComment() {
  50      $parser = new DifferentialHunkParser();
  51      $hunks = $this->createSingleChange(1, 0, '-a');
  52      $context = $parser->makeContextDiff(
  53        $hunks,
  54        0,
  55        1,
  56        0,
  57        0);
  58      $this->assertEqual("@@ -1,1 @@\n-a", $context);
  59    }
  60  
  61    public function testOneLineNewComment() {
  62      $parser = new DifferentialHunkParser();
  63      $hunks = $this->createSingleChange(0, 1, '+a');
  64      $context = $parser->makeContextDiff(
  65        $hunks,
  66        1,
  67        1,
  68        0,
  69        0);
  70      $this->assertEqual("@@ +1,1 @@\n+a", $context);
  71    }
  72  
  73    public function testCannotFindContext() {
  74      $parser = new DifferentialHunkParser();
  75      $hunks = $this->createSingleChange(0, 1, '+a');
  76      $context = $parser->makeContextDiff(
  77        $hunks,
  78        1,
  79        2,
  80        0,
  81        0);
  82      $this->assertEqual('', $context);
  83    }
  84  
  85    public function testOverlapFromStartOfHunk() {
  86      $parser = new DifferentialHunkParser();
  87      $hunks = array(
  88        0 => $this->createHunk(23, 2, 42, 2, " 1\n 2"),
  89      );
  90      $context = $parser->makeContextDiff(
  91        $hunks,
  92        1,
  93        41,
  94        1,
  95        0);
  96      $this->assertEqual("@@ -23,1 +42,1 @@\n 1", $context);
  97    }
  98  
  99    public function testOverlapAfterEndOfHunk() {
 100      $parser = new DifferentialHunkParser();
 101      $hunks = array(
 102        0 => $this->createHunk(23, 2, 42, 2, " 1\n 2"),
 103      );
 104      $context = $parser->makeContextDiff(
 105        $hunks,
 106        1,
 107        43,
 108        1,
 109        0);
 110      $this->assertEqual("@@ -24,1 +43,1 @@\n 2", $context);
 111    }
 112  
 113    public function testInclusionOfNewFileInOldCommentFromStart() {
 114      $parser = new DifferentialHunkParser();
 115      $hunks = $this->createSingleChange(2, 3,
 116           "+n1\n".
 117           " e1/2\n".
 118           "-o2\n".
 119           "+n3\n");
 120      $context = $parser->makeContextDiff(
 121        $hunks,
 122        0,
 123        1,
 124        1,
 125        0);
 126      $this->assertEqual(
 127          "@@ -1,2 +2,1 @@\n".
 128          " e1/2\n".
 129          "-o2", $context);
 130    }
 131  
 132    public function testInclusionOfOldFileInNewCommentFromStart() {
 133      $parser = new DifferentialHunkParser();
 134      $hunks = $this->createSingleChange(2, 2,
 135           "-o1\n".
 136           " e2/1\n".
 137           "-o3\n".
 138           "+n2\n");
 139      $context = $parser->makeContextDiff(
 140        $hunks,
 141        1,
 142        1,
 143        1,
 144        0);
 145      $this->assertEqual(
 146          "@@ -2,1 +1,2 @@\n".
 147          " e2/1\n".
 148          "+n2", $context);
 149    }
 150  
 151    public function testNoNewlineAtEndOfFile() {
 152      $parser = new DifferentialHunkParser();
 153      $hunks = $this->createSingleChange(0, 1,
 154          "+a\n".
 155          "\\No newline at end of file");
 156      // Note that this only works with additional context.
 157      $context = $parser->makeContextDiff(
 158        $hunks,
 159        1,
 160        2,
 161        0,
 162        1);
 163      $this->assertEqual(
 164          "@@ +1,1 @@\n".
 165          "+a\n".
 166          "\\No newline at end of file", $context);
 167    }
 168  
 169    public function testMultiLineNewComment() {
 170      $parser = new DifferentialHunkParser();
 171      $hunks = $this->createSingleChange(7, 7,
 172          " e1\n".
 173          " e2\n".
 174          "-o3\n".
 175          "-o4\n".
 176          "+n3\n".
 177          " e5/4\n".
 178          " e6/5\n".
 179          "+n6\n".
 180          " e7\n");
 181      $context = $parser->makeContextDiff(
 182        $hunks,
 183        1,
 184        2,
 185        4,
 186        0);
 187      $this->assertEqual(
 188          "@@ -2,5 +2,5 @@\n".
 189          " e2\n".
 190          "-o3\n".
 191          "-o4\n".
 192          "+n3\n".
 193          " e5/4\n".
 194          " e6/5\n".
 195          "+n6", $context);
 196    }
 197  
 198    public function testMultiLineOldComment() {
 199      $parser = new DifferentialHunkParser();
 200      $hunks = $this->createSingleChange(7, 7,
 201          " e1\n".
 202          " e2\n".
 203          "-o3\n".
 204          "-o4\n".
 205          "+n3\n".
 206          " e5/4\n".
 207          " e6/5\n".
 208          "+n6\n".
 209          " e7\n");
 210      $context = $parser->makeContextDiff(
 211        $hunks,
 212        0,
 213        2,
 214        4,
 215        0);
 216      $this->assertEqual(
 217          "@@ -2,5 +2,4 @@\n".
 218          " e2\n".
 219          "-o3\n".
 220          "-o4\n".
 221          "+n3\n".
 222          " e5/4\n".
 223          " e6/5", $context);
 224    }
 225  
 226    public function testInclusionOfNewFileInOldCommentFromStartWithContext() {
 227      $parser = new DifferentialHunkParser();
 228      $hunks = $this->createSingleChange(2, 3,
 229           "+n1\n".
 230           " e1/2\n".
 231           "-o2\n".
 232           "+n3\n");
 233      $context = $parser->makeContextDiff(
 234        $hunks,
 235        0,
 236        1,
 237        1,
 238        1);
 239      $this->assertEqual(
 240          "@@ -1,2 +1,2 @@\n".
 241          "+n1\n".
 242          " e1/2\n".
 243          "-o2", $context);
 244    }
 245  
 246    public function testInclusionOfOldFileInNewCommentFromStartWithContext() {
 247      $parser = new DifferentialHunkParser();
 248      $hunks = $this->createSingleChange(2, 2,
 249           "-o1\n".
 250           " e2/1\n".
 251           "-o3\n".
 252           "+n2\n");
 253      $context = $parser->makeContextDiff(
 254        $hunks,
 255        1,
 256        1,
 257        1,
 258        1);
 259      $this->assertEqual(
 260          "@@ -1,3 +1,2 @@\n".
 261          "-o1\n".
 262          " e2/1\n".
 263          "-o3\n".
 264          "+n2", $context);
 265    }
 266  
 267    public function testMissingContext() {
 268      $tests = array(
 269        'missing_context.diff' => array(
 270          4 => true,
 271        ),
 272        'missing_context_2.diff' => array(
 273          5 => true,
 274        ),
 275        'missing_context_3.diff' => array(
 276          4 => true,
 277          13 => true,
 278        ),
 279      );
 280  
 281      foreach ($tests as $name => $expect) {
 282        $hunks = $this->createHunksFromFile($name);
 283  
 284        $parser = new DifferentialHunkParser();
 285        $actual = $parser->getHunkStartLines($hunks);
 286  
 287        $this->assertEqual($expect, $actual, $name);
 288      }
 289    }
 290  
 291  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1