Rename to slixmpp
[slixmpp.git] / tests / test_tostring.py
blob72718beb4b2c5b9479f811a4a88de94e66971f3a
1 import unittest
2 from slixmpp.test import SlixTest
3 from slixmpp.xmlstream.stanzabase import ET
4 from slixmpp.xmlstream.tostring import tostring, escape
7 class TestToString(SlixTest):
9 """
10 Test the implementation of slixmpp.xmlstream.tostring
11 """
13 def tearDown(self):
14 self.stream_close()
16 def tryTostring(self, original='', expected=None, message='', **kwargs):
17 """
18 Compare the result of calling tostring against an
19 expected result.
20 """
21 if not expected:
22 expected=original
23 if isinstance(original, str):
24 xml = ET.fromstring(original)
25 else:
26 xml=original
27 result = tostring(xml, **kwargs)
28 self.failUnless(result == expected, "%s: %s" % (message, result))
30 def testXMLEscape(self):
31 """Test escaping XML special characters."""
32 original = """<foo bar="baz">'Hi & welcome!'</foo>"""
33 escaped = escape(original)
34 desired = """&lt;foo bar=&quot;baz&quot;&gt;&apos;Hi"""
35 desired += """ &amp; welcome!&apos;&lt;/foo&gt;"""
37 self.failUnless(escaped == desired,
38 "XML escaping did not work: %s." % escaped)
40 def testEmptyElement(self):
41 """Test converting an empty element to a string."""
42 self.tryTostring(
43 original='<bar xmlns="foo" />',
44 message="Empty element not serialized correctly")
46 def testEmptyElementWrapped(self):
47 """Test converting an empty element inside another element."""
48 self.tryTostring(
49 original='<bar xmlns="foo"><baz /></bar>',
50 message="Wrapped empty element not serialized correctly")
52 def testEmptyElementWrappedText(self):
53 """
54 Test converting an empty element wrapped with text
55 inside another element.
56 """
57 self.tryTostring(
58 original='<bar xmlns="foo">Some text. <baz /> More text.</bar>',
59 message="Text wrapped empty element serialized incorrectly")
61 def testMultipleChildren(self):
62 """Test converting multiple child elements to a Unicode string."""
63 self.tryTostring(
64 original='<bar xmlns="foo"><baz><qux /></baz><quux /></bar>',
65 message="Multiple child elements not serialized correctly")
67 def testXMLNS(self):
68 """
69 Test using xmlns tostring parameter, which will prevent adding
70 an xmlns attribute to the serialized element if the element's
71 namespace is the same.
72 """
73 self.tryTostring(
74 original='<bar xmlns="foo" />',
75 expected='<bar />',
76 message="The xmlns parameter was not used properly.",
77 xmlns='foo')
79 def testTailContent(self):
80 """
81 Test that elements of the form <a>foo <b>bar</b> baz</a> only
82 include " baz" once.
83 """
84 self.tryTostring(
85 original='<a>foo <b>bar</b> baz</a>',
86 message='Element tail content is incorrect.')
88 def testStanzaStr(self):
89 """
90 Test that stanza objects are serialized properly.
91 """
92 self.stream_start()
94 utf8_message = '\xe0\xb2\xa0_\xe0\xb2\xa0'
95 if not hasattr(utf8_message, 'decode'):
96 # Python 3
97 utf8_message = bytes(utf8_message, encoding='utf-8')
98 msg = self.Message()
99 msg['body'] = utf8_message.decode('utf-8')
100 expected = '<message><body>\xe0\xb2\xa0_\xe0\xb2\xa0</body></message>'
101 result = msg.__str__()
102 self.failUnless(result == expected,
103 "Stanza Unicode handling is incorrect: %s" % result)
105 def testXMLLang(self):
106 """Test that serializing xml:lang works."""
108 self.stream_start()
110 msg = self.Message()
111 msg._set_attr('{%s}lang' % msg.xml_ns, "no")
113 expected = '<message xml:lang="no" />'
114 result = msg.__str__()
115 self.failUnless(expected == result,
116 "Serialization with xml:lang failed: %s" % result)
119 suite = unittest.TestLoader().loadTestsFromTestCase(TestToString)