lxml.tests.test_doctestcompare

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
     68 -    def test_tags_upper_lower_case(self):
     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
     74 -    def test_tags_upper_lower_case_html(self):
     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
     80 -    def test_differing_attributes(self):
     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 -    def test_extra_children(self):
     87          # https://bugs.launchpad.net/lxml/+bug/1238503
     88          self.assert_diff(
     89              '<p><span>One</span></p>',
     90              '<p><span>One</span><b>Two</b><em>Three</em></p>',
     91              '<p>\n'
     92              '  <span>One</span>\n'
     93              '  +<b>Two</b>\n'
     94              '  +<em>Three</em>\n'
     95              '</p>\n')
     96
     97 -    def test_missing_children(self):
     98          self.assert_diff(
     99              '<p><span>One</span><b>Two</b><em>Three</em></p>',
    100              '<p><span>One</span></p>',
    101              '<p>\n'
    102              '  <span>One</span>\n'
    103              '  -<b>Two</b>\n'
    104              '  -<em>Three</em>\n'
    105              '</p>\n')
    106
    107 -    def test_extra_attributes(self):
    108          self.assert_diff(
    109              '<p><span class="foo">Text</span></p>',
    110              '<p><span class="foo" id="bar">Text</span></p>',
    111              '<p>\n'
    112              '  <span class="foo" +id="bar">Text</span>\n'
    113              '</p>\n')
    114
    115 -    def test_missing_attributes(self):
    116          self.assert_diff(
    117              '<p><span class="foo" id="bar">Text</span></p>',
    118              '<p><span class="foo">Text</span></p>',
    119              '<p>\n'
    120              '  <span class="foo" -id="bar">Text</span>\n'
    121              '</p>\n')
    122
    123
    124 -def test_suite():
    125      suite = unittest.TestSuite()
    126      if sys.version_info >= (2,4):
    127          suite.addTests([unittest.makeSuite(DoctestCompareTest)])
    128      return suite
    129
    130
    131  if __name__ == '__main__':
    132      unittest.main()
    133