~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/test/test_xpath.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2004-06-21 22:01:11 UTC
  • mto: (2.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040621220111-vkf909euqnyrp3nr
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
# Twisted, the Framework of Your Internet
 
4
# Copyright (C) 2001 Matthew W. Lefkowitz
 
5
 
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.
 
9
 
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.
 
14
 
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
 
18
 
 
19
from twisted.trial import unittest
 
20
import sys, os
 
21
 
 
22
from twisted.xish.domish import Element
 
23
from twisted.xish.xpath import XPathQuery
 
24
from twisted.xish import xpath
 
25
 
 
26
class XPathTest(unittest.TestCase):
 
27
    def setUp(self):
 
28
        # Build element:
 
29
        # <foo xmlns='testns' attrib1='value1' attrib3="user@host/resource">
 
30
        #     somecontent
 
31
        #     <bar>
 
32
        #        <foo>
 
33
        #         <gar>DEF</gar>
 
34
        #        </foo>
 
35
        #     </bar>
 
36
        #     somemorecontent
 
37
        #     <bar attrib2="value2">
 
38
        #        <bar>
 
39
        #          <foo/>
 
40
        #          <gar>ABC</gar>
 
41
        #        </bar>
 
42
        #     <bar/>
 
43
        # </foo>
 
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")
 
60
 
 
61
    
 
62
    def testStaticMethods(self):
 
63
        self.assertEquals(xpath.matches("/foo/bar", self.e),
 
64
                          True)
 
65
        self.assertEquals(xpath.queryForNodes("/foo/bar", self.e),
 
66
                          [self.bar1, self.bar2, self.bar4])
 
67
        self.assertEquals(xpath.queryForString("/foo", self.e),
 
68
                          "somecontent")
 
69
        self.assertEquals(xpath.queryForStringList("/foo", self.e),
 
70
                          ["somecontent", "somemorecontent"])
 
71
        
 
72
    def testFunctionality(self):
 
73
        xp = XPathQuery("/foo/bar")
 
74
        self.assertEquals(xp.matches(self.e), 1)
 
75
 
 
76
        xp = XPathQuery("/foo/bar/foo")
 
77
        self.assertEquals(xp.matches(self.e), 1)
 
78
        self.assertEquals(xp.queryForNodes(self.e), [self.subfoo])
 
79
        
 
80
        xp = XPathQuery("/foo/bar3")
 
81
        self.assertEquals(xp.matches(self.e), 0)
 
82
 
 
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])
 
86
 
 
87
        xp = XPathQuery("/foo[@attrib1]")
 
88
        self.assertEquals(xp.matches(self.e), True)
 
89
 
 
90
        xp = XPathQuery("/foo/*[@attrib2='value2']")
 
91
        self.assertEquals(xp.matches(self.e), True)
 
92
        self.assertEquals(xp.queryForNodes(self.e), [self.bar2])
 
93
 
 
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])
 
98
 
 
99
        xp = XPathQuery("/foo[@xmlns='testns']/bar")
 
100
        self.assertEquals(xp.matches(self.e), 1)
 
101
 
 
102
        xp = XPathQuery("/foo[@xmlns='badns']/bar2")
 
103
        self.assertEquals(xp.matches(self.e), 0)
 
104
 
 
105
        xp = XPathQuery("/foo[@attrib1='value1']")
 
106
        self.assertEquals(xp.matches(self.e), 1)
 
107
 
 
108
        xp = XPathQuery("/foo")
 
109
        self.assertEquals(xp.queryForString(self.e), "somecontent")
 
110
        self.assertEquals(xp.queryForStringList(self.e), ["somecontent", "somemorecontent"])
 
111
 
 
112
        xp = XPathQuery("/foo/bar")
 
113
        self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, self.bar4])
 
114
 
 
115
        xp = XPathQuery("/foo[text() = 'somecontent']")
 
116
        self.assertEquals(xp.matches(self.e), True)
 
117
 
 
118
        xp = XPathQuery("/foo[not(@nosuchattrib)]")
 
119
        self.assertEquals(xp.matches(self.e), True)
 
120
 
 
121
        xp = XPathQuery("/foo[juserhost(@attrib3) = 'user@host']")
 
122
        self.assertEquals(xp.matches(self.e), True)
 
123
 
 
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"])
 
128
 
 
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])
 
132
 
 
133
 
 
134