VirtualBox

source: vbox/trunk/src/libs/libxml2-2.12.6/python/tests/tstLastError.py@ 105635

最後變更 在這個檔案從105635是 104106,由 vboxsync 提交於 10 月 前

libxml2-2.9.14: Applied and adjusted our libxml2 changes to 2.9.14. bugref:10640

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:executable 設為 *
檔案大小: 2.9 KB
 
1#!/usr/bin/env python3
2import sys, unittest
3
4import setup_test
5import libxml2
6
7class TestCase(unittest.TestCase):
8
9 def runTest(self):
10 self.test1()
11 self.test2()
12
13 def setUp(self):
14 libxml2.debugMemory(1)
15
16 def tearDown(self):
17 libxml2.cleanupParser()
18 if libxml2.debugMemory(1) != 0:
19 self.fail("Memory leak %d bytes" % (libxml2.debugMemory(1),))
20 else:
21 print("OK")
22
23 def failUnlessXmlError(self,f,args,exc,domain,code,message,level,file,line):
24 """Run function f, with arguments args and expect an exception exc;
25 when the exception is raised, check the libxml2.lastError for
26 expected values."""
27 # disable the default error handler
28 def noerr(ctx, str):
29 pass
30 # None is not acceptable as function.
31 libxml2.registerErrorHandler(noerr,None)
32 try:
33 f(*args)
34 except exc:
35 e = libxml2.lastError()
36 if e is None:
37 self.fail("lastError not set")
38 if 0:
39 print("domain = ",e.domain())
40 print("code = ",e.code())
41 print("message =",repr(e.message()))
42 print("level =",e.level())
43 print("file =",e.file())
44 print("line =",e.line())
45 print()
46 self.assertEqual(domain,e.domain())
47 self.assertEqual(code,e.code())
48 self.assertEqual(message,e.message())
49 self.assertEqual(level,e.level())
50 self.assertEqual(file,e.file())
51 self.assertEqual(line,e.line())
52 else:
53 self.fail("exception %s should have been raised" % exc)
54
55 def test1(self):
56 """Test readFile with a file that does not exist"""
57 self.failUnlessXmlError(libxml2.readFile,
58 ("dummy.xml",None,0),
59 libxml2.treeError,
60 domain=libxml2.XML_FROM_IO,
61 code=libxml2.XML_IO_LOAD_ERROR,
62 message='failed to load external entity "dummy.xml"\n',
63 level=libxml2.XML_ERR_WARNING,
64 file=None,
65 line=0)
66
67 def test2(self):
68 """Test a well-formedness error: we get the last error only"""
69 s = "<x>\n<a>\n</x>"
70 self.failUnlessXmlError(libxml2.readMemory,
71 (s,len(s),"dummy.xml",None,0),
72 libxml2.treeError,
73 domain=libxml2.XML_FROM_PARSER,
74 code=libxml2.XML_ERR_TAG_NAME_MISMATCH,
75 message='Opening and ending tag mismatch: a line 2 and x\n',
76 level=libxml2.XML_ERR_FATAL,
77 file='dummy.xml',
78 line=3)
79
80if __name__ == "__main__":
81 test = TestCase()
82 test.setUp()
83 test.test1()
84 test.test2()
85 test.tearDown()
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette