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
8namespace cebe\markdown\tests;
9
10use 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 */
18class 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	public function testKeepZeroAlive()
61	{
62		$parser = new TestParser();
63
64		$this->assertEquals("0", $parser->parseParagraph("0"));
65		$this->assertEquals("<p>0</p>\n", $parser->parse("0"));
66	}
67}
68
69class TestParser extends Parser
70{
71	public $markers = [];
72
73	protected function inlineMarkers()
74	{
75		return $this->markers;
76	}
77
78	protected function parseMarkerA($text)
79	{
80		return [['text', 'A'], strrpos($text, ']') + 1];
81	}
82
83	protected function parseMarkerB($text)
84	{
85		return [['text', 'B'], strrpos($text, ']') + 1];
86	}
87
88	protected function parseMarkerC($text)
89	{
90		$terminatingMarkerPos = strrpos($text, ']');
91		$inside = $this->parseInline(substr($text, 1, $terminatingMarkerPos - 1));
92		return [['text', '(C-' . $this->renderAbsy($inside) . ')'], $terminatingMarkerPos + 1];
93	}
94}
95