diff --git a/.travis.yml b/.travis.yml index cfb4d8d..db5d725 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,8 +20,9 @@ matrix: - php: nightly - php: hhvm-nightly -before_script: +install: - composer install --prefer-dist --no-interaction --no-progress script: - vendor/bin/phpunit + - vendor/bin/phpunit test/CommonMarkTestWeak.php || true diff --git a/composer.json b/composer.json index b7f8aea..dc349d4 100644 --- a/composer.json +++ b/composer.json @@ -20,5 +20,13 @@ }, "autoload": { "psr-0": {"Parsedown": ""} + }, + "autoload-dev": { + "psr-0": { + "TestParsedown": "test/", + "ParsedownTest": "test/", + "CommonMarkTest": "test/", + "CommonMarkTestWeak": "test/" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dd9da22..4fe3177 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + test/ParsedownTest.php diff --git a/test/CommonMarkTest.php b/test/CommonMarkTest.php deleted file mode 100644 index 7111f0b..0000000 --- a/test/CommonMarkTest.php +++ /dev/null @@ -1,77 +0,0 @@ -setUrlsLinked(false); - - $actualHtml = $Parsedown->text($markdown); - $actualHtml = $this->normalizeMarkup($actualHtml); - - $this->assertEquals($expectedHtml, $actualHtml); - } - - function data() - { - $spec = file_get_contents(self::SPEC_URL); - $spec = strstr($spec, '', true); - - $tests = array(); - $currentSection = ''; - - preg_replace_callback( - '/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/m', - function($matches) use ( & $tests, & $currentSection, & $testCount) { - if (isset($matches[3]) and $matches[3]) { - $currentSection = $matches[3]; - } else { - $testCount++; - $markdown = $matches[1]; - $markdown = preg_replace('/→/', "\t", $markdown); - $expectedHtml = $matches[2]; - $expectedHtml = $this->normalizeMarkup($expectedHtml); - $tests []= array( - $currentSection, # section - $markdown, # markdown - $expectedHtml, # html - ); - } - }, - $spec - ); - - return $tests; - } - - 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; - } -} diff --git a/test/CommonMarkTestStrict.php b/test/CommonMarkTestStrict.php new file mode 100644 index 0000000..3837738 --- /dev/null +++ b/test/CommonMarkTestStrict.php @@ -0,0 +1,71 @@ +parsedown = new TestParsedown(); + $this->parsedown->setUrlsLinked(false); + } + + /** + * @dataProvider data + * @param $id + * @param $section + * @param $markdown + * @param $expectedHtml + */ + public function testExample($id, $section, $markdown, $expectedHtml) + { + $actualHtml = $this->parsedown->text($markdown); + $this->assertEquals($expectedHtml, $actualHtml); + } + + /** + * @return array + */ + public function data() + { + $spec = file_get_contents(self::SPEC_URL); + if ($spec === false) { + $this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL); + } + + $spec = str_replace("\r\n", "\n", $spec); + $spec = strstr($spec, '', true); + + $matches = array(); + preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER); + + $data = array(); + $currentId = 0; + $currentSection = ''; + foreach ($matches as $match) { + if (isset($match[3])) { + $currentSection = $match[3]; + } else { + $currentId++; + $markdown = str_replace('→', "\t", $match[1]); + $expectedHtml = isset($match[2]) ? str_replace('→', "\t", $match[2]) : ''; + + $data[$currentId] = array( + 'id' => $currentId, + 'section' => $currentSection, + 'markdown' => $markdown, + 'expectedHtml' => $expectedHtml + ); + } + } + + return $data; + } +} diff --git a/test/CommonMarkTestWeak.php b/test/CommonMarkTestWeak.php new file mode 100644 index 0000000..ef4081a --- /dev/null +++ b/test/CommonMarkTestWeak.php @@ -0,0 +1,63 @@ +parsedown->getTextLevelElements(); + array_walk($textLevelElements, function (&$element) { + $element = preg_quote($element, '/'); + }); + $this->textLevelElementRegex = '\b(?:' . implode('|', $textLevelElements) . ')\b'; + } + + /** + * @dataProvider data + * @param $id + * @param $section + * @param $markdown + * @param $expectedHtml + */ + public function testExample($id, $section, $markdown, $expectedHtml) + { + $expectedHtml = $this->cleanupHtml($expectedHtml); + + $actualHtml = $this->parsedown->text($markdown); + $actualHtml = $this->cleanupHtml($actualHtml); + + $this->assertEquals($expectedHtml, $actualHtml); + } + + protected function cleanupHtml($markup) + { + // invisible whitespaces at the beginning and end of block elements + // however, whitespaces at the beginning of
 elements do matter
+        $markup = preg_replace(
+            array(
+                '/(<(?!(?:' . $this->textLevelElementRegex . '|\bpre\b))\w+\b[^>]*>(?:<' . $this->textLevelElementRegex . '[^>]*>)*)\s+/s',
+                '/\s+((?:<\/' . $this->textLevelElementRegex . '>)*<\/(?!' . $this->textLevelElementRegex . ')\w+\b>)/s'
+            ),
+            '$1',
+            $markup
+        );
+
+        return $markup;
+    }
+}
diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php
index e7fc5c2..c28cedf 100644
--- a/test/ParsedownTest.php
+++ b/test/ParsedownTest.php
@@ -29,7 +29,7 @@ class ParsedownTest extends TestCase
      */
     protected function initParsedown()
     {
-        $Parsedown = new Parsedown();
+        $Parsedown = new TestParsedown();
 
         return $Parsedown;
     }
@@ -136,15 +136,14 @@ color: red;
 

comment

<!-- html comment -->

EXPECTED_HTML; - $parsedownWithNoMarkup = new Parsedown(); + + $parsedownWithNoMarkup = new TestParsedown(); $parsedownWithNoMarkup->setMarkupEscaped(true); $this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml)); } public function testLateStaticBinding() { - include __DIR__ . '/TestParsedown.php'; - $parsedown = Parsedown::instance(); $this->assertInstanceOf('Parsedown', $parsedown); diff --git a/test/TestParsedown.php b/test/TestParsedown.php index 7024dfb..2faa0ab 100644 --- a/test/TestParsedown.php +++ b/test/TestParsedown.php @@ -2,4 +2,8 @@ class TestParsedown extends Parsedown { + public function getTextLevelElements() + { + return $this->textLevelElements; + } } diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index c89724b..0000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,3 +0,0 @@ -