-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
115 lines (87 loc) · 3.55 KB
/
tests.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
from unittest import TestCase
import xmltodict
from markdown import Markdown
from markdown.util import etree
from mdx_variables import VariablesExtension, VariablePattern, makeExtension
class XmlTestCaseMixin(object):
""" Helper class for asserting that XML documents describe the same XML
structures.
"""
def mk_doc(self, s):
return etree.fromstring(
"<div>" + s.strip() + "</div>")
def assert_xml_equal(self, a, b):
self.assertEqual(
xmltodict.parse(etree.tostring(a)),
xmltodict.parse(etree.tostring(b)))
def assert_xmltext_equal(self, a, b):
self.assert_xml_equal(self.mk_doc(a), self.mk_doc(b))
class TestVariablesPattern(XmlTestCaseMixin, TestCase):
""" Test VariablesPattern and handling of variables. """
def mk_markdown(self, conf):
md = Markdown()
ext = VariablesExtension(conf)
ext.extendMarkdown(md)
return md
def test_known_variable_function(self):
def bar():
return "bar"
md = self.mk_markdown({'vars': {'foo': bar}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>bar</p>")
def test_known_variable_string(self):
md = self.mk_markdown({'vars': {'foo': "zoom"}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>zoom</p>")
def test_known_variable_integer(self):
md = self.mk_markdown({'vars': {'foo': 5}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>5</p>")
def test_bad_variable(self):
def err():
raise RuntimeError("should be caught")
md = self.mk_markdown({'vars': {'foo': err}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>BAD VARIABLE: foo</p>")
def test_unknown_variable_without_default(self):
md = self.mk_markdown({'vars': {}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>MISSING VARIABLE: foo</p>")
def test_unknown_variable_with_default(self):
def default(varname):
return "got %s" % (varname,)
md = self.mk_markdown({'vars': {'__getattr__': default}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>got foo</p>")
def test_bad_default(self):
def default_err(varname):
raise RuntimeError("Should be caught")
md = self.mk_markdown({'vars': {'__getattr__': default_err}})
xml = md.convert("${foo}")
self.assert_xmltext_equal(xml, "<p>BAD VARIABLE: foo</p>")
class TestVariablesExtension(TestCase):
""" Test VariablesExtension class. """
def mk_markdown(self):
md = Markdown()
return md
def assert_registered(self, md):
pattern = md.inlinePatterns['variable']
self.assertTrue(isinstance(pattern, VariablePattern))
def assert_not_registered(self, md):
self.assertFalse('variable' in md.inlinePatterns)
def text_create(self):
ext = VariablesExtension({'a': 'b'})
self.assertEqual(ext.conf, {'a': 'b'})
def test_extend_markdown(self):
md = self.mk_markdown()
ext = VariablesExtension({})
self.assert_not_registered(md)
ext.extendMarkdown(md)
self.assert_registered(md)
class TestExtensionRegistration(TestCase):
""" Test registration of variables extension. """
def test_make_extension(self):
configs = {'a': 'b'}
ext = makeExtension(**configs)
self.assertTrue(isinstance(ext, VariablesExtension))
self.assertEqual(ext.conf, configs)