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
      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_invalid(self):
     37          root = etree.XML("<b><a/></b>")
     38          dtd = etree.DTD(StringIO("<!ELEMENT b EMPTY>"))
     39          self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)
     40
     41 -    def test_dtd_assertValid(self):
     42          root = etree.XML("<b><a/></b>")
     43          dtd = etree.DTD(StringIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>"))
     44          dtd.assertValid(root)
     45
     46 -    def test_dtd_internal(self):
     47          root = etree.XML('''
     48          <!DOCTYPE b SYSTEM "none" [
     49          <!ELEMENT b (a)>
     50          <!ELEMENT a EMPTY>
     51          ]>
     52          <b><a/></b>
     53          ''')
     54          dtd = etree.ElementTree(root).docinfo.internalDTD
     55          self.assert_(dtd)
     56          dtd.assertValid(root)
     57
     58 -    def test_dtd_internal_invalid(self):
     59          root = etree.XML('''
     60          <!DOCTYPE b SYSTEM "none" [
     61          <!ELEMENT b (a)>
     62          <!ELEMENT a (c)>
     63          <!ELEMENT c EMPTY>
     64          ]>
     65          <b><a/></b>
     66          ''')
     67          dtd = etree.ElementTree(root).docinfo.internalDTD
     68          self.assert_(dtd)
     69          self.assertFalse(dtd.validate(root))
     70
     71 -    def test_dtd_broken(self):
     72          self.assertRaises(etree.DTDParseError, etree.DTD,
     73                            StringIO("<!ELEMENT b HONKEY>"))
     74
     75 -    def test_parse_file_dtd(self):
     76          parser = etree.XMLParser(attribute_defaults=True)
     77
     78          tree = etree.parse(fileInTestDir('test.xml'), parser)
     79          root = tree.getroot()
     80
     81          self.assertEquals(
     82              "valueA",
     83              root.get("default"))
     84          self.assertEquals(
     85              "valueB",
     86              root[0].get("default"))
     87
     88 -    def test_iterparse_file_dtd(self):
     89          iterparse = etree.iterparse
     90          iterator = iterparse(fileInTestDir("test.xml"), events=("start",),
     91                               attribute_defaults=True)
     92          attributes = [ element.get("default")
     93                         for event, element in iterator ]
     94          self.assertEquals(
     95              ["valueA", "valueB"],
     96              attributes)
     97
     98
     99 -def test_suite():
    100      suite = unittest.TestSuite()
    101      suite.addTests([unittest.makeSuite(ETreeDtdTestCase)])
    102      suite.addTests(
    103          [doctest.DocFileSuite('../../../doc/validation.txt')])
    104      return suite
    105
    106  if __name__ == '__main__':
    107      print 'to test use test.py %s' % __file__
    108