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
|
import unittest, sys, os
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
lib_dir = os.path.abspath(os.path.join(cur_dir, '..', 'scripts'))
sys.path.append(lib_dir)
import docutils_to_docbook
import validate_docbook
from xml.dom import minidom
class TestDocutilsToXml(unittest.TestCase):
def setUp(self):
pass
def isEqualXML(self, a, b):
da, db= minidom.parseString(a), minidom.parseString(b)
return self.isEqualElement(da.documentElement, db.documentElement)
def isEqualElement(self, a, b):
ignored_atts = ['source']
a_atts = a.attributes.items()
b_atts = b.attributes.items()
def delete_ignored(att_list):
temp = []
for tup in att_list:
if tup[0] not in ignored_atts:
temp.append(tup)
return temp
a_atts = delete_ignored(a_atts)
b_atts = delete_ignored(b_atts)
if sorted(a_atts) != sorted(b_atts):
return False
if a.tagName!=b.tagName:
return False
if len(a.childNodes)!=len(b.childNodes):
return False
for ac, bc in zip(a.childNodes, b.childNodes):
if ac.nodeType!=bc.nodeType:
return False
if ac.nodeType==ac.TEXT_NODE and ac.data.strip()!=bc.data.strip():
return False
if ac.nodeType==ac.ELEMENT_NODE and not self.isEqualElement(ac, bc):
return False
return True
def _get_file(self, name):
in_file = os.path.join(os.path.dirname(sys.argv[0]), 'data', name)
if not os.path.isfile(in_file):
raise IOError('file "{0}"does not exist'.format(in_file))
return in_file
def _file_to_string(self, in_file):
if not os.path.isfile(in_file):
raise IOError('file "{0}"does not exist'.format(in_file))
with open(in_file, 'r') as read_obj:
lines = ''.join(read_obj.readlines())
return lines
def _fix_line(self, line):
return line
def test_utf8(self):
convert_obj = docutils_to_docbook.ToXml(in_file = None)
in_file = self._get_file('utf8.rst')
needed = """<?xml version="1.0" encoding="ascii"?>\n<!-- Generated by Docutils 0.9 -->\n<document source="data/utf8.rst"><paragraph>LATIN CAPITAL LETTER O WITH DIAERESIS: Ö</paragraph></document>"""
return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf8')
same = self.isEqualXML(return_string, needed)
self.assertTrue(same)
def test_utf16(self):
convert_obj = docutils_to_docbook.ToXml(in_file = None)
in_file = self._get_file('utf16.rst')
needed = """<?xml version="1.0" encoding="ascii"?>\n<!-- Generated by Docutils 0.9 -->\n<document source="data/utf16.rst"><paragraph>LATIN CAPITAL LETTER O WITH DIAERESIS: Ö</paragraph></document>"""
return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf16')
same = self.isEqualXML(return_string, needed)
self.assertTrue(same)
def test_math_no_conversion(self):
convert_obj = docutils_to_docbook.ToXml(in_file = None)
in_file = self._get_file('math.rst')
needed="""<?xml version="1.0" encoding="ascii"?>
<!-- Generated by Docutils 0.9 -->
<document source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve">quad quad x = (-b +- sqrt(b^2-4ac))/(2a)</math_block></document>"""
return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf8')
same = self.isEqualXML(return_string, needed)
self.assertTrue(same)
def test_math_conversion(self):
convert_obj = docutils_to_docbook.ToXml(in_file = None)
in_string = """<?xml version="1.0" encoding="ascii"?>
<!-- Generated by Docutils 0.9 -->
<document source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve">quad quad x = (-b +- sqrt(b^2-4ac))/(2a)</math_block></document>"""
needed = """<?xml version='1.0' encoding='ASCII'?>\n<document xmlns:ns0="http://www.w3.org/1998/Math/MathML" source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve"><ns0:math><ns0:mstyle displaystyle="true"><ns0:mo>  </ns0:mo><ns0:mo>  </ns0:mo><ns0:mi>x</ns0:mi><ns0:mo>=</ns0:mo><ns0:mfrac><ns0:mrow class="nominator"><ns0:mo>-</ns0:mo><ns0:mi>b</ns0:mi><ns0:mo>±</ns0:mo><ns0:msqrt><ns0:mrow class="radical"><ns0:msup><ns0:mi>b</ns0:mi><ns0:mn>2</ns0:mn></ns0:msup><ns0:mo>-</ns0:mo><ns0:mn>4</ns0:mn><ns0:mi>a</ns0:mi><ns0:mi>c</ns0:mi></ns0:mrow></ns0:msqrt></ns0:mrow><ns0:mrow class="denominator"><ns0:mn>2</ns0:mn><ns0:mi>a</ns0:mi></ns0:mrow></ns0:mfrac></ns0:mstyle></ns0:math></math_block></document>"""
return_string = convert_obj.insert_math_elements(xml_string = in_string)
same = self.isEqualXML(return_string, needed)
self.assertTrue(same)
def test_to_docbook_simple(self):
in_file = os.path.join('data', 'simple_docbook_res.xml')
convert_obj = docutils_to_docbook.ToXml(in_file = in_file )
raw_path = os.path.join('data', 'simple_raw.xml')
result_file = convert_obj.to_docbook(raw_path = raw_path)
return_string = self._file_to_string(result_file)
needed = self._file_to_string(in_file)
same = self.isEqualXML(return_string, needed)
self.assertTrue(same)
def test_validate_docbook(self):
valid_obj = validate_docbook.ValidateDocbook()
in_files = os.path.join('data', 'simple_docbook.xml')
valid = valid_obj.is_valid(in_files = in_files)
self.assertTrue(valid)
def test_validate_invalid_docbook(self):
valid_obj = validate_docbook.ValidateDocbook()
in_files = os.path.join('data', 'invalid_docbook.xml')
valid = valid_obj.is_valid(in_files = in_files)
self.assertFalse(valid)
if __name__ == '__main__':
unittest.main()
|