annotate genshi/tests/core.py @ 718:d143dd73789b experimental-match-fastpaths

update to trunk through r833
author aflett
date Tue, 08 Apr 2008 23:45:32 +0000
parents af57b12e3dd2
children
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 27
diff changeset
3 # Copyright (C) 2006 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
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
16 from StringIO import StringIO
703
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
17 try:
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
18 from cStringIO import StringIO as cStringIO
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
19 except ImportError:
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
20 cStringIO = StringIO
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
23 from genshi import core
718
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
24 from genshi.core import Markup, Attrs, Namespace, QName, escape, unescape
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 212
diff changeset
25 from genshi.input import XML, ParseError
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
26
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
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
28 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
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_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
31 xml = XML('<li>Über uns</li>')
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
32 self.assertEqual('<li>Über uns</li>', xml.render())
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
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 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
35 xml = XML('<li>Über uns</li>')
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 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
37
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
38 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
39 xml = XML('<li>Über uns</li>')
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
40 self.assertEqual('<li>&#220;ber uns</li>', xml.render(encoding='ascii'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
41
703
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
42 def test_render_output_stream_utf8(self):
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
43 xml = XML('<li>Über uns</li>')
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
44 strio = cStringIO()
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
45 self.assertEqual(None, xml.render(out=strio))
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
46 self.assertEqual('<li>Über uns</li>', strio.getvalue())
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
47
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
48 def test_render_output_stream_unicode(self):
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
49 xml = XML('<li>Über uns</li>')
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
50 strio = StringIO()
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
51 self.assertEqual(None, xml.render(encoding=None, out=strio))
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
52 self.assertEqual(u'<li>Über uns</li>', strio.getvalue())
af57b12e3dd2 merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
aflett
parents: 666
diff changeset
53
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
54 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
55 xml = XML('<li>Foo</li>')
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
56 buf = StringIO()
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
57 pickle.dump(xml, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
58 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
59 xml = pickle.load(buf)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
60 self.assertEquals('<li>Foo</li>', xml.render())
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
61
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
62
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
63 class MarkupTestCase(unittest.TestCase):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
64
718
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
65 def test_new_with_encoding(self):
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
66 markup = Markup('Döner', encoding='utf-8')
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
67 self.assertEquals("<Markup u'D\\xf6ner'>", repr(markup))
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
68
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
69 def test_repr(self):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
70 markup = Markup('foo')
382
2682dabbcd04 * Added documentation for the various stream event kinds.
cmlenz
parents: 326
diff changeset
71 self.assertEquals("<Markup u'foo'>", repr(markup))
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
72
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
73 def test_escape(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
74 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
75 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
76 self.assertEquals('&lt;b&gt;&#34;&amp;&#34;&lt;/b&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
77
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
78 def test_escape_noquotes(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
79 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
80 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
81 self.assertEquals('&lt;b&gt;"&amp;"&lt;/b&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
82
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
83 def test_unescape_markup(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
84 string = '<b>"&"</b>'
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
85 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
86 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
87 self.assertEquals(string, unescape(markup))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
88
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
89 def test_add_str(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
90 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
91 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
92 self.assertEquals('<b>foo</b>&lt;br/&gt;', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
93
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
94 def test_add_markup(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
95 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
96 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
97 self.assertEquals('<b>foo</b><br/>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
98
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
99 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
100 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
101 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
102 self.assertEquals('&lt;br/&gt;<b>bar</b>', markup)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
103
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
104 def test_mod(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
105 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
106 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
107 self.assertEquals('<b>&amp;</b>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
108
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
109 def test_mod_multi(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
110 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
111 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
112 self.assertEquals('<b>&amp;</b> boo', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
113
718
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
114 def test_mod_mapping(self):
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
115 markup = Markup('<b>%(foo)s</b>') % {'foo': '&'}
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
116 assert type(markup) is Markup
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
117 self.assertEquals('<b>&amp;</b>', markup)
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
118
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
119 def test_mod_noescape(self):
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
120 markup = Markup('<b>%(amp)s</b>') % {'amp': Markup('&amp;')}
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
121 assert type(markup) is Markup
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
122 self.assertEquals('<b>&amp;</b>', markup)
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
123
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
124 def test_mul(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
125 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
126 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127 self.assertEquals('<b>foo</b><b>foo</b>', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128
204
51d4101f49ca * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 147
diff changeset
129 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
130 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
131 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
132 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
133
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
134 def test_join(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
135 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
136 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
137 self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
138
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
139 def test_stripentities_all(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
140 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
141 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
142 self.assertEquals('& j', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
143
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
144 def test_stripentities_keepxml(self):
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
145 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
146 assert type(markup) is Markup
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
147 self.assertEquals('&amp; j', markup)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
148
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
149 def test_striptags_empty(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
150 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
151 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
152 self.assertEquals('', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
153
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
154 def test_striptags_mid(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
155 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
156 assert type(markup) is Markup
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
157 self.assertEquals('foo', markup)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
158
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
159 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
160 markup = Markup('foo')
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
161 buf = StringIO()
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
162 pickle.dump(markup, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
163 buf.seek(0)
382
2682dabbcd04 * Added documentation for the various stream event kinds.
cmlenz
parents: 326
diff changeset
164 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
165
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
166
718
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
167 class AttrsTestCase(unittest.TestCase):
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
168
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
169 def test_pickle(self):
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
170 attrs = Attrs([("attr1", "foo"), ("attr2", "bar")])
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
171 buf = StringIO()
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
172 pickle.dump(attrs, buf, 2)
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
173 buf.seek(0)
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
174 unpickled = pickle.load(buf)
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
175 self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])",
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
176 repr(unpickled))
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
177
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
178
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
179 class NamespaceTestCase(unittest.TestCase):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
180
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
181 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
182 ns = Namespace('http://www.example.org/namespace')
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
183 buf = StringIO()
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
184 pickle.dump(ns, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
185 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
186 unpickled = pickle.load(buf)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
187 self.assertEquals('<Namespace "http://www.example.org/namespace">',
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
188 repr(unpickled))
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
189 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
190
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
191
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
192 class QNameTestCase(unittest.TestCase):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
193
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
194 def test_pickle(self):
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
195 qname = QName('http://www.example.org/namespace}elem')
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
196 buf = StringIO()
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
197 pickle.dump(qname, buf, 2)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
198 buf.seek(0)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
199 unpickled = pickle.load(buf)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
200 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
201 self.assertEquals('http://www.example.org/namespace',
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
202 unpickled.namespace)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
203 self.assertEquals('elem', unpickled.localname)
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
204
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
205 def test_repr(self):
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
206 self.assertEqual("QName(u'elem')", repr(QName('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
207 self.assertEqual("QName(u'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
208 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
209
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
210 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
211 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
212 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
213 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
214
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
215
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
216 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
217 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
218 suite.addTest(unittest.makeSuite(StreamTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
219 suite.addTest(unittest.makeSuite(MarkupTestCase, 'test'))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
220 suite.addTest(unittest.makeSuite(NamespaceTestCase, 'test'))
718
d143dd73789b update to trunk through r833
aflett
parents: 703
diff changeset
221 suite.addTest(unittest.makeSuite(AttrsTestCase, 'test'))
279
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
222 suite.addTest(unittest.makeSuite(QNameTestCase, 'test'))
a99666402b12 Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 230
diff changeset
223 suite.addTest(doctest.DocTestSuite(core))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
224 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
225
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
226 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
227 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software