Skip to content

Commit 9e2ea44

Browse files
dgsebastianbergmann
authored andcommitted
Parser: 0 is valid range
1 parent 9f7efbc commit 9e2ea44

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ private function parseFileDiff(Diff $diff, array $lines): void
7777
if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
7878
$chunk = new Chunk(
7979
(int) $match['start'],
80-
isset($match['startrange']) ? max(1, (int) $match['startrange']) : 1,
80+
isset($match['startrange']) ? max(0, (int) $match['startrange']) : 1,
8181
(int) $match['end'],
82-
isset($match['endrange']) ? max(1, (int) $match['endrange']) : 1
82+
isset($match['endrange']) ? max(0, (int) $match['endrange']) : 1
8383
);
8484

8585
$chunks[] = $chunk;

tests/ParserTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,31 @@ public function testParseDiffForMulitpleFiles(): void
193193
$this->assertCount(1, $diff->getChunks());
194194
}
195195

196+
public function testParseWithRange(): void
197+
{
198+
$content = <<<'END'
199+
diff --git a/Test.txt b/Test.txt
200+
index abcdefg..abcdefh 100644
201+
--- a/Test.txt
202+
+++ b/Test.txt
203+
@@ -49,0 +49,0 @@
204+
END;
205+
$diffs = $this->parser->parse($content);
206+
$this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
207+
$this->assertCount(1, $diffs);
208+
209+
$chunks = $diffs[0]->getChunks();
210+
211+
$this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
212+
$this->assertCount(1, $chunks);
213+
214+
$chunk = $chunks[0];
215+
$this->assertSame(49, $chunk->getStart());
216+
$this->assertSame(49, $chunk->getEnd());
217+
$this->assertSame(0, $chunk->getStartRange());
218+
$this->assertSame(0, $chunk->getEndRange());
219+
}
220+
196221
/**
197222
* @psalm-param list<Diff> $expected
198223
*/

0 commit comments

Comments
 (0)