Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

95 linhas
2.5KB

  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. }