Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ParserTest.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Test case for the parser base class.
  11. *
  12. * @author Carsten Brandt <mail@cebe.cc>
  13. * @group default
  14. */
  15. class ParserTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testMarkerOrder()
  18. {
  19. $parser = new TestParser();
  20. $parser->markers = [
  21. '[' => 'parseMarkerA',
  22. '[[' => 'parseMarkerB',
  23. ];
  24. $this->assertEquals("<p>Result is A</p>\n", $parser->parse('Result is [abc]'));
  25. $this->assertEquals("<p>Result is B</p>\n", $parser->parse('Result is [[abc]]'));
  26. $this->assertEquals('Result is A', $parser->parseParagraph('Result is [abc]'));
  27. $this->assertEquals('Result is B', $parser->parseParagraph('Result is [[abc]]'));
  28. $parser = new TestParser();
  29. $parser->markers = [
  30. '[[' => 'parseMarkerB',
  31. '[' => 'parseMarkerA',
  32. ];
  33. $this->assertEquals("<p>Result is A</p>\n", $parser->parse('Result is [abc]'));
  34. $this->assertEquals("<p>Result is B</p>\n", $parser->parse('Result is [[abc]]'));
  35. $this->assertEquals('Result is A', $parser->parseParagraph('Result is [abc]'));
  36. $this->assertEquals('Result is B', $parser->parseParagraph('Result is [[abc]]'));
  37. }
  38. public function testMaxNestingLevel()
  39. {
  40. $parser = new TestParser();
  41. $parser->markers = [
  42. '[' => 'parseMarkerC',
  43. ];
  44. $parser->maximumNestingLevel = 3;
  45. $this->assertEquals("(C-a(C-b(C-c)))", $parser->parseParagraph('[a[b[c]]]'));
  46. $parser->maximumNestingLevel = 2;
  47. $this->assertEquals("(C-a(C-b[c]))", $parser->parseParagraph('[a[b[c]]]'));
  48. $parser->maximumNestingLevel = 1;
  49. $this->assertEquals("(C-a[b[c]])", $parser->parseParagraph('[a[b[c]]]'));
  50. }
  51. public function testKeepZeroAlive()
  52. {
  53. $parser = new TestParser();
  54. $this->assertEquals("0", $parser->parseParagraph("0"));
  55. $this->assertEquals("<p>0</p>\n", $parser->parse("0"));
  56. }
  57. }
  58. class TestParser extends Parser
  59. {
  60. public $markers = [];
  61. protected function inlineMarkers()
  62. {
  63. return $this->markers;
  64. }
  65. protected function parseMarkerA($text)
  66. {
  67. return [['text', 'A'], strrpos($text, ']') + 1];
  68. }
  69. protected function parseMarkerB($text)
  70. {
  71. return [['text', 'B'], strrpos($text, ']') + 1];
  72. }
  73. protected function parseMarkerC($text)
  74. {
  75. $terminatingMarkerPos = strrpos($text, ']');
  76. $inside = $this->parseInline(substr($text, 1, $terminatingMarkerPos - 1));
  77. return [['text', '(C-' . $this->renderAbsy($inside) . ')'], $terminatingMarkerPos + 1];
  78. }
  79. }