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