Revision: af712866dc49
Author: Janne Härkönen <[email protected]>
Date: Sun Nov 27 23:53:58 2011
Log: XmlSource: tests + correct str() for files and StringIO
http://code.google.com/p/robotframework/source/detail?r=af712866dc49
Added:
/utest/utils/test_xmlsource.py
Modified:
/atest/robot/cli/rebot/invalid_usage.txt
/src/robot/utils/xmlsource.py
=======================================
--- /dev/null
+++ /utest/utils/test_xmlsource.py Sun Nov 27 23:53:58 2011
@@ -0,0 +1,37 @@
+import unittest
+
+from robot.utils.asserts import assert_equals, assert_raises, assert_true
+from robot.utils.xmlsource import XmlSource
+from robot.errors import DataError
+
+
+class TestXmlSource(unittest.TestCase):
+
+ def test_creating_from_filename(self):
+ source = XmlSource(__file__)
+ with source as src:
+ assert_equals(src, __file__)
+ self._verify_string_representation(source, __file__)
+
+ def test_xml_like_string_is_opened(self):
+ xml = '<tag>content</tag>'
+ source = XmlSource(xml)
+ with source as src:
+ assert_equals(src.read(), xml)
+ self._verify_string_representation(source, '<in-memory file>')
+
+ def test_opened_file_object_can_be_used(self):
+ source = XmlSource(open(__file__))
+ with source as src:
+ assert_true(src.read().startswith('import'))
+ assert_true(src.closed is False)
+ self._verify_string_representation(source, __file__)
+
+ def test_filename_is_validated(self):
+ def use(src):
+ with src as s:
+ pass
+ assert_raises(DataError, use, XmlSource('nonex.xml'))
+
+ def _verify_string_representation(self, source, expected):
+ assert_equals(str(source), expected)
=======================================
--- /atest/robot/cli/rebot/invalid_usage.txt Fri Nov 18 04:07:46 2011
+++ /atest/robot/cli/rebot/invalid_usage.txt Sun Nov 27 23:53:58 2011
@@ -13,7 +13,7 @@
Rebot should fail ${EMPTY} Expected at least 1 argument, got 0.
Non-Existing Input
- Rebot should fail nonex.xml Output file 'nonex\\.xml' does not exist.
+ Rebot should fail nonex.xml Reading XML source 'nonex\\.xml' failed:
Source file 'nonex\\.xml' does not exist.*
Non-XML Input
Create File ${MYOUTDIR}/invalid.txt Hello, world
=======================================
--- /src/robot/utils/xmlsource.py Fri Nov 18 04:07:46 2011
+++ /src/robot/utils/xmlsource.py Sun Nov 27 23:53:58 2011
@@ -22,20 +22,33 @@
def __init__(self, source):
self._source = source
- if isinstance(source, basestring) and not os.path.isfile(source):
- if source.startswith('<'):
- self._source = StringIO(source)
- else:
- raise DataError("Output file '%s' does not exist." %
source)
-
- def __enter__(self):
+
+ def _get_or_create(self):
+ if self._is_filename() and not os.path.isfile(self._source):
+ raise DataError("Source file '%s' does not exist."
+ % self._source)
+ self._open_if_necessary()
return self._source
+ def _is_filename(self):
+ return isinstance(self._source, basestring) \
+ and not self._source.startswith('<')
+
+ def _open_if_necessary(self):
+ if isinstance(self._source, basestring) and not
self._is_filename():
+ self._source = StringIO(self._source)
+
+ def __enter__(self):
+ return self._get_or_create()
+
def __exit__(self, exc_type, exc_value, exc_trace):
if exc_type is None or exc_type is DataError:
return False
raise DataError(exc_value)
def __str__(self):
- return str(self._source)
-
+ if hasattr(self._source, 'name'):
+ return self._source.name
+ if isinstance(self._source, basestring):
+ return str(self._source)
+ return '<in-memory file>'