CommonMarkTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Test Parsedown against the CommonMark spec.
  4. *
  5. * Some code based on the original JavaScript test runner by jgm.
  6. *
  7. * @link http://commonmark.org/ CommonMark
  8. * @link http://git.io/8WtRvQ JavaScript test runner
  9. */
  10. class CommonMarkTest extends PHPUnit_Framework_TestCase
  11. {
  12. const SPEC_URL = 'https://raw.githubusercontent.com/jgm/stmd/master/spec.txt';
  13. /**
  14. * @dataProvider data
  15. * @param $section
  16. * @param $markdown
  17. * @param $expectedHtml
  18. */
  19. function test_($section, $markdown, $expectedHtml)
  20. {
  21. $Parsedown = new Parsedown();
  22. $Parsedown->setUrlsLinked(false);
  23. $actualHtml = $Parsedown->text($markdown);
  24. $actualHtml = $this->normalizeMarkup($actualHtml);
  25. $this->assertEquals($expectedHtml, $actualHtml);
  26. }
  27. function data()
  28. {
  29. $spec = file_get_contents(self::SPEC_URL);
  30. $spec = strstr($spec, '<!-- END TESTS -->', true);
  31. $tests = array();
  32. $currentSection = '';
  33. preg_replace_callback(
  34. '/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/m',
  35. function($matches) use ( & $tests, & $currentSection, & $testCount) {
  36. if (isset($matches[3]) and $matches[3]) {
  37. $currentSection = $matches[3];
  38. } else {
  39. $testCount++;
  40. $markdown = $matches[1];
  41. $markdown = preg_replace('/→/', "\t", $markdown);
  42. $expectedHtml = $matches[2];
  43. $expectedHtml = $this->normalizeMarkup($expectedHtml);
  44. $tests []= array(
  45. $currentSection, # section
  46. $markdown, # markdown
  47. $expectedHtml, # html
  48. );
  49. }
  50. },
  51. $spec
  52. );
  53. return $tests;
  54. }
  55. private function normalizeMarkup($markup)
  56. {
  57. $markup = preg_replace("/\n+/", "\n", $markup);
  58. $markup = preg_replace('/^\s+/m', '', $markup);
  59. $markup = preg_replace('/^((?:<[\w]+>)+)\n/m', '$1', $markup);
  60. $markup = preg_replace('/\n((?:<\/[\w]+>)+)$/m', '$1', $markup);
  61. $markup = trim($markup);
  62. return $markup;
  63. }
  64. }