-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
118 lines (96 loc) · 3.76 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
116
117
118
from unittest import TestCase
import xmltodict
from markdown import Markdown
from markdown.util import etree
from mdx_attr_cols import AttrColTreeProcessor, AttrColExtension, makeExtension
class XmlTestCaseMixin(object):
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)))
class TestAttrColTreeProcessor(XmlTestCaseMixin, TestCase):
def mk_processor(self, **conf):
md = Markdown()
return AttrColTreeProcessor(md, conf)
def test_config_none(self):
md = Markdown
p = AttrColTreeProcessor(md, None)
self.assertEqual(p.columns, 12)
self.assertEqual(p.attr, 'cols')
self.assertEqual(p.tags, set(['section']))
def test_config_defaults(self):
p = self.mk_processor()
self.assertEqual(p.columns, 12)
self.assertEqual(p.attr, 'cols')
self.assertEqual(p.tags, set(['section']))
def test_config_overrides(self):
p = self.mk_processor(
columns=16,
attr='columns',
tags=['section', 'div'],
)
self.assertEqual(p.columns, 16)
self.assertEqual(p.attr, 'columns')
self.assertEqual(p.tags, set(['section', 'div']))
def test_simple_rows(self):
root = self.mk_doc("""
<section cols='4'>Foo</section>
<section cols='6'>Bar</section>
<section cols='2'>Beep</section>
""")
p = self.mk_processor()
new_root = p.run(root)
self.assert_xml_equal(new_root, self.mk_doc("""
<div class="row"><div class="col-md-4"><section>Foo</section>
</div><div class="col-md-6"><section>Bar</section>
</div><div class="col-md-2"><section>Beep</section>
</div></div>
"""))
class TestAttrColExtension(TestCase):
def mk_markdown(self, extensions=None):
if extensions is None:
extensions = ['attr_list', 'mdx_outline']
md = Markdown(extensions=extensions)
return md
def assert_registered(self, md):
processor = md.treeprocessors['attr_cols']
self.assertTrue(isinstance(processor, AttrColTreeProcessor))
def assert_not_registered(self, md):
self.assertFalse('attr_cols' in md.treeprocessors)
def text_create(self):
ext = AttrColExtension({'a': 'b'})
self.assertEqual(ext.conf, {'a': 'b'})
def test_extend_markdown(self):
md = self.mk_markdown()
ext = AttrColExtension({})
ext.extendMarkdown(md)
self.assert_registered(md)
def test_missing_attr_list(self):
md = self.mk_markdown(['mdx_outline'])
ext = AttrColExtension({})
self.assertRaisesRegexp(
RuntimeError,
"The attr_cols markdown extension depends the following"
" extensions which must preceded it in the extension list:"
" attr_list, mdx_outline",
ext.extendMarkdown, md)
self.assert_not_registered(md)
def test_missing_outline(self):
md = self.mk_markdown([])
ext = AttrColExtension({})
self.assertRaisesRegexp(
RuntimeError,
"The attr_cols markdown extension depends the following"
" extensions which must preceded it in the extension list:"
" attr_list, mdx_outline",
ext.extendMarkdown, md)
self.assert_not_registered(md)
class TestExtensionRegistration(TestCase):
def test_make_extension(self):
configs = {'a': 'b'}
ext = makeExtension(**configs)
self.assertTrue(isinstance(ext, AttrColExtension))
self.assertEqual(ext.conf, configs)