diff --git a/src/TextEdit.php b/src/TextEdit.php index 867dd19b..cf1b778a 100644 --- a/src/TextEdit.php +++ b/src/TextEdit.php @@ -36,10 +36,14 @@ public static function applyEdits(array $edits, string $text) : string { $prevEditStart = PHP_INT_MAX; for ($i = \count($edits) - 1; $i >= 0; $i--) { $edit = $edits[$i]; - \assert( - $prevEditStart > $edit->start && $prevEditStart > $edit->start + $edit->length, - "Supplied TextEdit[] must not overlap, and be in increasing start position order." - ); + + if ($prevEditStart < $edit->start || $prevEditStart < $edit->start + $edit->length) { + throw new \OutOfBoundsException(sprintf( + 'Supplied TextEdit[] "%s" must not overlap and be in increasing start position order.', + $edit->content + )); + } + if ($edit->start < 0 || $edit->length < 0 || $edit->start + $edit->length > \strlen($text)) { throw new \OutOfBoundsException("Applied TextEdit range out of bounds."); } diff --git a/tests/api/TextEditTest.php b/tests/api/TextEditTest.php index 0a666ce4..b549378a 100644 --- a/tests/api/TextEditTest.php +++ b/tests/api/TextEditTest.php @@ -75,6 +75,27 @@ public function testApplyMultipleEdits() { $this->assertEquals($expected, TextEdit::applyEdits($edits, $content)); } + public function testApplyMultipleEditsAtSameOffset() { + $content = self::INPUT_TEXT; + + $expected = <<< 'PHP' +helloawesomeassertEquals($expected, TextEdit::applyEdits($edits, $content)); + } + + public function testApplyingEmptyTextEditArray() { $content = self::INPUT_TEXT; @@ -88,7 +109,7 @@ public function testOutOfOrderTextEdits() { new TextEdit(0, 10, 10), new TextEdit(0, 4 ,3) ]; - $this->expectException(AssertionError::class); + $this->expectException(OutOfBoundsException::class); TextEdit::applyEdits($edits, $content); } @@ -98,7 +119,7 @@ public function testOverlappingTextEdits() { new TextEdit(0, 4, 10), new TextEdit(0, 10, 10) ]; - $this->expectException(AssertionError::class); + $this->expectException(OutOfBoundsException::class); TextEdit::applyEdits($edits, $content); }