Edgewall Software

source: trunk/genshi/tests/builder.py

Last change on this file was 1195, checked in by hodgestar, 11 years ago

Fix another test that fails with the new randomized hashes.

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006 Edgewall Software
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
8# are also available at http://genshi.edgewall.org/wiki/License.
9#
10# This software consists of voluntary contributions made by many
11# individuals. For the exact contribution history, see the revision
12# history and logs, available at http://genshi.edgewall.org/log/.
13
14import doctest
15import unittest
16
17from genshi.builder import Element, tag
18from genshi.core import Attrs, Markup, Stream
19from genshi.input import XML
20
21
22class ElementFactoryTestCase(unittest.TestCase):
23
24    def test_link(self):
25        link = tag.a(href='#', accesskey=None)('Bar')
26        events = list(link.generate())
27        self.assertEqual((Stream.START,
28                          ('a', Attrs([('href', "#")])),
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])
32
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        """
39        events = list(tag.foo(id=3))
40        self.assertEqual((Stream.START, ('foo', Attrs([('id', '3')])),
41                          (None, -1, -1)), events[0])
42
43    def test_duplicate_attributes(self):
44        link = tag.a(href='#1', href_='#1')('Bar')
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])
50
51    def test_stream_as_child(self):
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])
59
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>'))
65
66
67def suite():
68    suite = unittest.TestSuite()
69    suite.addTest(doctest.DocTestSuite(Element.__module__))
70    suite.addTest(unittest.makeSuite(ElementFactoryTestCase, 'test'))
71    return suite
72
73
74if __name__ == '__main__':
75    unittest.main(defaultTest='suite')
Note: See TracBrowser for help on using the repository browser.