Mercurial > genshi > genshi-test
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 | 1 # -*- coding: utf-8 -*- |
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 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
230 | 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 | 20 |
21 | |
22 class ElementFactoryTestCase(unittest.TestCase): | |
23 | |
24 def test_link(self): | |
20 | 25 link = tag.a(href='#', title='Foo', accesskey=None)('Bar') |
858 | 26 events = list(link.generate()) |
345 | 27 self.assertEqual((Stream.START, |
28 ('a', Attrs([('href', "#"), ('title', "Foo")])), | |
858 | 29 (None, -1, -1)), events[0]) |
30 self.assertEqual((Stream.TEXT, 'Bar', (None, -1, -1)), events[1]) | |
31 self.assertEqual((Stream.END, 'a', (None, -1, -1)), events[2]) | |
1 | 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 | 39 events = list(tag.foo(id=3)) |
345 | 40 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])), |
858 | 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 | 45 events = list(link.generate()) |
46 self.assertEqual((Stream.START, ('a', Attrs([('href', "#1")])), | |
47 (None, -1, -1)), events[0]) | |
48 self.assertEqual((Stream.TEXT, 'Bar', (None, -1, -1)), events[1]) | |
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 | 52 events = list(tag.span(XML('<b>Foo</b>')).generate()) |
53 self.assertEqual(5, len(events)) | |
54 self.assertEqual((Stream.START, ('span', ())), events[0][:2]) | |
55 self.assertEqual((Stream.START, ('b', ())), events[1][:2]) | |
56 self.assertEqual((Stream.TEXT, 'Foo'), events[2][:2]) | |
57 self.assertEqual((Stream.END, 'b'), events[3][:2]) | |
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 | 65 |
858 | 66 |
1 | 67 def suite(): |
68 suite = unittest.TestSuite() | |
69 suite.addTest(doctest.DocTestSuite(Element.__module__)) | |
70 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test')) | |
71 return suite | |
72 | |
858 | 73 |
1 | 74 if __name__ == '__main__': |
75 unittest.main(defaultTest='suite') |