blob: 442609cad33344f8e78b6eca42cb83e0f723cd81 [file] [log] [blame]
Daniel Veillard46da4642004-01-06 22:54:57 +00001#!/usr/bin/python -u
2import sys, unittest
3
4import libxml2
5
6class 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 Veillardb3ac18d2004-05-11 13:06:29 +000024 apply(f,args)
Daniel Veillard46da4642004-01-06 22:54:57 +000025 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 Veillard4f4a27f2004-01-14 23:50:34 +000051 domain=libxml2.XML_FROM_IO,
52 code=libxml2.XML_IO_LOAD_ERROR,
Daniel Veillard46da4642004-01-06 22:54:57 +000053 message='failed to load external entity "dummy.xml"\n',
Daniel Veillard4f4a27f2004-01-14 23:50:34 +000054 level=libxml2.XML_ERR_WARNING,
Daniel Veillard46da4642004-01-06 22:54:57 +000055 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 Veillard4f4a27f2004-01-14 23:50:34 +000064 domain=libxml2.XML_FROM_PARSER,
65 code=libxml2.XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard46da4642004-01-06 22:54:57 +000066 message='Premature end of data in tag x line 1\n',
Daniel Veillard4f4a27f2004-01-14 23:50:34 +000067 level=libxml2.XML_ERR_FATAL,
Daniel Veillard46da4642004-01-06 22:54:57 +000068 file='dummy.xml',
69 line=3)
70
71if __name__ == "__main__":
72 unittest.main()