1
2 import unittest, doctest
3
4
5
6
7
8
9 import sys, gc, os.path
10 from lxml import etree
11
12 this_dir = os.path.dirname(__file__)
13 if this_dir not in sys.path:
14 sys.path.insert(0, this_dir)
15
16 from common_imports import HelperTestCase
17
18
20 etree = etree
21
23
24
25 self.assertRaises(TypeError, self.etree.Element, 'a', 'b')
26
29
31
32 Element = self.etree.Element
33
34
35 trace_func = sys.gettrace()
36 try:
37 sys.settrace(None)
38 gc.collect()
39
40 count = sys.getrefcount(None)
41
42 l = [Element('name'), Element('name')]
43 l.append(l)
44
45 del l
46 gc.collect()
47
48 self.assertEqual(sys.getrefcount(None), count)
49 finally:
50 sys.settrace(trace_func)
51
53 broken_xml_name = 'test_broken.xml'
54 broken_xml_path = os.path.join(this_dir, broken_xml_name)
55 fail_msg = 'test_broken.xml should raise an etree.XMLSyntaxError'
56 try:
57 etree.parse(broken_xml_path)
58 except etree.XMLSyntaxError as e:
59
60 self.assertEqual(e.position, (e.lineno, e.offset + 1), 'position and lineno/offset out of sync')
61
62 self.assertTrue(e.filename.endswith(broken_xml_name), 'filename must be preserved')
63 self.assertEqual(e.lineno, 1)
64 self.assertEqual(e.offset, 10)
65 except Exception as e:
66 self.fail('{0}, not {1}'.format(fail_msg, type(e)))
67 else:
68 self.fail('test_broken.xml should raise an etree.XMLSyntaxError')
69
70
72 suite = unittest.TestSuite()
73 suite.addTests([unittest.makeSuite(ErrorTestCase)])
74 return suite
75
76 if __name__ == '__main__':
77 print('to test use test.py %s' % __file__)
78