OLD | NEW |
| (Empty) |
1 # -*- test-case-name: twisted.web.test.test_domhelpers -*- | |
2 # | |
3 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
4 # See LICENSE for details. | |
5 | |
6 # | |
7 | |
8 """Specific tests for (some of) the methods in t.web.domhelpers""" | |
9 | |
10 from twisted.trial.unittest import TestCase | |
11 | |
12 from twisted.web import microdom | |
13 | |
14 from twisted.web import domhelpers | |
15 | |
16 class DomHelpersTest(TestCase): | |
17 def test_getElementsByTagName(self): | |
18 doc1=microdom.parseString('<foo/>') | |
19 actual=domhelpers.getElementsByTagName(doc1, 'foo')[0].nodeName | |
20 expected='foo' | |
21 self.assertEquals(actual, expected) | |
22 el1=doc1.documentElement | |
23 actual=domhelpers.getElementsByTagName(el1, 'foo')[0].nodeName | |
24 self.assertEqual(actual, expected) | |
25 | |
26 doc2_xml='<a><foo in="a"/><b><foo in="b"/></b><c><foo in="c"/></c><foo i
n="d"/><foo in="ef"/><g><foo in="g"/><h><foo in="h"/></h></g></a>' | |
27 doc2=microdom.parseString(doc2_xml) | |
28 tag_list=domhelpers.getElementsByTagName(doc2, 'foo') | |
29 actual=''.join([node.getAttribute('in') for node in tag_list]) | |
30 expected='abcdefgh' | |
31 self.assertEquals(actual, expected) | |
32 el2=doc2.documentElement | |
33 tag_list=domhelpers.getElementsByTagName(el2, 'foo') | |
34 actual=''.join([node.getAttribute('in') for node in tag_list]) | |
35 self.assertEqual(actual, expected) | |
36 | |
37 doc3_xml=''' | |
38 <a><foo in="a"/> | |
39 <b><foo in="b"/> | |
40 <d><foo in="d"/> | |
41 <g><foo in="g"/></g> | |
42 <h><foo in="h"/></h> | |
43 </d> | |
44 <e><foo in="e"/> | |
45 <i><foo in="i"/></i> | |
46 </e> | |
47 </b> | |
48 <c><foo in="c"/> | |
49 <f><foo in="f"/> | |
50 <j><foo in="j"/></j> | |
51 </f> | |
52 </c> | |
53 </a>''' | |
54 doc3=microdom.parseString(doc3_xml) | |
55 tag_list=domhelpers.getElementsByTagName(doc3, 'foo') | |
56 actual=''.join([node.getAttribute('in') for node in tag_list]) | |
57 expected='abdgheicfj' | |
58 self.assertEquals(actual, expected) | |
59 el3=doc3.documentElement | |
60 tag_list=domhelpers.getElementsByTagName(el3, 'foo') | |
61 actual=''.join([node.getAttribute('in') for node in tag_list]) | |
62 self.assertEqual(actual, expected) | |
63 | |
64 doc4_xml='<foo><bar></bar><baz><foo/></baz></foo>' | |
65 doc4=microdom.parseString(doc4_xml) | |
66 actual=domhelpers.getElementsByTagName(doc4, 'foo') | |
67 root=doc4.documentElement | |
68 expected=[root, root.lastChild().firstChild()] | |
69 self.assertEquals(actual, expected) | |
70 actual=domhelpers.getElementsByTagName(root, 'foo') | |
71 self.assertEqual(actual, expected) | |
72 | |
73 | |
74 def test_gatherTextNodes(self): | |
75 doc1=microdom.parseString('<a>foo</a>') | |
76 actual=domhelpers.gatherTextNodes(doc1) | |
77 expected='foo' | |
78 self.assertEqual(actual, expected) | |
79 actual=domhelpers.gatherTextNodes(doc1.documentElement) | |
80 self.assertEqual(actual, expected) | |
81 | |
82 doc2_xml='<a>a<b>b</b><c>c</c>def<g>g<h>h</h></g></a>' | |
83 doc2=microdom.parseString(doc2_xml) | |
84 actual=domhelpers.gatherTextNodes(doc2) | |
85 expected='abcdefgh' | |
86 self.assertEqual(actual, expected) | |
87 actual=domhelpers.gatherTextNodes(doc2.documentElement) | |
88 self.assertEqual(actual, expected) | |
89 | |
90 doc3_xml=('<a>a<b>b<d>d<g>g</g><h>h</h></d><e>e<i>i</i></e></b>' + | |
91 '<c>c<f>f<j>j</j></f></c></a>') | |
92 doc3=microdom.parseString(doc3_xml) | |
93 actual=domhelpers.gatherTextNodes(doc3) | |
94 expected='abdgheicfj' | |
95 self.assertEqual(actual, expected) | |
96 actual=domhelpers.gatherTextNodes(doc3.documentElement) | |
97 self.assertEqual(actual, expected) | |
98 | |
99 doc4_xml='''<html> | |
100 <head> | |
101 </head> | |
102 <body> | |
103 stuff | |
104 </body> | |
105 </html> | |
106 ''' | |
107 doc4=microdom.parseString(doc4_xml) | |
108 actual=domhelpers.gatherTextNodes(doc4) | |
109 expected='\n stuff\n ' | |
110 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
111 actual=domhelpers.gatherTextNodes(doc4.documentElement) | |
112 self.assertEqual(actual, expected) | |
113 | |
114 doc5_xml='<x>Soufflé</x>' | |
115 doc5=microdom.parseString(doc5_xml) | |
116 actual=domhelpers.gatherTextNodes(doc5) | |
117 expected='Soufflé' | |
118 self.assertEqual(actual, expected) | |
119 actual=domhelpers.gatherTextNodes(doc5.documentElement) | |
120 self.assertEqual(actual, expected) | |
121 | |
122 def test_clearNode(self): | |
123 doc1=microdom.parseString('<a><b><c><d/></c></b></a>') | |
124 a_node=doc1.documentElement | |
125 domhelpers.clearNode(a_node) | |
126 actual=doc1.documentElement.toxml() | |
127 expected='<a></a>' | |
128 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
129 | |
130 doc2=microdom.parseString('<a><b><c><d/></c></b></a>') | |
131 b_node=doc2.documentElement.childNodes[0] | |
132 domhelpers.clearNode(b_node) | |
133 actual=doc2.documentElement.toxml() | |
134 expected='<a><b></b></a>' | |
135 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
136 | |
137 doc3=microdom.parseString('<a><b><c><d/></c></b></a>') | |
138 c_node=doc3.documentElement.childNodes[0].childNodes[0] | |
139 domhelpers.clearNode(c_node) | |
140 actual=doc3.documentElement.toxml() | |
141 expected='<a><b><c></c></b></a>' | |
142 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
143 | |
144 def test_get(self): | |
145 doc1=microdom.parseString('<a><b id="bar"/><c class="foo"/></a>') | |
146 node=domhelpers.get(doc1, "foo") | |
147 actual=node.toxml() | |
148 expected='<c class="foo"></c>' | |
149 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
150 | |
151 node=domhelpers.get(doc1, "bar") | |
152 actual=node.toxml() | |
153 expected='<b id="bar"></b>' | |
154 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
155 | |
156 self.assertRaises(domhelpers.NodeLookupError, | |
157 domhelpers.get, | |
158 doc1, | |
159 "pzork") | |
160 | |
161 def test_getIfExists(self): | |
162 doc1=microdom.parseString('<a><b id="bar"/><c class="foo"/></a>') | |
163 node=domhelpers.getIfExists(doc1, "foo") | |
164 actual=node.toxml() | |
165 expected='<c class="foo"></c>' | |
166 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
167 | |
168 node=domhelpers.getIfExists(doc1, "pzork") | |
169 assert node==None, 'expected None, didn\'t get None' | |
170 | |
171 def test_getAndClear(self): | |
172 doc1=microdom.parseString('<a><b id="foo"><c></c></b></a>') | |
173 node=domhelpers.getAndClear(doc1, "foo") | |
174 actual=node.toxml() | |
175 expected='<b id="foo"></b>' | |
176 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
177 | |
178 def test_locateNodes(self): | |
179 doc1=microdom.parseString('<a><b foo="olive"><c foo="olive"/></b><d foo=
"poopy"/></a>') | |
180 node_list=domhelpers.locateNodes(doc1.childNodes, 'foo', 'olive', | |
181 noNesting=1) | |
182 actual=''.join([node.toxml() for node in node_list]) | |
183 expected='<b foo="olive"><c foo="olive"></c></b>' | |
184 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
185 | |
186 node_list=domhelpers.locateNodes(doc1.childNodes, 'foo', 'olive', | |
187 noNesting=0) | |
188 actual=''.join([node.toxml() for node in node_list]) | |
189 expected='<b foo="olive"><c foo="olive"></c></b><c foo="olive"></c>' | |
190 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
191 | |
192 def test_getParents(self): | |
193 doc1=microdom.parseString('<a><b><c><d/></c><e/></b><f/></a>') | |
194 node_list=domhelpers.getParents(doc1.childNodes[0].childNodes[0].childNo
des[0]) | |
195 actual=''.join([node.tagName for node in node_list | |
196 if hasattr(node, 'tagName')]) | |
197 expected='cba' | |
198 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
199 | |
200 def test_findElementsWithAttribute(self): | |
201 doc1=microdom.parseString('<a foo="1"><b foo="2"/><c foo="1"/><d/></a>') | |
202 node_list=domhelpers.findElementsWithAttribute(doc1, 'foo') | |
203 actual=''.join([node.tagName for node in node_list]) | |
204 expected='abc' | |
205 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
206 | |
207 node_list=domhelpers.findElementsWithAttribute(doc1, 'foo', '1') | |
208 actual=''.join([node.tagName for node in node_list]) | |
209 expected='ac' | |
210 assert actual==expected, 'expected %s, got %s' % (expected, actual) | |
211 | |
212 def test_findNodesNamed(self): | |
213 doc1=microdom.parseString('<doc><foo/><bar/><foo>a</foo></doc>') | |
214 node_list=domhelpers.findNodesNamed(doc1, 'foo') | |
215 actual=len(node_list) | |
216 expected=2 | |
217 assert actual==expected, 'expected %d, got %d' % (expected, actual) | |
218 | |
219 # NOT SURE WHAT THESE ARE SUPPOSED TO DO.. | |
220 # def test_RawText FIXME | |
221 # def test_superSetAttribute FIXME | |
222 # def test_superPrependAttribute FIXME | |
223 # def test_superAppendAttribute FIXME | |
224 # def test_substitute FIXME | |
225 | |
226 def test_escape(self): | |
227 j='this string " contains many & characters> xml< won\'t like' | |
228 expected='this string " contains many & characters> xml<
won\'t like' | |
229 self.assertEqual(domhelpers.escape(j), expected) | |
230 | |
231 def test_unescape(self): | |
232 j='this string " has && entities > < and some characters
xml won\'t like<' | |
233 expected='this string " has && entities > < and some characters xml won\
't like<' | |
234 self.assertEqual(domhelpers.unescape(j), expected) | |
OLD | NEW |