annotate genshi/tests/builder.py @ 730:5e9d250ad3ad trunk

Fix for potential duplicate attributes making it through the builder API. Closes #216.
author cmlenz
date Sat, 31 May 2008 21:34:16 +0000
parents c66370dfc41b
children ca72e3dc443d
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
730
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
45 def test_duplicate_attributes(self):
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
46 link = tag.a(href='#1', href_='#2')('Bar')
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
47 bits = iter(link.generate())
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
48 self.assertEqual((Stream.START,
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
49 ('a', Attrs([('href', "#1")])),
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
50 (None, -1, -1)), bits.next())
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
51 self.assertEqual((Stream.TEXT, u'Bar', (None, -1, -1)), bits.next())
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
52 self.assertEqual((Stream.END, 'a', (None, -1, -1)), bits.next())
5e9d250ad3ad Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
53
379
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
54 def test_stream_as_child(self):
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
55 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
56 self.assertEqual(5, len(xml))
386
c66370dfc41b Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
57 self.assertEqual((Stream.START, ('span', ())), xml[0][:2])
c66370dfc41b Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
58 self.assertEqual((Stream.START, ('b', ())), xml[1][:2])
c66370dfc41b Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
59 self.assertEqual((Stream.TEXT, 'Foo'), xml[2][:2])
c66370dfc41b Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
60 self.assertEqual((Stream.END, 'b'), xml[3][:2])
c66370dfc41b Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
61 self.assertEqual((Stream.END, 'span'), xml[4][:2])
379
e1d659c87ddf The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
62
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
63
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
64 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
65 suite = unittest.TestSuite()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
66 suite.addTest(doctest.DocTestSuite(Element.__module__))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
67 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test'))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
68 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
69
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
70 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
71 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software