ParserTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  52. class TestParser extends Parser
  53. {
  54. public $markers = [];
  55. protected function inlineMarkers()
  56. {
  57. return $this->markers;
  58. }
  59. protected function parseMarkerA($text)
  60. {
  61. return [['text', 'A'], strrpos($text, ']') + 1];
  62. }
  63. protected function parseMarkerB($text)
  64. {
  65. return [['text', 'B'], strrpos($text, ']') + 1];
  66. }
  67. protected function parseMarkerC($text)
  68. {
  69. $terminatingMarkerPos = strrpos($text, ']');
  70. $inside = $this->parseInline(substr($text, 1, $terminatingMarkerPos - 1));
  71. return [['text', '(C-' . $this->renderAbsy($inside) . ')'], $terminatingMarkerPos + 1];
  72. }
  73. }