-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_gh_links.py
202 lines (170 loc) · 7.55 KB
/
test_gh_links.py
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
"""
Github Links - A Python-Markdown Extension.
BSD License
Copyright (c) 2017-2018 by Waylan Limberg. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of HTMLTree nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY WAYLAN LIMBERG ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO Github-Links Extension
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import unittest
from markdown import markdown
from mdx_gh_links import GithubLinks
class TestGithubLinks(unittest.TestCase):
maxDiff = None
def assertMarkdownRenders(self, source, expected, **kwargs):
'Test that source Markdown text renders to expected output.'
configs = {'user': 'Python-Markdown', 'repo': 'github-links'}
configs.update(kwargs)
output = markdown(source, extensions=[GithubLinks(**configs)])
self.assertMultiLineEqual(output, expected)
# Issue Tests
def test_issue(self):
self.assertMarkdownRenders(
'Issue #123.',
'<p>Issue <a class="gh-link gh-issue" '
'href="https://github.com/Python-Markdown/github-links/issues/123" '
'title="GitHub Issue Python-Markdown/github-links #123">#123</a>.</p>',
)
def test_issue_leading_zero(self):
self.assertMarkdownRenders(
'Issue #012.',
'<p>Issue <a class="gh-link gh-issue" '
'href="https://github.com/Python-Markdown/github-links/issues/12" '
'title="GitHub Issue Python-Markdown/github-links #12">#012</a>.</p>',
)
def test_non_issue(self):
self.assertMarkdownRenders(
'Issue #notanissue.',
'<p>Issue #notanissue.</p>',
)
def test_issue_with_repo(self):
self.assertMarkdownRenders(
'Issue Organization/Repo#123.',
'<p>Issue <a class="gh-link gh-issue" '
'href="https://github.com/Organization/Repo/issues/123" '
'title="GitHub Issue Organization/Repo #123">Organization/Repo#123</a>.</p>',
)
def test_issue_leading_zero_with_repo(self):
self.assertMarkdownRenders(
'Issue Organization/Repo#012.',
'<p>Issue <a class="gh-link gh-issue" '
'href="https://github.com/Organization/Repo/issues/12" '
'title="GitHub Issue Organization/Repo #12">Organization/Repo#012</a>.</p>',
)
def test_non_issue_with_repo(self):
self.assertMarkdownRenders(
'Issue Organization/Repo#notanissue.',
'<p>Issue Organization/Repo#notanissue.</p>',
)
def test_escaped_issue(self):
self.assertMarkdownRenders(
'Issue \\#123.',
'<p>Issue #123.</p>',
)
def test_escaped_issue_with_repo(self):
self.assertMarkdownRenders(
'Issue Organization/Repo\\#123.',
'<p>Issue Organization/Repo#123.</p>',
)
def test_issue_in_link(self):
self.assertMarkdownRenders(
'[Issue #123](#).',
'<p><a href="#">Issue #123</a>.</p>',
)
# Mention Tests
def test_mention_user(self):
self.assertMarkdownRenders(
'User @foo.',
'<p>User <a class="gh-link gh-mention" '
'href="https://github.com/foo" '
'title="GitHub User: @foo">@foo</a>.</p>',
)
def test_mention_complex_user(self):
self.assertMarkdownRenders(
'User @Foo_Bar-42.',
'<p>User <a class="gh-link gh-mention" '
'href="https://github.com/Foo_Bar-42" '
'title="GitHub User: @Foo_Bar-42">@Foo_Bar-42</a>.</p>',
)
def test_escape_mention_user(self):
self.assertMarkdownRenders(
'User \\@foo.',
'<p>User @foo.</p>',
)
def test_mention_repo(self):
self.assertMarkdownRenders(
'User @foo/bar.',
'<p>User <a class="gh-link gh-mention" '
'href="https://github.com/foo/bar" '
'title="GitHub Repository: @foo/bar">@foo/bar</a>.</p>',
)
def test_mention_repo_complex(self):
self.assertMarkdownRenders(
'User @foo/bar_baz-42.0.',
'<p>User <a class="gh-link gh-mention" '
'href="https://github.com/foo/bar_baz-42.0" '
'title="GitHub Repository: @foo/bar_baz-42.0">@foo/bar_baz-42.0</a>.</p>',
)
def test_escape_mention_repo(self):
self.assertMarkdownRenders(
'User \\@foo/bar.',
'<p>User @foo/bar.</p>',
)
def test_mention_in_link(self):
self.assertMarkdownRenders(
'User [@foo](#).',
'<p>User <a href="#">@foo</a>.</p>',
)
# Commit Tests
def test_commit(self):
self.assertMarkdownRenders(
'Commit 83fb46b3b7ab8ad4316681fc4637c521da265f1d.',
'<p>Commit <a class="gh-link gh-commit" '
'href="https://github.com/Python-Markdown/github-links/commit/83fb46b3b7ab8ad4316681fc4637c521da265f1d" '
'title="GitHub Commit: Python-Markdown/github-links@83fb46b3b7ab8ad4316681fc4637c521da265f1d"'
'>83fb46b</a>.</p>',
)
def test_commit_user(self):
self.assertMarkdownRenders(
'Commit foo@15abb8b3b02df0e380e9b4c71f3bd206c9751a93.',
'<p>Commit <a class="gh-link gh-commit" '
'href="https://github.com/foo/github-links/commit/15abb8b3b02df0e380e9b4c71f3bd206c9751a93" '
'title="GitHub Commit: foo/github-links@15abb8b3b02df0e380e9b4c71f3bd206c9751a93"''>foo@15abb8b</a>.</p>',
)
def test_commit_user_repo(self):
self.assertMarkdownRenders(
'Commit foo/bar@a75944f869d728ca9bc5472daf3f249b6c341308.',
'<p>Commit <a class="gh-link gh-commit" '
'href="https://github.com/foo/bar/commit/a75944f869d728ca9bc5472daf3f249b6c341308" '
'title="GitHub Commit: foo/bar@a75944f869d728ca9bc5472daf3f249b6c341308">foo/bar@a75944f</a>.</p>',
)
def test_escape_commit(self):
self.assertMarkdownRenders(
'Commit `83fb46b3b7ab8ad4316681fc4637c521da265f1d`.',
'<p>Commit <code>83fb46b3b7ab8ad4316681fc4637c521da265f1d</code>.</p>',
)
def test_commit_in_link(self):
self.assertMarkdownRenders(
'Commit [83fb46b3b7ab8ad4316681fc4637c521da265f1d](#).',
'<p>Commit <a href="#">83fb46b3b7ab8ad4316681fc4637c521da265f1d</a>.</p>',
)
if __name__ == '__main__':
unittest.main()