test_xmllib.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. '''Test module to thest the xmllib module.
  2. Sjoerd Mullender
  3. '''
  4. testdoc = """\
  5. <?xml version="1.0" encoding="UTF-8" standalone='yes' ?>
  6. <!-- comments aren't allowed before the <?xml?> tag,
  7. but they are allowed before the <!DOCTYPE> tag -->
  8. <?processing instructions are allowed in the same places as comments ?>
  9. <!DOCTYPE greeting [
  10. <!ELEMENT greeting (#PCDATA)>
  11. ]>
  12. <greeting>Hello, world!</greeting>
  13. """
  14. nsdoc = "<foo xmlns='URI' attr='val'/>"
  15. from test import test_support
  16. import unittest
  17. # Silence Py3k warning
  18. xmllib = test_support.import_module('xmllib', deprecated=True)
  19. class XMLParserTestCase(unittest.TestCase):
  20. def test_simple(self):
  21. parser = xmllib.XMLParser()
  22. for c in testdoc:
  23. parser.feed(c)
  24. parser.close()
  25. def test_default_namespace(self):
  26. class H(xmllib.XMLParser):
  27. def unknown_starttag(self, name, attr):
  28. self.name, self.attr = name, attr
  29. h=H()
  30. h.feed(nsdoc)
  31. h.close()
  32. # The default namespace applies to elements...
  33. self.assertEqual(h.name, "URI foo")
  34. # but not to attributes
  35. self.assertEqual(h.attr, {'attr':'val'})
  36. def test_main():
  37. test_support.run_unittest(XMLParserTestCase)
  38. if __name__ == "__main__":
  39. test_main()