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
|
"""Utilities."""
import sys
import os
import unittest
import textwrap
import markdown
import difflib
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
if sys.platform.startswith('win'):
_PLATFORM = "windows"
elif sys.platform == "darwin":
_PLATFORM = "osx"
else:
_PLATFORM = "linux"
def is_win(): # pragma: no cover
"""Is Windows."""
return _PLATFORM == "windows"
def is_linux(): # pragma: no cover
"""Is Linux."""
return _PLATFORM == "linux"
def is_mac(): # pragma: no cover
"""Is macOS."""
return _PLATFORM == "osx"
class MdCase(unittest.TestCase):
"""Markdown unittest test case base."""
extension = []
extension_configs = {}
base = CURRENT_DIR
def setUp(self):
"""Setup."""
for k1, v1 in self.extension_configs.items():
if v1 is not None:
for k2, v2 in v1.items():
if isinstance(v2, str):
v1[k2] = v2.replace(
'{{BASE}}', self.base
).replace(
'{{RELATIVE}}', CURRENT_DIR
)
self.extension_configs[k1] = v1
self.md = markdown.Markdown(extensions=self.extension, extension_configs=self.extension_configs)
def dedent(self, text, strip=False):
"""Reduce indentation."""
return textwrap.dedent(text).strip('\n') if strip else textwrap.dedent(text)
def check_markdown(self, text, expected, dedent=False):
"""Check the markdown."""
if dedent:
# For markdown, beginning and ending new lines get stripped out with
# no issues, but for HTML (expected), we need to trim.
# If there are tests that are newline sensitive, it may make sense
# to call dedent directly to control this.
text = self.dedent(text)
expected = self.dedent(expected, True)
results = self.md.convert(text)
diff = [
l for l in difflib.unified_diff(
expected.splitlines(True),
results.splitlines(True),
'Expected',
'Actual',
n=3
)
]
print(''.join(diff))
self.assertTrue(not diff)
|