2014-09-06 00:12:33 +04:00
|
|
|
<?php
|
2018-04-17 16:44:38 +03:00
|
|
|
|
|
|
|
namespace Erusev\Parsedown\Tests;
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2014-09-06 00:28:07 +04:00
|
|
|
/**
|
2016-09-05 05:51:28 +03:00
|
|
|
* Test Parsedown against the CommonMark spec
|
2014-09-06 00:28:07 +04:00
|
|
|
*
|
2014-09-14 01:02:11 +04:00
|
|
|
* @link http://commonmark.org/ CommonMark
|
2014-09-06 00:28:07 +04:00
|
|
|
*/
|
2016-10-12 03:01:40 +03:00
|
|
|
class CommonMarkTestStrict extends PHPUnit_Framework_TestCase
|
2014-09-06 00:12:33 +04:00
|
|
|
{
|
2016-10-13 23:16:46 +03:00
|
|
|
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt';
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
protected $parsedown;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2016-09-05 15:38:47 +03:00
|
|
|
$this->parsedown = new TestParsedown();
|
2016-09-05 05:51:28 +03:00
|
|
|
$this->parsedown->setUrlsLinked(false);
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:34:46 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider data
|
2016-10-12 03:01:40 +03:00
|
|
|
* @param $id
|
2014-11-30 00:53:38 +03:00
|
|
|
* @param $section
|
2014-11-29 22:34:46 +03:00
|
|
|
* @param $markdown
|
|
|
|
* @param $expectedHtml
|
|
|
|
*/
|
2016-10-12 03:01:40 +03:00
|
|
|
public function testExample($id, $section, $markdown, $expectedHtml)
|
2014-11-29 22:34:46 +03:00
|
|
|
{
|
2016-09-05 05:51:28 +03:00
|
|
|
$actualHtml = $this->parsedown->text($markdown);
|
2014-11-29 22:34:46 +03:00
|
|
|
$this->assertEquals($expectedHtml, $actualHtml);
|
|
|
|
}
|
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function data()
|
2014-09-06 00:12:33 +04:00
|
|
|
{
|
2014-11-29 21:18:23 +03:00
|
|
|
$spec = file_get_contents(self::SPEC_URL);
|
2016-09-05 05:51:28 +03:00
|
|
|
if ($spec === false) {
|
|
|
|
$this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
|
|
|
|
}
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
$spec = str_replace("\r\n", "\n", $spec);
|
|
|
|
$spec = strstr($spec, '<!-- END TESTS -->', true);
|
2014-09-06 00:12:33 +04:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
$matches = array();
|
2016-10-13 23:16:46 +03:00
|
|
|
preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER);
|
2014-11-29 23:58:42 +03:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
$data = array();
|
2016-10-12 03:01:40 +03:00
|
|
|
$currentId = 0;
|
2016-09-05 05:51:28 +03:00
|
|
|
$currentSection = '';
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
if (isset($match[3])) {
|
|
|
|
$currentSection = $match[3];
|
|
|
|
} else {
|
2016-10-13 23:16:46 +03:00
|
|
|
$currentId++;
|
|
|
|
$markdown = str_replace('→', "\t", $match[1]);
|
|
|
|
$expectedHtml = isset($match[2]) ? str_replace('→', "\t", $match[2]) : '';
|
|
|
|
|
|
|
|
$data[$currentId] = array(
|
|
|
|
'id' => $currentId,
|
2016-09-05 05:51:28 +03:00
|
|
|
'section' => $currentSection,
|
2016-10-13 23:16:46 +03:00
|
|
|
'markdown' => $markdown,
|
|
|
|
'expectedHtml' => $expectedHtml
|
2016-09-05 05:51:28 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 23:58:42 +03:00
|
|
|
|
2016-09-05 05:51:28 +03:00
|
|
|
return $data;
|
2014-11-29 23:58:42 +03:00
|
|
|
}
|
2014-09-06 00:12:33 +04:00
|
|
|
}
|