Mercurial > genshi > mirror
annotate genshi/tests/builder.py @ 379:e1d659c87ddf trunk
The builder API now accepts streams as children of elements and fragments.
author | cmlenz |
---|---|
date | Thu, 23 Nov 2006 17:48:17 +0000 |
parents | 2aa7ca37ae6a |
children | c66370dfc41b |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
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 | 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
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 | 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
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 | 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 | 41 self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])), |
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 |
379
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
45 def test_stream_as_child(self): |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
46 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
|
47 self.assertEqual(5, len(xml)) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
48 self.assertEqual((Stream.START, ('span', ()), (None, -1, -1)), xml[0]) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
49 self.assertEqual((Stream.START, ('b', ()), (None, 1, 0)), xml[1]) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
50 self.assertEqual((Stream.TEXT, 'Foo', (None, 1, 3)), xml[2]) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
51 self.assertEqual((Stream.END, 'b', (None, 1, 6)), xml[3]) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
52 self.assertEqual((Stream.END, 'span', (None, -1, -1)), xml[4]) |
e1d659c87ddf
The builder API now accepts streams as children of elements and fragments.
cmlenz
parents:
345
diff
changeset
|
53 |
1 | 54 |
55 def suite(): | |
56 suite = unittest.TestSuite() | |
57 suite.addTest(doctest.DocTestSuite(Element.__module__)) | |
58 suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test')) | |
59 return suite | |
60 | |
61 if __name__ == '__main__': | |
62 unittest.main(defaultTest='suite') |