From e1bcc1c47279a945af7e04e7f5c1f3615b01a825 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Mon, 5 Sep 2016 04:51:28 +0200 Subject: [PATCH 01/12] Fix test/CommonMarkTest.php --- test/CommonMarkTest.php | 82 ++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/test/CommonMarkTest.php b/test/CommonMarkTest.php index 9b8d116..680201b 100644 --- a/test/CommonMarkTest.php +++ b/test/CommonMarkTest.php @@ -1,74 +1,64 @@ parsedown = new Parsedown(); + $this->parsedown->setUrlsLinked(false); + } + /** * @dataProvider data * @param $section * @param $markdown * @param $expectedHtml */ - function test_($section, $markdown, $expectedHtml) + public function testExample($section, $markdown, $expectedHtml) { - $Parsedown = new Parsedown(); - $Parsedown->setUrlsLinked(false); - - $actualHtml = $Parsedown->text($markdown); - $actualHtml = $this->normalizeMarkup($actualHtml); - + $actualHtml = $this->parsedown->text($markdown); $this->assertEquals($expectedHtml, $actualHtml); } - function data() + /** + * @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); - $tests = array(); + $matches = array(); + preg_match_all('/^(?s)`{32} example\n(.*?)\n\.\n(.*?)\n`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER); + + $data = array(); $currentSection = ''; + foreach ($matches as $match) { + if (isset($match[3])) { + $currentSection = $match[3]; + } else { + $data[] = array( + 'section' => $currentSection, + 'markdown' => str_replace('→', "\t", $match[1]), + 'expectedHtml' => str_replace('→', "\t", $match[2]) + ); + } + } - 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; + return $data; } } From 3a46a31e09e5c54f02dc170af83221e0a4b9fef3 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Mon, 5 Sep 2016 14:37:34 +0200 Subject: [PATCH 02/12] Fix test/CommonMarkTest.php example regex --- test/CommonMarkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CommonMarkTest.php b/test/CommonMarkTest.php index 680201b..fd07a49 100644 --- a/test/CommonMarkTest.php +++ b/test/CommonMarkTest.php @@ -43,7 +43,7 @@ class CommonMarkTest extends PHPUnit_Framework_TestCase $spec = strstr($spec, '', true); $matches = array(); - preg_match_all('/^(?s)`{32} example\n(.*?)\n\.\n(.*?)\n`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER); + preg_match_all('/^`{32} example\n((?s).*?)\n\.\n((?s).*?)\n`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER); $data = array(); $currentSection = ''; From d33e736fa32cbab800936c2e910d986b9b32781e Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Mon, 5 Sep 2016 14:38:47 +0200 Subject: [PATCH 03/12] Add test/CommonMarkTestWeak.php --- test/CommonMarkTest.php | 4 ++- test/CommonMarkTestWeak.php | 61 +++++++++++++++++++++++++++++++++++++ test/TestParsedown.php | 4 +++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/CommonMarkTestWeak.php diff --git a/test/CommonMarkTest.php b/test/CommonMarkTest.php index fd07a49..c7a1d52 100644 --- a/test/CommonMarkTest.php +++ b/test/CommonMarkTest.php @@ -13,7 +13,9 @@ class CommonMarkTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->parsedown = new Parsedown(); + require_once(__DIR__ . '/TestParsedown.php'); + + $this->parsedown = new TestParsedown(); $this->parsedown->setUrlsLinked(false); } diff --git a/test/CommonMarkTestWeak.php b/test/CommonMarkTestWeak.php new file mode 100644 index 0000000..9b0b730 --- /dev/null +++ b/test/CommonMarkTestWeak.php @@ -0,0 +1,61 @@ +parsedown->getTextLevelElements(); + + array_walk($textLevelElements, function (&$element) { + $element = preg_quote($element, '/'); + }); + $this->textLevelElementRegex = '\b(?:' . implode('|', $textLevelElements) . ')\b'; + } + + /** + * @dataProvider data + * @param $section + * @param $markdown + * @param $expectedHtml + */ + public function testExample($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/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;
+    }
 }

From 2cacfb8da4d81e8a417e17da20e52e99d5a0e98e Mon Sep 17 00:00:00 2001
From: Daniel Rudolf 
Date: Mon, 5 Sep 2016 15:17:52 +0200
Subject: [PATCH 04/12] Improve test/CommonMarkTestWeak.php

---
 test/CommonMarkTestWeak.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/CommonMarkTestWeak.php b/test/CommonMarkTestWeak.php
index 9b0b730..751cad5 100644
--- a/test/CommonMarkTestWeak.php
+++ b/test/CommonMarkTestWeak.php
@@ -49,8 +49,8 @@ class CommonMarkTestWeak extends CommonMarkTest
         // 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'
+                '/(<(?!(?:' . $this->textLevelElementRegex . '|\bpre\b))\w+\b[^>]*>(?:<' . $this->textLevelElementRegex . '[^>]*>)*)\s+/s',
+                '/\s+((?:<\/' . $this->textLevelElementRegex . '>)*<\/(?!' . $this->textLevelElementRegex . ')\w+\b>)/s'
             ),
             '$1',
             $markup

From 228d5f4754fbd5e2f2e12437cab121c2f05004d8 Mon Sep 17 00:00:00 2001
From: Daniel Rudolf 
Date: Mon, 5 Sep 2016 15:31:07 +0200
Subject: [PATCH 05/12] Improve test/CommonMarkTestWeak.php

---
 test/CommonMarkTestWeak.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/test/CommonMarkTestWeak.php b/test/CommonMarkTestWeak.php
index 751cad5..e467201 100644
--- a/test/CommonMarkTestWeak.php
+++ b/test/CommonMarkTestWeak.php
@@ -15,12 +15,13 @@ require_once(__DIR__ . '/CommonMarkTest.php');
  */
 class CommonMarkTestWeak extends CommonMarkTest
 {
+    protected $textLevelElementRegex;
+
     protected function setUp()
     {
         parent::setUp();
 
         $textLevelElements = $this->parsedown->getTextLevelElements();
-
         array_walk($textLevelElements, function (&$element) {
             $element = preg_quote($element, '/');
         });

From 33a23fbfb22902e6268b8459af2784803aec8848 Mon Sep 17 00:00:00 2001
From: Daniel Rudolf 
Date: Mon, 5 Sep 2016 21:10:23 +0200
Subject: [PATCH 06/12] Refactor PHPUnit bootstrap

This allows Parsedown extensions (like Parsedown Extra) to reuse existing Parsedown tests. See erusev/parsedown-extra#96 for details.
---
 test/CommonMarkTest.php |  2 --
 test/ParsedownTest.php  |  7 +++----
 test/bootstrap.php      | 16 +++++++++++++++-
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/test/CommonMarkTest.php b/test/CommonMarkTest.php
index c7a1d52..18bca8b 100644
--- a/test/CommonMarkTest.php
+++ b/test/CommonMarkTest.php
@@ -13,8 +13,6 @@ class CommonMarkTest extends PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        require_once(__DIR__ . '/TestParsedown.php');
-
         $this->parsedown = new TestParsedown();
         $this->parsedown->setUrlsLinked(false);
     }
diff --git a/test/ParsedownTest.php b/test/ParsedownTest.php
index c922ab1..7d07183 100644
--- a/test/ParsedownTest.php
+++ b/test/ParsedownTest.php
@@ -27,7 +27,7 @@ class ParsedownTest extends PHPUnit_Framework_TestCase
      */
     protected function initParsedown()
     {
-        $Parsedown = new Parsedown();
+        $Parsedown = new TestParsedown();
 
         return $Parsedown;
     }
@@ -132,15 +132,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 'test/TestParsedown.php'; - $parsedown = Parsedown::instance(); $this->assertInstanceOf('Parsedown', $parsedown); diff --git a/test/bootstrap.php b/test/bootstrap.php index 5f264d2..76011c9 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -1,3 +1,17 @@ Date: Mon, 5 Sep 2016 22:04:46 +0200 Subject: [PATCH 07/12] Remove PHPUnit bootstrap in favour of composer --- .travis.yml | 3 +++ composer.json | 8 ++++++++ phpunit.xml.dist | 4 ++-- test/bootstrap.php | 17 ----------------- 4 files changed, 13 insertions(+), 19 deletions(-) delete mode 100644 test/bootstrap.php diff --git a/.travis.yml b/.travis.yml index 256dcf1..5d420e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,3 +13,6 @@ matrix: fast_finish: true allow_failures: - php: hhvm-nightly + +install: + - composer install diff --git a/composer.json b/composer.json index 28145af..b6a376d 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,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 b2d5e9d..4fe3177 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,8 @@ - + test/ParsedownTest.php - \ No newline at end of file + diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index 76011c9..0000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,17 +0,0 @@ - Date: Sun, 9 Oct 2016 14:17:03 +0200 Subject: [PATCH 08/12] Add test/CommonMarkTestWeak.php to .travis.yml Failing tests don't break builds on purpose, Parsedown doesn't fully comply with the CommonMark specs at the moment. We should switch to test/CommonMarkTest.php later, see #423 for details. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5d420e1..09c8e2b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,3 +16,6 @@ matrix: install: - composer install + +script: + - phpunit test/CommonMarkTestWeak.php || true From be671e72a39484ff9354cd7f1177d5901e1bdf9b Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sun, 9 Oct 2016 14:21:17 +0200 Subject: [PATCH 09/12] Don't let Travis skip Parsedown's phpunit tests --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 09c8e2b..3320963 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,4 +18,5 @@ install: - composer install script: + - phpunit - phpunit test/CommonMarkTestWeak.php || true From 2423644d728075dfd55199e6314d4ad3b24d9072 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Wed, 12 Oct 2016 02:01:40 +0200 Subject: [PATCH 10/12] Move test/CommonMarkTest.php to test/CommonMarkTestStrict.php Add parameter `$id` to CommonMark tests --- test/{CommonMarkTest.php => CommonMarkTestStrict.php} | 7 +++++-- test/CommonMarkTestWeak.php | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) rename test/{CommonMarkTest.php => CommonMarkTestStrict.php} (88%) diff --git a/test/CommonMarkTest.php b/test/CommonMarkTestStrict.php similarity index 88% rename from test/CommonMarkTest.php rename to test/CommonMarkTestStrict.php index 18bca8b..bd16ffa 100644 --- a/test/CommonMarkTest.php +++ b/test/CommonMarkTestStrict.php @@ -5,7 +5,7 @@ * * @link http://commonmark.org/ CommonMark */ -class CommonMarkTest extends PHPUnit_Framework_TestCase +class CommonMarkTestStrict extends PHPUnit_Framework_TestCase { const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt'; @@ -19,11 +19,12 @@ class CommonMarkTest extends PHPUnit_Framework_TestCase /** * @dataProvider data + * @param $id * @param $section * @param $markdown * @param $expectedHtml */ - public function testExample($section, $markdown, $expectedHtml) + public function testExample($id, $section, $markdown, $expectedHtml) { $actualHtml = $this->parsedown->text($markdown); $this->assertEquals($expectedHtml, $actualHtml); @@ -46,12 +47,14 @@ class CommonMarkTest extends PHPUnit_Framework_TestCase 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 { $data[] = array( + 'id' => ++$currentId, 'section' => $currentSection, 'markdown' => str_replace('→', "\t", $match[1]), 'expectedHtml' => str_replace('→', "\t", $match[2]) diff --git a/test/CommonMarkTestWeak.php b/test/CommonMarkTestWeak.php index e467201..ef4081a 100644 --- a/test/CommonMarkTestWeak.php +++ b/test/CommonMarkTestWeak.php @@ -1,5 +1,5 @@ cleanupHtml($expectedHtml); From a9f696f7bb413cbf7b14b744806fce504353e072 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 13 Oct 2016 22:16:46 +0200 Subject: [PATCH 11/12] Improve CommonMark spec example regex CommonMark spec example [#170](http://spec.commonmark.org/0.26/#example-170) has a empty HTML result. --- test/CommonMarkTestStrict.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/CommonMarkTestStrict.php b/test/CommonMarkTestStrict.php index bd16ffa..3837738 100644 --- a/test/CommonMarkTestStrict.php +++ b/test/CommonMarkTestStrict.php @@ -7,7 +7,7 @@ */ class CommonMarkTestStrict extends PHPUnit_Framework_TestCase { - const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt'; + const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt'; protected $parsedown; @@ -44,7 +44,7 @@ class CommonMarkTestStrict extends PHPUnit_Framework_TestCase $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); + preg_match_all('/^`{32} example\n((?s).*?)\n\.\n(?:|((?s).*?)\n)`{32}$|^#{1,6} *(.*?)$/m', $spec, $matches, PREG_SET_ORDER); $data = array(); $currentId = 0; @@ -53,11 +53,15 @@ class CommonMarkTestStrict extends PHPUnit_Framework_TestCase if (isset($match[3])) { $currentSection = $match[3]; } else { - $data[] = array( - 'id' => ++$currentId, + $currentId++; + $markdown = str_replace('→', "\t", $match[1]); + $expectedHtml = isset($match[2]) ? str_replace('→', "\t", $match[2]) : ''; + + $data[$currentId] = array( + 'id' => $currentId, 'section' => $currentSection, - 'markdown' => str_replace('→', "\t", $match[1]), - 'expectedHtml' => str_replace('→', "\t", $match[2]) + 'markdown' => $markdown, + 'expectedHtml' => $expectedHtml ); } } From ae0211a84c92a1eab7892a7c1876b5a5f7402f28 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 13 Oct 2016 22:17:03 +0200 Subject: [PATCH 12/12] Travis: Add PHP nightly --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 3320963..3259ca8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,12 +6,14 @@ php: - 5.5 - 5.4 - 5.3 + - nightly - hhvm - hhvm-nightly matrix: fast_finish: true allow_failures: + - php: nightly - php: hhvm-nightly install: