<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14018] CalDAVTester/trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14018">14018</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-09-29 08:51:07 -0700 (Mon, 29 Sep 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add more sophisticated xpath testing to allow tests for sibling elements that match specific values.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalDAVTestertrunkREADMEtxt">CalDAVTester/trunk/README.txt</a></li>
<li><a href="#CalDAVTestertrunkverifiersxmlElementMatchpy">CalDAVTester/trunk/verifiers/xmlElementMatch.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalDAVTestertrunkREADMEtxt"></a>
<div class="modfile"><h4>Modified: CalDAVTester/trunk/README.txt (14017 => 14018)</h4>
<pre class="diff"><span>
<span class="info">--- CalDAVTester/trunk/README.txt        2014-09-29 09:29:56 UTC (rev 14017)
+++ CalDAVTester/trunk/README.txt        2014-09-29 15:51:07 UTC (rev 14018)
</span><span class="lines">@@ -830,6 +830,12 @@
</span><span class="cx">         [json] - node contains valid JSON data.
</span><span class="cx">         [icalendar] - node contains valid iCalendare data.
</span><span class="cx">         
</span><ins>+        Each path segment can now have its own test and &quot;../&quot; can be used to move up to
+        the parent. This allows testing for an element matching specific content plus
+        its sibling matching other specific content. e.g., &quot;/{D}A/{D}B[=b]/../{D}C[=c]
+        which checks for an element {D}A with two child elements {D}B and {D}C each
+        with a specific value.
+        
</ins><span class="cx">         Argument: 'parent'
</span><span class="cx">                 ElementTree style path for an XML element to use as the root for any
</span><span class="cx">                 subsequent &quot;exists&quot; or &quot;notexists&quot; tests. This is useful for targeting
</span></span></pre></div>
<a id="CalDAVTestertrunkverifiersxmlElementMatchpy"></a>
<div class="modfile"><h4>Modified: CalDAVTester/trunk/verifiers/xmlElementMatch.py (14017 => 14018)</h4>
<pre class="diff"><span>
<span class="info">--- CalDAVTester/trunk/verifiers/xmlElementMatch.py        2014-09-29 09:29:56 UTC (rev 14017)
+++ CalDAVTester/trunk/verifiers/xmlElementMatch.py        2014-09-29 15:51:07 UTC (rev 14018)
</span><span class="lines">@@ -67,12 +67,12 @@
</span><span class="cx">         resulttxt = &quot;&quot;
</span><span class="cx">         for path in exists:
</span><span class="cx"> 
</span><del>-            matched, txt = self.matchPath(root, path)
</del><ins>+            matched, txt = self.matchNode(root, path)
</ins><span class="cx">             result &amp;= matched
</span><span class="cx">             resulttxt += txt
</span><span class="cx"> 
</span><span class="cx">         for path in notexists:
</span><del>-            matched, _ignore_txt = self.matchPath(root, path)
</del><ins>+            matched, _ignore_txt = self.matchNode(root, path)
</ins><span class="cx">             if matched:
</span><span class="cx">                 resulttxt += &quot;        Items returned in XML for %s\n&quot; % (path,)
</span><span class="cx">                 result = False
</span><span class="lines">@@ -153,26 +153,93 @@
</span><span class="cx">         return results
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def matchPath(self, root, path):
</del><ins>+    @classmethod
+    def testNode(cls, node, node_path, test):
+        result = None
+        if test[0] == '@':
+            if '=' in test:
+                attr, value = test[1:].split('=')
+                value = value[1:-1]
+            else:
+                attr = test[1:]
+                value = None
+            if attr not in node.keys():
+                result = &quot;        Missing attribute returned in XML for %s\n&quot; % (node_path,)
+            if value is not None and node.get(attr) != value:
+                result = &quot;        Incorrect attribute value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '=':
+            if node.text != test[1:]:
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '!':
+            if node.text == test[1:]:
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '*':
+            if node.text is None or node.text.find(test[1:]) == -1:
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '$':
+            if node.text is None or node.text.find(test[1:]) != -1:
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '+':
+            if node.text is None or not node.text.startswith(test[1:]):
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        elif test[0] == '^':
+            if &quot;=&quot; in test:
+                element, value = test[1:].split(&quot;=&quot;, 1)
+            else:
+                element = test[1:]
+                value = None
+            for child in node.getchildren():
+                if child.tag == element and (value is None or child.text == value):
+                    break
+            else:
+                result = &quot;        Missing child returned in XML for %s\n&quot; % (node_path,)
</ins><span class="cx"> 
</span><ins>+        # Try to parse as iCalendar
+        elif test == 'icalendar':
+            try:
+                Calendar.parseText(node.text)
+            except:
+                result = &quot;        Incorrect value returned in iCalendar for %s\n&quot; % (node_path,)
+
+        # Try to parse as JSON
+        elif test == 'json':
+            try:
+                json.loads(node.text)
+            except:
+                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (node_path,)
+        return result
+
+
+    @classmethod
+    def matchNode(cls, root, xpath, parent_map=None, title=None):
+
+        if title is None:
+            title = xpath
</ins><span class="cx">         result = True
</span><span class="cx">         resulttxt = &quot;&quot;
</span><span class="cx"> 
</span><del>-        if '[' in path:
-            actual_path, tests = path.split('[', 1)
</del><ins>+        # Find the first test in the xpath
+        if '[' in xpath:
+            actual_xpath, tests = xpath.split('[', 1)
</ins><span class="cx">         else:
</span><del>-            actual_path = path
</del><ins>+            actual_xpath = xpath
</ins><span class="cx">             tests = None
</span><span class="cx"> 
</span><del>-        # Handle absolute root element
-        if actual_path[0] == '/':
-            actual_path = actual_path[1:]
-        r = re.search(&quot;(\{[^\}]+\}[^/]+)(.*)&quot;, actual_path)
</del><ins>+        if parent_map is None:
+            parent_map = dict((c, p) for p in root.getiterator() for c in p)
+
+        # Handle parents
+        if actual_xpath.startswith(&quot;../&quot;):
+            root = parent_map[root]
+            actual_xpath = &quot;./&quot; + actual_xpath[3:]
+
+        # Handle absolute root element and find all matching nodes
+        r = re.search(&quot;(/\{[^\}]+\}[^/]+|\.)(.*)&quot;, actual_xpath)
</ins><span class="cx">         if r.group(2):
</span><span class="cx">             root_path = r.group(1)
</span><span class="cx">             child_path = r.group(2)[1:]
</span><del>-            if root.tag != root_path:
-                resulttxt += &quot;        Items not returned in XML for %s\n&quot; % (path,)
</del><ins>+            if root_path != &quot;.&quot; and root.tag != root_path[1:]:
+                resulttxt += &quot;        Items not returned in XML for %s\n&quot; % (title,)
</ins><span class="cx">                 result = False
</span><span class="cx">                 return result, resulttxt
</span><span class="cx">             nodes = root.findall(child_path)
</span><span class="lines">@@ -180,76 +247,58 @@
</span><span class="cx">             nodes = (root,)
</span><span class="cx"> 
</span><span class="cx">         if len(nodes) == 0:
</span><del>-            resulttxt += &quot;        Items not returned in XML for %s\n&quot; % (path,)
</del><ins>+            resulttxt += &quot;        Items not returned in XML for %s\n&quot; % (title,)
</ins><span class="cx">             result = False
</span><span class="cx">             return result, resulttxt
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx">         if tests:
</span><del>-            tests = [item[:-1] for item in tests.split('[')]
-            for test in tests:
</del><ins>+            # Split the tests into tests plus additional path
+            pos = tests.find(&quot;]/&quot;)
+            if pos != -1:
+                node_tests = tests[:pos + 1]
+                next_path = tests[pos + 1:]
+            else:
+                node_tests = tests
+                next_path = None
+
+            node_tests = [item[:-1] for item in node_tests.split('[')]
+            for test in node_tests:
</ins><span class="cx">                 for node in nodes:
</span><ins>+                    testresult = cls.testNode(node, title, test)
+                    if testresult is None:
+                        if next_path:
+                            next_result, testresult = cls.matchNode(node, next_path[1:], parent_map, title)
+                            if next_result:
+                                break
+                        else:
+                            break
</ins><span class="cx"> 
</span><del>-                    def _doTest():
-                        result = None
-                        if test[0] == '@':
-                            if '=' in test:
-                                attr, value = test[1:].split('=')
-                                value = value[1:-1]
-                            else:
-                                attr = test[1:]
-                                value = None
-                            if attr not in node.keys():
-                                result = &quot;        Missing attribute returned in XML for %s\n&quot; % (path,)
-                            if value is not None and node.get(attr) != value:
-                                result = &quot;        Incorrect attribute value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '=':
-                            if node.text != test[1:]:
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '!':
-                            if node.text == test[1:]:
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '*':
-                            if node.text is None or node.text.find(test[1:]) == -1:
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '$':
-                            if node.text is None or node.text.find(test[1:]) != -1:
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '+':
-                            if node.text is None or not node.text.startswith(test[1:]):
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        elif test[0] == '^':
-                            if &quot;=&quot; in test:
-                                element, value = test[1:].split(&quot;=&quot;, 1)
-                            else:
-                                element = test[1:]
-                                value = None
-                            for child in node.getchildren():
-                                if child.tag == element and (value is None or child.text == value):
-                                    break
-                            else:
-                                result = &quot;        Missing child returned in XML for %s\n&quot; % (path,)
-
-                        # Try to parse as iCalendar
-                        elif test == 'icalendar':
-                            try:
-                                Calendar.parseText(node.text)
-                            except:
-                                result = &quot;        Incorrect value returned in iCalendar for %s\n&quot; % (path,)
-
-                        # Try to parse as JSON
-                        elif test == 'json':
-                            try:
-                                json.loads(node.text)
-                            except:
-                                result = &quot;        Incorrect value returned in XML for %s\n&quot; % (path,)
-                        return result
-
-                    testresult = _doTest()
-                    if testresult is None:
-                        break
-                if testresult is not None:
</del><ins>+                if testresult:
</ins><span class="cx">                     resulttxt += testresult
</span><span class="cx">                     result = False
</span><span class="cx">                     break
</span><span class="cx"> 
</span><span class="cx">         return result, resulttxt
</span><ins>+
+
+# Tests
+if __name__ == '__main__':
+    xmldata = &quot;&quot;&quot;
+&lt;D:test xmlns:D=&quot;DAV:&quot;&gt;
+    &lt;D:a&gt;A&lt;/D:a&gt;
+    &lt;D:b&gt;
+        &lt;D:c&gt;C&lt;/D:c&gt;
+        &lt;D:d&gt;D&lt;/D:d&gt;
+    &lt;/D:b&gt;
+    &lt;D:b&gt;
+        &lt;D:c&gt;C&lt;/D:c&gt;
+        &lt;D:d&gt;F&lt;/D:d&gt;
+    &lt;/D:b&gt;
+&lt;/D:test&gt;
+&quot;&quot;&quot;
+
+    node = ElementTree(file=StringIO.StringIO(xmldata)).getroot()
+
+    assert Verifier.matchNode(node, &quot;/{DAV:}test/{DAV:}b/{DAV:}c[=C]/../{DAV:}d[=D]&quot;)[0]
+    assert not Verifier.matchNode(node, &quot;/{DAV:}test/{DAV:}b/{DAV:}c[=C]/../{DAV:}d[=E]&quot;)[0]
</ins></span></pre>
</div>
</div>

</body>
</html>