1
2 from __future__ import absolute_import
3
4 import unittest
5
6
7
8
9
10
11 import sys, gc, os.path
12 from lxml import etree
13
14 from .common_imports import HelperTestCase
15
16
18 etree = etree
19
21
22
23 self.assertRaises(TypeError, self.etree.Element, 'a', 'b')
24
27
29
30 Element = self.etree.Element
31 getrefcount = sys.getrefcount
32
33
34 trace_func = sys.gettrace()
35 try:
36 sys.settrace(None)
37 gc.collect()
38
39 count = getrefcount(None)
40
41 l = [Element('name'), Element('name')]
42 l.append(l)
43
44 del l
45 gc.collect()
46 count = getrefcount(None) - count
47
48 self.assertEqual(count, 0)
49 finally:
50 sys.settrace(trace_func)
51
53 broken_xml_name = 'test_broken.xml'
54 broken_xml_path = os.path.join(os.path.dirname(__file__), 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