lxml.tests.test_dtd

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