Skip to content

Commit

Permalink
Playing around with a new way to organize functional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Nov 21, 2018
1 parent f1f97e1 commit cb831fb
Show file tree
Hide file tree
Showing 166 changed files with 1,123 additions and 1,372 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"phpstan/phpstan-deprecation-rules": "^0.10",
"phpstan/phpstan-phpunit": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^7.0",
"gajus/dindent": "^2.0.2"
},
"autoload": {
"psr-4": {
Expand Down
47 changes: 46 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\RST\Functional;

use Doctrine\RST\Configuration;
use Doctrine\RST\Formats\Format;
use Doctrine\RST\Kernel;
use Doctrine\RST\Parser;
use Gajus\Dindent\Indenter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
use function array_map;
use function basename;
use function explode;
use function file_exists;
use function file_get_contents;
use function implode;
use function in_array;
use function rtrim;
use function str_replace;
use function strpos;
use function trim;

class FunctionalTest extends TestCase
{
private const RENDER_DOCUMENT_FILES = ['main-directive'];

/**
* @dataProvider getFunctionalTests
*/
public function testFunctional(
string $file,
Parser $parser,
string $renderMethod,
string $format,
string $rst,
string $expected
) : void {
$expectedLines = explode("\n", $expected);
$firstLine = $expectedLines[0];

if (strpos($firstLine, 'Exception:') === 0) {
$exceptionClass = str_replace('Exception: ', '', $firstLine);
$this->expectException($exceptionClass);

$expectedExceptionMessage = $expectedLines;
unset($expectedExceptionMessage[0]);
$expectedExceptionMessage = implode("\n", $expectedExceptionMessage);

$this->expectExceptionMessage($expectedExceptionMessage);
}

$document = $parser->parse($rst);

$rendered = $document->$renderMethod();

if ($format === Format::HTML) {
$indenter = new Indenter();
$rendered = $indenter->indent($rendered);
}

self::assertSame(
$this->trimTrailingWhitespace($expected),
$this->trimTrailingWhitespace($rendered)
);
}

/**
* @return mixed[]
*/
public function getFunctionalTests() : array
{
$finder = new Finder();
$finder
->files()
->in(__DIR__ . '/tests')
->name('*.rst');

$tests = [];

foreach ($finder as $file) {
$rst = $file->getContents();
$filename = $file->getFilename();
$basename = basename($filename, '.rst');

$dir = $file->getPathInfo();

$formats = [Format::HTML, Format::LATEX];

foreach ($formats as $format) {
$formatPath = $dir . '/' . $basename . '.' . $format;

if (! file_exists($formatPath)) {
continue;
}

$expected = file_get_contents($formatPath);

if ($expected === false) {
continue;
}

$configuration = new Configuration();
$configuration->setFileExtension($format);

$kernel = new Kernel($configuration);
$parser = new Parser($kernel);

$environment = $parser->getEnvironment();
$environment->setCurrentDirectory(__DIR__ . '/tests/' . $basename);

$renderMethod = in_array($basename, self::RENDER_DOCUMENT_FILES, true)
? 'renderDocument'
: 'render';

$tests[] = [$basename, $parser, $renderMethod, $format, $rst, trim($expected)];
}
}

return $tests;
}

private function trimTrailingWhitespace(string $string) : string
{
$lines = explode("\n", $string);

$lines = array_map(static function (string $line) {
return rtrim($line);
}, $lines);

return trim(implode("\n", $lines));
}
}
1 change: 1 addition & 0 deletions tests/Functional/tests/anchor-failure/anchor-failure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>@Anchor Section</p>
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/Functional/tests/anchor/anchor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="section" id="anchors">
<h1>
Anchors
</h1>
<a id="lists"></a>

<p><a href="#lists">go to lists</a></p>
</div>
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/Functional/tests/anchor/anchor.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\chapter{Anchors}
\label{lists}
\ref{#lists}
1 change: 1 addition & 0 deletions tests/Functional/tests/anonymous/anonymous.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>I love <a href="http://www.github.com/">GitHub</a></p>
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

I love GitHub__

.. __: http://www.github.com/

1 change: 1 addition & 0 deletions tests/Functional/tests/anonymous/anonymous.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I love \href{http://www.github.com/}{GitHub}
1 change: 1 addition & 0 deletions tests/Functional/tests/bom/bom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
35 changes: 35 additions & 0 deletions tests/Functional/tests/class-directive/class-directive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p class="special-paragraph1">Test special-paragraph1 1.</p>
<p>Test special-paragraph1 2.</p>
<p class="special-paragraph2">Test special-paragraph2 1.</p>
<p class="special-paragraph2">Test special-paragraph2 2.</p>
<div class="note">
<p class="special-paragraph3">Test</p>
</div>
<ul class="special-list">
<li class="dash">Test list item 1.</li>
<li class="dash">Test list item 2.</li>
</ul>
<p class="rot-gelb-blau grun-2008">Weird class names.</p>
<p class="level1">Level 1</p>
<blockquote class="level1">
<p class="level2">Level2 1</p>
<p class="level2">Level2 2</p>
</blockquote>
<dl class="special-definition-list">
<dt>term 1</dt>
<dd>Definition 1 </dd>
</dl>
<table class="special-table">
<thead>
<tr>
<th>First col</th>
<th>Second col</th>
</tr>
</thead>
<tbody>
<tr>
<td>Second row</td>
<td>Other col</td>
</tr>
</tbody>
</table>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<pre><code class="">A
B C
</code></pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block::
A
B
C
4 changes: 4 additions & 0 deletions tests/Functional/tests/code-block/code-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<pre><code class="c++">#include &lt;iostream&gt; using namespace std; int main(void)
{ cout &lt;&lt; &quot;Hello world!&quot; &lt;&lt; endl;
}
</code></pre>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. code-block:: c++

#include <iostream>
Expand Down
4 changes: 4 additions & 0 deletions tests/Functional/tests/code-java/code-java.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<pre><code class="java">protected void f()
{
}
</code></pre>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. code-block:: java
protected void f()
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/tests/code-list/code-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<pre><code class="">* Testing
* Hey
</code></pre>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. This should not be interpreted as a list
.. code-block::
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<hr />
<blockquote>
<p>Test code block with whitespace.</p>
</blockquote>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
::

Test code block with whitespace.
3 changes: 3 additions & 0 deletions tests/Functional/tests/code/code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Code block:</p>
<pre><code class="">This is a code block You hou!
</code></pre>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Code block::

This is a code block
Expand Down
8 changes: 8 additions & 0 deletions tests/Functional/tests/code/code.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Code block:
\lstset{language=}
\begin{lstlisting}
This is a code block

You hou!

\end{lstlisting}
1 change: 1 addition & 0 deletions tests/Functional/tests/comment-3/comment-3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>... This is not a comment!</p>
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@

.. This is a comment!
... This is not a comment!

2 changes: 2 additions & 0 deletions tests/Functional/tests/comment/comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Text before</p>
<p>Text after</p>
3 changes: 3 additions & 0 deletions tests/Functional/tests/comment/comment.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Text before
.. Testing comment
Text after
1 change: 1 addition & 0 deletions tests/Functional/tests/comments/comments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
6 changes: 6 additions & 0 deletions tests/Functional/tests/crlf/crlf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="section" id="hello-world">
<h1>
Hello world
</h1>
<p>Hey!</p>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Hello world
===========

Hey!
Hey!
1 change: 1 addition & 0 deletions tests/Functional/tests/css/css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Testing page!</p>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. stylesheet:: style.css

Testing page!
Loading

0 comments on commit cb831fb

Please sign in to comment.