Added:
trunk/atest/testresources/testlibs/extendingjava.py
Removed:
trunk/atest/testresources/testlibs/ExtendJavaLib.py
Modified:
trunk/atest/testresources/testlibs/newstyleclasses.py
trunk/src/robot/running/testlibraries.py
trunk/utest/running/test_testlibrary.py
Log:
Added some unit tests for library init.
Also refactored the code a bit.
Added: trunk/atest/testresources/testlibs/extendingjava.py
==============================================================================
--- (empty file)
+++ trunk/atest/testresources/testlibs/extendingjava.py Mon Jan 12 12:52:29
2009
@@ -0,0 +1,39 @@
+import ExampleJavaLibrary
+import JavaVarArgsConstructor
+
+
+class ExtendJavaLib(ExampleJavaLibrary):
+
+ def kw_in_java_extender(self, arg):
+ return arg*2
+
+ def javaSleep(self, secs):
+ raise Exception('Overridden kw executed!')
+
+ def using_method_from_java_parent(self):
+ self.divByZero()
+
+
+class ExtendJavaLibWithConstructor(JavaVarArgsConstructor):
+
+ def keyword(self):
+ return None
+
+
+class ExtendJavaLibWithInit(ExampleJavaLibrary):
+
+ def __init__(self, *args):
+ self.args = args
+
+ def get_args(self):
+ return self.args
+
+
+class ExtendJavaLibWithInitAndConstructor(JavaVarArgsConstructor):
+
+ def __init__(self, *args):
+ if len(args) == 1:
+ JavaVarArgsConstructor.__init__(self, args[0])
+ self.kw = lambda self: "Hello, world!"
+
+
\ No newline at end of file
Modified: trunk/atest/testresources/testlibs/newstyleclasses.py
==============================================================================
--- trunk/atest/testresources/testlibs/newstyleclasses.py (original)
+++ trunk/atest/testresources/testlibs/newstyleclasses.py Mon Jan 12
12:52:29 2009
@@ -11,6 +11,12 @@
prop = property(_property_getter)
+class NewStyleClassArgsLibrary(object):
+
+ def __init__(self, param):
+ self.get_param = lambda self: param
+
+
class _MyMetaClass(type):
def __new__(cls, name, bases, ns):
Modified: trunk/src/robot/running/testlibraries.py
==============================================================================
--- trunk/src/robot/running/testlibraries.py (original)
+++ trunk/src/robot/running/testlibraries.py Mon Jan 12 12:52:29 2009
@@ -26,6 +26,7 @@
if utils.is_jython:
from org.python.core import PyReflectedFunction, PyReflectedConstructor
+ from java.lang import Object
def TestLibrary(name, args=None, syslog=None):
@@ -115,11 +116,17 @@
def _create_init_handler(self, libcode):
init_method = getattr(libcode, '__init__', None)
- if not ((utils.is_jython and isinstance(init_method,
PyReflectedConstructor))\
- or isinstance(init_method, (types.MethodType,
types.FunctionType))):
+ if not self._valid_init(init_method):
init_method = None
return InitHandler(self, init_method)
-
+
+ def _valid_init(self, init_method):
+ if utils.is_jython and isinstance(init_method,
PyReflectedConstructor):
+ return True
+ if isinstance(init_method, (types.MethodType, types.FunctionType)):
+ return True
+ return False
+
def _init_scope_handling(self):
if self.scope == 'GLOBAL':
return
@@ -221,9 +228,9 @@
# importantly bean properties generated by Jython (issue 188).
for item in (libcode,) + inspect.getmro(libcode.__class__):
try:
- if self._isroutine(item.__dict__[name]) and \
- (item is libcode or \
- self._is_valid_handler(item, getattr(libcode, name))):
+ attr = item.__dict__[name]
+ if self._isroutine(attr) and \
+ (item is libcode or self._is_valid_handler(item,
attr)):
return getattr(libcode, name)
except (KeyError, AttributeError):
# Java classes don't always have __dict__
@@ -235,14 +242,13 @@
or (utils.is_jython and isinstance(item,
PyReflectedFunction))
def _is_valid_handler(self, handler_class, handler):
- co = handler.im_func
- if not utils.is_jython or not isinstance(co, PyReflectedFunction):
+ "Filters methods defined in java.lang.Object or generated by
Jython."
+ if not utils.is_jython or not isinstance(handler,
PyReflectedFunction):
return True
- from java.lang import Object
if handler_class is Object:
return False
# signature is an instance of org.python.ReflectedArgs
- for signature in co.argslist[:co.nargs]:
+ for signature in handler.argslist[:handler.nargs]:
# 'getName' may raise an exception -- not sure why but that
happens
# at least with handlers declared in class extending JUnit
TestCase.
# Would be better to investigate but just not excluding these
Modified: trunk/utest/running/test_testlibrary.py
==============================================================================
--- trunk/utest/running/test_testlibrary.py (original)
+++ trunk/utest/running/test_testlibrary.py Mon Jan 12 12:52:29 2009
@@ -3,6 +3,7 @@
from robot.running.testlibraries import TestLibrary, _ClassLibrary, \
_ModuleLibrary, _DynamicLibrary
+from robot.running.handlers import _NoInitHandler
from robot.utils.asserts import *
from robot import utils
from robot.errors import DataError
@@ -86,7 +87,7 @@
TestLibrary(name)
except DataError, err:
module = name.split('.')[0]
- assert_true(str(err).startswith(exp % (name, module)))
+ assert_true(str(err).startswith(exp % (name, module)), err)
else:
raise AssertionError("DataError not raised")
@@ -164,7 +165,55 @@
exp = "%s.%s" % (libname, name)
assert_equals(utils.normalize(handler.longname),
utils.normalize(exp))
+
+
+class TestLibraryInit(unittest.TestCase):
+
+ def test_python_library_without_init(self):
+ self._test_init_handler('ExampleLibrary')
+
+ def test_python_library_with_init(self):
+ self._test_init_handler('ParameterLibrary', ['foo'], 0, 2)
+
+ def test_new_style_class_without_init(self):
+ self._test_init_handler('newstyleclasses.NewStyleClassLibrary')
+
+ def test_new_style_class_with_init(self):
+ lib =
self._test_init_handler('newstyleclasses.NewStyleClassArgsLibrary',
['value'], 1, 1)
+ assert_equals(len(lib.handlers), 1)
+
+ def test_library_with_metaclass(self):
+ self._test_init_handler('newstyleclasses.MetaClassLibrary')
+
+ def _test_init_handler(self, libname, args=None, min=None, max=None):
+ lib = TestLibrary(libname, args)
+ if min is None and max is None:
+ assert_equals(lib.init.__class__, _NoInitHandler)
+ else:
+ assert_equals(lib.init.minargs, min)
+ assert_equals(lib.init.maxargs, max)
+ return lib
+
+ if utils.is_jython:
+
+ def test_java_library_without_constructor(self):
+ self._test_init_handler('ExampleJavaLibrary', None, 0, 0)
+
+ def test_java_library_with_constructor(self):
+ self._test_init_handler('JavaVarArgsConstructor',
['arg1', 'arg2'], 1, 3)
+
+ def test_extended_java_lib_with_no_init_and_no_constructor(self):
+ self._test_init_handler('extendingjava.ExtendJavaLib', None,
0, 0)
+
+ def test_extended_java_lib_with_no_init_and_contructor(self):
+
self._test_init_handler('extendingjava.ExtendJavaLibWithConstructor',
['arg'], 1, 3)
+
+ def test_extended_java_lib_with_init_and_no_constructor(self):
+ self._test_init_handler('extendingjava.ExtendJavaLibWithInit',
[1,2,3], 0, sys.maxint)
+ def test_extended_java_lib_with_init_and_constructor(self):
+
self._test_init_handler('extendingjava.ExtendJavaLibWithInitAndConstructor',
['arg'], 0, sys.maxint)
+
class TestVersion(unittest.TestCase):