1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import os |
---|
12 | import shutil |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | |
---|
16 | from bitten.util import xmlio |
---|
17 | |
---|
18 | class XMLIOTestCase(unittest.TestCase): |
---|
19 | |
---|
20 | def test_parse(self): |
---|
21 | """Tests that simple test data is parsed correctly""" |
---|
22 | s = """<build xmlns:c="http://bitten.edgewall.org/tools/c"> |
---|
23 | <step id="build" description="Configure and build"> |
---|
24 | <c:configure /> |
---|
25 | </step>\ |
---|
26 | </build>""" |
---|
27 | x = xmlio.parse(s) |
---|
28 | assert x.name == "build" |
---|
29 | |
---|
30 | def test_Element_encoding(self): |
---|
31 | self.assertEquals('<\xc3\xb8\xc3\xbc arg="\xc3\xa9\xe2\x82\xac"/>', |
---|
32 | str(xmlio.Element(u'\xf8\xfc', arg=u'\xe9\u20ac'.encode('utf-8')))) |
---|
33 | |
---|
34 | def test_ParsedElement_encoding(self): |
---|
35 | u = u'<root foo="øüé€"/>' |
---|
36 | s = '<root foo="\xc3\xb8\xc3\xbc\xc3\xa9\xe2\x82\xac"/>' |
---|
37 | self.assertEquals(u, s.decode('utf-8')) |
---|
38 | # unicode input |
---|
39 | x = xmlio.parse(u) |
---|
40 | out_u = str(x) |
---|
41 | self.assertEquals(out_u, s) |
---|
42 | self.assertEquals(out_u.decode('utf-8'), u) |
---|
43 | # utf-8 input |
---|
44 | x = xmlio.parse(s) |
---|
45 | out_s = str(x) |
---|
46 | self.assertEquals(out_s, s) |
---|
47 | self.assertEquals(out_s.decode('utf-8'), u) |
---|
48 | # identical results |
---|
49 | self.assertEquals(out_u, out_s) |
---|
50 | |
---|
51 | def test_escape_text(self): |
---|
52 | # unicode |
---|
53 | self.assertEquals(u"Me & you!", |
---|
54 | xmlio._escape_text(u"Me\x01 & you\x86!")) |
---|
55 | # str |
---|
56 | self.assertEquals("Me & you!", |
---|
57 | xmlio._escape_text("Me\x01 & you\x86!")) |
---|
58 | # not basestring |
---|
59 | self.assertEquals(42, xmlio._escape_text(42)) |
---|
60 | |
---|
61 | def test_escape_attr(self): |
---|
62 | # unicode |
---|
63 | self.assertEquals(u'"Me & you!"', |
---|
64 | xmlio._escape_attr(u'"Me\x01 & you\x86!"')) |
---|
65 | # str |
---|
66 | self.assertEquals('"Me & you!"', |
---|
67 | xmlio._escape_attr('"Me\x01 & you\x86!"')) |
---|
68 | # not basestring |
---|
69 | self.assertEquals(42, xmlio._escape_text(42)) |
---|
70 | |
---|
71 | def suite(): |
---|
72 | suite = unittest.TestSuite() |
---|
73 | suite.addTest(unittest.makeSuite(XMLIOTestCase, 'test')) |
---|
74 | return suite |
---|
75 | |
---|
76 | if __name__ == '__main__': |
---|
77 | unittest.main(defaultTest='suite') |
---|