MarkdownTokenParserTest.php
3.08 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace Aptoma\Twig\TokenParser;
use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
use Aptoma\Twig\Node\MarkdownNode;
/**
* @author Gunnar Lium <gunnar@aptoma.com>
*/
class MarkdownTokenParserTest extends \Twig_Test_NodeTestCase
{
public function testConstructor()
{
$body = new \Twig_Node(array(new \Twig_Node_Text("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);
$this->assertEquals($body, $node->getNode('body'));
}
/**
* Test that the generated code actually do what we expect
*
* The contents of this test is the same that we write in the compile method.
* This requires manual synchronization, which we should probably not rely on.
*/
public function testMarkdownPrepareBehavior()
{
$body = " #Title\n\n paragraph\n\n code";
$bodyPrepared = "#Title\n\nparagraph\n\n code";
ob_start();
echo $body;
$content = ob_get_clean();
preg_match("/^\s*/", $content, $matches);
$lines = explode("\n", $content);
$content = preg_replace('/^' . $matches[0]. '/', "", $lines);
$content = join("\n", $content);
// Assert prepared content looks right
$this->assertEquals($bodyPrepared, $content);
// Assert Markdown output
$expectedOutput = "<h1>Title</h1>\n\n<p>paragraph</p>\n\n<pre><code>code\n</code></pre>\n";
$this->assertEquals($expectedOutput, $this->getEngine()->transform($content));
}
/**
* Test that the generated code looks as expected
*
* @dataProvider getTests
*/
public function testCompile($node, $source, $environment = null)
{
parent::testCompile($node, $source, $environment);
}
protected function getEngine()
{
return new MichelfMarkdownEngine();
}
public function getTests()
{
$tests = array();
$body = new \Twig_Node(array(new \Twig_Node_Text("#Title\n\nparagraph\n", 1)));
$node = new MarkdownNode($body, 1);
$tests['simple text'] = array($node, <<<EOF
// line 1
ob_start();
echo "#Title
paragraph
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getTokenParsers()->getTokenParser('markdown')->getEngine()->transform(\$content);
EOF
);
$body = new \Twig_Node(array(new \Twig_Node_Text(" #Title\n\n paragraph\n\n code\n", 1)));
$node = new MarkdownNode($body, 1);
$tests['text with leading indent'] = array($node, <<<EOF
// line 1
ob_start();
echo " #Title
paragraph
code
";
\$content = ob_get_clean();
preg_match("/^\s*/", \$content, \$matches);
\$lines = explode("\\n", \$content);
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
\$content = join("\\n", \$content);
echo \$this->env->getTokenParsers()->getTokenParser('markdown')->getEngine()->transform(\$content);
EOF
);
return $tests;
}
}