Debian Codegraph @

tests/util.py

raw
1 """Utilities."""
2 import yaml
3 from collections import OrderedDict
4 import sys
5 import os
6 import unittest
7 import textwrap
8 import markdown
9 import difflib
10
11 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
12
13 if sys.platform.startswith('win'):
14 _PLATFORM = "windows"
15 elif sys.platform == "darwin":
16 _PLATFORM = "osx"
17 else:
18 _PLATFORM = "linux"
19
20
21 def is_win(): # pragma: no cover
22 """Is Windows."""
23
24 return _PLATFORM == "windows"
25
26
27 def is_linux(): # pragma: no cover
28 """Is Linux."""
29
30 return _PLATFORM == "linux"
31
32
33 def is_mac(): # pragma: no cover
34 """Is macOS."""
35
36 return _PLATFORM == "osx"
37
38
39 class MdCase(unittest.TestCase):
40 """Markdown unittest test case base."""
41
42 extension = []
43 extension_configs = {}
44 base = CURRENT_DIR
45
46 def setUp(self):
47 """Setup."""
48
49 for k1, v1 in self.extension_configs.items():
50 if v1 is not None:
51 for k2, v2 in v1.items():
52 if isinstance(v2, str):
53 v1[k2] = v2.replace(
54 '{{BASE}}', self.base
55 ).replace(
56 '{{RELATIVE}}', CURRENT_DIR
57 )
58 self.extension_configs[k1] = v1
59
60 self.md = markdown.Markdown(extensions=self.extension, extension_configs=self.extension_configs)
61
62 def dedent(self, text, strip=False):
63 """Reduce indentation."""
64
65 return textwrap.dedent(text).strip('\n') if strip else textwrap.dedent(text)
66
67 def check_markdown(self, text, expected, dedent=False):
68 """Check the markdown."""
69
70 if dedent:
71 # For markdown, beginning and ending new lines get stripped out with
72 # no issues, but for HTML (expected), we need to trim.
73 # If there are tests that are newline sensitive, it may make sense
74 # to call dedent directly to control this.
75 text = self.dedent(text)
76 expected = self.dedent(expected, True)
77
78 results = self.md.convert(text)
79
80 diff = list(
81 difflib.unified_diff(
82 expected.splitlines(True),
83 results.splitlines(True),
84 'Expected',
85 'Actual',
86 n=3
87 )
88 )
89
90 print(''.join(diff))
91 self.assertTrue(not diff)
92
93
94 def yaml_load(stream, loader=yaml.Loader, object_pairs_hook=OrderedDict):
95 """
96 Custom YAML loader.
97
98 Make all YAML dictionaries load as ordered Dicts.
99 http://stackoverflow.com/a/21912744/3609487
100
101 Load all strings as unicode.
102 http://stackoverflow.com/a/2967461/3609487
103 """
104
105 def construct_mapping(loader, node):
106 """Convert to ordered dict."""
107
108 loader.flatten_mapping(node)
109 return object_pairs_hook(loader.construct_pairs(node))
110
111 def construct_yaml_str(self, node):
112 """Override the default string handling function to always return unicode objects."""
113
114 return self.construct_scalar(node)
115
116 class Loader(loader):
117 """Custom Loader."""
118
119 Loader.add_constructor(
120 yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
121 construct_mapping
122 )
123
124 Loader.add_constructor(
125 'tag:yaml.org,2002:str',
126 construct_yaml_str
127 )
128
129 return yaml.load(stream, Loader)