/usr/lib/python2.7/dist-packages/twisted/words/test/test_xpath.py is in python-twisted-words 13.2.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | # Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from twisted.trial import unittest
import sys, os
from twisted.words.xish.domish import Element
from twisted.words.xish.xpath import XPathQuery
from twisted.words.xish import xpath
class XPathTest(unittest.TestCase):
def setUp(self):
# Build element:
# <foo xmlns='testns' attrib1='value1' attrib3="user@host/resource">
# somecontent
# <bar>
# <foo>
# <gar>DEF</gar>
# </foo>
# </bar>
# somemorecontent
# <bar attrib2="value2">
# <bar>
# <foo/>
# <gar>ABC</gar>
# </bar>
# <bar/>
# <bar attrib4='value4' attrib5='value5'>
# <foo/>
# <gar>JKL</gar>
# </bar>
# <bar attrib4='value4' attrib5='value4'>
# <foo/>
# <gar>MNO</gar>
# </bar>
# <bar attrib4='value4' attrib5='value6'/>
# </foo>
self.e = Element(("testns", "foo"))
self.e["attrib1"] = "value1"
self.e["attrib3"] = "user@host/resource"
self.e.addContent("somecontent")
self.bar1 = self.e.addElement("bar")
self.subfoo = self.bar1.addElement("foo")
self.gar1 = self.subfoo.addElement("gar")
self.gar1.addContent("DEF")
self.e.addContent("somemorecontent")
self.bar2 = self.e.addElement("bar")
self.bar2["attrib2"] = "value2"
self.bar3 = self.bar2.addElement("bar")
self.subfoo2 = self.bar3.addElement("foo")
self.gar2 = self.bar3.addElement("gar")
self.gar2.addContent("ABC")
self.bar4 = self.e.addElement("bar")
self.bar5 = self.e.addElement("bar")
self.bar5["attrib4"] = "value4"
self.bar5["attrib5"] = "value5"
self.subfoo3 = self.bar5.addElement("foo")
self.gar3 = self.bar5.addElement("gar")
self.gar3.addContent("JKL")
self.bar6 = self.e.addElement("bar")
self.bar6["attrib4"] = "value4"
self.bar6["attrib5"] = "value4"
self.subfoo4 = self.bar6.addElement("foo")
self.gar4 = self.bar6.addElement("gar")
self.gar4.addContent("MNO")
self.bar7 = self.e.addElement("bar")
self.bar7["attrib4"] = "value4"
self.bar7["attrib5"] = "value6"
def test_staticMethods(self):
"""
Test basic operation of the static methods.
"""
self.assertEqual(xpath.matches("/foo/bar", self.e),
True)
self.assertEqual(xpath.queryForNodes("/foo/bar", self.e),
[self.bar1, self.bar2, self.bar4,
self.bar5, self.bar6, self.bar7])
self.assertEqual(xpath.queryForString("/foo", self.e),
"somecontent")
self.assertEqual(xpath.queryForStringList("/foo", self.e),
["somecontent", "somemorecontent"])
def test_locationFooBar(self):
"""
Test matching foo with child bar.
"""
xp = XPathQuery("/foo/bar")
self.assertEqual(xp.matches(self.e), 1)
def test_locationFooBarFoo(self):
"""
Test finding foos at the second level.
"""
xp = XPathQuery("/foo/bar/foo")
self.assertEqual(xp.matches(self.e), 1)
self.assertEqual(xp.queryForNodes(self.e), [self.subfoo,
self.subfoo3,
self.subfoo4])
def test_locationNoBar3(self):
"""
Test not finding bar3.
"""
xp = XPathQuery("/foo/bar3")
self.assertEqual(xp.matches(self.e), 0)
def test_locationAllChilds(self):
"""
Test finding childs of foo.
"""
xp = XPathQuery("/foo/*")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar1, self.bar2,
self.bar4, self.bar5,
self.bar6, self.bar7])
def test_attribute(self):
"""
Test matching foo with attribute.
"""
xp = XPathQuery("/foo[@attrib1]")
self.assertEqual(xp.matches(self.e), True)
def test_attributeWithValueAny(self):
"""
Test find nodes with attribute having value.
"""
xp = XPathQuery("/foo/*[@attrib2='value2']")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar2])
def test_position(self):
"""
Test finding element at position.
"""
xp = XPathQuery("/foo/bar[2]")
self.assertEqual(xp.matches(self.e), 1)
self.assertEqual(xp.queryForNodes(self.e), [self.bar1])
test_position.todo = "XPath queries with position are not working."
def test_namespaceFound(self):
"""
Test matching node with namespace.
"""
xp = XPathQuery("/foo[@xmlns='testns']/bar")
self.assertEqual(xp.matches(self.e), 1)
def test_namespaceNotFound(self):
"""
Test not matching node with wrong namespace.
"""
xp = XPathQuery("/foo[@xmlns='badns']/bar2")
self.assertEqual(xp.matches(self.e), 0)
def test_attributeWithValue(self):
"""
Test matching node with attribute having value.
"""
xp = XPathQuery("/foo[@attrib1='value1']")
self.assertEqual(xp.matches(self.e), 1)
def test_queryForString(self):
"""
Test for queryForString and queryForStringList.
"""
xp = XPathQuery("/foo")
self.assertEqual(xp.queryForString(self.e), "somecontent")
self.assertEqual(xp.queryForStringList(self.e),
["somecontent", "somemorecontent"])
def test_queryForNodes(self):
"""
Test finding nodes.
"""
xp = XPathQuery("/foo/bar")
self.assertEqual(xp.queryForNodes(self.e), [self.bar1, self.bar2,
self.bar4, self.bar5,
self.bar6, self.bar7])
def test_textCondition(self):
"""
Test matching a node with given text.
"""
xp = XPathQuery("/foo[text() = 'somecontent']")
self.assertEqual(xp.matches(self.e), True)
def test_textNotOperator(self):
"""
Test for not operator.
"""
xp = XPathQuery("/foo[not(@nosuchattrib)]")
self.assertEqual(xp.matches(self.e), True)
def test_anyLocationAndText(self):
"""
Test finding any nodes named gar and getting their text contents.
"""
xp = XPathQuery("//gar")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.gar1, self.gar2,
self.gar3, self.gar4])
self.assertEqual(xp.queryForStringList(self.e), ["DEF", "ABC",
"JKL", "MNO"])
def test_anyLocation(self):
"""
Test finding any nodes named bar.
"""
xp = XPathQuery("//bar")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar1, self.bar2,
self.bar3, self.bar4,
self.bar5, self.bar6,
self.bar7])
def test_anyLocationQueryForString(self):
"""
L{XPathQuery.queryForString} should raise a L{NotImplementedError}
for any location.
"""
xp = XPathQuery("//bar")
self.assertRaises(NotImplementedError, xp.queryForString, None)
def test_andOperator(self):
"""
Test boolean and operator in condition.
"""
xp = XPathQuery("//bar[@attrib4='value4' and @attrib5='value5']")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar5])
def test_orOperator(self):
"""
Test boolean or operator in condition.
"""
xp = XPathQuery("//bar[@attrib5='value4' or @attrib5='value5']")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar5, self.bar6])
def test_booleanOperatorsParens(self):
"""
Test multiple boolean operators in condition with parens.
"""
xp = XPathQuery("""//bar[@attrib4='value4' and
(@attrib5='value4' or @attrib5='value6')]""")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar6, self.bar7])
def test_booleanOperatorsNoParens(self):
"""
Test multiple boolean operators in condition without parens.
"""
xp = XPathQuery("""//bar[@attrib5='value4' or
@attrib5='value5' or
@attrib5='value6']""")
self.assertEqual(xp.matches(self.e), True)
self.assertEqual(xp.queryForNodes(self.e), [self.bar5, self.bar6, self.bar7])
|