Package lxml :: Package tests :: Module test_doctestcompare

[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_doctestcompare

      1  import unittest
      2
      3  from lxml import etree
      4  from lxml.tests.common_imports import HelperTestCase
      5  from lxml.doctestcompare import LXMLOutputChecker, PARSE_HTML, PARSE_XML
      6
      7
      8 -class DummyInput:
      9 -    def __init__(self, **kw):
     10          for name, value in kw.items():
     11              setattr(self, name, value)
     12
     13
     14 -def indent(elem, level=0):
     15      i = "\n" + level*"  "
     16      if len(elem):
     17          if not elem.text or not elem.text.strip():
     18              elem.text = i + "  "
     19          if not elem.tail or not elem.tail.strip():
     20              elem.tail = i
     21          for elem in elem:
     22              indent(elem, level+1)
     23          if not elem.tail or not elem.tail.strip():
     24              elem.tail = i
     25      else:
     26          if level and (not elem.tail or not elem.tail.strip()):
     27              elem.tail = i
     28
     29
     30 -class DoctestCompareTest(HelperTestCase):
     31      _checker = LXMLOutputChecker()
     32
     33 -    def compare(self, want, got, html=False):
     34          if html:
     35              options = PARSE_HTML
     36          else:
     37              options = PARSE_XML
     38
     39          parse = self._checker.get_parser(want, got, options)
     40          want_doc = parse(want)
     41          got_doc = parse(got)
     42          return self._checker.collect_diff(
     43              want_doc, got_doc, html, indent=0).lstrip()
     44
     45 -    def assert_diff(self, want, got, diff, html=False):
     46          self.assertEqual(self.compare(want, got, html), diff)
     47
     48 -    def assert_nodiff(self, want, got, html=False):
     49          root = etree.fromstring(want)
     50          root.tail = '\n'
     51          indent(root)
     52          diff = etree.tostring(
     53              root, encoding='unicode', method=html and 'html' or 'xml')
     54          self.assert_diff(want, got, diff, html=html)
     55
     56 -    def test_equal_input(self):
     57          self.assert_nodiff(
     58              '<p title="expected">Expected</p>',
     59              '<p title="expected">Expected</p>')
     60
     61 -    def test_differing_tags(self):
     62          self.assert_diff(
     63              '<p title="expected">Expected</p>',
     64              '<b title="expected">Expected</b>',
     65              '<p (got: b) title="expected">Expected</p (got: b)>\n')
     66
     67 -    def test_tags_upper_lower_case(self):
     68          self.assert_diff(
     69              '<p title="expected">Expected</p>',
     70              '<P title="expected">Expected</P>',
     71              '<p (got: P) title="expected">Expected</p (got: P)>\n')
     72
     73 -    def test_tags_upper_lower_case_html(self):
     74          self.assert_nodiff(
     75              '<html><body><p title="expected">Expected</p></body></html>',
     76              '<HTML><BODY><P title="expected">Expected</P></BODY></HTML>',
     77              html=True)
     78
     79 -    def test_differing_attributes(self):
     80          self.assert_diff(
     81              '<p title="expected">Expected</p>',
     82              '<p title="actual">Actual</p>',
     83              '<p title="expected (got: actual)">Expected (got: Actual)</p>\n')
     84
     85 -    def test_extra_children(self):
     86          # https://bugs.launchpad.net/lxml/+bug/1238503
     87          self.assert_diff(
     88              '<p><span>One</span></p>',
     89              '<p><span>One</span><b>Two</b><em>Three</em></p>',
     90              '<p>\n'
     91              '  <span>One</span>\n'
     92              '  +<b>Two</b>\n'
     93              '  +<em>Three</em>\n'
     94              '</p>\n')
     95
     96 -    def test_missing_children(self):
     97          self.assert_diff(
     98              '<p><span>One</span><b>Two</b><em>Three</em></p>',
     99              '<p><span>One</span></p>',
    100              '<p>\n'
    101              '  <span>One</span>\n'
    102              '  -<b>Two</b>\n'
    103              '  -<em>Three</em>\n'
    104              '</p>\n')
    105
    106 -    def test_extra_attributes(self):
    107          self.assert_diff(
    108              '<p><span class="foo">Text</span></p>',
    109              '<p><span class="foo" id="bar">Text</span></p>',
    110              '<p>\n'
    111              '  <span class="foo" +id="bar">Text</span>\n'
    112              '</p>\n')
    113
    114 -    def test_missing_attributes(self):
    115          self.assert_diff(
    116              '<p><span class="foo" id="bar">Text</span></p>',
    117              '<p><span class="foo">Text</span></p>',
    118              '<p>\n'
    119              '  <span class="foo" -id="bar">Text</span>\n'
    120              '</p>\n')
    121
    122
    123 -def test_suite():
    124      suite = unittest.TestSuite()
    125      suite.addTests([unittest.makeSuite(DoctestCompareTest)])
    126      return suite
    127
    128
    129  if __name__ == '__main__':
    130      unittest.main()
    131