Revision: 28d8342cd98b
Author: Janne Härkönen <[email protected]>
Date: Fri Jun 29 02:29:41 2012
Log: XML library: Elements Should Match
http://code.google.com/p/robotframework/source/detail?r=28d8342cd98b
Added:
/atest/testdata/standard_libraries/xml/elements_should_match.txt
Modified:
/atest/testdata/standard_libraries/xml/elements_should_be_equal.txt
/src/robot/libraries/XML.py
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/xml/elements_should_match.txt Fri
Jun 29 02:29:41 2012
@@ -0,0 +1,42 @@
+*** Settings ***
+Resource resource.txt
+Test Template Elements do not match
+
+*** Test cases ***
+Elements should match
+ [Template] Match Elements
+ <tag>content</tag> <tag>c*</tag>
+ <tag a="1" b="421"/> <tag a="?" b="4*"/>
+ <root>\n<tag>some\ntext</tag>tail text</root>
<root>\n<tag>some*</tag>tail??ex?</root>
+
+Tag names are different
+ <tag/> <täg/> Different tag name: tag != täg
+
+Different attribute values
+ <tag a="12"/> <tag a="?"/> Different value for attribute a: '12'
does not match '?'
+ <tag a="1" c="3"/> <tag a="?" b="?"/>
+ ... Different attribute names: ['a', 'c'] != ['a', 'b']
+
+Texts are different
+ <tag>some text</tag> <tag>no match*</tag> Different text: 'some
text' does not match 'no match*'
+ <tag></tag> <tag>?*</tag> Different text: '' does not match '?*'
+
+Different tails
+ <root><tag/>tail</root> <root><tag/>wrong*</root> Different tail
text: 'tail' does not match 'wrong*'
+ <root><tag/></root> <root><tag/>?</root> Different tail text: ''
does not match '?'
+
+Differences in child
+ <root><tag>content</tag></root> <root><tag>invalid*</tag></root>
+ ... Different text: 'content' does not match 'invalid*'
+
+*** Keywords ***
+Match Elements
+ [Arguments] ${source} ${match}
+ Elements Should Match ${source} ${match}
+ ${source}= Parse XML ${source}
+ Elements Should Match ${source} ${match}
+
+Elements do not match
+ [Arguments] ${source} ${expected} ${error}
+ Run Keyword and Expect Error ${error}
+ ... Elements Should Match ${source} ${expected}
=======================================
--- /atest/testdata/standard_libraries/xml/elements_should_be_equal.txt Fri
Jun 29 01:29:50 2012
+++ /atest/testdata/standard_libraries/xml/elements_should_be_equal.txt Fri
Jun 29 02:29:41 2012
@@ -14,9 +14,9 @@
<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"/> <tag a="2"/> Different value for attribute a: 1 != 2
<tag a="1" c="3"/> <tag b="1"/>
- ... Different attributes: {'a': '1', 'c': '3'} != {'b': '1'}
+ ... Different attribute names: ['a', 'c'] != ['b']
Texts are different
<tag>some text</tag> <tag>different</tag> Different text: some
text != different
=======================================
--- /src/robot/libraries/XML.py Fri Jun 29 02:01:34 2012
+++ /src/robot/libraries/XML.py Fri Jun 29 02:29:41 2012
@@ -110,21 +110,24 @@
self._compare(self.get_element(source), self.get_element(expected),
normalize_whitespace, self._should_be_equal)
- def _compare(self, actual, expected, normalize_whitespace,
- comparator):
+ 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')
+ self._should_be_equal(sorted(actual.attrib.keys()),
+ sorted(expected.attrib.keys()), 'Different attribute
names')
+ for key in actual.attrib:
+ act, exp = actual.attrib[key], expected.attrib[key]
+ comparator(act, exp, 'Different value for attribute %s' % key)
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 elements_should_match(self, source, expected,
normalize_whitespace=False):
+ self._compare(self.get_element(source), self.get_element(expected),
+ normalize_whitespace, self._should_match)
def log_element(self, source, level='INFO'):
logger.write(self.element_to_string(source), level)