annotate genshi/tests/builder.py @ 379:e1d659c87ddf trunk

The builder API now accepts streams as children of elements and fragments.
author cmlenz
date Thu, 23 Nov 2006 17:48:17 +0000
parents 2aa7ca37ae6a
children c66370dfc41b
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: 98
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: 98
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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 from HTMLParser import HTMLParseError
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 98
diff changeset
18 from genshi.builder import Element, tag
345
2aa7ca37ae6a Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
19 from genshi.core import Attrs, Stream
379
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
20 from genshi.input import XML
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
23 class ElementFactoryTestCase(unittest.TestCase):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
25 def test_link(self):
20
cc92d74ce9e5 Fix tests broken in [20].
cmlenz
parents: 1
diff changeset
26 link = tag.a(href='#', title='Foo', accesskey=None)('Bar')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
27 bits = iter(link.generate())
345
2aa7ca37ae6a Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
28 self.assertEqual((Stream.START,
2aa7ca37ae6a Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
29 ('a', Attrs([('href', "#"), ('title', "Foo")])),
94
0f8800c46e21 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 66
diff changeset
30 (None, -1, -1)), bits.next())
0f8800c46e21 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 66
diff changeset
31 self.assertEqual((Stream.TEXT, u'Bar', (None, -1, -1)), bits.next())
0f8800c46e21 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 66
diff changeset
32 self.assertEqual((Stream.END, 'a', (None, -1, -1)), bits.next())
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
33
98
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
34 def test_nonstring_attributes(self):
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
35 """
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
36 Verify that if an attribute value is given as an int (or some other
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
37 non-string type), it is coverted to a string when the stream is
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
38 generated.
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
39 """
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
40 event = iter(tag.foo(id=3)).next()
345
2aa7ca37ae6a Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
41 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])),
2aa7ca37ae6a Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
42 (None, -1, -1)),
98
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
43 event)
44af12832c5a Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
44
379
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
45 def test_stream_as_child(self):
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
46 xml = list(tag.span(XML('<b>Foo</b>')).generate())
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
47 self.assertEqual(5, len(xml))
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
48 self.assertEqual((Stream.START, ('span', ()), (None, -1, -1)), xml[0])
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
49 self.assertEqual((Stream.START, ('b', ()), (None, 1, 0)), xml[1])
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
50 self.assertEqual((Stream.TEXT, 'Foo', (None, 1, 3)), xml[2])
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
51 self.assertEqual((Stream.END, 'b', (None, 1, 6)), xml[3])
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
52 self.assertEqual((Stream.END, 'span', (None, -1, -1)), xml[4])
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
53
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
54
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
55 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
56 suite = unittest.TestSuite()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
57 suite.addTest(doctest.DocTestSuite(Element.__module__))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
58 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
59 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
60
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
61 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
62 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software