annotate genshi/tests/core.py @ 1041:cddcb39ce5dd stable-0.7.x

Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
author hodgestar
date Thu, 20 Mar 2014 11:44:48 +0000
parents 657f9ecffa3c
children
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
854
4d9bef447df9 More work on reducing the size of the diff produced by 2to3.
cmlenz
parents: 852
diff changeset
3 # Copyright (C) 2006-2009 Edgewall Software
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 import doctest
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
15 import pickle
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
18 from genshi import core
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
19 from genshi.core import Markup, Attrs, Namespace, QName, escape, unescape
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
20 from genshi.input import XML, ParseError
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
21 from genshi.compat import StringIO, BytesIO
147
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
22
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
23
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
24 class StreamTestCase(unittest.TestCase):
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
25
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
26 def test_render_utf8(self):
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
27 xml = XML('<li>Über uns</li>')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
28 self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), xml.render(encoding='utf-8'))
147
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
29
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
30 def test_render_unicode(self):
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
31 xml = XML('<li>Über uns</li>')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
32 self.assertEqual(u'<li>Über uns</li>', xml.render())
147
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
33 self.assertEqual(u'<li>Über uns</li>', xml.render(encoding=None))
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
34
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
35 def test_render_ascii(self):
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
36 xml = XML('<li>Über uns</li>')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
37 self.assertEqual(u'<li>&#220;ber uns</li>'.encode('ascii'), xml.render(encoding='ascii'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
38
688
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
39 def test_render_output_stream_utf8(self):
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
40 xml = XML('<li>Über uns</li>')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
41 strio = BytesIO()
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
42 self.assertEqual(None, xml.render(encoding='utf-8', out=strio))
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
43 self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), strio.getvalue())
688
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
44
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
45 def test_render_output_stream_unicode(self):
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
46 xml = XML('<li>Über uns</li>')
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
47 strio = StringIO()
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
48 self.assertEqual(None, xml.render(encoding=None, out=strio))
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
49 self.assertEqual(u'<li>Über uns</li>', strio.getvalue())
d8571da25bc5 The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents: 666
diff changeset
50
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
51 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
52 xml = XML('<li>Foo</li>')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
53 buf = BytesIO()
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
54 pickle.dump(xml, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
55 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
56 xml = pickle.load(buf)
862
f4543bebe41b Make the output tests skip the encoding step.
cmlenz
parents: 857
diff changeset
57 self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
58
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
59
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
60 class MarkupTestCase(unittest.TestCase):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
61
713
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
62 def test_new_with_encoding(self):
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
63 markup = Markup(u'Döner'.encode('utf-8'), encoding='utf-8')
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
64 # mimic Markup.__repr__ when constructing output for Python 2/3 compatibility
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
65 self.assertEquals("<Markup %r>" % u'D\u00f6ner', repr(markup))
713
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
66
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
67 def test_repr(self):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
68 markup = Markup('foo')
382
2682dabbcd04 * Added documentation for the various stream event kinds.
cmlenz
parents: 326
diff changeset
69 self.assertEquals("<Markup u'foo'>", repr(markup))
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
70
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
71 def test_escape(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
72 markup = escape('<b>"&"</b>')
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
73 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
74 self.assertEquals('&lt;b&gt;&#34;&amp;&#34;&lt;/b&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
75
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
76 def test_escape_noquotes(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
77 markup = escape('<b>"&"</b>', quotes=False)
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
78 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
79 self.assertEquals('&lt;b&gt;"&amp;"&lt;/b&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
81 def test_unescape_markup(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
82 string = '<b>"&"</b>'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
83 markup = Markup.escape(string)
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
84 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
85 self.assertEquals(string, unescape(markup))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
86
943
657f9ecffa3c Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents: 932
diff changeset
87 def test_Markup_escape_None_noquotes(self):
657f9ecffa3c Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents: 932
diff changeset
88 markup = Markup.escape(None, False)
657f9ecffa3c Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents: 932
diff changeset
89 assert type(markup) is Markup
657f9ecffa3c Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents: 932
diff changeset
90 self.assertEquals('', markup)
657f9ecffa3c Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents: 932
diff changeset
91
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
92 def test_add_str(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
93 markup = Markup('<b>foo</b>') + '<br/>'
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
94 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
95 self.assertEquals('<b>foo</b>&lt;br/&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
97 def test_add_markup(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
98 markup = Markup('<b>foo</b>') + Markup('<br/>')
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
99 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
100 self.assertEquals('<b>foo</b><br/>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
101
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
102 def test_add_reverse(self):
204
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
103 markup = '<br/>' + Markup('<b>bar</b>')
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
104 assert type(markup) is Markup
204
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
105 self.assertEquals('&lt;br/&gt;<b>bar</b>', markup)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
106
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
107 def test_mod(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
108 markup = Markup('<b>%s</b>') % '&'
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
109 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
110 self.assertEquals('<b>&amp;</b>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
111
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
112 def test_mod_multi(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
113 markup = Markup('<b>%s</b> %s') % ('&', 'boo')
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
114 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
115 self.assertEquals('<b>&amp;</b> boo', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
116
713
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
117 def test_mod_mapping(self):
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
118 markup = Markup('<b>%(foo)s</b>') % {'foo': '&'}
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
119 assert type(markup) is Markup
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
120 self.assertEquals('<b>&amp;</b>', markup)
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
121
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
122 def test_mod_noescape(self):
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
123 markup = Markup('<b>%(amp)s</b>') % {'amp': Markup('&amp;')}
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
124 assert type(markup) is Markup
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
125 self.assertEquals('<b>&amp;</b>', markup)
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 688
diff changeset
126
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127 def test_mul(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128 markup = Markup('<b>foo</b>') * 2
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
129 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
130 self.assertEquals('<b>foo</b><b>foo</b>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
131
204
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
132 def test_mul_reverse(self):
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
133 markup = 2 * Markup('<b>foo</b>')
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
134 assert type(markup) is Markup
204
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
135 self.assertEquals('<b>foo</b><b>foo</b>', markup)
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
136
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
137 def test_join(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
138 markup = Markup('<br />').join(['foo', '<bar />', Markup('<baz />')])
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
139 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
140 self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
141
1041
cddcb39ce5dd Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents: 943
diff changeset
142 def test_join_over_iter(self):
cddcb39ce5dd Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents: 943
diff changeset
143 items = ['foo', '<bar />', Markup('<baz />')]
cddcb39ce5dd Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents: 943
diff changeset
144 markup = Markup('<br />').join(i for i in items)
cddcb39ce5dd Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents: 943
diff changeset
145 self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
cddcb39ce5dd Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents: 943
diff changeset
146
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
147 def test_stripentities_all(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
148 markup = Markup('&amp; &#106;').stripentities()
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
149 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
150 self.assertEquals('& j', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
151
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
152 def test_stripentities_keepxml(self):
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
153 markup = Markup('&amp; &#106;').stripentities(keepxmlentities=True)
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
154 assert type(markup) is Markup
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
155 self.assertEquals('&amp; j', markup)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
156
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
157 def test_striptags_empty(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
158 markup = Markup('<br />').striptags()
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
159 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
160 self.assertEquals('', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
161
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
162 def test_striptags_mid(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
163 markup = Markup('<a href="#">fo<br />o</a>').striptags()
212
0141f45c18e1 Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents: 204
diff changeset
164 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
165 self.assertEquals('foo', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
166
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
167 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
168 markup = Markup('foo')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
169 buf = BytesIO()
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
170 pickle.dump(markup, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
171 buf.seek(0)
382
2682dabbcd04 * Added documentation for the various stream event kinds.
cmlenz
parents: 326
diff changeset
172 self.assertEquals("<Markup u'foo'>", repr(pickle.load(buf)))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
173
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
174
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
175 class AttrsTestCase(unittest.TestCase):
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
176
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
177 def test_pickle(self):
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
178 attrs = Attrs([("attr1", "foo"), ("attr2", "bar")])
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
179 buf = BytesIO()
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
180 pickle.dump(attrs, buf, 2)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
181 buf.seek(0)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
182 unpickled = pickle.load(buf)
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
183 self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])",
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
184 repr(unpickled))
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
185
852
07f4339fecb0 Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents: 715
diff changeset
186 def test_non_ascii(self):
07f4339fecb0 Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents: 715
diff changeset
187 attrs_tuple = Attrs([("attr1", u"föö"), ("attr2", u"bär")]).totuple()
07f4339fecb0 Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents: 715
diff changeset
188 self.assertEqual(u'fööbär', attrs_tuple[1])
07f4339fecb0 Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents: 715
diff changeset
189
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
190
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
191 class NamespaceTestCase(unittest.TestCase):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
192
857
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
193 def test_repr(self):
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
194 self.assertEqual("Namespace('http://www.example.org/namespace')",
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
195 repr(Namespace('http://www.example.org/namespace')))
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
196
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
197 def test_repr_eval(self):
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
198 ns = Namespace('http://www.example.org/namespace')
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
199 self.assertEqual(eval(repr(ns)), ns)
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
200
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
201 def test_repr_eval_non_ascii(self):
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
202 ns = Namespace(u'http://www.example.org/nämespäcé')
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
203 self.assertEqual(eval(repr(ns)), ns)
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
204
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
205 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
206 ns = Namespace('http://www.example.org/namespace')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
207 buf = BytesIO()
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
208 pickle.dump(ns, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
209 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
210 unpickled = pickle.load(buf)
857
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
211 self.assertEquals("Namespace('http://www.example.org/namespace')",
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
212 repr(unpickled))
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
213 self.assertEquals('http://www.example.org/namespace', unpickled.uri)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
214
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
215
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
216 class QNameTestCase(unittest.TestCase):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
217
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
218 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
219 qname = QName('http://www.example.org/namespace}elem')
932
18209925c54e Merge r1140 from py3k:
hodgestar
parents: 923
diff changeset
220 buf = BytesIO()
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
221 pickle.dump(qname, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
222 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
223 unpickled = pickle.load(buf)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
224 self.assertEquals('{http://www.example.org/namespace}elem', unpickled)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
225 self.assertEquals('http://www.example.org/namespace',
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
226 unpickled.namespace)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
227 self.assertEquals('elem', unpickled.localname)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
228
326
f999da894391 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
229 def test_repr(self):
857
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
230 self.assertEqual("QName('elem')", repr(QName('elem')))
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
231 self.assertEqual("QName('http://www.example.org/namespace}elem')",
326
f999da894391 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
232 repr(QName('http://www.example.org/namespace}elem')))
f999da894391 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
233
857
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
234 def test_repr_eval(self):
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
235 qn = QName('elem')
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
236 self.assertEqual(eval(repr(qn)), qn)
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
237
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
238 def test_repr_eval_non_ascii(self):
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
239 qn = QName(u'élem')
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
240 self.assertEqual(eval(repr(qn)), qn)
129e54866a98 Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents: 854
diff changeset
241
666
050657e221d4 `QName` can now be constructed from a string with a leading curly brace, and some doc improvements. Closes #164.
cmlenz
parents: 382
diff changeset
242 def test_leading_curly_brace(self):
050657e221d4 `QName` can now be constructed from a string with a leading curly brace, and some doc improvements. Closes #164.
cmlenz
parents: 382
diff changeset
243 qname = QName('{http://www.example.org/namespace}elem')
050657e221d4 `QName` can now be constructed from a string with a leading curly brace, and some doc improvements. Closes #164.
cmlenz
parents: 382
diff changeset
244 self.assertEquals('http://www.example.org/namespace', qname.namespace)
050657e221d4 `QName` can now be constructed from a string with a leading curly brace, and some doc improvements. Closes #164.
cmlenz
parents: 382
diff changeset
245 self.assertEquals('elem', qname.localname)
050657e221d4 `QName` can now be constructed from a string with a leading curly brace, and some doc improvements. Closes #164.
cmlenz
parents: 382
diff changeset
246
923
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
247 def test_curly_brace_equality(self):
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
248 qname1 = QName('{http://www.example.org/namespace}elem')
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
249 qname2 = QName('http://www.example.org/namespace}elem')
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
250 self.assertEqual(qname1.namespace, qname2.namespace)
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
251 self.assertEqual(qname1.localname, qname2.localname)
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
252 self.assertEqual(qname1, qname2)
374203a70ef8 Pull up r1145 to trunk.
jruigrok
parents: 862
diff changeset
253
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
254
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
255 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
256 suite = unittest.TestSuite()
147
a4a0ca41b6ad Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 116
diff changeset
257 suite.addTest(unittest.makeSuite(StreamTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
258 suite.addTest(unittest.makeSuite(MarkupTestCase, 'test'))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
259 suite.addTest(unittest.makeSuite(NamespaceTestCase, 'test'))
715
b5bd8c109209 Enable pickling of `Template` and `Code` objects.
cmlenz
parents: 713
diff changeset
260 suite.addTest(unittest.makeSuite(AttrsTestCase, 'test'))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
261 suite.addTest(unittest.makeSuite(QNameTestCase, 'test'))
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
262 suite.addTest(doctest.DocTestSuite(core))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
263 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
264
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
265 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
266 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software