3
# Twisted, the Framework of Your Internet
4
# Copyright (C) 2001 Matthew W. Lefkowitz
6
# This library is free software; you can redistribute it and/or
7
# modify it under the terms of version 2.1 of the GNU Lesser General Public
8
# License as published by the Free Software Foundation.
10
# This library is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# Lesser General Public License for more details.
15
# You should have received a copy of the GNU Lesser General Public
16
# License along with this library; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
from twisted.trial import unittest
22
from twisted.xish.domish import Element
23
from twisted.xish.xpath import XPathQuery
24
from twisted.xish import xpath
26
class XPathTest(unittest.TestCase):
29
# <foo xmlns='testns' attrib1='value1' attrib3="user@host/resource">
37
# <bar attrib2="value2">
44
self.e = Element(("testns", "foo"))
45
self.e["attrib1"] = "value1"
46
self.e["attrib3"] = "user@host/resource"
47
self.e.addContent("somecontent")
48
self.bar1 = self.e.addElement("bar")
49
self.subfoo = self.bar1.addElement("foo")
50
self.gar1 = self.subfoo.addElement("gar")
51
self.gar1.addContent("DEF")
52
self.e.addContent("somemorecontent")
53
self.bar2 = self.e.addElement("bar")
54
self.bar2["attrib2"] = "value2"
55
self.bar3 = self.bar2.addElement("bar")
56
self.subfoo2 = self.bar3.addElement("foo")
57
self.gar2 = self.bar3.addElement("gar")
58
self.gar2.addContent("ABC")
59
self.bar4 = self.e.addElement("bar")
62
def testStaticMethods(self):
63
self.assertEquals(xpath.matches("/foo/bar", self.e),
65
self.assertEquals(xpath.queryForNodes("/foo/bar", self.e),
66
[self.bar1, self.bar2, self.bar4])
67
self.assertEquals(xpath.queryForString("/foo", self.e),
69
self.assertEquals(xpath.queryForStringList("/foo", self.e),
70
["somecontent", "somemorecontent"])
72
def testFunctionality(self):
73
xp = XPathQuery("/foo/bar")
74
self.assertEquals(xp.matches(self.e), 1)
76
xp = XPathQuery("/foo/bar/foo")
77
self.assertEquals(xp.matches(self.e), 1)
78
self.assertEquals(xp.queryForNodes(self.e), [self.subfoo])
80
xp = XPathQuery("/foo/bar3")
81
self.assertEquals(xp.matches(self.e), 0)
83
xp = XPathQuery("/foo/*")
84
self.assertEquals(xp.matches(self.e), True)
85
self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, self.bar4])
87
xp = XPathQuery("/foo[@attrib1]")
88
self.assertEquals(xp.matches(self.e), True)
90
xp = XPathQuery("/foo/*[@attrib2='value2']")
91
self.assertEquals(xp.matches(self.e), True)
92
self.assertEquals(xp.queryForNodes(self.e), [self.bar2])
94
# XXX: Revist this, given new grammar
95
# xp = XPathQuery("/foo/bar[2]")
96
# self.assertEquals(xp.matches(self.e), 1)
97
# self.assertEquals(xp.queryForNodes(self.e), [self.bar1])
99
xp = XPathQuery("/foo[@xmlns='testns']/bar")
100
self.assertEquals(xp.matches(self.e), 1)
102
xp = XPathQuery("/foo[@xmlns='badns']/bar2")
103
self.assertEquals(xp.matches(self.e), 0)
105
xp = XPathQuery("/foo[@attrib1='value1']")
106
self.assertEquals(xp.matches(self.e), 1)
108
xp = XPathQuery("/foo")
109
self.assertEquals(xp.queryForString(self.e), "somecontent")
110
self.assertEquals(xp.queryForStringList(self.e), ["somecontent", "somemorecontent"])
112
xp = XPathQuery("/foo/bar")
113
self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, self.bar4])
115
xp = XPathQuery("/foo[text() = 'somecontent']")
116
self.assertEquals(xp.matches(self.e), True)
118
xp = XPathQuery("/foo[not(@nosuchattrib)]")
119
self.assertEquals(xp.matches(self.e), True)
121
xp = XPathQuery("/foo[juserhost(@attrib3) = 'user@host']")
122
self.assertEquals(xp.matches(self.e), True)
124
xp = XPathQuery("//gar")
125
self.assertEquals(xp.matches(self.e), True)
126
self.assertEquals(xp.queryForNodes(self.e), [self.gar1, self.gar2])
127
self.assertEquals(xp.queryForStringList(self.e), ["DEF", "ABC"])
129
xp = XPathQuery("//bar")
130
self.assertEquals(xp.matches(self.e), True)
131
self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, self.bar3, self.bar4])