Mercurial > genshi > genshi-test
annotate genshi/tests/core.py @ 279:4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
author | cmlenz |
---|---|
date | Mon, 02 Oct 2006 19:08:36 +0000 |
parents | 24757b771651 |
children | 08ada6b4b767 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 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 | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
15 import pickle |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
16 from StringIO import StringIO |
1 | 17 import unittest |
18 | |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
19 from genshi import core |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
20 from genshi.core import Markup, Namespace, QName, escape, unescape |
230 | 21 from genshi.input import XML, ParseError |
147
f7fb556f2678
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 |
f7fb556f2678
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 |
f7fb556f2678
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): |
f7fb556f2678
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 |
f7fb556f2678
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): |
f7fb556f2678
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>') |
f7fb556f2678
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 self.assertEqual('<li>Über uns</li>', xml.render()) |
f7fb556f2678
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 |
f7fb556f2678
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): |
f7fb556f2678
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>') |
f7fb556f2678
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(u'<li>Über uns</li>', xml.render(encoding=None)) |
f7fb556f2678
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 |
f7fb556f2678
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_ascii(self): |
f7fb556f2678
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>') |
f7fb556f2678
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('<li>Über uns</li>', xml.render(encoding='ascii')) |
1 | 37 |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
38 def test_pickle(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
39 xml = XML('<li>Foo</li>') |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
40 buf = StringIO() |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
41 pickle.dump(xml, buf, 2) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
42 buf.seek(0) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
43 xml = pickle.load(buf) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
44 self.assertEquals('<li>Foo</li>', xml.render()) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
45 |
1 | 46 |
47 class MarkupTestCase(unittest.TestCase): | |
48 | |
116 | 49 def test_repr(self): |
50 markup = Markup('foo') | |
51 self.assertEquals('<Markup "foo">', repr(markup)) | |
52 | |
1 | 53 def test_escape(self): |
54 markup = escape('<b>"&"</b>') | |
212
e8c43127d9a9
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
|
55 assert type(markup) is Markup |
1 | 56 self.assertEquals('<b>"&"</b>', markup) |
57 | |
58 def test_escape_noquotes(self): | |
59 markup = escape('<b>"&"</b>', quotes=False) | |
212
e8c43127d9a9
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
|
60 assert type(markup) is Markup |
1 | 61 self.assertEquals('<b>"&"</b>', markup) |
62 | |
63 def test_unescape_markup(self): | |
64 string = '<b>"&"</b>' | |
65 markup = Markup.escape(string) | |
212
e8c43127d9a9
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
|
66 assert type(markup) is Markup |
1 | 67 self.assertEquals(string, unescape(markup)) |
68 | |
69 def test_add_str(self): | |
70 markup = Markup('<b>foo</b>') + '<br/>' | |
212
e8c43127d9a9
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
|
71 assert type(markup) is Markup |
1 | 72 self.assertEquals('<b>foo</b><br/>', markup) |
73 | |
74 def test_add_markup(self): | |
75 markup = Markup('<b>foo</b>') + Markup('<br/>') | |
212
e8c43127d9a9
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
|
76 assert type(markup) is Markup |
1 | 77 self.assertEquals('<b>foo</b><br/>', markup) |
78 | |
79 def test_add_reverse(self): | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
80 markup = '<br/>' + Markup('<b>bar</b>') |
212
e8c43127d9a9
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
|
81 assert type(markup) is Markup |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
82 self.assertEquals('<br/><b>bar</b>', markup) |
1 | 83 |
84 def test_mod(self): | |
85 markup = Markup('<b>%s</b>') % '&' | |
212
e8c43127d9a9
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 | 87 self.assertEquals('<b>&</b>', markup) |
88 | |
89 def test_mod_multi(self): | |
90 markup = Markup('<b>%s</b> %s') % ('&', 'boo') | |
212
e8c43127d9a9
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 | 92 self.assertEquals('<b>&</b> boo', markup) |
93 | |
94 def test_mul(self): | |
95 markup = Markup('<b>foo</b>') * 2 | |
212
e8c43127d9a9
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 | 97 self.assertEquals('<b>foo</b><b>foo</b>', markup) |
98 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
99 def test_mul_reverse(self): |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
100 markup = 2 * Markup('<b>foo</b>') |
212
e8c43127d9a9
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
516a6cae0aa8
* 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('<b>foo</b><b>foo</b>', markup) |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
103 |
1 | 104 def test_join(self): |
105 markup = Markup('<br />').join(['foo', '<bar />', Markup('<baz />')]) | |
212
e8c43127d9a9
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 | 107 self.assertEquals('foo<br /><bar /><br /><baz />', markup) |
108 | |
109 def test_stripentities_all(self): | |
110 markup = Markup('& j').stripentities() | |
212
e8c43127d9a9
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 | 112 self.assertEquals('& j', markup) |
113 | |
114 def test_stripentities_keepxml(self): | |
116 | 115 markup = Markup('& j').stripentities(keepxmlentities=True) |
212
e8c43127d9a9
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
|
116 assert type(markup) is Markup |
116 | 117 self.assertEquals('& j', markup) |
1 | 118 |
119 def test_striptags_empty(self): | |
120 markup = Markup('<br />').striptags() | |
212
e8c43127d9a9
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
|
121 assert type(markup) is Markup |
1 | 122 self.assertEquals('', markup) |
123 | |
124 def test_striptags_mid(self): | |
125 markup = Markup('<a href="#">fo<br />o</a>').striptags() | |
212
e8c43127d9a9
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 | 127 self.assertEquals('foo', markup) |
128 | |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
129 def test_pickle(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
130 markup = Markup('foo') |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
131 buf = StringIO() |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
132 pickle.dump(markup, buf, 2) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
133 buf.seek(0) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
134 self.assertEquals('<Markup "foo">', repr(pickle.load(buf))) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
135 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
136 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
137 class NamespaceTestCase(unittest.TestCase): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
138 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
139 def test_pickle(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
140 ns = Namespace('http://www.example.org/namespace') |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
141 buf = StringIO() |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
142 pickle.dump(ns, buf, 2) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
143 buf.seek(0) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
144 unpickled = pickle.load(buf) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
145 self.assertEquals('<Namespace "http://www.example.org/namespace">', |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
146 repr(unpickled)) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
147 self.assertEquals('http://www.example.org/namespace', unpickled.uri) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
148 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
149 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
150 class QNameTestCase(unittest.TestCase): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
151 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
152 def test_pickle(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
153 qname = QName('http://www.example.org/namespace}elem') |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
154 buf = StringIO() |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
155 pickle.dump(qname, buf, 2) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
156 buf.seek(0) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
157 unpickled = pickle.load(buf) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
158 self.assertEquals('{http://www.example.org/namespace}elem', unpickled) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
159 self.assertEquals('http://www.example.org/namespace', |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
160 unpickled.namespace) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
161 self.assertEquals('elem', unpickled.localname) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
162 |
1 | 163 |
164 def suite(): | |
165 suite = unittest.TestSuite() | |
147
f7fb556f2678
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
|
166 suite.addTest(unittest.makeSuite(StreamTestCase, 'test')) |
1 | 167 suite.addTest(unittest.makeSuite(MarkupTestCase, 'test')) |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
168 suite.addTest(unittest.makeSuite(NamespaceTestCase, 'test')) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
169 suite.addTest(unittest.makeSuite(QNameTestCase, 'test')) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
230
diff
changeset
|
170 suite.addTest(doctest.DocTestSuite(core)) |
1 | 171 return suite |
172 | |
173 if __name__ == '__main__': | |
174 unittest.main(defaultTest='suite') |