annotate markup/tests/core.py @ 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.
author cmlenz
date Wed, 30 Aug 2006 12:40:44 +0000
parents 516a6cae0aa8
children
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 import doctest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15 import unittest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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>&#220;ber uns</li>', xml.render(encoding='ascii'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
35
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36 class MarkupTestCase(unittest.TestCase):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
37
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
38 def test_repr(self):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
39 markup = Markup('foo')
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
40 self.assertEquals('<Markup "foo">', repr(markup))
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
41
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
42 def test_escape(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
45 self.assertEquals('&lt;b&gt;&#34;&amp;&#34;&lt;/b&gt;', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
46
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
47 def test_escape_noquotes(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 self.assertEquals('&lt;b&gt;"&amp;"&lt;/b&gt;', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
52 def test_unescape_markup(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53 string = '<b>"&"</b>'
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
56 self.assertEquals(string, unescape(markup))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
57
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
58 def test_add_str(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
61 self.assertEquals('<b>foo</b>&lt;br/&gt;', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
62
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63 def test_add_markup(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66 self.assertEquals('<b>foo</b><br/>', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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('&lt;br/&gt;<b>bar</b>', markup)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
72
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73 def test_mod(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76 self.assertEquals('<b>&amp;</b>', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
78 def test_mod_multi(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
81 self.assertEquals('<b>&amp;</b> boo', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
82
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
83 def test_mul(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
86 self.assertEquals('<b>foo</b><b>foo</b>', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
93 def test_join(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
96 self.assertEquals('foo<br />&lt;bar /&gt;<br /><baz />', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
97
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
98 def test_stripentities_all(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
99 markup = Markup('&amp; &#106;').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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
101 self.assertEquals('& j', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
102
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
103 def test_stripentities_keepxml(self):
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
104 markup = Markup('&amp; &#106;').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
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 113
diff changeset
106 self.assertEquals('&amp; j', markup)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
107
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
108 def test_striptags_empty(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
111 self.assertEquals('', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
112
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
113 def test_striptags_mid(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
116 self.assertEquals('foo', markup)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
118
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
124 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
125
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
126 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
127 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software