2014-09-06 00:12:33 +04:00
|
|
|
<?php
|
|
|
|
|
2014-09-06 00:28:07 +04:00
|
|
|
/**
|
2014-09-06 02:12:35 +04:00
|
|
|
* Test Parsedown against the CommonMark spec.
|
2014-09-06 00:28:07 +04:00
|
|
|
*
|
|
|
|
* Some code based on the original JavaScript test runner by jgm.
|
|
|
|
*
|
2014-09-14 01:02:11 +04:00
|
|
|
* @link http://commonmark.org/ CommonMark
|
2014-09-06 00:28:07 +04:00
|
|
|
* @link http://git.io/8WtRvQ JavaScript test runner
|
|
|
|
*/
|
2017-11-11 05:56:03 +03:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class CommonMarkTest extends TestCase
|
2014-09-06 00:12:33 +04:00
|
|
|
{
|
|
|
|
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt';
|
|
|
|
|
2014-11-29 22:34:46 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider data
|
2014-11-30 00:53:38 +03:00
|
|
|
* @param $section
|
2014-11-29 22:34:46 +03:00
|
|
|
* @param $markdown
|
|
|
|
* @param $expectedHtml
|
|
|
|
*/
|
2014-11-30 00:53:38 +03:00
|
|
|
function test_($section, $markdown, $expectedHtml)
|
2014-11-29 22:34:46 +03:00
|
|
|
{
|
2015-01-09 01:08:14 +03:00
|
|
|
$Parsedown = new Parsedown();
|
|
|
|
$Parsedown->setUrlsLinked(false);
|
2014-11-29 22:34:46 +03:00
|
|
|
|
2015-01-09 01:08:14 +03:00
|
|
|
$actualHtml = $Parsedown->text($markdown);
|
2014-11-29 23:58:42 +03:00
|
|
|
$actualHtml = $this->normalizeMarkup($actualHtml);
|
2014-11-29 22:34:46 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expectedHtml, $actualHtml);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-11-29 22:34:46 +03:00
|
|
|
$spec = strstr($spec, '<!-- END TESTS -->', true);
|
2014-09-06 00:12:33 +04:00
|
|
|
|
|
|
|
$tests = array();
|
|
|
|
$currentSection = '';
|
|
|
|
|
|
|
|
preg_replace_callback(
|
|
|
|
'/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/m',
|
2014-11-29 22:34:46 +03:00
|
|
|
function($matches) use ( & $tests, & $currentSection, & $testCount) {
|
2014-09-06 00:12:33 +04:00
|
|
|
if (isset($matches[3]) and $matches[3]) {
|
|
|
|
$currentSection = $matches[3];
|
|
|
|
} else {
|
2014-11-29 22:34:46 +03:00
|
|
|
$testCount++;
|
2014-11-29 23:58:42 +03:00
|
|
|
$markdown = $matches[1];
|
|
|
|
$markdown = preg_replace('/→/', "\t", $markdown);
|
|
|
|
$expectedHtml = $matches[2];
|
|
|
|
$expectedHtml = $this->normalizeMarkup($expectedHtml);
|
2014-09-06 00:12:33 +04:00
|
|
|
$tests []= array(
|
2014-11-30 00:53:38 +03:00
|
|
|
$currentSection, # section
|
2014-11-29 22:34:46 +03:00
|
|
|
$markdown, # markdown
|
2014-11-29 23:58:42 +03:00
|
|
|
$expectedHtml, # html
|
2014-09-06 00:12:33 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$spec
|
|
|
|
);
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
2014-11-29 23:58:42 +03:00
|
|
|
|
|
|
|
private function normalizeMarkup($markup)
|
|
|
|
{
|
|
|
|
$markup = preg_replace("/\n+/", "\n", $markup);
|
|
|
|
$markup = preg_replace('/^\s+/m', '', $markup);
|
|
|
|
$markup = preg_replace('/^((?:<[\w]+>)+)\n/m', '$1', $markup);
|
|
|
|
$markup = preg_replace('/\n((?:<\/[\w]+>)+)$/m', '$1', $markup);
|
|
|
|
$markup = trim($markup);
|
|
|
|
|
|
|
|
return $markup;
|
|
|
|
}
|
2014-09-06 00:12:33 +04:00
|
|
|
}
|