annotate genshi/tests/builder.py @ 938:a5a1c9a11135 tip

update tags
author convert-repo
date Tue, 31 May 2011 20:05:15 +0000
parents df860bdad9ca
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
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 import unittest
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 98
diff changeset
17 from genshi.builder import Element, tag
737
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
18 from genshi.core import Attrs, Markup, Stream
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
19 from genshi.input import XML
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22 class ElementFactoryTestCase(unittest.TestCase):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
23
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24 def test_link(self):
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 1
diff changeset
25 link = tag.a(href='#', title='Foo', accesskey=None)('Bar')
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
26 events = list(link.generate())
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
27 self.assertEqual((Stream.START,
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
28 ('a', Attrs([('href', "#"), ('title', "Foo")])),
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
29 (None, -1, -1)), events[0])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
30 self.assertEqual((Stream.TEXT, 'Bar', (None, -1, -1)), events[1])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
31 self.assertEqual((Stream.END, 'a', (None, -1, -1)), events[2])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
32
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
33 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
34 """
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
35 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
36 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
37 generated.
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
38 """
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
39 events = list(tag.foo(id=3))
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 230
diff changeset
40 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])),
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
41 (None, -1, -1)), events[0])
98
bc73d3ab823f Bugfix in `builder` module: attribute values need to be converted to strings when generating streams.
cmlenz
parents: 94
diff changeset
42
730
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
43 def test_duplicate_attributes(self):
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
44 link = tag.a(href='#1', href_='#2')('Bar')
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
45 events = list(link.generate())
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
46 self.assertEqual((Stream.START, ('a', Attrs([('href', "#1")])),
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
47 (None, -1, -1)), events[0])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
48 self.assertEqual((Stream.TEXT, 'Bar', (None, -1, -1)), events[1])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
49 self.assertEqual((Stream.END, 'a', (None, -1, -1)), events[2])
730
a424b2443e11 Fix for potential duplicate attributes making it through the builder API. Closes #216.
cmlenz
parents: 386
diff changeset
50
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
51 def test_stream_as_child(self):
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
52 events = list(tag.span(XML('<b>Foo</b>')).generate())
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
53 self.assertEqual(5, len(events))
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
54 self.assertEqual((Stream.START, ('span', ())), events[0][:2])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
55 self.assertEqual((Stream.START, ('b', ())), events[1][:2])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
56 self.assertEqual((Stream.TEXT, 'Foo'), events[2][:2])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
57 self.assertEqual((Stream.END, 'b'), events[3][:2])
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
58 self.assertEqual((Stream.END, 'span'), events[4][:2])
379
d8c5045b547a The builder API now accepts streams as children of elements and fragments.
cmlenz
parents: 345
diff changeset
59
737
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
60 def test_markup_escape(self):
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
61 m = Markup('See %s') % tag.a('genshi',
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
62 href='http://genshi.edgwall.org')
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
63 self.assertEqual(m, Markup('See <a href="http://genshi.edgwall.org">'
686bbeecb9ac Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents: 730
diff changeset
64 'genshi</a>'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
66
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
68 suite = unittest.TestSuite()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
69 suite.addTest(doctest.DocTestSuite(Element.__module__))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
70 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test'))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
71 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
72
858
df860bdad9ca Yet more 2to3 diff size reduction.
cmlenz
parents: 737
diff changeset
73
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
74 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
75 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software