ParserTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers SebastianBergmann\Diff\Parser
  14. *
  15. * @uses SebastianBergmann\Diff\Chunk
  16. * @uses SebastianBergmann\Diff\Diff
  17. * @uses SebastianBergmann\Diff\Line
  18. */
  19. class ParserTest extends TestCase
  20. {
  21. /**
  22. * @var Parser
  23. */
  24. private $parser;
  25. protected function setUp()
  26. {
  27. $this->parser = new Parser;
  28. }
  29. public function testParse()
  30. {
  31. $content = \file_get_contents(__DIR__ . '/fixtures/patch.txt');
  32. $diffs = $this->parser->parse($content);
  33. $this->assertInternalType('array', $diffs);
  34. $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Diff', $diffs);
  35. $this->assertCount(1, $diffs);
  36. $chunks = $diffs[0]->getChunks();
  37. $this->assertInternalType('array', $chunks);
  38. $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Chunk', $chunks);
  39. $this->assertCount(1, $chunks);
  40. $this->assertEquals(20, $chunks[0]->getStart());
  41. $this->assertCount(4, $chunks[0]->getLines());
  42. }
  43. public function testParseWithMultipleChunks()
  44. {
  45. $content = \file_get_contents(__DIR__ . '/fixtures/patch2.txt');
  46. $diffs = $this->parser->parse($content);
  47. $this->assertCount(1, $diffs);
  48. $chunks = $diffs[0]->getChunks();
  49. $this->assertCount(3, $chunks);
  50. $this->assertEquals(20, $chunks[0]->getStart());
  51. $this->assertEquals(320, $chunks[1]->getStart());
  52. $this->assertEquals(600, $chunks[2]->getStart());
  53. $this->assertCount(5, $chunks[0]->getLines());
  54. $this->assertCount(5, $chunks[1]->getLines());
  55. $this->assertCount(4, $chunks[2]->getLines());
  56. }
  57. public function testParseWithRemovedLines()
  58. {
  59. $content = <<<A
  60. diff --git a/Test.txt b/Test.txt
  61. index abcdefg..abcdefh 100644
  62. --- a/Test.txt
  63. +++ b/Test.txt
  64. @@ -49,9 +49,8 @@
  65. A
  66. -B
  67. A;
  68. $diffs = $this->parser->parse($content);
  69. $this->assertInternalType('array', $diffs);
  70. $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Diff', $diffs);
  71. $this->assertCount(1, $diffs);
  72. $chunks = $diffs[0]->getChunks();
  73. $this->assertInternalType('array', $chunks);
  74. $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Chunk', $chunks);
  75. $this->assertCount(1, $chunks);
  76. $chunk = $chunks[0];
  77. $this->assertSame(49, $chunk->getStart());
  78. $this->assertSame(49, $chunk->getEnd());
  79. $this->assertSame(9, $chunk->getStartRange());
  80. $this->assertSame(8, $chunk->getEndRange());
  81. $lines = $chunk->getLines();
  82. $this->assertInternalType('array', $lines);
  83. $this->assertContainsOnlyInstancesOf('SebastianBergmann\Diff\Line', $lines);
  84. $this->assertCount(2, $lines);
  85. /** @var Line $line */
  86. $line = $lines[0];
  87. $this->assertSame('A', $line->getContent());
  88. $this->assertSame(Line::UNCHANGED, $line->getType());
  89. $line = $lines[1];
  90. $this->assertSame('B', $line->getContent());
  91. $this->assertSame(Line::REMOVED, $line->getType());
  92. }
  93. public function testParseDiffForMulitpleFiles()
  94. {
  95. $content = <<<A
  96. diff --git a/Test.txt b/Test.txt
  97. index abcdefg..abcdefh 100644
  98. --- a/Test.txt
  99. +++ b/Test.txt
  100. @@ -1,3 +1,2 @@
  101. A
  102. -B
  103. diff --git a/Test123.txt b/Test123.txt
  104. index abcdefg..abcdefh 100644
  105. --- a/Test2.txt
  106. +++ b/Test2.txt
  107. @@ -1,2 +1,3 @@
  108. A
  109. +B
  110. A;
  111. $diffs = $this->parser->parse($content);
  112. $this->assertCount(2, $diffs);
  113. /** @var Diff $diff */
  114. $diff = $diffs[0];
  115. $this->assertSame('a/Test.txt', $diff->getFrom());
  116. $this->assertSame('b/Test.txt', $diff->getTo());
  117. $this->assertCount(1, $diff->getChunks());
  118. $diff = $diffs[1];
  119. $this->assertSame('a/Test2.txt', $diff->getFrom());
  120. $this->assertSame('b/Test2.txt', $diff->getTo());
  121. $this->assertCount(1, $diff->getChunks());
  122. }
  123. }