Skip to content

Commit

Permalink
Add tests for paragraph to prepare for refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed Apr 6, 2022
1 parent d2b37d9 commit 5327beb
Showing 1 changed file with 217 additions and 0 deletions.
@@ -0,0 +1,217 @@
<?php

declare(strict_types=1);

namespace unit\Parser\Productions;

use League\Flysystem\FilesystemInterface;
use phpDocumentor\Guides\Nodes\ParagraphNode;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\ParserContext;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule;
use phpDocumentor\Guides\UrlGenerator;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

final class ParagraphRuleTest extends TestCase
{
use ProphecyTrait;

/**
* @uses \phpDocumentor\Guides\RestructuredText\Parser\LinesIterator
*
* @covers \phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule::__construct
* @covers \phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule::apply
* @covers \phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule::applies
* @covers \phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule::isWhiteline
* @dataProvider paragraphProvider
*/
public function testParagraphNodeFromLinesIterator(
string $input,
ParagraphNode $node,
?string $nextLine,
bool $nextLiteral = false
): void {
$iterator = new LinesIterator();
$iterator->load($input);

$parser = $this->prophesize(MarkupLanguageParser::class);
$parser->getEnvironment()->willReturn(
new ParserContext(
'test',
'test',
1,
$this->prophesize(FilesystemInterface::class)->reveal(),
new UrlGenerator()
)
);
$documentParser = $this->prophesize(DocumentParser::class);
$documentParser->getDocumentIterator()->willReturn($iterator);

$rule = new ParagraphRule(
$parser->reveal(),
$documentParser->reveal()
);

self::assertTrue($rule->applies($documentParser->reveal()));
$result = $rule->apply($iterator);
self::assertEquals(
$node,
$result
);

self::assertSame($nextLine, $iterator->getNextLine());
self::assertSame($nextLiteral, $documentParser->nextIndentedBlockShouldBeALiteralBlock);
}

public function paragraphProvider(): array
{
return [
[
'input' => 'some text.',
'output' => new ParagraphNode(new SpanNode('some text.', [])),
'remaining' => null,
],
[
'input' => <<<RST
some multiline
paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline
paragraph
RST,
[]
)
),
'remaining' => null,
],
[
'input' => <<<RST
some multiline
paragraph
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline
paragraph
RST,
[]
)
),
'remaining' => '',
],
[
'input' => <<<RST
some multiline
paragraph
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline
paragraph
RST,
[]
)
),
'remaining' => '',
],
[
'input' => <<<RST
some multiline next paragraph is a literal block
paragraph::
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline next paragraph is a literal block
paragraph:
RST,
[]
)
),
'remaining' => '',
'nextLiteral' => true,
],
[
'input' => <<<RST
some multiline next paragraph is a literal block
paragraph::
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline next paragraph is a literal block
paragraph:
RST,
[]
)
),
'remaining' => '',
'nextLiteral' => true,
],
[
'input' => <<<RST
some multiline next paragraph is a literal block
paragraph: ::
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline next paragraph is a literal block
paragraph:
RST,
[]
)
),
'remaining' => '',
'nextLiteral' => true,
],
[
'input' => <<<RST
some multiline next paragraph is a literal block
paragraph:
::
This is a new paragraph
RST
,
'output' => new ParagraphNode(
new SpanNode(
<<<RST
some multiline next paragraph is a literal block
paragraph:
RST,
[]
)
),
'remaining' => '',
'nextLiteral' => false,
],
];
}
}

0 comments on commit 5327beb

Please sign in to comment.