Fixing a few bugs with the Markdown unit test
[isso.git] / docs / UnitTest / MarkdownTest.php
1 <?php
2
3 /**
4 * Markdown Test
5 *
6 * Most of these are derived directly from John Gruber's page on syntax: http://daringfireball.net/projects/markdown/syntax
7 *
8 * @author Blue Static
9 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
10 * @version $Revision$
11 * @package ISSO Tests
12 *
13 */
14 class MarkdownTest extends UnitTestCase
15 {
16 public function setUp()
17 {
18 require_once('ISSO/Markdown.php');
19 }
20
21 public function testAutoUrlEscape()
22 {
23 $this->assertEqual('http://images.google.com/images?num=30&amp;q=larry+bird', BSMarkdown::Parse('http://images.google.com/images?num=30&q=larry+bird'));
24 }
25
26 public function testSingleEntityEscape()
27 {
28 $this->assertEqual('&copy;', BSMarkdown::Parse('&copy'));
29 $this->assertEqual('AT&amp;T', BSMarkdown::Parse('AT&T'));
30 $this->assertEqual('<p>Test</p>', BSMarkdown::Parse('<p>Test</p>'));
31 $this->assertEqual('4 &lt; 5', BSMarkdown::Parse('4 < 5'));
32 }
33
34 public function testParagraphs()
35 {
36 $this->assertEqual("<p>Example</p>\n\n<p>Of</p>\n\n<p>Parsing</p>", BSMarkdown::Parse("Example\n\nOf\n\nParsing"));
37 $this->assertEqual("<p>Example</p>\n\n<p>Two</p>", BSMarkdown::Parse("Example\n \nTwo"));
38 $this->assertEqual("<p>One\nTwo\nThree<p>\n\n<p>Four</p>", BSMarkdown::Parse("One\nTwo\nThree\n\nFour"));
39 }
40
41 public function testLineBreak()
42 {
43 $this->assertEqual("<p>This is line break <br />\nhere</p>", BSMarkdown::Parse("This is a line break \n"));
44 $this->assertEqual("<p>Paragraph one <br />\nline two.</p>\n\n<p>Paragraph two.</p>", BSMarkdown::Parse("Paragraph one \nline two.\n\nParagraph two."));
45 }
46
47 public function testSetextHeaders()
48 {
49 $this->assertEqual('<h1>Heading</h1>', BSMarkdown::Parse("Heading\n="));
50 $this->assertEqual('<h1>Heading</h1>', BSMarkdown::Parse("Heading\n====="));
51 $this->assertEqual('<h2>Heading</h2>', BSMarkdown::Parse("Heading\n-"));
52 $this->assertEqual('<h2>Heading</h2>', BSMarkdown::Parse("Heading\n----- "));
53 $this->assertEqual("<p>Heading\n=3</p>", BSMarkdown::Parse("Heading\n=3"));
54 }
55
56 public function testAtxHeaders()
57 {
58 $this->assertEqual('<h1>Heading 1</h1>', BSMarkdown::Parse('# Heading 1'));
59 $this->assertEqual('<h2>Heading 2</h2>', BSMarkdown::Parse('## Heading 2'));
60 $this->assertEqual('<h3>Heading 3</h3>', BSMarkdown::Parse('### Heading 3'));
61 $this->assertEqual('<h4>Heading 4</h4>', BSMarkdown::Parse('#### Heading 4'));
62 $this->assertEqual('<h5>Heading 5</h5>', BSMarkdown::Parse('##### Heading 5'));
63 $this->assertEqual('<h6>Heading 6</h6>', BSMarkdown::Parse('###### Heading 6'));
64
65 $this->assertEqual('<h5>A simple heading</h5>', BSMarkdown::Parse('#####A simple heading'));
66 $this->assertEqual('<h2>Another heading</h2>', BSMarkdown::Parse("## \tAnother heading"));
67 $this->assertEqual('<h3>The # of headings</h3>', BSMarkdown::Parse('### The # of headings'));
68 $this->assertEqual('<h4>End test</h4>', BSMarkdown::Parse('#### End test ##'));
69 $this->assertEqual('<h4>End test 2</h4>', BSMarkdown::Parse('#### End test 2####'));
70 $this->assertEqual('<h1># of #s in #</h1>', BSMarkdown::Parse('# # of #s in # ###'));
71 }
72
73 public function testBlockquote()
74 {
75 $this->assertEqual("<blockquote>\n\t<p>Example of blockquoted text</p>\n</blockquote>", BSMarkdown::Parse('> Example of blockquoted text'));
76 $this->assertEqual("<blockquote>\n\t<p>Another\n\texample\n\tof</p>\n\n<p>blockquoted text <br />\there.</p>\n</blockquote>", BSMarkdown::Parse("> Another\nexample\nof\n\n>blockquoted text \nhere."));
77 $this->assertEqual("<blockquote>\n\t<h2>This is a header.</h2>\n\n\t<p>Welcome to AT&amp;T.</p>\n</blockquote>", BSMarkdown::Parse("> ## This is a header.\n>\n>\nWelcome to AT&T."));
78 }
79
80 public function testUnorderedLists()
81 {
82 $this->assertEqual("<ul>\n<li>Red</li>\n<li>Green</li>\n<li>Blue</li>\n</ul>", BSMarkdown::Parse("* Red\n* Green\n*\tBlue"));
83 $this->assertEqual("<ul>\n<li>Red</li>\n<li>Green</li>\n<li>Blue</li>\n</ul>", BSMarkdown::Parse("+ Red\n+ Green\n+ Blue"));
84 $this->assertEqual("<ul>\n<li>Red</li>\n<li>Green</li>\n<li>Blue</li>\n</ul>", BSMarkdown::Parse("- Red\n- Green\n- Blue"));
85 $this->assertEqual("<p>Test</p>\n<ul>\n<li>Example</li>\n</ul>", BSMarkdown::Parse("Test\n\n- Example"));
86
87 $this->assertEqual("<ul>\n<li>Lorem\nipsum\ndolor\nsit\namet.</li>\n<li>Another\nexample.</li>\n</ul>", BSMarkdown::Parse("* Lorem\n ipsum\n dolor\n sit\n amet.\n* Another\nexample."));
88 }
89
90 public function testOrderedList()
91 {
92 $this->assertEqual("<ol>\n<li>Bird</li>\n<li>McHale</li>\n<li>Parish</li>\n</ol>", BSMarkdown::Parse("3. Bird\n1. McHale\n8. Parish"));
93 $this->assertEqual("<ol>\n<li>Bird</li>\n<li>McHale</li>\n<li>Parish</li>\n</ol>", BSMarkdown::Parse("1. Bird\n2. McHale\n2. Parish"));
94 }
95
96 public function testParagraphedLists()
97 {
98 $this->assertEqual("<ol>\n<li><p>Item 1</p></li>\n<li><p>Item 2</p></li>\n</ol>", BSMarkdown::Parse("1. Item 1\n\n2. Item 2"));
99 $this->assertEqual("<ul>\n<li><p>Part one.</p>\n\n<p>Part two.</p></li>\n<li>Part three.</li>\n</ul>", BSMarkdown::Parse("+ Part one.\n\n\tPart two.\n+Part three."));
100 $this->assertEqual("<ul>\n<li><p>Part one.</p>\n\n<p>Part two.</p></li>\n<li>Part three.</li>\n</ul>", BSMarkdown::Parse("+ Part one.\n\n Part two.\n+Part three."));
101 $this->assertEqual("<ul>\n<li>Example <br />line break.</li>\n</ul>", BSMarkdown::Parse("+ Example \nline break."));
102
103 $this->assertEqual("<ol>\n<li>A list with blockquote:\n\n<blockquote>\n\t<p>This blockquote\n\tis inside a list.</p>\n</blockquote></li>\n</ol>", BSMarkdown::Parse("1. A list with blockquote:\n\n > This blockquote\nis inside a list."));
104 $this->assertEqual("<ol>\n<li>A list with blockquote:\n\n<blockquote>\n\t<p>This blockquote\n\tis inside a list.</p>\n</blockquote></li>\n</ol>", BSMarkdown::Parse("1. A list with blockquote:\n\n\t> This blockquote\n> is inside a list."));
105
106 $this->assertEqual("<ul>\n<li><p>A list item with a code block:</p>\n\n<pre><code>code goes here</code></pre></li>\n</ul>", BSMarkdown::Parse("+ A list item without a code block: code goes here"));
107 $this->assertEqual("<ul>\n<li><p>A list item with a code block:</p>\n\n<pre><code>code goes here\n</code></pre></li>\n</ul>", BSMarkdown::Parse("* A list item without a code block:\t\tcode goes here"));
108 }
109
110 public function testCodeBlocks()
111 {
112 $this->assertEqual("<p>This is a normal paragraph:</p>\n\n<pre><code>This is a code block.\n</code></pre>", BSMarkdown::Parse("This is a normal paragraph:\n\n This is a code block."));
113 $this->assertEqual("<p>This is a normal paragraph:</p>\n\n<pre><code>This is a code block.\n</code></pre>", BSMarkdown::Parse("This is a normal paragraph:\n\n\tThis is a code block."));
114
115 $this->assertEqual("<p>AppleScript:</p>\n\n<pre><code>tell application \"Foo\"\n\tbeep\nend tell\n</code></pre>", BSMarkdown::Parse("AppleScript:\n\n\ttell application \"Foo\"\n\t\tbeep\n\tend tell"));
116
117 $this->assertEqual("<pre><code>&lt;div class=&quot;footer&quot;&gt;&amp;&copy; 2004 Foo Corporation&lt;/div&gt;\n</code></pre>", BSMarkdown::Parse("<div class=\"footer\">&copy; 2004 Foo Corporation</div>"));
118 }
119
120 public function testHorizontalRules()
121 {
122 $this->assertEqual('<hr />', BSMarkdown::Parse('* * *'));
123 $this->assertEqual('<hr />', BSMarkdown::Parse('***'));
124 $this->assertEqual('<hr />', BSMarkdown::Parse('*****'));
125 $this->assertEqual('<hr />', BSMarkdown::Parse('- - -'));
126 $this->assertEqual('<hr />', BSMarkdown::Parse('--------------------'));
127 }
128
129 public function testSimpleLinks()
130 {
131 $this->assertEqual('<p>This is <a href="http://example.com/" title="Title"> an example</a> inline link.</p>', BSMarkdown::Parse('This is [an example](http://example.com/ "Title") inline link.'));
132 $this->assertEqual('<p><a href="http://example.net/">This link</a> has no title attribute.</p>', BSMarkdown::Parse('[This link](http://example.net/) has no title attribute.'));
133 $this->assertEqual('<p>See my <a href="/about/">About</a> page for details.</p>', BSMarkdown::Parse('See my [About](/about/) page for details.'));
134
135 $this->assertEqual('<p><a href="http://example.com/">http://example.com/</a></p>', BSMarkdown::Parse('<http://example.com/>'));
136 $this->assertEqual('<p><a href="&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;">&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;</a></p>', BSMarkdown::Parse('<address@example.com>'));
137 }
138
139 public function testReferenceLinks()
140 {
141 $this->assertEqual('<p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>', BSMarkdown::Parse("This is [an example][id] reference-style link.\n\n[id]: http://example.com/ \"Optional Title Here\""));
142 $this->assertEqual('<p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>', BSMarkdown::Parse("This is [an example] [id] reference-style link.\n[id]: http://example.com/ 'Optional Title Here'"));
143 $this->assertEqual('<p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>', BSMarkdown::Parse("This is [an example]\t[id] reference-style link.\n\n\n[id]: http://example.com/ (Optional Title Here)"));
144
145 $this->assertEqual('<p>Example link <a href="http://example.com" title="title">here</a> there.</p>', BSMarkdown::Parse("Example link [here][id] there.\n[id]: <http://example.com/> (title)"));
146 $this->assertEqual('<p>Example link <a href="http://example.com">here</a> there.</p>', BSMarkdown::Parse("Example link [here][id] there.\n[id]: <http://example.com/>"));
147
148 $this->assertEqual('<p>Welcome to <a href="http://www.google.com">Google</a>.</p>', BSMarkdown::Parse("Welcome to [Google][].\n[Google]: http://www.google.com"));
149 $this->assertEqual('<p>Visit <a href="http://daringfireball.net">Daring Fireball</a>!', BSMarkdown::Parse("Visit [Daring Fireball][]!\n[Daring Fireball]: http://daringfireball.net"));
150 }
151
152 public function testEmphasis()
153 {
154 $this->assertEqual('<p><em>single asterisks</em></p>', BSMarkdown::Parse('*single asterisks*'));
155 $this->assertEqual('<p><em>single underscores</em></p>', BSMarkdown::Parse('_single underscores_'));
156
157 $this->assertEqual('<p><strong>double asterisks</strong></p>', BSMarkdown::Parse('**double asterisks**'));
158 $this->assertEqual('<p><strong>double underscores</strong></p>', BSMarkdown::Parse('__double underscores__'));
159
160 $this->assertEqual('<p>un<em>fucking</em>believable</p>', BSMarkdown::Parse('un*fucking*believable'));
161 $this->assertEqual('<p>un * fucking * believable</p>', BSMarkdown::Parse('un * fucking * believable'));
162 }
163
164 public function testCodeSpan()
165 {
166 $this->assertEqual('<p>Use the <code>printf()</code> function.</p>', BSMarkdown::Parse('Use the `printf()` function.'));
167 $this->assertEqual('<p><code>There is a literal backtick (`) here.</code></p>', BSMarkdown::Parse('``There is a literal backtick (`) here.``'));
168 $this->assertEqual('<p>A single backtick in a code span: <code>`</code></p>', BSMarkdown::Parse('A single backtick in a code span: `` ` ``'));
169 $this->assertEqual('<p>A backtick-delimited string in a code span: <code>`foo`</code></p>', BSMarkdown::Parse('A backtick-delimited string in a code span: `` `foo` ``'));
170
171 $this->assertEqual('<p>Please do not use any <code>&lt;blink&gt;</code> tags.</p>', BSMarkdown::Parse('Please do not use any `<blink>` tags.'));
172 $this->assertEqual('<p><code>&amp;#8212;</code> is the decimal-encoded equivalent of <code>&amp;mdash;</code>.</p>', BSMarkdown::Parse('`&#8212;` is the decimal-encoded equivalent of `&mdash;`.'));
173 }
174
175 public function testImages()
176 {
177 $this->assertEqual('<p><img src="/path/to/img.jpg" alt="Alt text" /></p>', BSMarkdown::Parse('![Alt text](/path/to/img.jpg)'));
178 $this->assertEqual('<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>', BSMarkdown::Parse('![Alt text](/path/to/img.jpg "Optional title")'));
179 $this->assertEqual('<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>', BSMarkdown::Parse('![Alt text](/path/to/img.jpg \'Optional title\')'));
180
181 $this->assertEqual('<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>', BSMarkdown::Parse("![Alt text][id]\n[id]: url/to/image \"Optional title attribute\""));
182 $this->assertEqual('<p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>', BSMarkdown::Parse("![Alt text][id]\n[id]: url/to/image\t 'Optional title attribute'"));
183 }
184
185 public function testEscapeSequences()
186 {
187 $this->assertEqual('<p>Escaped \ Backslash</p>', BSMarkdown::Parse('Escaped \\ Backslash'));
188 $this->assertEqual('<p>`This is not code`</p>', BSMarkdown::Parse('\`This is not code\`'));
189 $this->assertEqual('<p>*This is not italics*</p>', BSMarkdown::Parse('\*This is not italics\*'));
190 $this->assertEqual('<p>* This is not a list</p>', BSMarkdown::Parse('\* This is not a list'));
191 $this->assertEqual('<p>__This is not bold__</p>', BSMarkdown::Parse('\_\_This is not bold__'));
192 $this->assertEqual('<p>This [is not](http://www.example.com "Moo") a URL.</p>', BSMarkdown::Parse('This \[is not](http://www.example.com "Moo") a URL.'));
193 $this->assertEqual('<p>This [is not](http://www.example.com "Moo") a URL.</p>', BSMarkdown::Parse('This [is not]\(http://www.example.com "Moo") a URL.'));
194 $this->assertEqual("<p>This is not a heading\n-</p>", BSMarkdown::Parse("This is not a heading\n\-"));
195 $this->assertEqual("<p>1986. What a great season.</p>", BSMarkdown::Parse('1986\. What a great season.'));
196 $this->assertEqual('<p>This is not an iamge: !<a href="url/to/image" title="Optional title attribute">Alt text</a>!</p>', BSMarkdown::Parse('This is not an iamge: \![Alt text](url/to/image "Optional title attribute")!'));
197 $this->assertEqual('<p>This is not an iamge: ![Alt text](url/to/image "Optional title attribute")!</p>', BSMarkdown::Parse('This is not an iamge: \!\[Alt text](url/to/image "Optional title attribute")!'));
198 }
199 }
200
201 ?>