Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | import sys, unittest |
| 3 | |
| 4 | import libxml2 |
| 5 | |
| 6 | class TestCase(unittest.TestCase): |
| 7 | |
| 8 | def setUp(self): |
| 9 | libxml2.debugMemory(1) |
| 10 | |
| 11 | def tearDown(self): |
| 12 | libxml2.cleanupParser() |
| 13 | if libxml2.debugMemory(1) != 0: |
| 14 | libxml2.dumpMemory() |
| 15 | self.fail("Memory leak %d bytes" % (libxml2.debugMemory(1),)) |
| 16 | |
| 17 | def failUnlessXmlError(self,f,args,exc,domain,code,message,level,file,line): |
| 18 | """Run function f, with arguments args and expect an exception exc; |
| 19 | when the exception is raised, check the libxml2.lastError for |
| 20 | expected values.""" |
| 21 | # disable the default error handler |
| 22 | libxml2.registerErrorHandler(None,None) |
| 23 | try: |
Daniel Veillard | b3ac18d | 2004-05-11 13:06:29 +0000 | [diff] [blame^] | 24 | apply(f,args) |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 25 | except exc: |
| 26 | e = libxml2.lastError() |
| 27 | if e is None: |
| 28 | self.fail("lastError not set") |
| 29 | if 0: |
| 30 | print "domain = ",e.domain() |
| 31 | print "code = ",e.code() |
| 32 | print "message =",repr(e.message()) |
| 33 | print "level =",e.level() |
| 34 | print "file =",e.file() |
| 35 | print "line =",e.line() |
| 36 | print |
| 37 | self.failUnlessEqual(domain,e.domain()) |
| 38 | self.failUnlessEqual(code,e.code()) |
| 39 | self.failUnlessEqual(message,e.message()) |
| 40 | self.failUnlessEqual(level,e.level()) |
| 41 | self.failUnlessEqual(file,e.file()) |
| 42 | self.failUnlessEqual(line,e.line()) |
| 43 | else: |
| 44 | self.fail("exception %s should have been raised" % exc) |
| 45 | |
| 46 | def test1(self): |
| 47 | """Test readFile with a file that does not exist""" |
| 48 | self.failUnlessXmlError(libxml2.readFile, |
| 49 | ("dummy.xml",None,0), |
| 50 | libxml2.treeError, |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 51 | domain=libxml2.XML_FROM_IO, |
| 52 | code=libxml2.XML_IO_LOAD_ERROR, |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 53 | message='failed to load external entity "dummy.xml"\n', |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 54 | level=libxml2.XML_ERR_WARNING, |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 55 | file=None, |
| 56 | line=0) |
| 57 | |
| 58 | def test2(self): |
| 59 | """Test a well-formedness error: we get the last error only""" |
| 60 | s = "<x>\n<a>\n</x>" |
| 61 | self.failUnlessXmlError(libxml2.readMemory, |
| 62 | (s,len(s),"dummy.xml",None,0), |
| 63 | libxml2.treeError, |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 64 | domain=libxml2.XML_FROM_PARSER, |
| 65 | code=libxml2.XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 66 | message='Premature end of data in tag x line 1\n', |
Daniel Veillard | 4f4a27f | 2004-01-14 23:50:34 +0000 | [diff] [blame] | 67 | level=libxml2.XML_ERR_FATAL, |
Daniel Veillard | 46da464 | 2004-01-06 22:54:57 +0000 | [diff] [blame] | 68 | file='dummy.xml', |
| 69 | line=3) |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | unittest.main() |