選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

116 行
3.7KB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2014 Carsten Brandt
  4. * @license https://github.com/cebe/markdown/blob/master/LICENSE
  5. * @link https://github.com/cebe/markdown#readme
  6. */
  7. namespace cebe\markdown\tests;
  8. use cebe\markdown\Parser;
  9. /**
  10. * Base class for all Test cases.
  11. *
  12. * @author Carsten Brandt <mail@cebe.cc>
  13. */
  14. abstract class BaseMarkdownTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $outputFileExtension = '.html';
  17. abstract public function getDataPaths();
  18. /**
  19. * @return Parser
  20. */
  21. abstract public function createMarkdown();
  22. /**
  23. * @dataProvider dataFiles
  24. */
  25. public function testParse($path, $file)
  26. {
  27. list($markdown, $html) = $this->getTestData($path, $file);
  28. // Different OS line endings should not affect test
  29. $html = str_replace(["\r\n", "\n\r", "\r"], "\n", $html);
  30. $m = $this->createMarkdown();
  31. $this->assertEquals($html, $m->parse($markdown));
  32. }
  33. public function testUtf8()
  34. {
  35. $this->assertSame("<p>абвгдеёжзийклмнопрстуфхцчшщъыьэюя</p>\n", $this->createMarkdown()->parse('абвгдеёжзийклмнопрстуфхцчшщъыьэюя'));
  36. $this->assertSame("<p>there is a charater, 配</p>\n", $this->createMarkdown()->parse('there is a charater, 配'));
  37. $this->assertSame("<p>Arabic Latter \"م (M)\"</p>\n", $this->createMarkdown()->parse('Arabic Latter "م (M)"'));
  38. $this->assertSame("<p>電腦</p>\n", $this->createMarkdown()->parse('電腦'));
  39. $this->assertSame('абвгдеёжзийклмнопрстуфхцчшщъыьэюя', $this->createMarkdown()->parseParagraph('абвгдеёжзийклмнопрстуфхцчшщъыьэюя'));
  40. $this->assertSame('there is a charater, 配', $this->createMarkdown()->parseParagraph('there is a charater, 配'));
  41. $this->assertSame('Arabic Latter "م (M)"', $this->createMarkdown()->parseParagraph('Arabic Latter "م (M)"'));
  42. $this->assertSame('電腦', $this->createMarkdown()->parseParagraph('電腦'));
  43. }
  44. public function testInvalidUtf8()
  45. {
  46. $m = $this->createMarkdown();
  47. $this->assertEquals("<p><code>�</code></p>\n", $m->parse("`\x80`"));
  48. $this->assertEquals('<code>�</code>', $m->parseParagraph("`\x80`"));
  49. }
  50. public function pregData()
  51. {
  52. // http://en.wikipedia.org/wiki/Newline#Representations
  53. return [
  54. ["a\r\nb", "a\nb"],
  55. ["a\n\rb", "a\nb"], // Acorn BBC and RISC OS spooled text output :)
  56. ["a\nb", "a\nb"],
  57. ["a\rb", "a\nb"],
  58. ["a\n\nb", "a\n\nb", "a</p>\n<p>b"],
  59. ["a\r\rb", "a\n\nb", "a</p>\n<p>b"],
  60. ["a\n\r\n\rb", "a\n\nb", "a</p>\n<p>b"], // Acorn BBC and RISC OS spooled text output :)
  61. ["a\r\n\r\nb", "a\n\nb", "a</p>\n<p>b"],
  62. ];
  63. }
  64. /**
  65. * @dataProvider pregData
  66. */
  67. public function testPregReplaceR($input, $exptected, $pexpect = null)
  68. {
  69. $this->assertSame($exptected, $this->createMarkdown()->parseParagraph($input));
  70. $this->assertSame($pexpect === null ? "<p>$exptected</p>\n" : "<p>$pexpect</p>\n", $this->createMarkdown()->parse($input));
  71. }
  72. public function getTestData($path, $file)
  73. {
  74. return [
  75. file_get_contents($this->getDataPaths()[$path] . '/' . $file . '.md'),
  76. file_get_contents($this->getDataPaths()[$path] . '/' . $file . $this->outputFileExtension),
  77. ];
  78. }
  79. public function dataFiles()
  80. {
  81. $files = [];
  82. foreach ($this->getDataPaths() as $name => $src) {
  83. $handle = opendir($src);
  84. if ($handle === false) {
  85. throw new \Exception('Unable to open directory: ' . $src);
  86. }
  87. while (($file = readdir($handle)) !== false) {
  88. if ($file === '.' || $file === '..') {
  89. continue;
  90. }
  91. if (substr($file, -3, 3) === '.md' && file_exists($src . '/' . substr($file, 0, -3) . $this->outputFileExtension)) {
  92. $files[] = [$name, substr($file, 0, -3)];
  93. }
  94. }
  95. closedir($handle);
  96. }
  97. return $files;
  98. }
  99. }