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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2019, Wolf Vollprecht <w.vollprecht@gmail.com>
# 2021, Manuel Genovés <manuel.genoves@gmail.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
### END LICENSE
import unittest
import regex as re
from apostrophe import markup_regex
def regex_tests(test_texts, regex_pattern):
"""Decorator to generate regex test cases for a unittest.TestCase method."""
def decorator(test_method):
def wrapper(self):
for test, expected in test_texts.items():
with self.subTest(test=test):
match = re.search(regex_pattern, test)
if expected is None:
self.assertIsNone(match, msg=f"Test failed: {test}")
else:
self.assertIsNotNone(match, msg=f"Test failed: {test}")
if isinstance(expected, dict):
for key, value in expected.items():
self.assertEqual(match.group(key), value,
msg=f"{test} - group '{key}'")
elif isinstance(expected, bool):
self.assertTrue(match, msg=f"Test failed: {test}")
else:
self.assertEqual(match.group("text"), expected,
msg=f"{test} - text")
return wrapper
return decorator
class TestRegex(unittest.TestCase):
"""Test cases based on CommonMark's specs and demo:
- https://spec.commonmark.org/
- https://spec.commonmark.org/dingus/
CommonMark is the Markdown variant chosen as first-class. It's great and encouraged that
others are supported as well, but when in conflict or undecided, CommonMark should be picked.
"""
@regex_tests(
test_texts={
"*italic*": "italic",
"*i*": "i", # one letter
"This is *italic* text": "italic", # in sentence
"This is *1italic* text": "1italic", # starting with number
"This is *和italic* text": "和italic", # unicode
"before*middle*end": "middle", # within word
"leading * space*": None, # leading space
"This is **italic** text": None, # bold
"This is *italic** text": None, # mismatch
"This is \\*italic** text": None, # escaped
"This is *italic\\* text": None, # escaped
"This is **italic* text": None, # mismatch
"before* middle *end": None,
"before* middle*end": None,
"before*middle *end": "middle ",
"*partial* match*": "partial",
"*multi\nline*": None, # shouldn't match across lines
"empty * * italic": None # empty
},
regex_pattern=markup_regex.ITALIC_ASTERISK
)
def test_italic_asterisk(self):
pass
@regex_tests(
test_texts={
"_italic_": "italic",
"_i_": "i", # one letter
"This is _italic_ text": "italic", # in sentence
"This is _1italic_ text": "1italic", # starting with number
"This is _和italic_ text": "和italic", # unicode
"before_middle_end": None, # within word
"This is __italic__ text": None, # bold
"This is __italic_ text": None, # mismatch
"This is _italic__ text": None, # mismatch
"before_ middle _end": None,
"before_ middle_end": None,
"before_middle _end": None,
"_partial_ match_": "partial",
"_multi\nline_": None, # shouldn't match across lines
"empty _ _ italic": None, # empty
},
regex_pattern=markup_regex.ITALIC_UNDERSCORE
)
def test_italic_underscore(self):
pass
@regex_tests(
test_texts={
"**bold**": "bold",
"__bold__": "bold",
"**b**": "b",
"__b__": "b",
"This is **bold** text": "bold",
"This is **1bold** text": "1bold",
"This is **和bold** text": "和bold",
"This is __bold__ text": "bold",
"before**middle**end": "middle",
"before** middle **end": None,
"before** middle**end": None,
"before**middle **end": "middle ",
"**bold__": None,
"\\**bold**": None,
"**multi\nline**": None, # shouldn't match across lines
"empty * * bold": None
},
regex_pattern=markup_regex.BOLD
)
def test_bold(self):
pass
@regex_tests(
test_texts={
"**_text_**": "text",
"***text***": "text",
"___text___": "text",
"__*text*__": "text",
"__*text_**": None,
"__*text__*": None,
"***t***": "t",
"___t___": "t",
"This is ***text*** text": "text",
"This is ***1text*** text": "1text",
"This is ***和text*** text": "和text",
"This is ___text___ text": "text",
"before***middle***end": "middle",
"before*** middle ***end": None,
"before*** middle***end": None,
"before***middle ***end": "middle ",
"empty * * text": None
},
regex_pattern=markup_regex.BOLD_ITALIC
)
def test_bold_italic(self):
pass
@regex_tests(
test_texts={
"~~text~~": "text",
"~~text text~~": "text text",
"~~text~": None,
"~~t~~": "t",
"~~text ~~": None,
"~~ text~~": None,
"\\~~text~~": None,
"~~text\\~~": None,
"empty ~~ ~~ text": None
},
regex_pattern=markup_regex.STRIKETHROUGH
)
def test_strikethrough(self):
pass
@regex_tests(
test_texts={
"`code`": "code",
"``code with ` backtick``": "code with ` backtick",
"```multi tick```": "multi tick",
"` leading space`": " leading space",
"`trailing space `": "trailing space ",
"````": None,
"`": None,
"`` code `": None,
"`\nmulti line`": None
},
regex_pattern=markup_regex.CODE
)
def test_code(self):
pass
@regex_tests(
test_texts={
"[text](url)": {"text": "text", "url": "url"},
"[test](http://example.com)": {"text": "test", "url": "http://example.com"},
"[test](url \"title\")": {"text": "test", "url": "url", "title": "title"},
"[empty]()": None,
"[invalid](url": None,
"[](url)": {"text": "", "url": "url"},
# "[text](url \"title)": None,
"[text] (url)": None
},
regex_pattern=markup_regex.LINK
)
def test_link(self):
pass
@regex_tests(
test_texts={
"": {"text": "alt", "url": "img.jpg"},
"": {"text": "test", "url": "http://example.com/image.png", "title": "title"},
"![empty]()": None,
"!invalid](url)": None,
"
def test_image(self):
pass
@regex_tests(
test_texts={
"---": True,
"***": True,
"___": True,
"- - -": True,
"--": None,
"=====": None,
"* *": None,
"*****": True,
" *** ": True,
"\t---\t": None,
"-*--": None
},
regex_pattern=markup_regex.HORIZONTAL_RULE
)
def test_hr(self):
pass
@regex_tests(
test_texts={
# Valid checklist items
"- [ ] item": {"check": " ", "text": "item"},
"+ [X] done": {"check": "X", "text": "done"},
# Invalid checklist items
"- [] missing check": None,
"[ ] standalone": None,
"-[ ] no space": None,
"- [x]extra": None,
},
regex_pattern=markup_regex.CHECKLIST
)
def test_checklist(self):
pass
@regex_tests(
test_texts={
"# Header 1": "Header 1",
"## Header 2": "Header 2",
"### Header 3": "Header 3",
"#### Header 4": "Header 4",
"##### Header 5": "Header 5",
"###### Header 6": "Header 6",
"#Header 1": None,
" # Header 1": None,
"#": None,
"#######": None,
"before\n# Header\nafter": "Header"
},
regex_pattern=markup_regex.HEADER
)
def test_header(self):
pass
@regex_tests(
test_texts={
"Header 1\n=": None, # techincally correct, but better from an usability POV
"Header 1##\n=": None,
"Header 1\n=\n": "Header 1",
"Header 1##\n=\n": "Header 1##",
"Header 2\n-- \n": "Header 2",
"Header 1\n=f": None,
"Header 1\n =": None
},
regex_pattern=markup_regex.HEADER_UNDER
)
def test_header_under(self):
pass
@regex_tests(
test_texts={
"- item": {"indent": "", "symbol": "-", "text": "item"},
"\t+ item": {"indent": "\t", "symbol": "+", "text": "item"},
"*item": None,
"not a list": None
},
regex_pattern=markup_regex.LIST
)
def test_list(self):
pass
@regex_tests(
test_texts={
"1. item": {"indent": "", "prefix": "1.", "number": "1", "delimiter": ".", "text": "item"},
"a) item": {"indent": "", "prefix": "a)", "delimiter": ")", "text": "item"},
" 2) indented": {"indent": " ", "prefix": "2)", "number": "2", "delimiter": ")", "text": "indented"},
"1 item": None
},
regex_pattern=markup_regex.ORDERED_LIST
)
def test_ordered_list(self):
pass
@regex_tests(
test_texts={
"> blockquote": "blockquote",
" > spaced blockquote": "spaced blockquote",
"no blockquote": None
},
regex_pattern=markup_regex.BLOCK_QUOTE
)
def test_block_quote(self):
pass
@regex_tests(
test_texts={
"```code block```": "code block",
"~~~example~~~": "example",
"```not closed": None
},
regex_pattern=markup_regex.CODE_BLOCK
)
def test_code_block(self):
pass
@regex_tests(
test_texts={
"$a$": "a",
"$ab$": "ab",
"$a b$": "a b",
"$ a$": None,
"$abc": None,
"$$math$$": "math"
},
regex_pattern=markup_regex.MATH
)
def test_math(self):
pass
@regex_tests(
test_texts={
"ref[^1]": {"text": "ref", "id": "1"},
"note[^note]": {"text": "note", "id": "note"},
"[^id]": None
},
regex_pattern=markup_regex.FOOTNOTE_ID
)
def test_footnote_id(self):
pass
@regex_tests(
test_texts={
"[^1]: This is a footnote.\n": {"id": "1", "text": "This is a footnote."},
" [^note]: Footnote text\n": {"id": "note", "text": "Footnote text"},
"[^2]: First line\n second line\n": {"id": "2", "text": "First line\n second line"},
"Not a footnote": None
},
regex_pattern=markup_regex.FOOTNOTE
)
def test_footnote(self):
pass
@regex_tests(
test_texts={
"---\ntitle: Test\ndate: 2025-03-06\n---\n": "title: Test\ndate: 2025-03-06",
"---\ntitle: Another\ndate: 2025-03-06\n...\n": "title: Another\ndate: 2025-03-06",
"----\ntitle: Test\ntitle: Test\n----\n": None
},
regex_pattern=markup_regex.FRONTMATTER
)
def test_frontmatter(self):
pass
if __name__ == '__main__':
unittest.main()
|