-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
130 lines (98 loc) · 4.16 KB
/
test.py
File metadata and controls
130 lines (98 loc) · 4.16 KB
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
__author__ = 'jiang'
import unittest
from os.path import basename, join, splitext, split, dirname
import re
import glob
import logging
from pymarked import marked, defaults, pdict
class FileTestCase(unittest.TestCase):
def __init__(self, textpath):
super(FileTestCase, self).__init__()
self.textpath = textpath
def read(self, filepath):
with open(filepath) as f:
return f.read()
def fix_pl(self, html):
#fix unencoded quotes
html = re.sub(r'''='([^\n']*?)'(?=[^<>\n]*>)''', r'=&__APOS__;\1&__APOS__;', html)
html = re.sub(r'''="([^\n"]*?)"(?=[^<>\n]*>)''', r'=&__QUOT__;\1&__QUOT__;', html)
html = re.sub(r'"', '"', html)
html = re.sub(r"'", ''', html)
html = re.sub(r'&__QUOT__;', '"', html)
html = re.sub(r'&__APOS__;', "'", html)
# hash > and <
html = re.sub(r'(\d+[ ]*)>([ ]*\d+)', r'\1>\2', html)
html = re.sub(r'(\d+[ ]*)<([ ]*\d+)', r'\1<\2', html)
# fix img, in pl markdown, img element has title attribute even which is ``,
# delete blank title atrr from img element
html = re.sub(r'''(<img[^>]+?)title=""/>''', r'\1/>', html)
return html
def get_options(self, filename):
options = pdict([(k, False) if isinstance(v, bool) else (k, v) for k, v in defaults.items()])
if filename.find('gfm') >= 0:
options['gfm'] = True
if filename.find('_break') > 0:
options['breaks'] = True
if filename.find('smartypants') > 0:
options['smartypants'] = True
if filename.find('smartlist') >= 0:
options['smartLists'] = True
return options
def assert_mk_equals(self, textpath, htmlpath):
filename = basename(textpath)
output = marked(self.read(textpath), None, **self.get_options(filename))
wanted = self.read(htmlpath)
dir = basename(dirname(textpath))
if dir == 'basic':
wanted = self.fix_pl(wanted)
output = re.sub(r'\s', '', output)
wanted = re.sub(r'\s', '', wanted)
logging.debug([output])
logging.debug([wanted])
self.assertEqual(output, wanted, 'Failed %s' % filename)
def runTest(self):
test_name = splitext(basename(self.textpath))[0]
htmlpath = join(dirname(self.textpath), '%s.html' % test_name)
self.assert_mk_equals(self.textpath, htmlpath)
def repeat_macro(body, environ, **kwargs):
return (body+' ') * int(kwargs['count'])
def hello_macro(body, environ, **kwargs):
return 'Hello'
class MacroTestCase(unittest.TestCase):
def runTest(self):
text = '''<<repeat count="2">>
<<hello/>> tom
<</repeat>>'''
output = marked(text,
macros=dict(repeat=repeat_macro),
inline_macros=dict(hello=hello_macro))
self.assertEquals(output, 'Hello tom Hello tom ')
def environ_macro(body, environ, **kwargs):
return environ
class EnvironTestCase(unittest.TestCase):
def runTest(self):
text='''<<environ>>
<</environ>>'''
environ_value = 'ok'
output = marked(text, environ=environ_value, macros=dict(environ=environ_macro))
self.assertEquals(output, environ_value)
def suite():
suite = unittest.TestSuite()
path = join(dirname(__file__), 'tests/basic/*.text')
tests = glob.glob(path)
suite.addTests(FileTestCase(test) for test in tests)
path = join(dirname(__file__), 'tests/marked/*.text')
tests = glob.glob(path)
suite.addTests(FileTestCase(test) for test in tests)
path = join(dirname(__file__), 'tests/extra/*.text')
tests = glob.glob(path)
suite.addTests(FileTestCase(test) for test in tests)
suite.addTests([MacroTestCase(), EnvironTestCase()])
return suite
if __name__ == '__main__':
level = logging.INFO
#level = logging.DEBUG
logging.basicConfig(
format='%(levelname)s - %(name)s[%(lineno)d]: %(message)s',
level=level)
unittest.TextTestRunner().run(suite())