Skip to content

Commit

Permalink
Updating SubDirective to no longer use wantCode()
Browse files Browse the repository at this point in the history
This also fixes a test class that didn't end in Test... and so wasn't being run
  • Loading branch information
weaverryan committed Mar 18, 2021
1 parent 9c4c7c8 commit a45df6e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 29 deletions.
48 changes: 39 additions & 9 deletions lib/Directives/SubDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Doctrine\RST\Directives;

use Doctrine\RST\Nodes\CodeNode;
use Doctrine\RST\Nodes\BlockNode;
use Doctrine\RST\Nodes\Node;
use Doctrine\RST\Nodes\SpanNode;
use Doctrine\RST\Parser;

/**
Expand All @@ -30,10 +31,44 @@ final public function process(
string $data,
array $options
): void {
$subParser = $parser->getSubParser();
if (null === $node) {
/*
* A directive with no content (which is not allowed for sub-directives.
*
* For example:
*
* .. note::
*
* Now more normal text.
*/
throw new \RuntimeException('Content expected, none found.');
}

if ($node instanceof CodeNode) {
$document = $subParser->parseLocal($node->getValue());
/*
* A BlockNode indicates that the content was passed in "raw" and should
* be sub-parsed. This is the main use-case.
*
* However, there are some odd cases where a directive appears
* to "end"... and DocumentParser continues parsing the next
* section, for example as a ParagraphNode or ListNode. When
* that terminates, the original directive is THEN processed
* and called.
*
* A key example is the "class::" directive (see class-directive.rst test case).
* That is where it is legal to have a format like this:
*
* .. class:: special-list
*
* - Test list item 1.
* - Test list item 2.
*
* Notice the 2 list items are NOT indented. This is legal, and ultimately
* those two items would be parsed as a ListNode and THEN passed to
* ClassDirective (which extends SubDirective) for processing.
*/
if ($node instanceof BlockNode) {
$subParser = $parser->getSubParser();
$document = $subParser->parseLocal((string) $node->getValue());;
} else {
$document = $node;
}
Expand Down Expand Up @@ -63,9 +98,4 @@ public function processSub(
): ?Node {
return null;
}

public function wantCode(): bool
{
return true;
}
}
16 changes: 9 additions & 7 deletions tests/BuilderWithErrors/BuilderWithErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
use Doctrine\RST\Builder;
use Doctrine\Tests\RST\BaseBuilderTest;

class BuilderWithErrors extends BaseBuilderTest
class BuilderWithErrorsTest extends BaseBuilderTest
{
protected function configureBuilder(Builder $builder): void
{
$builder->getConfiguration()->abortOnError(false);
$builder->getConfiguration()->silentOnError(true);
}

public function testMalformedTable(): void
public function testNoContentDirectiveError(): void
{
$contents = $this->getFileContents($this->targetFile('index.html'));
self::assertStringContainsString('<table', $contents);
self::assertStringNotContainsString('<tr', $contents);

var_dump($this->builder->getErrorManager()->getErrors());die;
self::assertEquals(
[
'Error while processing "note" directive in "no_content_directive": Content expected, none found.'
],
$this->builder->getErrorManager()->getErrors()
);
}

protected function getFixturesDirectory(): string
Expand Down
4 changes: 4 additions & 0 deletions tests/BuilderWithErrors/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. toctree::
:glob:

no_content_directive
14 changes: 2 additions & 12 deletions tests/BuilderWithErrors/input/no_content_directive.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
Title
=====
Testing wrapper node at end of file

Here is a malformed table.

========== ======================================== ==========================================
Route path If the requested URL is ``/foo`` If the requested URL is ``/foo/``
---------- ----------------------------------- ------------------------------------------
``/foo`` It matches (``200`` status response) It makes a ``301`` redirect to ``/foo``
``/foo/`` It makes a ``301`` redirect to ``/foo/`` It matches (``200`` status response)
========== ======================================== ==========================================

And some text after!
.. note::
4 changes: 3 additions & 1 deletion tests/Functional/tests/wrap/wrap.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<p>Testing wrapper node at end of file</p>
<div class="note"></div>
<div class="note">
<p>I am a note!</p>
</div>
2 changes: 2 additions & 0 deletions tests/Functional/tests/wrap/wrap.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Testing wrapper node at end of file

.. note::

I am a note!

0 comments on commit a45df6e

Please # to comment.