| [2] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| [71] | 3 | # Copyright (C) 2006 Edgewall Software |
|---|
| [2] | 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 |
|---|
| [287] | 8 | # are also available at http://genshi.edgewall.org/wiki/License. |
|---|
| [2] | 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| [287] | 12 | # history and logs, available at http://genshi.edgewall.org/log/. |
|---|
| [2] | 13 | |
|---|
| 14 | import doctest |
|---|
| 15 | import unittest |
|---|
| 16 | |
|---|
| [287] | 17 | from genshi.builder import Element, tag |
|---|
| [861] | 18 | from genshi.core import Attrs, Markup, Stream |
|---|
| [461] | 19 | from genshi.input import XML |
|---|
| [2] | 20 | |
|---|
| 21 | |
|---|
| 22 | class ElementFactoryTestCase(unittest.TestCase): |
|---|
| 23 | |
|---|
| 24 | def test_link(self): |
|---|
| [1191] | 25 | link = tag.a(href='#', accesskey=None)('Bar') |
|---|
| [1081] | 26 | events = list(link.generate()) |
|---|
| [424] | 27 | self.assertEqual((Stream.START, |
|---|
| [1191] | 28 | ('a', Attrs([('href', "#")])), |
|---|
| [1081] | 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]) |
|---|
| [2] | 32 | |
|---|
| [103] | 33 | def test_nonstring_attributes(self): |
|---|
| 34 | """ |
|---|
| 35 | Verify that if an attribute value is given as an int (or some other |
|---|
| 36 | non-string type), it is coverted to a string when the stream is |
|---|
| 37 | generated. |
|---|
| 38 | """ |
|---|
| [1081] | 39 | events = list(tag.foo(id=3)) |
|---|
| [424] | 40 | self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])), |
|---|
| [1081] | 41 | (None, -1, -1)), events[0]) |
|---|
| [2] | 42 | |
|---|
| [854] | 43 | def test_duplicate_attributes(self): |
|---|
| [1195] | 44 | link = tag.a(href='#1', href_='#1')('Bar') |
|---|
| [1081] | 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]) |
|---|
| [854] | 50 | |
|---|
| [461] | 51 | def test_stream_as_child(self): |
|---|
| [1081] | 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]) |
|---|
| [103] | 59 | |
|---|
| [861] | 60 | def test_markup_escape(self): |
|---|
| 61 | m = Markup('See %s') % tag.a('genshi', |
|---|
| 62 | href='http://genshi.edgwall.org') |
|---|
| 63 | self.assertEqual(m, Markup('See <a href="http://genshi.edgwall.org">' |
|---|
| 64 | 'genshi</a>')) |
|---|
| [461] | 65 | |
|---|
| [1081] | 66 | |
|---|
| [2] | 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 | |
|---|
| [1081] | 73 | |
|---|
| [2] | 74 | if __name__ == '__main__': |
|---|
| 75 | unittest.main(defaultTest='suite') |
|---|