Revision: 7ad2e38bb934
Author: Robot Framework Developers <[email protected]>
Date: Thu Jun 28 04:35:54 2012
Log: XML library: Elements Should be Equal
http://code.google.com/p/robotframework/source/detail?r=7ad2e38bb934
Added:
/atest/testdata/standard_libraries/xml/element_comparison.txt
Modified:
/src/robot/libraries/XML.py
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/xml/element_comparison.txt Thu Jun
28 04:35:54 2012
@@ -0,0 +1,46 @@
+*** Settings ***
+Resource resource.txt
+Test Template Elements are different
+
+*** Test cases ***
+Elements should be equal
+ [Template] Element should be equal to itself
+ <tag/>
+ <root><tag/></root>
+ <tag a="1" b="2"/>
+ <root>\n<tag>some\ntext</tag>tail text</root>
+
+Tag names are different
+ <tag/> <täg/> Different tag name: tag != täg
+
+Different attribute values
+ <tag a="1"/> <tag a="2"/> Different attributes: {'a': '1'} !=
{'a': '2'}
+ <tag a="1" c="3"/> <tag b="1"/>
+ ... Different attributes: {'a': '1', 'c': '3'} != {'b': '1'}
+
+Texts are different
+ <tag>some text</tag> <tag>different</tag> Different text: some
text != different
+ <tag>some text</tag> <tag/> Different text: some text !=${SPACE}
+
+Different tails
+ <root><tag/>tail</root> <root><tag/>wrong</root> Different tail
text: tail != wrong
+ <root><tag/>tail</root> <root><tag/></root> Different tail text:
tail !=${SPACE}
+
+Different number of children
+ <root><tag/><tag/></root> <root><tag/></root> Different number of
child elements: 2 != 1
+
+Differences in child
+ <root><tag><child/></tag></root>
<root><different></different>tail</root>
+ ... Different tag name: tag != different
+
+*** Keywords ***
+Element should be equal to itself
+ [Arguments] ${source}
+ ${xml}= Parse XML ${source}
+ Elements Should Be Equal ${source} ${xml}
+ Elements Should Be Equal ${xml} ${source}
+
+Elements are different
+ [Arguments] ${source} ${expected} ${error}
+ Run Keyword and Expect Error ${error}
+ ... Elements Should Be Equal ${source} ${expected}
=======================================
--- /src/robot/libraries/XML.py Thu Jun 28 03:21:55 2012
+++ /src/robot/libraries/XML.py Thu Jun 28 04:35:54 2012
@@ -28,8 +28,8 @@
only in 1.3 i.e in Python 2.7!
"""
- _should_be_equal = partial(BuiltIn().should_be_equal, values=False)
- _should_match = partial(BuiltIn().should_match, values=False)
+ _should_be_equal = BuiltIn().should_be_equal
+ _should_match = BuiltIn().should_match
_normalize_whitespace = partial(re.compile('\s+').sub, ' ')
def parse_xml(self, source):
@@ -41,7 +41,7 @@
return self.parse_xml(source)
return source
- def get_element(self, source, xpath):
+ def get_element(self, source, xpath='.'):
if xpath == '.': # TODO: Is this good workaround for ET 1.2 not
supporting '.'?
return self._get_parent(source)
elements = self.get_elements(source, xpath)
@@ -78,12 +78,12 @@
def element_text_should_be(self, source, expected, xpath='.',
normalize_whitespace=False, message=None):
text = self.get_element_text(source, xpath, normalize_whitespace)
- self._should_be_equal(text, expected, message)
+ self._should_be_equal(text, expected, message, values=False)
def element_text_should_match(self, source, pattern, xpath='.',
normalize_whitespace=False,
message=None):
text = self.get_element_text(source, xpath, normalize_whitespace)
- self._should_match(text, pattern, message)
+ self._should_match(text, pattern, message, values=False)
def get_element_attribute(self, source, name, xpath='.', default=None):
return self.get_element(source, xpath).get(name, default)
@@ -94,11 +94,39 @@
def element_attribute_should_be(self, source, name, expected,
xpath='.',
message=None):
attr = self.get_element_attribute(source, name, xpath)
- self._should_be_equal(attr, expected, message)
+ self._should_be_equal(attr, expected, message, values=False)
def element_attribute_should_match(self, source, name, pattern,
xpath='.',
message=None):
attr = self.get_element_attribute(source, name, xpath)
if attr is None:
raise AssertionError("Attribute '%s' does not exist." % name)
- self._should_match(attr, pattern, message)
+ self._should_match(attr, pattern, message, values=False)
+
+ def elements_should_be_equal(self, source, expected,
+ normalize_whitespace=False):
+ self._compare(self.get_element(source), self.get_element(expected),
+ normalize_whitespace, self._should_be_equal)
+
+ def _compare(self, actual, expected, normalize_whitespace,
+ comparator):
+ self._should_be_equal(actual.tag, expected.tag,
+ 'Different tag name')
+ comparator(actual.text or '', expected.text or '', 'Different
text')
+ comparator(actual.attrib, expected.attrib, 'Different attributes')
+ comparator(actual.tail or '', expected.tail or '', 'Different tail
text')
+ self._should_be_equal(len(actual), len(expected),
+ 'Different number of child elements')
+ for a, e in zip(actual, expected):
+ self._compare(a, e, normalize_whitespace, comparator)
+
+ def elements_should_match(self, source, expected,
+ normalize_whitespace=False, message=None):
+ raise NotImplementedError
+
+ def log_element(self, source):
+ raise NotImplementedError
+
+ def element_to_string(self, source):
+ raise NotImplementedError
+