Package lxml :: Package tests :: Module test_doctestcompare
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_doctestcompare

 1  import sys 
 2  import unittest 
 3   
 4  from lxml import etree 
 5  from lxml.tests.common_imports import HelperTestCase 
 6  from lxml.doctestcompare import LXMLOutputChecker, PARSE_HTML, PARSE_XML 
 7   
 8   
9 -class DummyInput:
10 - def __init__(self, **kw):
11 for name, value in kw.items(): 12 setattr(self, name, value)
13 14
15 -def indent(elem, level=0):
16 i = "\n" + level*" " 17 if len(elem): 18 if not elem.text or not elem.text.strip(): 19 elem.text = i + " " 20 if not elem.tail or not elem.tail.strip(): 21 elem.tail = i 22 for elem in elem: 23 indent(elem, level+1) 24 if not elem.tail or not elem.tail.strip(): 25 elem.tail = i 26 else: 27 if level and (not elem.tail or not elem.tail.strip()): 28 elem.tail = i
29 30
31 -class DoctestCompareTest(HelperTestCase):
32 _checker = LXMLOutputChecker() 33
34 - def compare(self, want, got, html=False):
35 if html: 36 options = PARSE_HTML 37 else: 38 options = PARSE_XML 39 40 parse = self._checker.get_parser(want, got, options) 41 want_doc = parse(want) 42 got_doc = parse(got) 43 return self._checker.collect_diff( 44 want_doc, got_doc, html, indent=0).lstrip()
45
46 - def assert_diff(self, want, got, diff, html=False):
47 self.assertEqual(self.compare(want, got, html), diff)
48
49 - def assert_nodiff(self, want, got, html=False):
50 root = etree.fromstring(want) 51 root.tail = '\n' 52 indent(root) 53 diff = etree.tostring( 54 root, encoding='unicode', method=html and 'html' or 'xml') 55 self.assert_diff(want, got, diff, html=html)
56
57 - def test_equal_input(self):
58 self.assert_nodiff( 59 '<p title="expected">Expected</p>', 60 '<p title="expected">Expected</p>')
61
62 - def test_differing_tags(self):
63 self.assert_diff( 64 '<p title="expected">Expected</p>', 65 '<b title="expected">Expected</b>', 66 '<p (got: b) title="expected">Expected</p (got: b)>\n')
67
69 self.assert_diff( 70 '<p title="expected">Expected</p>', 71 '<P title="expected">Expected</P>', 72 '<p (got: P) title="expected">Expected</p (got: P)>\n')
73
75 self.assert_nodiff( 76 '<html><body><p title="expected">Expected</p></body></html>', 77 '<HTML><BODY><P title="expected">Expected</P></BODY></HTML>', 78 html=True)
79
81 self.assert_diff( 82 '<p title="expected">Expected</p>', 83 '<p title="actual">Actual</p>', 84 '<p title="expected (got: actual)">Expected (got: Actual)</p>\n')
85 86
87 -def test_suite():
88 suite = unittest.TestSuite() 89 if sys.version_info >= (2,4): 90 suite.addTests([unittest.makeSuite(DoctestCompareTest)]) 91 return suite
92 93 94 if __name__ == '__main__': 95 unittest.main() 96