1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Type check tests

This commit is contained in:
Aidan Woods 2019-02-10 22:38:34 +00:00
parent 30613b2430
commit dbe37bcb0e
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
5 changed files with 116 additions and 51 deletions

View File

@ -6,11 +6,13 @@
> >
<projectFiles> <projectFiles>
<directory name="src" /> <directory name="src" />
<directory name="tests" />
</projectFiles> </projectFiles>
<issueHandlers> <issueHandlers>
<PossiblyUnusedMethod> <PossiblyUnusedMethod>
<errorLevel type="suppress"> <errorLevel type="suppress">
<directory name="tests" />
<referencedMethod name="Erusev\Parsedown\Components\Inlines\PlainText::text" /> <referencedMethod name="Erusev\Parsedown\Components\Inlines\PlainText::text" />
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Container::contents" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Container::contents" />
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::create" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::create" />
@ -18,14 +20,8 @@
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingName" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingName" />
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingAttributes" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingAttributes" />
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingContents" /> <referencedMethod name="Erusev\Parsedown\Html\Renderables\Element::settingContents" />
<referencedMethod name="Erusev\Parsedown\Parsedown::__construct" />
<referencedMethod name="Erusev\Parsedown\Parsedown::text" />
<referencedMethod name="Erusev\Parsedown\Parsing\Lines::appendingContext" /> <referencedMethod name="Erusev\Parsedown\Parsing\Lines::appendingContext" />
<referencedMethod name="Erusev\Parsedown\Parsing\Lines::last" /> <referencedMethod name="Erusev\Parsedown\Parsing\Lines::last" />
<referencedMethod name="Erusev\Parsedown\Configurables\StrictMode::enabled" />
<referencedMethod name="Erusev\Parsedown\Configurables\SafeMode::enabled" />
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::removing" />
<referencedMethod name="Erusev\Parsedown\Configurables\Breaks::enabled" />
<referencedMethod name="Erusev\Parsedown\State::mergingWith" /> <referencedMethod name="Erusev\Parsedown\State::mergingWith" />
<referencedMethod name="Erusev\Parsedown\Html\Sanitisation\Escaper::htmlElementValue" /> <referencedMethod name="Erusev\Parsedown\Html\Sanitisation\Escaper::htmlElementValue" />
<referencedMethod name="Erusev\Parsedown\Configurables\InlineTypes::addingHighPrecedence" /> <referencedMethod name="Erusev\Parsedown\Configurables\InlineTypes::addingHighPrecedence" />

View File

@ -25,7 +25,8 @@ namespace Erusev\Parsedown\Tests;
class CommonMarkTest extends CommonMarkTestStrict class CommonMarkTest extends CommonMarkTestStrict
{ {
/** /**
* @return array * @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
* @throws \PHPUnit\Framework\AssertionFailedError
*/ */
public function data() public function data()
{ {
@ -68,10 +69,13 @@ class CommonMarkTest extends CommonMarkTestStrict
/** /**
* @group update * @group update
* @dataProvider dataUpdate * @dataProvider dataUpdate
* @param $id * @param int $id
* @param $section * @param string $section
* @param $markdown * @param string $markdown
* @param $expectedHtml * @param string $expectedHtml
* @return void
* @throws \PHPUnit\Framework\AssertionFailedError
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/ */
public function testUpdateDatabase($id, $section, $markdown, $expectedHtml) public function testUpdateDatabase($id, $section, $markdown, $expectedHtml)
{ {
@ -79,19 +83,25 @@ class CommonMarkTest extends CommonMarkTestStrict
// you can only get here when the test passes // you can only get here when the test passes
$dir = static::getDataDir(true); $dir = static::getDataDir(true);
$basename = $id . '-' . \preg_replace('/[^\w.-]/', '_', $section); $basename = \strval($id) . '-' . \preg_replace('/[^\w.-]/', '_', $section);
\file_put_contents($dir . $basename . '.md', $markdown); \file_put_contents($dir . $basename . '.md', $markdown);
\file_put_contents($dir . $basename . '.html', $expectedHtml); \file_put_contents($dir . $basename . '.html', $expectedHtml);
} }
/** /**
* @return array * @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
* @throws \PHPUnit\Framework\AssertionFailedError
*/ */
public function dataUpdate() public function dataUpdate()
{ {
return parent::data(); return parent::data();
} }
/**
* @param bool $mkdir
* @return string
* @throws \PHPUnit\Framework\AssertionFailedError
*/
public static function getDataDir($mkdir = false) public static function getDataDir($mkdir = false)
{ {
$dir = __DIR__ . '/commonmark/'; $dir = __DIR__ . '/commonmark/';

View File

@ -20,30 +20,49 @@ class CommonMarkTestStrict extends TestCase
const SPEC_LOCAL_CACHE = 'spec_cache.txt'; const SPEC_LOCAL_CACHE = 'spec_cache.txt';
const SPEC_CACHE_SECONDS = 300; const SPEC_CACHE_SECONDS = 300;
protected $parsedown; /** @var Parsedown */
protected $Parsedown;
protected function setUp() /**
* @param string|null $name
* @param array $data
* @param string $dataName
*/
public function __construct($name = null, array $data = [], $dataName = '')
{ {
$this->parsedown = new Parsedown(new State([ $this->Parsedown = new Parsedown(new State([
StrictMode::enabled(), StrictMode::enabled(),
InlineTypes::initial()->removing([Url::class]), InlineTypes::initial()->removing([Url::class]),
])); ]));
$this->backupGlobals = false;
$this->backupStaticAttributes = false;
$this->runTestInSeparateProcess = false;
parent::__construct($name, $data, $dataName);
} }
/** /**
* @dataProvider data * @dataProvider data
* @param $id * @param int $_
* @param $section * @param string $__
* @param $markdown * @param string $markdown
* @param $expectedHtml * @param string $expectedHtml
* @return void
* @throws \PHPUnit\Framework\AssertionFailedError
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/ */
public function testExample($id, $section, $markdown, $expectedHtml) public function testExample($_, $__, $markdown, $expectedHtml)
{ {
$actualHtml = $this->parsedown->text($markdown); $actualHtml = $this->Parsedown->text($markdown);
$this->assertEquals($expectedHtml, $actualHtml); $this->assertEquals($expectedHtml, $actualHtml);
} }
public static function getSpec() /**
* @return string
* @throws \PHPUnit\Framework\AssertionFailedError
*/
public function getSpec()
{ {
$specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE; $specPath = __DIR__ .'/'.self::SPEC_LOCAL_CACHE;
@ -57,20 +76,23 @@ class CommonMarkTestStrict extends TestCase
\file_put_contents($specPath, $spec); \file_put_contents($specPath, $spec);
} }
return $spec;
}
/**
* @return array
*/
public function data()
{
$spec = self::getSpec();
if ($spec === false) { if ($spec === false) {
$this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL); $this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
} }
return $spec;
}
/**
* @return array<int, array{id: int, section: string, markdown: string, expectedHtml: string}>
* @throws \PHPUnit\Framework\AssertionFailedError
*/
public function data()
{
$spec = $this->getSpec();
$spec = \str_replace("\r\n", "\n", $spec); $spec = \str_replace("\r\n", "\n", $spec);
/** @var string */
$spec = \strstr($spec, '<!-- END TESTS -->', true); $spec = \strstr($spec, '<!-- END TESTS -->', true);
$matches = []; $matches = [];
@ -79,6 +101,7 @@ class CommonMarkTestStrict extends TestCase
$data = []; $data = [];
$currentId = 0; $currentId = 0;
$currentSection = ''; $currentSection = '';
/** @var array{0: string, 1: string, 2?: string, 3?: string} $match */
foreach ($matches as $match) { foreach ($matches as $match) {
if (isset($match[3])) { if (isset($match[3])) {
$currentSection = $match[3]; $currentSection = $match[3];

View File

@ -18,37 +18,57 @@ use Erusev\Parsedown\Html\Renderables\Element;
*/ */
class CommonMarkTestWeak extends CommonMarkTestStrict class CommonMarkTestWeak extends CommonMarkTestStrict
{ {
/** @var string */
protected $textLevelElementRegex; protected $textLevelElementRegex;
protected function setUp() /**
* @param string|null $name
* @param array $data
* @param string $dataName
*/
public function __construct($name = null, array $data = [], $dataName = '')
{ {
parent::setUp();
$textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS); $textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS);
\array_walk($textLevelElements, function (&$element) { \array_walk(
$textLevelElements,
/**
* @param string &$element
* @return void
*/
function (&$element) {
$element = \preg_quote($element, '/'); $element = \preg_quote($element, '/');
}); }
);
$this->textLevelElementRegex = '\b(?:' . \implode('|', $textLevelElements) . ')\b'; $this->textLevelElementRegex = '\b(?:' . \implode('|', $textLevelElements) . ')\b';
parent::__construct($name, $data, $dataName);
} }
/** /**
* @dataProvider data * @dataProvider data
* @param $id * @param int $_
* @param $section * @param string $__
* @param $markdown * @param string $markdown
* @param $expectedHtml * @param string $expectedHtml
* @return void
* @throws \PHPUnit\Framework\AssertionFailedError
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/ */
public function testExample($id, $section, $markdown, $expectedHtml) public function testExample($_, $__, $markdown, $expectedHtml)
{ {
$expectedHtml = $this->cleanupHtml($expectedHtml); $expectedHtml = $this->cleanupHtml($expectedHtml);
$actualHtml = $this->parsedown->text($markdown); $actualHtml = $this->Parsedown->text($markdown);
$actualHtml = $this->cleanupHtml($actualHtml); $actualHtml = $this->cleanupHtml($actualHtml);
$this->assertEquals($expectedHtml, $actualHtml); $this->assertEquals($expectedHtml, $actualHtml);
} }
/**
* @param string $markup
* @return string
*/
protected function cleanupHtml($markup) protected function cleanupHtml($markup)
{ {
// invisible whitespaces at the beginning and end of block elements // invisible whitespaces at the beginning and end of block elements

View File

@ -15,30 +15,40 @@ use PHPUnit\Framework\TestCase;
class ParsedownTest extends TestCase class ParsedownTest extends TestCase
{ {
/**
* @param string|null $name
* @param array $data
* @param string $dataName
*/
final public function __construct($name = null, array $data = [], $dataName = '') final public function __construct($name = null, array $data = [], $dataName = '')
{ {
$this->dirs = $this->initDirs(); $this->dirs = $this->initDirs();
$this->backupGlobals = false;
$this->backupStaticAttributes = false;
$this->runTestInSeparateProcess = false;
parent::__construct($name, $data, $dataName); parent::__construct($name, $data, $dataName);
} }
/** @var string[] */
private $dirs; private $dirs;
protected $Parsedown;
/** /**
* @return array * @return string[]
*/ */
protected function initDirs() protected function initDirs()
{ {
$dirs []= \dirname(__FILE__).'/data/'; return [\dirname(__FILE__).'/data/'];
return $dirs;
} }
/** /**
* @dataProvider data * @dataProvider data
* @param $test * @param string $test
* @param $dir * @param string $dir
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/ */
public function test_($test, $dir) public function test_($test, $dir)
{ {
@ -60,6 +70,7 @@ class ParsedownTest extends TestCase
$this->assertEquals($expectedMarkup, $actualMarkup); $this->assertEquals($expectedMarkup, $actualMarkup);
} }
/** @return array<int, array{0:string, 1:string} */
public function data() public function data()
{ {
$data = []; $data = [];
@ -93,6 +104,11 @@ class ParsedownTest extends TestCase
return $data; return $data;
} }
/**
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function test_no_markup() public function test_no_markup()
{ {
$markdownWithHtml = <<<MARKDOWN_WITH_MARKUP $markdownWithHtml = <<<MARKDOWN_WITH_MARKUP