annotate genshi/tests/builder.py @ 730:a424b2443e11

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 921c873c2f0e
children 686bbeecb9ac
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
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 98
diff changeset
8 # are also available at http://genshi.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
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 98
diff changeset
12 # history and logs, available at http://genshi.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 from HTMLParser import HTMLParseError
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 import unittest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 98
diff changeset
18 from genshi.builder import Element, tag
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
19 from genshi.core import Attrs, Stream
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
20 from genshi.input import XML
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
23 class ElementFactoryTestCase(unittest.TestCase):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25 def test_link(self):
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 1
diff changeset
26 link = tag.a(href='#', title='Foo', accesskey=None)('Bar')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
27 bits = iter(link.generate())
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
28 self.assertEqual((Stream.START,
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
29 ('a', Attrs([('href', "#"), ('title', "Foo")])),
94
80c72e936e72 Some bugfixes and minor performance improvements for the builder module.
cmlenz
parents: 66
diff changeset
30 (None, -1, -1)), bits.next())
80c72e936e72 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())
80c72e936e72 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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33
98
bc73d3ab823f 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):
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
35 """
bc73d3ab823f 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
bc73d3ab823f 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
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
38 generated.
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
39 """
bc73d3ab823f 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
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
41 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])),
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
42 (None, -1, -1)),
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
43 event)
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
44
730
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
45 def test_duplicate_attributes(self):
a424b2443e11 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')
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
47 bits = iter(link.generate())
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
48 self.assertEqual((Stream.START,
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
49 ('a', Attrs([('href', "#1")])),
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
50 (None, -1, -1)), bits.next())
a424b2443e11 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())
a424b2443e11 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())
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
53
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
54 def test_stream_as_child(self):
d8c5045b547a 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())
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
56 self.assertEqual(5, len(xml))
386
921c873c2f0e Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
57 self.assertEqual((Stream.START, ('span', ())), xml[0][:2])
921c873c2f0e Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
58 self.assertEqual((Stream.START, ('b', ())), xml[1][:2])
921c873c2f0e Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
59 self.assertEqual((Stream.TEXT, 'Foo'), xml[2][:2])
921c873c2f0e Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
60 self.assertEqual((Stream.END, 'b'), xml[3][:2])
921c873c2f0e Unit test fixes for Python 2.3.
cmlenz
parents: 379
diff changeset
61 self.assertEqual((Stream.END, 'span'), xml[4][:2])
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
62
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 suite = unittest.TestSuite()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66 suite.addTest(doctest.DocTestSuite(Element.__module__))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
68 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
69
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
70 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
71 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software