Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f84e584d1aa97a9b3f2ea70c6d3a495af40d0de (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*******************************************************************************
 * Copyright (c) 2012 Stefan Seelmann and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Stefan Seelmann - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.wikitext.markdown.tests;

import org.eclipse.mylyn.wikitext.tests.TestUtil;

/**
 * Tests for Markdown block elements. Follows specification at
 * <a>http://daringfireball.net/projects/markdown/syntax#block</a>.
 * 
 * @author Stefan Seelmann
 */
public class MarkdownLanguageBlockElementsTest extends MarkdownLanguageTestBase {

	/*
	 * Paragraphs and Line Breaks. A paragraph is simply one or more consecutive lines of text, separated by one or more
	 * blank lines. (A blank line is any line that looks like a blank line — a line containing nothing but spaces or
	 * tabs is considered blank.) Normal paragraphs should not be indented with spaces or tabs.
	 */
	public void testParagraphWithOneLine() {
		String html = parseToHtml("a paragraph");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>a paragraph</p>\n", html);
	}

	public void testParagraphWithMulitpleLines() {
		String html = parseToHtml("a paragraph\nwith multiple\nlines");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>a paragraph\nwith multiple\nlines</p>\n", html);
	}

	public void testParagraphsSeparatedBySingleBlankLine() {
		String html = parseToHtml("a paragraph\n\nanother paragraph\n\n");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>a paragraph</p>\n<p>another paragraph</p>\n", html);
	}

	public void testParagraphsSeparatedByMulitpleBlankLines() {
		String html = parseToHtml("a paragraph\n\n\nanother paragraph\n\n\n");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>a paragraph</p>\n<p>another paragraph</p>\n", html);
	}

	public void testParagraphsSeparatedByMulitpleBlankLinesWithSpacesAndTabs() {
		String html = parseToHtml("a paragraph\n \n\t\nanother paragraph");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>a paragraph</p>\n<p>another paragraph</p>\n", html);
	}

	/*
	 * When you do want to insert a <br />
	 * break tag using Markdown, you end a line with two or more spaces, then type return.
	 */
	public void testLineBreakInParagraph() {
		String html = parseToHtml("line  1  \nline  2    \nline  3");
		TestUtil.println("HTML: " + html);
		assertEquals("<p>line  1<br/>\nline  2<br/>\nline  3</p>\n", html);
	}

	/*
	 * Headers. Atx-style headers use 1-6 hash characters at the start of the line, corresponding to header levels 1-6.
	 */
	public void testAtxStyleHeaderH1() {
		String h1 = parseToHtml("# This is an H1");
		TestUtil.println("HTML: " + h1);
		assertEquals("<h1>This is an H1</h1>", h1);
	}

	public void testAtxStyleHeaderH2() {
		String h2 = parseToHtml("## This is an H2");
		TestUtil.println("HTML: " + h2);
		assertEquals("<h2>This is an H2</h2>", h2);
	}

	public void testAtxStyleHeaderH3() {
		String h3 = parseToHtml("### This is an H3");
		TestUtil.println("HTML: " + h3);
		assertEquals("<h3>This is an H3</h3>", h3);
	}

	public void testAtxStyleHeaderH4() {
		String h4 = parseToHtml("#### This is an H4");
		TestUtil.println("HTML: " + h4);
		assertEquals("<h4>This is an H4</h4>", h4);
	}

	public void testAtxStyleHeaderH5() {
		String h5 = parseToHtml("##### This is an H5");
		TestUtil.println("HTML: " + h5);
		assertEquals("<h5>This is an H5</h5>", h5);
	}

	public void testAtxStyleHeaderH6() {
		String h6 = parseToHtml("###### This is an H6");
		TestUtil.println("HTML: " + h6);
		assertEquals("<h6>This is an H6</h6>", h6);
	}

	/*
	 * Optionally, you may "close" atx-style headers. This is purely cosmetic - you can use this if you think it looks
	 * better. The closing hashes don't even need to match the number of hashes used to open the header. (The number of
	 * opening hashes determines the header level.)
	 */
	public void testClosedAtxStyleHeaderH1() {
		String h1 = parseToHtml("# This is an H1 #");
		TestUtil.println("HTML: " + h1);
		assertEquals("<h1>This is an H1</h1>", h1);
	}

	public void testClosedAtxStyleHeaderH2() {
		String h2 = parseToHtml("## This is an H2 ##");
		TestUtil.println("HTML: " + h2);
		assertEquals("<h2>This is an H2</h2>", h2);
	}

	public void testClosedAtxStyleHeaderH3() {
		String h3 = parseToHtml("### This is an H3 ###");
		TestUtil.println("HTML: " + h3);
		assertEquals("<h3>This is an H3</h3>", h3);
	}

	public void testClosedAtxStyleHeaderH4() {
		String h4 = parseToHtml("#### This is an H4 ####");
		TestUtil.println("HTML: " + h4);
		assertEquals("<h4>This is an H4</h4>", h4);
	}

	public void testClosedAtxStyleHeaderH5() {
		String h5 = parseToHtml("##### This is an H5 #####");
		TestUtil.println("HTML: " + h5);
		assertEquals("<h5>This is an H5</h5>", h5);
	}

	public void testClosedAtxStyleHeaderH6() {
		String h6 = parseToHtml("###### This is an H6 ######");
		TestUtil.println("HTML: " + h6);
		assertEquals("<h6>This is an H6</h6>", h6);
	}

	public void testClosedAtxStyleHeaderWithMoreClosingHashes() {
		String h1 = parseToHtml("# This is an H1 ################################");
		TestUtil.println("HTML: " + h1);
		assertEquals("<h1>This is an H1</h1>", h1);
	}

	public void testClosedAtxStyleHeaderWithLessCosingHashes() {
		String h6 = parseToHtml("###### This is an H6 #");
		TestUtil.println("HTML: " + h6);
		assertEquals("<h6>This is an H6</h6>", h6);
	}

	/*
	 * Setext-style headers are "underlined" using equal signs (for first-level headers) and dashes (for second-level
	 * headers). Any number of underlining ='s or -'s will work.
	 */
	public void testUnderlinedHeaderH1() {
		String h1 = parseToHtml("This is an H1\n============");
		TestUtil.println("HTML: " + h1);
		assertEquals("<h1>This is an H1</h1>", h1);
	}

	public void testUnderlinedHeaderH2() {
		String h2 = parseToHtml("This is an H2\n------------");
		TestUtil.println("HTML: " + h2);
		assertEquals("<h2>This is an H2</h2>", h2);
	}

	public void testSingleCharUnderlinedHeaderH1() {
		String h1 = parseToHtml("This is an H1\n= ");
		TestUtil.println("HTML: " + h1);
		assertEquals("<h1>This is an H1</h1>", h1);
	}

	public void testSingleCharUnderlinedHeaderH2() {
		String h2 = parseToHtml("This is an H2\n- ");
		TestUtil.println("HTML: " + h2);
		assertEquals("<h2>This is an H2</h2>", h2);
	}

	/*
	 * Blockquotes. Markdown uses email-style > characters for blockquoting. It looks best if you hard wrap the text and
	 * put a > before every line.
	 */
	public void testBlockquoteWithQuoteCharInEachLine() {
		String h1 = parseToHtml("> Lorem ipsum dolor sit amet, \n>  consetetur adipisici elit.\n");
		TestUtil.println("HTML: " + h1);
		assertEquals("<blockquote><p>Lorem ipsum dolor sit amet, \nconsetetur adipisici elit.</p>\n</blockquote>", h1);
	}

	/*
	 * Markdown allows you to be lazy and only put the > before the first line of a hard-wrapped paragraph.
	 */
	public void testBlockquoteWithSingleQuoteChar() {
		String h1 = parseToHtml("> Lorem ipsum dolor sit amet, \nconsetetur adipisici elit.\n");
		TestUtil.println("HTML: " + h1);
		assertEquals("<blockquote><p>Lorem ipsum dolor sit amet, \nconsetetur adipisici elit.</p>\n</blockquote>", h1);
	}

	/*
	 * Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by adding additional levels of >.
	 */
	public void testNestedBlockquotes() {
		String h1 = parseToHtml(">A1\n>>B1\n>A2\n");
		TestUtil.println("HTML: " + h1);
		assertEquals("<blockquote><p>A1</p>\n<blockquote><p>B1</p>\n</blockquote><p>A2</p>\n</blockquote>", h1);
	}

	/*
	 * Blockquotes can contain other Markdown elements, including headers, lists, and code blocks.
	 */
	public void testBlockquotesWithOtherElements() {
		String h1 = parseToHtml(">#H1");
		TestUtil.println("HTML: " + h1);
		assertEquals("<blockquote><h1>H1</h1></blockquote>", h1);
	}

	/*
	 * Markdown wraps a code block in both pre and code tags. To produce a code block in Markdown, simply indent every
	 * line of the block by at least 4 spaces or 1 tab.
	 */
	public void testCodeBlockIndentedByFourSpaces() {
		String html = parseToHtml("    This is a code block.");
		TestUtil.println("HTML: " + html);
		assertEquals("<pre><code>This is a code block.</code></pre>", html);
	}

	public void testCodeBlockIndentedByOneTab() {
		String html = parseToHtml("\tThis is a code block.");
		TestUtil.println("HTML: " + html);
		assertEquals("<pre><code>This is a code block.</code></pre>", html);
	}

	/*
	 * One level of indentation - 4 spaces or 1 tab - is removed from each line of the code block.
	 */
	public void testCodeBlockMultiLineIndentedByFourSpaces() {
		String html = parseToHtml("    aaa\n        bbb\n            ccc\n    \n    continue after empty line");
		TestUtil.println("HTML: " + html);
		String expectedHtml = "<pre><code>aaa\n    bbb\n        ccc\n\ncontinue after empty line</code></pre>";
		assertEquals(expectedHtml, html);
	}

	public void testCodeBlockMultiLineIndentedByOneTab() {
		String html = parseToHtml("\taaa\n\t\tbbb\n\t\t\tccc\n\t\n\tcontinue after empty line");
		TestUtil.println("HTML: " + html);
		String expectedHtml = "<pre><code>aaa\n    bbb\n        ccc\n\ncontinue after empty line</code></pre>";
		assertEquals(expectedHtml, html);
	}

	/*
	 * Within a code block, ampersands (&) and angle brackets (< and >) are automatically converted into HTML entities.
	 */
	public void testSpecialCharactersAreConvertedInCodeBlock() {
		String html = parseToHtml("    <div class=\"footer\">\n    &copy; 2004 Foo Bar\n    </div>");
		TestUtil.println("HTML: " + html);
		String exptectedHtml = "<pre><code>&lt;div class=\"footer\"&gt;\n&amp;copy; 2004 Foo Bar\n&lt;/div&gt;</code></pre>";
		assertEquals(exptectedHtml, html);
	}

	/*
	 * Regular Markdown syntax is not processed within code blocks.
	 */
	public void testNoProcessingInCodeBlock() {
		String html = parseToHtml("    ### Header 3\n    Lorem *ipsum*");
		TestUtil.println("HTML: " + html);
		assertEquals("<pre><code>### Header 3\nLorem *ipsum*</code></pre>", html);
	}

	/*
	 * Horizontal Rules. You can produce a horizontal rule tag ( hr/ ) by placing three or more hyphens, asterisks, or
	 * underscores on a line by themselves. If you wish, you may use spaces between the hyphens or asterisks.
	 */
	public void testHorizontalRulesWithAsterisksAndSpaces() {
		String html = parseToHtml("* * *");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}

	public void testHorizontalRulesWithAsterisks() {
		String html = parseToHtml("***");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}

	public void testHorizontalRulesWithMoreAsterisks() {
		String html = parseToHtml("*****");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}

	public void testHorizontalRulesWithHyphensAndSpaces() {
		String html = parseToHtml("- - -");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}

	public void testHorizontalRulesWithHyphens() {
		String html = parseToHtml("---------------------------------------");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}

	public void testHorizontalRulesWithUnderscores() {
		String html = parseToHtml("___");
		TestUtil.println("HTML: " + html);
		assertEquals("<hr/>", html);
	}
}

Back to the top