Blame view

vendor/cebe/markdown/tests/ParserTest.php 2.52 KB
2e86c939   xu   “首次提交”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
 * @copyright Copyright (c) 2014 Carsten Brandt
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
 * @link https://github.com/cebe/markdown#readme
 */

namespace cebe\markdown\tests;

use cebe\markdown\Parser;

/**
 * Test case for the parser base class.
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @group default
 */
class ParserTest extends  \PHPUnit_Framework_TestCase
{
	public function testMarkerOrder()
	{
		$parser = new TestParser();
		$parser->markers = [
			'[' => 'parseMarkerA',
			'[[' => 'parseMarkerB',
		];

		$this->assertEquals("<p>Result is A</p>\n", $parser->parse('Result is [abc]'));
		$this->assertEquals("<p>Result is B</p>\n", $parser->parse('Result is [[abc]]'));
		$this->assertEquals('Result is A', $parser->parseParagraph('Result is [abc]'));
		$this->assertEquals('Result is B', $parser->parseParagraph('Result is [[abc]]'));

		$parser = new TestParser();
		$parser->markers = [
			'[[' => 'parseMarkerB',
			'[' => 'parseMarkerA',
		];

		$this->assertEquals("<p>Result is A</p>\n", $parser->parse('Result is [abc]'));
		$this->assertEquals("<p>Result is B</p>\n", $parser->parse('Result is [[abc]]'));
		$this->assertEquals('Result is A', $parser->parseParagraph('Result is [abc]'));
		$this->assertEquals('Result is B', $parser->parseParagraph('Result is [[abc]]'));
	}

	public function testMaxNestingLevel()
	{
		$parser = new TestParser();
		$parser->markers = [
			'[' => 'parseMarkerC',
		];

		$parser->maximumNestingLevel = 3;
		$this->assertEquals("(C-a(C-b(C-c)))", $parser->parseParagraph('[a[b[c]]]'));
		$parser->maximumNestingLevel = 2;
		$this->assertEquals("(C-a(C-b[c]))", $parser->parseParagraph('[a[b[c]]]'));
		$parser->maximumNestingLevel = 1;
		$this->assertEquals("(C-a[b[c]])", $parser->parseParagraph('[a[b[c]]]'));
	}

	public function testKeepZeroAlive()
	{
		$parser = new TestParser();

		$this->assertEquals("0", $parser->parseParagraph("0"));
		$this->assertEquals("<p>0</p>\n", $parser->parse("0"));
	}
}

class TestParser extends Parser
{
	public $markers = [];

	protected function inlineMarkers()
	{
		return $this->markers;
	}

	protected function parseMarkerA($text)
	{
		return [['text', 'A'], strrpos($text, ']') + 1];
	}

	protected function parseMarkerB($text)
	{
		return [['text', 'B'], strrpos($text, ']') + 1];
	}

	protected function parseMarkerC($text)
	{
		$terminatingMarkerPos = strrpos($text, ']');
		$inside = $this->parseInline(substr($text, 1, $terminatingMarkerPos - 1));
		return [['text', '(C-' . $this->renderAbsy($inside) . ')'], $terminatingMarkerPos + 1];
	}
}