From 620432dead4e3a5daa755036251d52c534d0262b Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 25 Apr 2021 14:15:28 +0200 Subject: [PATCH 1/4] Adding an Error object --- lib/Environment.php | 15 ++++--- lib/Error.php | 70 +++++++++++++++++++++++++++++++ lib/ErrorManager.php | 78 +++++++++++++++++++++++++++++++++-- lib/Nodes/DocumentNode.php | 9 ++-- lib/Nodes/TableNode.php | 2 +- lib/Parser/DocumentParser.php | 35 +++++++--------- tests/ErrorManagerTest.php | 44 ++++++++++++++++++++ 7 files changed, 218 insertions(+), 35 deletions(-) create mode 100644 lib/Error.php diff --git a/lib/Environment.php b/lib/Environment.php index 4bff23d2..203f03cd 100644 --- a/lib/Environment.php +++ b/lib/Environment.php @@ -473,11 +473,17 @@ public function getTitleLetters(): array return $this->titleLetters; } + /** + * @deprecated use $this->getErrorManager()->addError() instead. + */ public function addError(string $message, ?Throwable $throwable = null): void { $this->errorManager->error($message, $throwable); } + /** + * @deprecated use $this->getErrorManager()->addWarning() instead. + */ public function addWarning(string $message): void { $this->errorManager->warning($message); @@ -508,10 +514,9 @@ public static function slugify(string $text): string private function addMissingReferenceSectionError(string $section): void { - $this->errorManager->error(sprintf( - 'Unknown reference section "%s"%s', - $section, - $this->getCurrentFileName() !== '' ? sprintf(' in "%s" ', $this->getCurrentFileName()) : '' - )); + $this->errorManager->addError( + sprintf('Unknown reference section "%s"', $section), + $this->getCurrentFileName() + ); } } diff --git a/lib/Error.php b/lib/Error.php new file mode 100644 index 00000000..667edaf1 --- /dev/null +++ b/lib/Error.php @@ -0,0 +1,70 @@ +message = $message; + $this->file = $file; + $this->line = $line; + $this->throwable = $throwable; + } + + public function asString(): string + { + $output = $this->message; + if ($this->getFile() !== null) { + $output .= sprintf(' in "%s"', $this->file); + + if ($this->line !== null) { + $output .= sprintf(' at line "%d"', $this->line); + } + } + + return $output; + } + + public function getMessage(): string + { + return $this->message; + } + + public function getFile(): ?string + { + if ($this->file === '') { + return null; + } + + return $this->file; + } + + public function getLine(): ?int + { + return $this->line; + } + + public function getThrowable(): ?Throwable + { + return $this->throwable; + } +} diff --git a/lib/ErrorManager.php b/lib/ErrorManager.php index 30779ca0..ee52b6ee 100644 --- a/lib/ErrorManager.php +++ b/lib/ErrorManager.php @@ -7,12 +7,14 @@ use Exception; use Throwable; +use function sprintf; + class ErrorManager { /** @var Configuration */ private $configuration; - /** @var string[] */ + /** @var list */ private $errors = []; public function __construct(Configuration $configuration) @@ -20,9 +22,69 @@ public function __construct(Configuration $configuration) $this->configuration = $configuration; } + public function addError(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void + { + $this->errors[] = $error = new Error($message, $file, $line, $throwable); + + if (! $this->configuration->isSilentOnError()) { + if ($this->configuration->getOutputFormat() === Configuration::OUTPUT_FORMAT_GITHUB) { + $file = $error->getFile(); + echo sprintf( + '::error %s%s::%s', + $file !== null ? 'file=' . $file : '', + $file !== null && $error->getLine() !== null ? ',linefile=' . $error->getLine() : '', + $error->getMessage() + ); + } else { + echo '⚠️ ' . $error->asString() . "\n"; + } + } + + if ($this->configuration->isAbortOnError()) { + throw new Exception($error->asString(), 0, $error->getThrowable()); + } + } + + public function addWarning(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void + { + if ($this->configuration->isWarningsAsError()) { + $this->addError($message, $file, $line, $throwable); + + return; + } + + if ($this->configuration->isSilentOnError()) { + return; + } + + $error = new Error($message, $file, $line, $throwable); + if ($this->configuration->getOutputFormat() === Configuration::OUTPUT_FORMAT_GITHUB) { + $file = $error->getFile(); + echo sprintf( + '::warning %s%s::%s', + $file !== null ? 'file=' . $file : '', + $file !== null && $error->getLine() !== null ? ',linefile=' . $error->getLine() : '', + $error->getMessage() + ); + } else { + echo $error->asString() . "\n"; + } + } + + /** + * @return list + */ + public function getAllErrors(): array + { + return $this->errors; + } + + /** + * @deprecated Use addError() instead + */ public function error(string $message, ?Throwable $throwable = null): void { - $this->errors[] = $message; + $this->errors[] = new Error($message, null, null, $throwable); if (! $this->configuration->isSilentOnError()) { echo '/!\\ ' . $message . "\n"; @@ -33,6 +95,9 @@ public function error(string $message, ?Throwable $throwable = null): void } } + /** + * @deprecated Use addWarning() instead + */ public function warning(string $message): void { if ($this->configuration->isWarningsAsError()) { @@ -49,10 +114,17 @@ public function warning(string $message): void } /** + * @deprecated use getAllErrors() instead + * * @return string[] */ public function getErrors(): array { - return $this->errors; + $outputs = []; + foreach ($this->errors as $error) { + $outputs[] = $error->asString(); + } + + return $outputs; } } diff --git a/lib/Nodes/DocumentNode.php b/lib/Nodes/DocumentNode.php index 11aaea22..0e55519a 100644 --- a/lib/Nodes/DocumentNode.php +++ b/lib/Nodes/DocumentNode.php @@ -236,11 +236,10 @@ private function postRenderValidate(): void $currentFileName = $this->environment->getCurrentFileName(); foreach ($this->environment->getInvalidLinks() as $invalidLink) { - $this->errorManager->error(sprintf( - 'Found invalid reference "%s"%s', - $invalidLink->getName(), - $currentFileName !== '' ? sprintf(' in file "%s"', $currentFileName) : '' - )); + $this->errorManager->addError( + sprintf('Found invalid reference "%s"', $invalidLink->getName()), + $currentFileName + ); } } } diff --git a/lib/Nodes/TableNode.php b/lib/Nodes/TableNode.php index a8b72820..b2deb477 100644 --- a/lib/Nodes/TableNode.php +++ b/lib/Nodes/TableNode.php @@ -153,7 +153,7 @@ public function finalize(Parser $parser): void if (count($this->errors) > 0) { $parser->getEnvironment() ->getErrorManager() - ->error(sprintf("%s\nin file %s\n\n%s", $this->errors[0], $parser->getFilename(), $tableAsString)); + ->addError(sprintf("%s\n\n%s", $this->errors[0], $tableAsString), $parser->getFilename()); $this->data = []; $this->headers = []; diff --git a/lib/Parser/DocumentParser.php b/lib/Parser/DocumentParser.php index fc7deba7..29ab94b9 100644 --- a/lib/Parser/DocumentParser.php +++ b/lib/Parser/DocumentParser.php @@ -318,11 +318,11 @@ private function parseLine(string $line): bool case State::LIST: if (! $this->lineChecker->isListLine($line, $this->listMarker, $this->listOffset) && ! $this->lineChecker->isBlockLine($line, max(1, $this->listOffset))) { if (trim($this->lines->getPreviousLine()) !== '') { - $this->environment->addWarning(sprintf( - 'Warning%s%s: List ends without a blank line; unexpected unindent.', - $this->environment->getCurrentFileName() !== '' ? sprintf(' in "%s"', $this->environment->getCurrentFileName()) : '', - $this->currentLineNumber !== null ? ' around line ' . ($this->currentLineNumber - 1) : '' - )); + $this->environment->getErrorManager()->addWarning( + 'List ends without a blank line; unexpected unindent', + $this->environment->getCurrentFileName(), + $this->currentLineNumber !== null ? $this->currentLineNumber - 1 : null + ); } $this->flush(); @@ -455,7 +455,7 @@ private function parseLine(string $line): bool break; default: - $this->environment->addError('Parser ended in an unexcepted state'); + $this->environment->getErrorManager()->addError('Parser ended in an unexcepted state'); } return true; @@ -589,15 +589,12 @@ private function flush(): void $this->directive->getOptions() ); } catch (Throwable $e) { - $message = sprintf( - 'Error while processing "%s" directive%s%s: %s', - $currentDirective->getName(), - $this->environment->getCurrentFileName() !== '' ? sprintf(' in "%s"', $this->environment->getCurrentFileName()) : '', - $this->currentLineNumber !== null ? ' around line ' . $this->currentLineNumber : '', - $e->getMessage() + $this->environment->getErrorManager()->addError( + sprintf('Error while processing "%s" directive: %s', $currentDirective->getName(), $e->getMessage()), + $this->environment->getCurrentFileName(), + $this->currentLineNumber ?? null, + $e ); - - $this->environment->addError($message, $e); } } @@ -655,15 +652,11 @@ private function initDirective(string $line): bool } if (! isset($this->directives[$parserDirective->getName()])) { - $message = sprintf( - 'Unknown directive: "%s" %sfor line "%s"', - $parserDirective->getName(), - $this->environment->getCurrentFileName() !== '' ? sprintf('in "%s" ', $this->environment->getCurrentFileName()) : '', - $line + $this->environment->getErrorManager()->addError( + sprintf('Unknown directive "%s": %s', $parserDirective->getName(), $line), + $this->environment->getCurrentFileName() ); - $this->environment->addError($message); - return false; } diff --git a/tests/ErrorManagerTest.php b/tests/ErrorManagerTest.php index 709c6d3c..fccf52c9 100644 --- a/tests/ErrorManagerTest.php +++ b/tests/ErrorManagerTest.php @@ -13,6 +13,9 @@ class ErrorManagerTest extends TestCase { + /** + * @group legacy + */ public function testGetErrors(): void { $configuration = $this->createMock(Configuration::class); @@ -27,4 +30,45 @@ public function testGetErrors(): void ob_end_clean(); self::assertSame(['ERROR FOO', 'ERROR BAR'], $errorManager->getErrors()); } + + /** + * Make sure the method is unchanged when addError() is used. + * + * @group legacy + */ + public function testGetErrorsWithNewErrorObject(): void + { + $configuration = $this->createMock(Configuration::class); + $configuration->expects(self::atLeastOnce()) + ->method('isAbortOnError') + ->willReturn(false); + $configuration->expects(self::atLeastOnce()) + ->method('isSilentOnError') + ->willReturn(true); + + $errorManager = new ErrorManager($configuration); + $errorManager->addError('ERROR FOO'); + $errorManager->addError('ERROR BAR'); + + self::assertSame(['ERROR FOO', 'ERROR BAR'], $errorManager->getErrors()); + } + + public function testGetAllErrors(): void + { + $configuration = $this->createMock(Configuration::class); + $configuration->expects(self::atLeastOnce()) + ->method('isAbortOnError') + ->willReturn(false); + $configuration->expects(self::atLeastOnce()) + ->method('isSilentOnError') + ->willReturn(true); + + $errorManager = new ErrorManager($configuration); + $errorManager->addError('ERROR FOO'); + $errorManager->addError('ERROR BAR'); + + $errors = $errorManager->getAllErrors(); + self::assertSame('ERROR FOO', $errors[0]->asString()); + self::assertSame('ERROR BAR', $errors[1]->asString()); + } } From ab4af20ebccb715eccfdaa6300515c86e9bb0c73 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 13 May 2021 15:35:35 +0200 Subject: [PATCH 2/4] Remove deprecation layer --- lib/Environment.php | 19 +----------- lib/ErrorManager.php | 57 +++-------------------------------- lib/Nodes/DocumentNode.php | 2 +- lib/Nodes/TableNode.php | 2 +- lib/Parser/DocumentParser.php | 8 ++--- tests/ErrorManagerTest.php | 49 ++---------------------------- 6 files changed, 14 insertions(+), 123 deletions(-) diff --git a/lib/Environment.php b/lib/Environment.php index 203f03cd..3eb4b048 100644 --- a/lib/Environment.php +++ b/lib/Environment.php @@ -11,7 +11,6 @@ use Doctrine\RST\References\ResolvedReference; use Doctrine\RST\Templates\TemplateRenderer; use InvalidArgumentException; -use Throwable; use function array_shift; use function dirname; @@ -473,22 +472,6 @@ public function getTitleLetters(): array return $this->titleLetters; } - /** - * @deprecated use $this->getErrorManager()->addError() instead. - */ - public function addError(string $message, ?Throwable $throwable = null): void - { - $this->errorManager->error($message, $throwable); - } - - /** - * @deprecated use $this->getErrorManager()->addWarning() instead. - */ - public function addWarning(string $message): void - { - $this->errorManager->warning($message); - } - public static function slugify(string $text): string { // replace non letter or digits by - @@ -514,7 +497,7 @@ public static function slugify(string $text): string private function addMissingReferenceSectionError(string $section): void { - $this->errorManager->addError( + $this->errorManager->error( sprintf('Unknown reference section "%s"', $section), $this->getCurrentFileName() ); diff --git a/lib/ErrorManager.php b/lib/ErrorManager.php index ee52b6ee..8d0ef582 100644 --- a/lib/ErrorManager.php +++ b/lib/ErrorManager.php @@ -22,7 +22,7 @@ public function __construct(Configuration $configuration) $this->configuration = $configuration; } - public function addError(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void + public function error(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void { $this->errors[] = $error = new Error($message, $file, $line, $throwable); @@ -45,10 +45,10 @@ public function addError(string $message, ?string $file = null, ?int $line = nul } } - public function addWarning(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void + public function warning(string $message, ?string $file = null, ?int $line = null, ?Throwable $throwable = null): void { if ($this->configuration->isWarningsAsError()) { - $this->addError($message, $file, $line, $throwable); + $this->error($message, $file, $line, $throwable); return; } @@ -74,57 +74,8 @@ public function addWarning(string $message, ?string $file = null, ?int $line = n /** * @return list */ - public function getAllErrors(): array - { - return $this->errors; - } - - /** - * @deprecated Use addError() instead - */ - public function error(string $message, ?Throwable $throwable = null): void - { - $this->errors[] = new Error($message, null, null, $throwable); - - if (! $this->configuration->isSilentOnError()) { - echo '/!\\ ' . $message . "\n"; - } - - if ($this->configuration->isAbortOnError()) { - throw new Exception($message, 0, $throwable); - } - } - - /** - * @deprecated Use addWarning() instead - */ - public function warning(string $message): void - { - if ($this->configuration->isWarningsAsError()) { - $this->error($message); - - return; - } - - if ($this->configuration->isSilentOnError()) { - return; - } - - echo $message . "\n"; - } - - /** - * @deprecated use getAllErrors() instead - * - * @return string[] - */ public function getErrors(): array { - $outputs = []; - foreach ($this->errors as $error) { - $outputs[] = $error->asString(); - } - - return $outputs; + return $this->errors; } } diff --git a/lib/Nodes/DocumentNode.php b/lib/Nodes/DocumentNode.php index 0e55519a..e818de5d 100644 --- a/lib/Nodes/DocumentNode.php +++ b/lib/Nodes/DocumentNode.php @@ -236,7 +236,7 @@ private function postRenderValidate(): void $currentFileName = $this->environment->getCurrentFileName(); foreach ($this->environment->getInvalidLinks() as $invalidLink) { - $this->errorManager->addError( + $this->errorManager->error( sprintf('Found invalid reference "%s"', $invalidLink->getName()), $currentFileName ); diff --git a/lib/Nodes/TableNode.php b/lib/Nodes/TableNode.php index b2deb477..184d4929 100644 --- a/lib/Nodes/TableNode.php +++ b/lib/Nodes/TableNode.php @@ -153,7 +153,7 @@ public function finalize(Parser $parser): void if (count($this->errors) > 0) { $parser->getEnvironment() ->getErrorManager() - ->addError(sprintf("%s\n\n%s", $this->errors[0], $tableAsString), $parser->getFilename()); + ->error(sprintf("%s\n\n%s", $this->errors[0], $tableAsString), $parser->getFilename()); $this->data = []; $this->headers = []; diff --git a/lib/Parser/DocumentParser.php b/lib/Parser/DocumentParser.php index 29ab94b9..9f524553 100644 --- a/lib/Parser/DocumentParser.php +++ b/lib/Parser/DocumentParser.php @@ -318,7 +318,7 @@ private function parseLine(string $line): bool case State::LIST: if (! $this->lineChecker->isListLine($line, $this->listMarker, $this->listOffset) && ! $this->lineChecker->isBlockLine($line, max(1, $this->listOffset))) { if (trim($this->lines->getPreviousLine()) !== '') { - $this->environment->getErrorManager()->addWarning( + $this->environment->getErrorManager()->warning( 'List ends without a blank line; unexpected unindent', $this->environment->getCurrentFileName(), $this->currentLineNumber !== null ? $this->currentLineNumber - 1 : null @@ -455,7 +455,7 @@ private function parseLine(string $line): bool break; default: - $this->environment->getErrorManager()->addError('Parser ended in an unexcepted state'); + $this->environment->getErrorManager()->error('Parser ended in an unexcepted state'); } return true; @@ -589,7 +589,7 @@ private function flush(): void $this->directive->getOptions() ); } catch (Throwable $e) { - $this->environment->getErrorManager()->addError( + $this->environment->getErrorManager()->error( sprintf('Error while processing "%s" directive: %s', $currentDirective->getName(), $e->getMessage()), $this->environment->getCurrentFileName(), $this->currentLineNumber ?? null, @@ -652,7 +652,7 @@ private function initDirective(string $line): bool } if (! isset($this->directives[$parserDirective->getName()])) { - $this->environment->getErrorManager()->addError( + $this->environment->getErrorManager()->error( sprintf('Unknown directive "%s": %s', $parserDirective->getName(), $line), $this->environment->getCurrentFileName() ); diff --git a/tests/ErrorManagerTest.php b/tests/ErrorManagerTest.php index fccf52c9..880bf006 100644 --- a/tests/ErrorManagerTest.php +++ b/tests/ErrorManagerTest.php @@ -8,35 +8,9 @@ use Doctrine\RST\ErrorManager; use PHPUnit\Framework\TestCase; -use function ob_end_clean; -use function ob_start; - class ErrorManagerTest extends TestCase { - /** - * @group legacy - */ public function testGetErrors(): void - { - $configuration = $this->createMock(Configuration::class); - $configuration->expects(self::atLeastOnce()) - ->method('isAbortOnError') - ->willReturn(false); - - $errorManager = new ErrorManager($configuration); - ob_start(); - $errorManager->error('ERROR FOO'); - $errorManager->error('ERROR BAR'); - ob_end_clean(); - self::assertSame(['ERROR FOO', 'ERROR BAR'], $errorManager->getErrors()); - } - - /** - * Make sure the method is unchanged when addError() is used. - * - * @group legacy - */ - public function testGetErrorsWithNewErrorObject(): void { $configuration = $this->createMock(Configuration::class); $configuration->expects(self::atLeastOnce()) @@ -47,27 +21,10 @@ public function testGetErrorsWithNewErrorObject(): void ->willReturn(true); $errorManager = new ErrorManager($configuration); - $errorManager->addError('ERROR FOO'); - $errorManager->addError('ERROR BAR'); - - self::assertSame(['ERROR FOO', 'ERROR BAR'], $errorManager->getErrors()); - } - - public function testGetAllErrors(): void - { - $configuration = $this->createMock(Configuration::class); - $configuration->expects(self::atLeastOnce()) - ->method('isAbortOnError') - ->willReturn(false); - $configuration->expects(self::atLeastOnce()) - ->method('isSilentOnError') - ->willReturn(true); - - $errorManager = new ErrorManager($configuration); - $errorManager->addError('ERROR FOO'); - $errorManager->addError('ERROR BAR'); + $errorManager->error('ERROR FOO'); + $errorManager->error('ERROR BAR'); - $errors = $errorManager->getAllErrors(); + $errors = $errorManager->getErrors(); self::assertSame('ERROR FOO', $errors[0]->asString()); self::assertSame('ERROR BAR', $errors[1]->asString()); } From a5d17025373293297449795f82bb4ba776f3faf7 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 13 May 2021 16:02:17 +0200 Subject: [PATCH 3/4] Fixed tests --- lib/Error.php | 4 ++-- lib/Parser/DocumentParser.php | 2 +- tests/BuilderWithErrors/BuilderWithErrorsTest.php | 4 ++-- tests/EnvironmentTest.php | 2 +- .../tests/main-directive/main-directive.html | 2 +- .../tests/pretty-table-error1/pretty-table-error1.html | 3 +-- .../tests/simple-table-error1/simple-table-error1.html | 3 +-- .../tests/unknown-directive/unknown-directive.html | 2 +- tests/Parser/DocumentParserTest.php | 10 ++++++++-- 9 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/Error.php b/lib/Error.php index 667edaf1..8c48cb68 100644 --- a/lib/Error.php +++ b/lib/Error.php @@ -34,10 +34,10 @@ public function asString(): string { $output = $this->message; if ($this->getFile() !== null) { - $output .= sprintf(' in "%s"', $this->file); + $output .= sprintf(' in file "%s"', $this->file); if ($this->line !== null) { - $output .= sprintf(' at line "%d"', $this->line); + $output .= sprintf(' at line %d', $this->line); } } diff --git a/lib/Parser/DocumentParser.php b/lib/Parser/DocumentParser.php index 9f524553..196c6a5a 100644 --- a/lib/Parser/DocumentParser.php +++ b/lib/Parser/DocumentParser.php @@ -590,7 +590,7 @@ private function flush(): void ); } catch (Throwable $e) { $this->environment->getErrorManager()->error( - sprintf('Error while processing "%s" directive: %s', $currentDirective->getName(), $e->getMessage()), + sprintf('Error while processing "%s" directive: "%s"', $currentDirective->getName(), $e->getMessage()), $this->environment->getCurrentFileName(), $this->currentLineNumber ?? null, $e diff --git a/tests/BuilderWithErrors/BuilderWithErrorsTest.php b/tests/BuilderWithErrors/BuilderWithErrorsTest.php index 903ee203..4b234a83 100644 --- a/tests/BuilderWithErrors/BuilderWithErrorsTest.php +++ b/tests/BuilderWithErrors/BuilderWithErrorsTest.php @@ -32,8 +32,8 @@ public function testNoContentDirectiveError(): void , $bodyHtml); self::assertEquals( - ['Error while processing "note" directive in "no_content_directive" around line 6: Content expected, none found.'], - $this->builder->getErrorManager()->getErrors() + 'Error while processing "note" directive: "Content expected, none found." in file "no_content_directive" at line 6', + $this->builder->getErrorManager()->getErrors()[0]->asString() ); } diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index 7239e28d..3e7d40b5 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -82,7 +82,7 @@ public function getMissingSectionTests(): iterable ]; yield 'with_current_filename' => [ - 'Unknown reference section "doc" in "current_doc_filename"', + 'Unknown reference section "doc" in file "current_doc_filename"', 'current_doc_filename', ]; } diff --git a/tests/Functional/tests/main-directive/main-directive.html b/tests/Functional/tests/main-directive/main-directive.html index d61914c4..667019e6 100644 --- a/tests/Functional/tests/main-directive/main-directive.html +++ b/tests/Functional/tests/main-directive/main-directive.html @@ -1,2 +1,2 @@ Exception: Exception -Unknown directive: "latex-main" for line ".. latex-main::" +Unknown directive "latex-main": .. latex-main:: diff --git a/tests/Functional/tests/pretty-table-error1/pretty-table-error1.html b/tests/Functional/tests/pretty-table-error1/pretty-table-error1.html index 2c6aff39..ed839ec5 100644 --- a/tests/Functional/tests/pretty-table-error1/pretty-table-error1.html +++ b/tests/Functional/tests/pretty-table-error1/pretty-table-error1.html @@ -4,7 +4,6 @@ forgot a blank line does not appear to be a complete table row -in file (unknown) +-----------+----------------+----------------------------+ | Type | Options | Description | @@ -13,4 +12,4 @@ +-----------+----------------+----------------------------+ | currency | currency (m) | A currency string | +-----------+----------------+----------------------------+ -forgot a blank line +forgot a blank line in file "(unknown)" diff --git a/tests/Functional/tests/simple-table-error1/simple-table-error1.html b/tests/Functional/tests/simple-table-error1/simple-table-error1.html index 103cab39..90c289a0 100644 --- a/tests/Functional/tests/simple-table-error1/simple-table-error1.html +++ b/tests/Functional/tests/simple-table-error1/simple-table-error1.html @@ -1,9 +1,8 @@ Exception: Exception Malformed table: content "l" appears in the "gap" on row "Second row Other colllOther col" -in file (unknown) =========== ========== ========= First col Second col Third col Second row Other colllOther col Third row Other col Last col -=========== ========== ========= +=========== ========== ========= in file "(unknown)" \ No newline at end of file diff --git a/tests/Functional/tests/unknown-directive/unknown-directive.html b/tests/Functional/tests/unknown-directive/unknown-directive.html index 4dab64b8..2b55cfbd 100644 --- a/tests/Functional/tests/unknown-directive/unknown-directive.html +++ b/tests/Functional/tests/unknown-directive/unknown-directive.html @@ -1,2 +1,2 @@ Exception: Exception -Unknown directive: "unknown-directive" for line ".. unknown-directive::" +Unknown directive "unknown-directive": .. unknown-directive:: diff --git a/tests/Parser/DocumentParserTest.php b/tests/Parser/DocumentParserTest.php index ebfff5b0..b3a45cbd 100644 --- a/tests/Parser/DocumentParserTest.php +++ b/tests/Parser/DocumentParserTest.php @@ -7,6 +7,7 @@ use Doctrine\Common\EventManager; use Doctrine\RST\Directives\Directive; use Doctrine\RST\Environment; +use Doctrine\RST\ErrorManager; use Doctrine\RST\NodeFactory\NodeFactory; use Doctrine\RST\Parser; use Doctrine\RST\Parser\DocumentParser; @@ -22,6 +23,7 @@ public function testErrorWhenDirectiveThrowsException(): void $nodeFactory = $this->createMock(NodeFactory::class); $eventManager = $this->createMock(EventManager::class); $codeBlockDirective = $this->createMock(Directive::class); + $errorManager = $this->createMock(ErrorManager::class); $docParser = new DocumentParser( $parser, @@ -41,8 +43,12 @@ public function testErrorWhenDirectiveThrowsException(): void ->willReturn('code-block-name'); $environment->expects(self::once()) - ->method('addError') - ->with('Error while processing "code-block-name" directive: Invalid something something!'); + ->method('getErrorManager') + ->willReturn($errorManager); + + $errorManager->expects(self::once()) + ->method('error') + ->with('Error while processing "code-block-name" directive: "Invalid something something!"'); $docParser->parse('.. code-block:: php'); } From 241b83af2c76a492569e049fdc4038ae934cd92d Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 13 May 2021 16:10:00 +0200 Subject: [PATCH 4/4] minors --- lib/Parser/DocumentParser.php | 2 +- tests/Functional/tests/main-directive/main-directive.html | 2 +- tests/Functional/tests/unknown-directive/unknown-directive.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Parser/DocumentParser.php b/lib/Parser/DocumentParser.php index 196c6a5a..78956fa7 100644 --- a/lib/Parser/DocumentParser.php +++ b/lib/Parser/DocumentParser.php @@ -653,7 +653,7 @@ private function initDirective(string $line): bool if (! isset($this->directives[$parserDirective->getName()])) { $this->environment->getErrorManager()->error( - sprintf('Unknown directive "%s": %s', $parserDirective->getName(), $line), + sprintf('Unknown directive "%s" for line "%s"', $parserDirective->getName(), $line), $this->environment->getCurrentFileName() ); diff --git a/tests/Functional/tests/main-directive/main-directive.html b/tests/Functional/tests/main-directive/main-directive.html index 667019e6..af779b57 100644 --- a/tests/Functional/tests/main-directive/main-directive.html +++ b/tests/Functional/tests/main-directive/main-directive.html @@ -1,2 +1,2 @@ Exception: Exception -Unknown directive "latex-main": .. latex-main:: +Unknown directive "latex-main" for line ".. latex-main::" diff --git a/tests/Functional/tests/unknown-directive/unknown-directive.html b/tests/Functional/tests/unknown-directive/unknown-directive.html index 2b55cfbd..3f29e8c4 100644 --- a/tests/Functional/tests/unknown-directive/unknown-directive.html +++ b/tests/Functional/tests/unknown-directive/unknown-directive.html @@ -1,2 +1,2 @@ Exception: Exception -Unknown directive "unknown-directive": .. unknown-directive:: +Unknown directive "unknown-directive" for line ".. unknown-directive::"