Mercurial > genshi > genshi-test
annotate markup/tests/core.py @ 221:c448cf114c30
Fix Python 2.3 incompatibility introduced in [276].
author | cmlenz |
---|---|
date | Tue, 05 Sep 2006 16:35:54 +0000 |
parents | e8c43127d9a9 |
children |
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 | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
8 # are also available at http://markup.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 | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
17 from markup.core import * |
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
|
18 from markup.input import XML, ParseError |
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
|
19 |
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
|
20 |
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
|
21 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
|
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 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
|
24 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
|
25 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
|
26 |
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 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
|
28 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
|
29 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
|
30 |
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 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
|
32 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
|
33 self.assertEqual('<li>Über uns</li>', xml.render(encoding='ascii')) |
1 | 34 |
35 | |
36 class MarkupTestCase(unittest.TestCase): | |
37 | |
116 | 38 def test_repr(self): |
39 markup = Markup('foo') | |
40 self.assertEquals('<Markup "foo">', repr(markup)) | |
41 | |
1 | 42 def test_escape(self): |
43 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
|
44 assert type(markup) is Markup |
1 | 45 self.assertEquals('<b>"&"</b>', markup) |
46 | |
47 def test_escape_noquotes(self): | |
48 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
|
49 assert type(markup) is Markup |
1 | 50 self.assertEquals('<b>"&"</b>', markup) |
51 | |
52 def test_unescape_markup(self): | |
53 string = '<b>"&"</b>' | |
54 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
|
55 assert type(markup) is Markup |
1 | 56 self.assertEquals(string, unescape(markup)) |
57 | |
58 def test_add_str(self): | |
59 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
|
60 assert type(markup) is Markup |
1 | 61 self.assertEquals('<b>foo</b><br/>', markup) |
62 | |
63 def test_add_markup(self): | |
64 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
|
65 assert type(markup) is Markup |
1 | 66 self.assertEquals('<b>foo</b><br/>', markup) |
67 | |
68 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
|
69 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
|
70 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
|
71 self.assertEquals('<br/><b>bar</b>', markup) |
1 | 72 |
73 def test_mod(self): | |
74 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
|
75 assert type(markup) is Markup |
1 | 76 self.assertEquals('<b>&</b>', markup) |
77 | |
78 def test_mod_multi(self): | |
79 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
|
80 assert type(markup) is Markup |
1 | 81 self.assertEquals('<b>&</b> boo', markup) |
82 | |
83 def test_mul(self): | |
84 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
|
85 assert type(markup) is Markup |
1 | 86 self.assertEquals('<b>foo</b><b>foo</b>', markup) |
87 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
147
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 |
1 | 93 def test_join(self): |
94 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
|
95 assert type(markup) is Markup |
1 | 96 self.assertEquals('foo<br /><bar /><br /><baz />', markup) |
97 | |
98 def test_stripentities_all(self): | |
99 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
|
100 assert type(markup) is Markup |
1 | 101 self.assertEquals('& j', markup) |
102 | |
103 def test_stripentities_keepxml(self): | |
116 | 104 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
|
105 assert type(markup) is Markup |
116 | 106 self.assertEquals('& j', markup) |
1 | 107 |
108 def test_striptags_empty(self): | |
109 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
|
110 assert type(markup) is Markup |
1 | 111 self.assertEquals('', markup) |
112 | |
113 def test_striptags_mid(self): | |
114 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
|
115 assert type(markup) is Markup |
1 | 116 self.assertEquals('foo', markup) |
117 | |
118 | |
119 def suite(): | |
120 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
|
121 suite.addTest(unittest.makeSuite(StreamTestCase, 'test')) |
1 | 122 suite.addTest(unittest.makeSuite(MarkupTestCase, 'test')) |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
123 suite.addTest(doctest.DocTestSuite(Markup.__module__)) |
1 | 124 return suite |
125 | |
126 if __name__ == '__main__': | |
127 unittest.main(defaultTest='suite') |