Package test_suite :: Package unit_tests :: Package _generic_fns :: Package _structure :: Module test_api_base
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests._generic_fns._structure.test_api_base

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  from inspect import getargspec 
 25  from re import search 
 26  import types 
 27  from unittest import TestCase 
 28   
 29  # relax module imports. 
 30  import dep_check 
 31  from generic_fns.structure.api_base import Base_struct_API 
 32  from generic_fns.structure.internal import Internal 
 33  from generic_fns.structure.scientific import Scientific_data 
 34  from status import Status; status = Status() 
 35   
 36   
37 -class Test_api_base(TestCase):
38 """Unit tests for the structural API base class.""" 39
40 - def __init__(self, methodName='runTest'):
41 """Skip scientific Python tests if not installed. 42 43 @keyword methodName: The name of the test. 44 @type methodName: str 45 """ 46 47 # Execute the base class method. 48 super(Test_api_base, self).__init__(methodName)
49 50
51 - def format_method(self, name, args, varargs, varkw, defaults):
52 """Method for formatting the method.""" 53 54 # Method start. 55 text = name + '(' 56 57 # No keywords. 58 if defaults == None: 59 defaults = () 60 61 # Args. 62 for i in xrange(len(args) - len(defaults)): 63 # Separator. 64 if i != 0: 65 text = text + ', ' 66 67 # The arg. 68 text = text + args[i] 69 70 # Shifted index. 71 index = i+1 72 73 # Keyword args. 74 for i in xrange(index, len(defaults)+1): 75 # Separator. 76 if i != 0: 77 text = text + ', ' 78 79 # The keyword. 80 text = text + args[i] + '=' + repr(defaults[i-index]) 81 82 # End. 83 text = text + ')' 84 return text
85 86
88 """The args of the public methods of the Internal structural object must be the same as the API base class.""" 89 90 # The base and internal objects. 91 base = Base_struct_API() 92 intern = Internal() 93 94 # Loop over the objects in the internal object. 95 for name in dir(intern): 96 # Skip anything starting with '_'. 97 if search('^_', name): 98 continue 99 100 # Get the object in the two classes. 101 obj_base = getattr(base, name) 102 obj_intern = getattr(intern, name) 103 104 # Skip non-method objects. 105 if not isinstance(obj_base, types.MethodType): 106 continue 107 108 # Get the args and their default values. 109 args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) 110 args_intern, varargs_intern, varkw_intern, defaults_intern = getargspec(obj_intern) 111 112 # Check the args. 113 if args_base != args_intern or varargs_base != varargs_intern or varkw_base != varkw_intern or defaults_base != defaults_intern: 114 # Get string representations of the methods. 115 doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) 116 doc_intern = self.format_method(name, args_intern, varargs_intern, varkw_intern, defaults_intern) 117 print(doc_base) 118 119 # Fail. 120 self.fail('The args of the method\n\t' + doc_intern + '\ndo not match those of the API method\n\t' + doc_base)
121 122
123 - def test_Internal_objects(self):
124 """Are the initial public objects of the Internal structural object all within the API base class?""" 125 126 # The base and internal objects. 127 base = Base_struct_API() 128 internal = Internal() 129 130 # The objects in the base class. 131 base_names = dir(base) 132 133 # Loop over the objects in the internal object. 134 for name in dir(internal): 135 # Skip anything starting with '_'. 136 if search('^_', name): 137 continue 138 139 # Get the object in the derived class. 140 obj = getattr(internal, name) 141 142 # Not present. 143 if name not in base_names: 144 self.fail('The object ' + repr(name) + ' ' + repr(type(obj)) + ' cannot be found in the structural API base class.')
145 146
148 """The args of the public methods of the Scientific structural object must be the same as the API base class.""" 149 150 # The base and Scientific objects. 151 base = Base_struct_API() 152 sci = Scientific_data() 153 154 # Loop over the objects in the Scientific object. 155 for name in dir(sci): 156 # Skip anything starting with '_'. 157 if search('^_', name): 158 continue 159 160 # Get the object in the two classes. 161 obj_base = getattr(base, name) 162 obj_sci = getattr(sci, name) 163 164 # Skip non-method objects. 165 if not isinstance(obj_base, types.MethodType): 166 continue 167 168 # Get the args and their default values. 169 args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) 170 args_sci, varargs_sci, varkw_sci, defaults_sci = getargspec(obj_sci) 171 172 # Check the args. 173 if args_base != args_sci or varargs_base != varargs_sci or varkw_base != varkw_sci or defaults_base != defaults_sci: 174 # Get string representations of the methods. 175 doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) 176 doc_sci = self.format_method(name, args_sci, varargs_sci, varkw_sci, defaults_sci) 177 178 # Fail. 179 self.fail('The args of the method\n\t' + doc_sci + '\ndo not match those of the API method\n\t' + doc_base)
180 181
182 - def test_Scientific_objects(self):
183 """Are the initial public objects of the Scientific structural object all within the API base class?""" 184 185 # The base and Scientific objects. 186 base = Base_struct_API() 187 sci = Scientific_data() 188 189 # The objects in the base class. 190 base_names = dir(base) 191 192 # Loop over the objects in the Scientific object. 193 for name in dir(sci): 194 # Skip anything starting with '_'. 195 if search('^_', name): 196 continue 197 198 # Get the object in the derived class. 199 obj = getattr(sci, name) 200 201 # Not present. 202 if name not in base_names: 203 self.fail('The object ' + repr(name) + ' ' + repr(type(obj)) + ' cannot be found in the structural API base class.')
204