Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aab0d497e03e93b8e1ff658387f28a0d33f98535 (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
/*******************************************************************************
 * Copyright (c) 2013 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 junit.framework.TestCase;

import org.eclipse.mylyn.internal.wikitext.markdown.core.LinkDefinition;
import org.eclipse.mylyn.internal.wikitext.markdown.core.LinkDefinitionParser;

/**
 * @author Stefan Seelmann
 */
public class LinkDefinitionParserTest extends TestCase {

	private LinkDefinitionParser linkDefinitionParser;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		linkDefinitionParser = new LinkDefinitionParser();
	}

	public void testWithoutTitle() {
		String markup = "[foo]: http://example.com/";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertNull(linkDefinition.getTitle());
	}

	public void testDoubleQuotedTitle() {
		String markup = "[foo]: http://example.com/ \"Optional Title Here\"";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testSingleQuotedTitle() {
		String markup = "[foo]: http://example.com/ 'Optional Title Here'";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testParenthesedTitle() {
		String markup = "[foo]: http://example.com/ (Optional Title Here)";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testUrlInAngleBrackets() {
		String markup = "[foo]: <http://example.com/> (Optional Title Here)";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testTitleCanBePutOnNextLine() {
		String markup = "   [foo]: <http://example.com/>\n      (Optional Title Here)";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testMayStartWithUpToThreeSpaces() {
		String markup = "   [foo]: <http://example.com/> (Optional Title Here)";
		linkDefinitionParser.parse(markup);
		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertEquals("Optional Title Here", linkDefinition.getTitle());
	}

	public void testMayNotStartWithMoreThanThreeSpaces() {
		String markup = "    [foo]: http://example.com/";
		linkDefinitionParser.parse(markup);
		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
	}

	public void testNoMatchInMiddleOfLine() {
		String markup = "Lorem [foo]: http://example.com/ ipsum.";
		linkDefinitionParser.parse(markup);
		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(linkDefinition);
	}

	public void testNotCaseSensitive() {
		String markup = "[foo]: http://example.com/";
		linkDefinitionParser.parse(markup);

		LinkDefinition linkDefinition = linkDefinitionParser.getLinkDefinition("FoO");
		assertNotNull(linkDefinition);
		assertEquals("foo", linkDefinition.getId());
		assertEquals("http://example.com/", linkDefinition.getUrl());
		assertNull(linkDefinition.getTitle());
	}

	public void testMultiline() {
		String markup = "aaa\n\n  [foo]: http://foo.com/\n  [bar]: http://bar.com/\n\nbbb";
		linkDefinitionParser.parse(markup);
		LinkDefinition fooLinkDefinition = linkDefinitionParser.getLinkDefinition("foo");
		assertNotNull(fooLinkDefinition);
		assertEquals("foo", fooLinkDefinition.getId());
		assertEquals("http://foo.com/", fooLinkDefinition.getUrl());
		assertNull(fooLinkDefinition.getTitle());
		LinkDefinition barLinkDefinition = linkDefinitionParser.getLinkDefinition("bar");
		assertNotNull(barLinkDefinition);
		assertEquals("bar", barLinkDefinition.getId());
		assertEquals("http://bar.com/", barLinkDefinition.getUrl());
		assertNull(barLinkDefinition.getTitle());
	}
}

Back to the top