3 # Author: David Goodger
4 # Contact: goodger@users.sourceforge.net
7 # Copyright: This module has been placed in the public domain.
10 Test module for nodes.py.
14 from types
import ClassType
15 from DocutilsTestSupport
import nodes
, utils
20 class TextTests(unittest
.TestCase
):
23 self
.text
= nodes
.Text('Line 1.\nLine 2.')
26 self
.assertEquals(repr(self
.text
), r
"<#text: 'Line 1.\nLine 2.'>")
29 self
.assertEquals(str(self
.text
), 'Line 1.\nLine 2.')
31 def test_astext(self
):
32 self
.assertEquals(self
.text
.astext(), 'Line 1.\nLine 2.')
34 def test_pformat(self
):
35 self
.assertEquals(self
.text
.pformat(), 'Line 1.\nLine 2.\n')
38 class ElementTests(unittest
.TestCase
):
41 element
= nodes
.Element()
42 self
.assertEquals(repr(element
), '<Element: >')
43 self
.assertEquals(str(element
), '<Element/>')
45 self
.assertEquals(dom
.toxml(), '<Element/>')
48 self
.assertEquals(repr(element
), '<Element: >')
49 self
.assertEquals(str(element
), '<Element attr="1"/>')
51 self
.assertEquals(dom
.toxml(), '<Element attr="1"/>')
53 self
.assertEquals(element
.pformat(), '<Element attr="1">\n')
55 element
['mark'] = u
'\u2022'
56 self
.assertEquals(repr(element
), '<Element: >')
57 self
.assertEquals(str(element
), '<Element mark="\\u2022"/>')
59 self
.assertEquals(dom
.toxml(), u
'<Element mark="\u2022"/>')
62 def test_withtext(self
):
63 element
= nodes
.Element('text\nmore', nodes
.Text('text\nmore'))
64 self
.assertEquals(repr(element
), r
"<Element: <#text: 'text\nmore'>>")
65 self
.assertEquals(str(element
), '<Element>text\nmore</Element>')
67 self
.assertEquals(dom
.toxml(), '<Element>text\nmore</Element>')
70 self
.assertEquals(repr(element
), r
"<Element: <#text: 'text\nmore'>>")
71 self
.assertEquals(str(element
),
72 '<Element attr="1">text\nmore</Element>')
74 self
.assertEquals(dom
.toxml(),
75 '<Element attr="1">text\nmore</Element>')
77 self
.assertEquals(element
.pformat(),
85 class MiscTests(unittest
.TestCase
):
87 def test_node_class_names(self
):
91 if type(c
) is ClassType
and issubclass(c
, nodes
.Node
) \
92 and len(c
.__bases
__) > 1:
93 node_class_names
.append(x
)
94 node_class_names
.sort()
95 nodes
.node_class_names
.sort()
96 self
.assertEquals(node_class_names
, nodes
.node_class_names
)
98 ids
= [('a', 'a'), ('A', 'a'), ('', ''), ('a b \n c', 'a-b-c'),
99 ('a.b.c', 'a-b-c'), (' - a - b - c - ', 'a-b-c'), (' - ', ''),
100 (u
'\u2020\u2066', ''), (u
'a \xa7 b \u2020 c', 'a-b-c'),
101 ('1', ''), ('1abc', 'abc')]
103 def test_make_id(self
):
104 for input, output
in self
.ids
:
105 normed
= nodes
.make_id(input)
106 self
.assertEquals(normed
, output
)
109 class TreeCopyVisitorTests(unittest
.TestCase
):
112 document
= utils
.new_document('test data')
113 document
+= nodes
.paragraph('', 'Paragraph 1.')
114 blist
= nodes
.bullet_list()
115 for i
in range(1, 6):
116 item
= nodes
.list_item()
117 for j
in range(1, 4):
118 item
+= nodes
.paragraph('', 'Item %s, paragraph %s.' % (i
, j
))
121 self
.document
= document
123 def compare_trees(self
, one
, two
):
124 self
.assertEquals(one
.__class
__, two
.__class
__)
125 self
.assertNotEquals(id(one
), id(two
))
126 children1
= one
.get_children()
127 children2
= two
.get_children()
128 self
.assertEquals(len(children1
), len(children2
))
129 for i
in range(len(children1
)):
130 self
.compare_trees(children1
[i
], children2
[i
])
132 def test_copy_whole(self
):
133 visitor
= nodes
.TreeCopyVisitor(self
.document
)
134 self
.document
.walkabout(visitor
)
135 newtree
= visitor
.get_tree_copy()
136 self
.assertEquals(self
.document
.pformat(), newtree
.pformat())
137 self
.compare_trees(self
.document
, newtree
)
140 class MiscFunctionTests(unittest
.TestCase
):
142 names
= [('a', 'a'), ('A', 'a'), ('A a A', 'a a a'),
143 ('A a A a', 'a a a a'),
144 (' AaA\n\r\naAa\tAaA\t\t', 'aaa aaa aaa')]
146 def test_normalize_name(self
):
147 for input, output
in self
.names
:
148 normed
= nodes
.fully_normalize_name(input)
149 self
.assertEquals(normed
, output
)
152 if __name__
== '__main__':