Edgewall Software

source: trunk/genshi/tests/core.py

Last change on this file was 1266, checked in by hodgestar, 10 years ago

Add support for iterator arguments to _speedups Markup.join implementation so that it matches the Python implementation (fixes #574).

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006-2009 Edgewall Software
4# All rights reserved.
5#
6# This software is licensed as described in the file COPYING, which
7# you should have received as part of this distribution. The terms
8# are also available at http://genshi.edgewall.org/wiki/License.
9#
10# This software consists of voluntary contributions made by many
11# individuals. For the exact contribution history, see the revision
12# history and logs, available at http://genshi.edgewall.org/log/.
13
14import doctest
15import pickle
16import unittest
17
18from genshi import core
19from genshi.core import Markup, Attrs, Namespace, QName, escape, unescape
20from genshi.input import XML, ParseError
21from genshi.compat import StringIO, BytesIO
22
23
24class StreamTestCase(unittest.TestCase):
25
26    def test_render_utf8(self):
27        xml = XML('<li>Über uns</li>')
28        self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), xml.render(encoding='utf-8'))
29
30    def test_render_unicode(self):
31        xml = XML('<li>Über uns</li>')
32        self.assertEqual(u'<li>Über uns</li>', xml.render())
33        self.assertEqual(u'<li>Über uns</li>', xml.render(encoding=None))
34
35    def test_render_ascii(self):
36        xml = XML('<li>Über uns</li>')
37        self.assertEqual(u'<li>&#220;ber uns</li>'.encode('ascii'), xml.render(encoding='ascii'))
38
39    def test_render_output_stream_utf8(self):
40        xml = XML('<li>Über uns</li>')
41        strio = BytesIO()
42        self.assertEqual(None, xml.render(encoding='utf-8', out=strio))
43        self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), strio.getvalue())
44
45    def test_render_output_stream_unicode(self):
46        xml = XML('<li>Über uns</li>')
47        strio = StringIO()
48        self.assertEqual(None, xml.render(encoding=None, out=strio))
49        self.assertEqual(u'<li>Über uns</li>', strio.getvalue())
50
51    def test_pickle(self):
52        xml = XML('<li>Foo</li>')
53        buf = BytesIO()
54        pickle.dump(xml, buf, 2)
55        buf.seek(0)
56        xml = pickle.load(buf)
57        self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
58
59
60class MarkupTestCase(unittest.TestCase):
61
62    def test_new_with_encoding(self):
63        markup = Markup(u'Döner'.encode('utf-8'), encoding='utf-8')
64        # mimic Markup.__repr__ when constructing output for Python 2/3 compatibility
65        self.assertEquals("<Markup %r>" % u'D\u00f6ner', repr(markup))
66
67    def test_repr(self):
68        markup = Markup('foo')
69        self.assertEquals("<Markup u'foo'>", repr(markup))
70
71    def test_escape(self):
72        markup = escape('<b>"&"</b>')
73        assert type(markup) is Markup
74        self.assertEquals('&lt;b&gt;&#34;&amp;&#34;&lt;/b&gt;', markup)
75
76    def test_escape_noquotes(self):
77        markup = escape('<b>"&"</b>', quotes=False)
78        assert type(markup) is Markup
79        self.assertEquals('&lt;b&gt;"&amp;"&lt;/b&gt;', markup)
80
81    def test_unescape_markup(self):
82        string = '<b>"&"</b>'
83        markup = Markup.escape(string)
84        assert type(markup) is Markup
85        self.assertEquals(string, unescape(markup))
86
87    def test_Markup_escape_None_noquotes(self):
88        markup = Markup.escape(None, False)
89        assert type(markup) is Markup
90        self.assertEquals('', markup)
91
92    def test_add_str(self):
93        markup = Markup('<b>foo</b>') + '<br/>'
94        assert type(markup) is Markup
95        self.assertEquals('<b>foo</b>&lt;br/&gt;', markup)
96
97    def test_add_markup(self):
98        markup = Markup('<b>foo</b>') + Markup('<br/>')
99        assert type(markup) is Markup
100        self.assertEquals('<b>foo</b><br/>', markup)
101
102    def test_add_reverse(self):
103        markup = '<br/>' + Markup('<b>bar</b>')
104        assert type(markup) is Markup
105        self.assertEquals('&lt;br/&gt;<b>bar</b>', markup)
106
107    def test_mod(self):
108        markup = Markup('<b>%s</b>') % '&'
109        assert type(markup) is Markup
110        self.assertEquals('<b>&amp;</b>', markup)
111
112    def test_mod_multi(self):
113        markup = Markup('<b>%s</b> %s') % ('&', 'boo')
114        assert type(markup) is Markup
115        self.assertEquals('<b>&amp;</b> boo', markup)
116
117    def test_mod_mapping(self):
118        markup = Markup('<b>%(foo)s</b>') % {'foo': '&'}
119        assert type(markup) is Markup
120        self.assertEquals('<b>&amp;</b>', markup)
121
122    def test_mod_noescape(self):
123        markup = Markup('<b>%(amp)s</b>') % {'amp': Markup('&amp;')}
124        assert type(markup) is Markup
125        self.assertEquals('<b>&amp;</b>', markup)
126
127    def test_mul(self):
128        markup = Markup('<b>foo</b>') * 2
129        assert type(markup) is Markup
130        self.assertEquals('<b>foo</b><b>foo</b>', markup)
131
132    def test_mul_reverse(self):
133        markup = 2 * Markup('<b>foo</b>')
134        assert type(markup) is Markup
135        self.assertEquals('<b>foo</b><b>foo</b>', markup)
136
137    def test_join(self):
138        markup = Markup('<br />').join(['foo', '<bar />', Markup('<baz />')])
139        assert type(markup) is Markup
140        self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
141
142    def test_join_over_iter(self):
143        items = ['foo', '<bar />', Markup('<baz />')]
144        markup = Markup('<br />').join(i for i in items)
145        self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
146
147    def test_stripentities_all(self):
148        markup = Markup('&amp; &#106;').stripentities()
149        assert type(markup) is Markup
150        self.assertEquals('& j', markup)
151
152    def test_stripentities_keepxml(self):
153        markup = Markup('&amp; &#106;').stripentities(keepxmlentities=True)
154        assert type(markup) is Markup
155        self.assertEquals('&amp; j', markup)
156
157    def test_striptags_empty(self):
158        markup = Markup('<br />').striptags()
159        assert type(markup) is Markup
160        self.assertEquals('', markup)
161
162    def test_striptags_mid(self):
163        markup = Markup('<a href="#">fo<br />o</a>').striptags()
164        assert type(markup) is Markup
165        self.assertEquals('foo', markup)
166
167    def test_pickle(self):
168        markup = Markup('foo')
169        buf = BytesIO()
170        pickle.dump(markup, buf, 2)
171        buf.seek(0)
172        self.assertEquals("<Markup u'foo'>", repr(pickle.load(buf)))
173
174
175class AttrsTestCase(unittest.TestCase):
176
177    def test_pickle(self):
178        attrs = Attrs([("attr1", "foo"), ("attr2", "bar")])
179        buf = BytesIO()
180        pickle.dump(attrs, buf, 2)
181        buf.seek(0)
182        unpickled = pickle.load(buf)
183        self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])",
184                          repr(unpickled))
185
186    def test_non_ascii(self):
187        attrs_tuple = Attrs([("attr1", u"föö"), ("attr2", u"bär")]).totuple()
188        self.assertEqual(u'fööbär', attrs_tuple[1])
189
190
191class NamespaceTestCase(unittest.TestCase):
192
193    def test_repr(self):
194        self.assertEqual("Namespace('http://www.example.org/namespace')",
195                         repr(Namespace('http://www.example.org/namespace')))
196
197    def test_repr_eval(self):
198        ns = Namespace('http://www.example.org/namespace')
199        self.assertEqual(eval(repr(ns)), ns)
200
201    def test_repr_eval_non_ascii(self):
202        ns = Namespace(u'http://www.example.org/nämespäcé')
203        self.assertEqual(eval(repr(ns)), ns)
204
205    def test_pickle(self):
206        ns = Namespace('http://www.example.org/namespace')
207        buf = BytesIO()
208        pickle.dump(ns, buf, 2)
209        buf.seek(0)
210        unpickled = pickle.load(buf)
211        self.assertEquals("Namespace('http://www.example.org/namespace')",
212                          repr(unpickled))
213        self.assertEquals('http://www.example.org/namespace', unpickled.uri)
214
215
216class QNameTestCase(unittest.TestCase):
217
218    def test_pickle(self):
219        qname = QName('http://www.example.org/namespace}elem')
220        buf = BytesIO()
221        pickle.dump(qname, buf, 2)
222        buf.seek(0)
223        unpickled = pickle.load(buf)
224        self.assertEquals('{http://www.example.org/namespace}elem', unpickled)
225        self.assertEquals('http://www.example.org/namespace',
226                          unpickled.namespace)
227        self.assertEquals('elem', unpickled.localname)
228
229    def test_repr(self):
230        self.assertEqual("QName('elem')", repr(QName('elem')))
231        self.assertEqual("QName('http://www.example.org/namespace}elem')",
232                         repr(QName('http://www.example.org/namespace}elem')))
233
234    def test_repr_eval(self):
235        qn = QName('elem')
236        self.assertEqual(eval(repr(qn)), qn)
237
238    def test_repr_eval_non_ascii(self):
239        qn = QName(u'élem')
240        self.assertEqual(eval(repr(qn)), qn)
241
242    def test_leading_curly_brace(self):
243        qname = QName('{http://www.example.org/namespace}elem')
244        self.assertEquals('http://www.example.org/namespace', qname.namespace)
245        self.assertEquals('elem', qname.localname)
246
247    def test_curly_brace_equality(self):
248        qname1 = QName('{http://www.example.org/namespace}elem')
249        qname2 = QName('http://www.example.org/namespace}elem')
250        self.assertEqual(qname1.namespace, qname2.namespace)
251        self.assertEqual(qname1.localname, qname2.localname)
252        self.assertEqual(qname1, qname2)
253
254
255def suite():
256    suite = unittest.TestSuite()
257    suite.addTest(unittest.makeSuite(StreamTestCase, 'test'))
258    suite.addTest(unittest.makeSuite(MarkupTestCase, 'test'))
259    suite.addTest(unittest.makeSuite(NamespaceTestCase, 'test'))
260    suite.addTest(unittest.makeSuite(AttrsTestCase, 'test'))
261    suite.addTest(unittest.makeSuite(QNameTestCase, 'test'))
262    suite.addTest(doctest.DocTestSuite(core))
263    return suite
264
265if __name__ == '__main__':
266    unittest.main(defaultTest='suite')
Note: See TracBrowser for help on using the repository browser.