Mercurial > genshi > genshi-test
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 | 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 from HTMLParser import HTMLParseError | |
16 import unittest | |
17 | |
230 | 18 from genshi.builder import Element, tag |
345 | 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 | 21 |
22 | |
23 class ElementFactoryTestCase(unittest.TestCase): | |
24 | |
25 def test_link(self): | |
20 | 26 link = tag.a(href='#', title='Foo', accesskey=None)('Bar') |
1 | 27 bits = iter(link.generate()) |
345 | 28 self.assertEqual((Stream.START, |
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 | 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 | 41 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])), |
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 | 57 self.assertEqual((Stream.START, ('span', ())), xml[0][:2]) |
58 self.assertEqual((Stream.START, ('b', ())), xml[1][:2]) | |
59 self.assertEqual((Stream.TEXT, 'Foo'), xml[2][:2]) | |
60 self.assertEqual((Stream.END, 'b'), xml[3][:2]) | |
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 | 63 |
64 def suite(): | |
65 suite = unittest.TestSuite() | |
66 suite.addTest(doctest.DocTestSuite(Element.__module__)) | |
67 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test')) | |
68 return suite | |
69 | |
70 if __name__ == '__main__': | |
71 unittest.main(defaultTest='suite') |