Author: bugman Date: Tue May 20 15:14:12 2008 New Revision: 6157 URL: http://svn.gna.org/viewcvs/relax?rev=6157&view=rev Log: Added 2 unit tests for the args of the structural API methods. Modified: 1.3/test_suite/unit_tests/_generic_fns/_structure/test_api_base.py Modified: 1.3/test_suite/unit_tests/_generic_fns/_structure/test_api_base.py URL: http://svn.gna.org/viewcvs/relax/1.3/test_suite/unit_tests/_generic_fns/_structure/test_api_base.py?rev=6157&r1=6156&r2=6157&view=diff ============================================================================== --- 1.3/test_suite/unit_tests/_generic_fns/_structure/test_api_base.py (original) +++ 1.3/test_suite/unit_tests/_generic_fns/_structure/test_api_base.py Tue May 20 15:14:12 2008 @@ -21,7 +21,9 @@ ############################################################################### # Python module imports. +from inspect import getargspec from re import search +import types from unittest import TestCase # relax module imports. @@ -32,6 +34,74 @@ class Test_api_base(TestCase): """Unit tests for the structural API base class.""" + + def format_method(self, name, args, varargs, varkw, defaults): + """Method for formatting the method.""" + + # Method start. + text = name + '(' + + # Args. + for i in xrange(len(args) - len(defaults)): + # Separator. + if i != 0: + text = text + ', ' + + # The arg. + text = text + args[i] + + # Shifted index. + index = i+1 + + # Keyword args. + for i in xrange(index, len(defaults)+1): + # Separator. + if i != 0: + text = text + ', ' + + # The keyword. + text = text + args[i] + '=' + `defaults[i-index]` + + # End. + text = text + ')' + return text + + + def test_Internal_method_args(self): + """The args of the public methods of the Internal structural object must be the same as the API base class.""" + + # The base and internal objects. + base = Base_struct_API() + intern = Internal() + + # Loop over the objects in the internal object. + for name in dir(intern): + # Skip anything starting with '_'. + if search('^_', name): + continue + + # Get the object in the two classes. + obj_base = getattr(base, name) + obj_intern = getattr(intern, name) + + # Skip non-method objects. + if type(obj_base) != types.MethodType: + continue + + # Get the args and their default values. + args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) + args_intern, varargs_intern, varkw_intern, defaults_intern = getargspec(obj_intern) + + # Check the args. + if args_base != args_intern or varargs_base != varargs_intern or varkw_base != varkw_intern or defaults_base != defaults_intern: + # Get string representations of the methods. + doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) + doc_intern = self.format_method(name, args_intern, varargs_intern, varkw_intern, defaults_intern) + print doc_base + + # Fail. + self.fail('The args of the method\n\t' + doc_intern + '\ndo not match those of the API method\n\t' + doc_base) + def test_Internal_objects(self): """Are the initial public objects of the Internal structural object all within the API base class?""" @@ -57,6 +127,42 @@ self.fail('The object ' + `name` + ' ' + `type(obj)` + ' cannot be found in the structural API base class.') + def test_Scientific_method_args(self): + """The args of the public methods of the Scientific structural object must be the same as the API base class.""" + + # The base and Scientific objects. + base = Base_struct_API() + sci = Scientific_data() + + # Loop over the objects in the Scientific object. + for name in dir(sci): + # Skip anything starting with '_'. + if search('^_', name): + continue + + # Get the object in the two classes. + obj_base = getattr(base, name) + obj_sci = getattr(sci, name) + + # Skip non-method objects. + if type(obj_base) != types.MethodType: + continue + + # Get the args and their default values. + args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) + args_sci, varargs_sci, varkw_sci, defaults_sci = getargspec(obj_sci) + + # Check the args. + if args_base != args_sci or varargs_base != varargs_sci or varkw_base != varkw_sci or defaults_base != defaults_sci: + # Get string representations of the methods. + doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) + doc_sci = self.format_method(name, args_sci, varargs_sci, varkw_sci, defaults_sci) + print doc_base + + # Fail. + self.fail('The args of the method\n\t' + doc_sci + '\ndo not match those of the API method\n\t' + doc_base) + + def test_Scientific_objects(self): """Are the initial public objects of the Scientific structural object all within the API base class?"""