CommonMarkTest

Test Parsedown against the CommonMark spec.

Defined (1)

The class is defined in the following location(s).

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