Revision: 61d2ed335344
Author:   Pekka Klärck
Date:     Mon Jul  2 15:51:25 2012
Log: XML lib: Better error reporting in 'Element Should (Be Equal/Match)' when children are different
http://code.google.com/p/robotframework/source/detail?r=61d2ed335344

Modified:
 /atest/testdata/standard_libraries/xml/elements_should_be_equal.txt
 /atest/testdata/standard_libraries/xml/elements_should_match.txt
 /src/robot/libraries/XML.py

=======================================
--- /atest/testdata/standard_libraries/xml/elements_should_be_equal.txt Sat Jun 30 13:13:45 2012 +++ /atest/testdata/standard_libraries/xml/elements_should_be_equal.txt Mon Jul 2 15:51:25 2012
@@ -45,6 +45,18 @@
     <root><tag><c1/><c2/></tag></root>    <root><tag/></root>
     ...    Different number of child elements at 'root/tag': 2 != 0

+Differences in children with same name
+    <root><tag>x</tag><tag/></root>    <root><tag>y</tag><tag/></root>
+    ...    Different text at 'root/tag': x != y
+    <root><tag/><tag>x</tag></root>    <root><tag/><tag>y</tag></root>
+    ...    Different text at 'root/tag[2]': x != y
+ <a><b/><b><c/><c/><d/><c/></b></a> <a><b/><b><c/><c/><d/><c><e/></c></b></a>
+    ...    Different number of child elements at 'a/b[2]/c[3]': 0 != 1
+
+Differences in children with non-ASCII path
+    <å><ä><ö/><ö>oikea</ö></ä></å>    <å><ä><ö/><ö>väärä</ö></ä></å>
+    ...    Different text at 'å/ä/ö[2]': oikea != väärä
+
 Normalize whitespace
     [Template]    NONE
Elements should be equal <p>\n\tThis \ \ \ text\n<i>spaces \ has</i> also \ in\ttail!\n</p>
=======================================
--- /atest/testdata/standard_libraries/xml/elements_should_match.txt Sat Jun 30 13:13:45 2012 +++ /atest/testdata/standard_libraries/xml/elements_should_match.txt Mon Jul 2 15:51:25 2012
@@ -41,6 +41,17 @@
     <root><tag><c1/><c2/></tag></root>    <root><tag/></root>
     ...    Different number of child elements at 'root/tag': 2 != 0

+Differences in children with same name
+    <root><tag>x</tag><tag/></root>    <root><tag>??</tag><tag/></root>
+    ...    Different text at 'root/tag': 'x' does not match '??'
+    <root><tag/><tag>x</tag></root>    <root><tag/><tag>??</tag></root>
+    ...    Different text at 'root/tag[2]': 'x' does not match '??'
+ <a><b/><b><c/><c/><d/><c d='e'/></b></a> <a><b/><b><c/><c/><d/><c d='e?'/></b></a> + ... Different value for attribute 'd' at 'a/b[2]/c[3]': 'e' does not match 'e?'
+
+Differences in children with non-ASCII path
+    <å><ä><ö/><ö>oikea</ö></ä></å>    <å><ä><ö/><ö>*väärä*</ö></ä></å>
+    ...    Different text at 'å/ä/ö[2]': 'oikea' does not match '*väärä*'

 Normalize whitespace
     [Template]    NONE
=======================================
--- /src/robot/libraries/XML.py Mon Jul  2 08:45:27 2012
+++ /src/robot/libraries/XML.py Mon Jul  2 15:51:25 2012
@@ -195,6 +195,24 @@
self._compare(len(actual), len(expected), 'Different number of child elements',
                       location, should_be_equal)
         if not location:
-            location = actual.tag
+            location = Location(actual.tag)
         for act, exp in zip(actual, expected):
-            self.compare(act, exp, '%s/%s' % (location, act.tag))
+            self.compare(act, exp, location.child(act.tag))
+
+
+class Location(object):
+
+    def __init__(self, path):
+        self._path = path
+        self._children = {}
+
+    def child(self, tag):
+        if tag not in self._children:
+            self._children[tag] = 1
+        else:
+            self._children[tag] += 1
+            tag += '[%d]' % self._children[tag]
+        return Location('%s/%s' % (self._path, tag))
+
+    def __str__(self):
+        return self._path

Reply via email to