/Lib/test/test_xmllib.py

http://unladen-swallow.googlecode.com/ · Python · 51 lines · 36 code · 10 blank · 5 comment · 2 complexity · 558bbf2b5fd2450c49bbea99fce3c69f MD5 · raw file

  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. import warnings
  16. warnings.filterwarnings("ignore", ".* xmllib .* obsolete.*",
  17. DeprecationWarning, r'xmllib$')
  18. from test import test_support
  19. import unittest
  20. import xmllib
  21. class XMLParserTestCase(unittest.TestCase):
  22. def test_simple(self):
  23. parser = xmllib.XMLParser()
  24. for c in testdoc:
  25. parser.feed(c)
  26. parser.close()
  27. def test_default_namespace(self):
  28. class H(xmllib.XMLParser):
  29. def unknown_starttag(self, name, attr):
  30. self.name, self.attr = name, attr
  31. h=H()
  32. h.feed(nsdoc)
  33. h.close()
  34. # The default namespace applies to elements...
  35. self.assertEquals(h.name, "URI foo")
  36. # but not to attributes
  37. self.assertEquals(h.attr, {'attr':'val'})
  38. def test_main():
  39. test_support.run_unittest(XMLParserTestCase)
  40. if __name__ == "__main__":
  41. test_main()