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