Package lxml :: Package tests :: Module test_dtd

[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_dtd

      1  # -*- coding: utf-8 -*-
      2
      3  """
      4  Test cases related to DTD parsing and validation
      5  """
      6
      7  import unittest, sys, os.path
      8
      9  this_dir = os.path.dirname(__file__)
     10  if this_dir not in sys.path:
     11      sys.path.insert(0, this_dir) # needed for Py3
     12
     13  from common_imports import etree, StringIO, BytesIO, _bytes, doctest
     14  from common_imports import HelperTestCase, fileInTestDir, make_doctest
     15
     16 -class ETreeDtdTestCase(HelperTestCase):
     17 -    def test_dtd(self):
     18          pass
     19
     20 -    def test_dtd_file(self):
     21          parse = etree.parse
     22          tree = parse(fileInTestDir("test.xml"))
     23          root = tree.getroot()
     24
     25          dtd = etree.DTD(fileInTestDir("test.dtd"))
     26          self.assert_(dtd.validate(root))
     27
     28 -    def test_dtd_stringio(self):
     29          root = etree.XML(_bytes("<b/>"))
     30          dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>"))
     31          self.assert_(dtd.validate(root))
     32
     33 -    def test_dtd_parse_invalid(self):
     34          fromstring = etree.fromstring
     35          parser = etree.XMLParser(dtd_validation=True)
     36          xml = _bytes('<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % fileInTestDir("test.dtd"))
     37          self.assertRaises(etree.XMLSyntaxError,
     38                            fromstring, xml, parser=parser)
     39
     40 -    def test_dtd_parse_file_not_found(self):
     41          fromstring = etree.fromstring
     42          dtd_filename = fileInTestDir("__nosuch.dtd")
     43          parser = etree.XMLParser(dtd_validation=True)
     44          xml = _bytes('<!DOCTYPE b SYSTEM "%s"><b><a/></b>' % dtd_filename)
     45          self.assertRaises(etree.XMLSyntaxError,
     46                            fromstring, xml, parser=parser)
     47          errors = None
     48          try:
     49              fromstring(xml, parser=parser)
     50          except etree.XMLSyntaxError:
     51              e = sys.exc_info()[1]
     52              errors = [ entry.message for entry in e.error_log
     53                         if dtd_filename in entry.message ]
     54          self.assert_(errors)
     55
     56 -    def test_dtd_parse_valid(self):
     57          parser = etree.XMLParser(dtd_validation=True)
     58          xml = '<!DOCTYPE a SYSTEM "%s"><a><b/></a>' % fileInTestDir("test.dtd")
     59          root = etree.fromstring(xml, parser=parser)
     60
     61 -    def test_dtd_parse_valid_relative(self):
     62          parser = etree.XMLParser(dtd_validation=True)
     63          xml = '<!DOCTYPE a SYSTEM "test.dtd"><a><b/></a>'
     64          root = etree.fromstring(xml, parser=parser,
     65                                  base_url=fileInTestDir("test.xml"))
     66
     67 -    def test_dtd_invalid(self):
     68          root = etree.XML("<b><a/></b>")
     69          dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>"))
     70          self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)
     71
     72 -    def test_dtd_assertValid(self):
     73          root = etree.XML("<b><a/></b>")
     74          dtd = etree.DTD(BytesIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>"))
     75          dtd.assertValid(root)
     76
     77 -    def test_dtd_internal(self):
     78          root = etree.XML(_bytes('''
     79          <!DOCTYPE b SYSTEM "none" [
     80          <!ELEMENT b (a)>
     81          <!ELEMENT a EMPTY>
     82          ]>
     83          <b><a/></b>
     84          '''))
     85          dtd = etree.ElementTree(root).docinfo.internalDTD
     86          self.assert_(dtd)
     87          dtd.assertValid(root)
     88
     89 -    def test_dtd_internal_invalid(self):
     90          root = etree.XML(_bytes('''
     91          <!DOCTYPE b SYSTEM "none" [
     92          <!ELEMENT b (a)>
     93          <!ELEMENT a (c)>
     94          <!ELEMENT c EMPTY>
     95          ]>
     96          <b><a/></b>
     97          '''))
     98          dtd = etree.ElementTree(root).docinfo.internalDTD
     99          self.assert_(dtd)
    100          self.assertFalse(dtd.validate(root))
    101
    102 -    def test_dtd_broken(self):
    103          self.assertRaises(etree.DTDParseError, etree.DTD,
    104                            BytesIO("<!ELEMENT b HONKEY>"))
    105
    106 -    def test_parse_file_dtd(self):
    107          parser = etree.XMLParser(attribute_defaults=True)
    108
    109          tree = etree.parse(fileInTestDir('test.xml'), parser)
    110          root = tree.getroot()
    111
    112          self.assertEquals(
    113              "valueA",
    114              root.get("default"))
    115          self.assertEquals(
    116              "valueB",
    117              root[0].get("default"))
    118
    119 -    def test_iterparse_file_dtd(self):
    120          iterparse = etree.iterparse
    121          iterator = iterparse(fileInTestDir("test.xml"), events=("start",),
    122                               attribute_defaults=True)
    123          attributes = [ element.get("default")
    124                         for event, element in iterator ]
    125          self.assertEquals(
    126              ["valueA", "valueB"],
    127              attributes)
    128
    129
    130 -def test_suite():
    131      suite = unittest.TestSuite()
    132      suite.addTests([unittest.makeSuite(ETreeDtdTestCase)])
    133      suite.addTests(
    134          [make_doctest('../../../doc/validation.txt')])
    135      return suite
    136
    137  if __name__ == '__main__':
    138      print('to test use test.py %s' % __file__)
    139