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-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 23  from inspect import getargspec 
 24  from re import search 
 25  import types 
 26  from unittest import TestCase 
 27   
 28  # relax module imports. 
 29  import dep_check 
 30  from generic_fns.structure.api_base import Base_struct_API 
 31  from generic_fns.structure.internal import Internal 
 32  from generic_fns.structure.scientific import Scientific_data 
 33  from status import Status; status = Status() 
 34   
 35   
36 -class Test_api_base(TestCase):
37 """Unit tests for the structural API base class.""" 38
39 - def __init__(self, methodName='runTest'):
40 """Skip scientific Python tests if not installed. 41 42 @keyword methodName: The name of the test. 43 @type methodName: str 44 """ 45 46 # Execute the base class method. 47 super(Test_api_base, self).__init__(methodName)
48 49
50 - def format_method(self, name, args, varargs, varkw, defaults):
51 """Method for formatting the method.""" 52 53 # Method start. 54 text = name + '(' 55 56 # No keywords. 57 if defaults == None: 58 defaults = () 59 60 # Args. 61 for i in range(len(args) - len(defaults)): 62 # Separator. 63 if i != 0: 64 text = text + ', ' 65 66 # The arg. 67 text = text + args[i] 68 69 # Shifted index. 70 index = i+1 71 72 # Keyword args. 73 for i in range(index, len(defaults)+1): 74 # Separator. 75 if i != 0: 76 text = text + ', ' 77 78 # The keyword. 79 text = text + args[i] + '=' + repr(defaults[i-index]) 80 81 # End. 82 text = text + ')' 83 return text
84 85
87 """The args of the public methods of the Internal structural object must be the same as the API base class.""" 88 89 # The base and internal objects. 90 base = Base_struct_API() 91 intern = Internal() 92 93 # Loop over the objects in the internal object. 94 for name in dir(intern): 95 # Skip anything starting with '_'. 96 if search('^_', name): 97 continue 98 99 # Get the object in the two classes. 100 obj_base = getattr(base, name) 101 obj_intern = getattr(intern, name) 102 103 # Skip non-method objects. 104 if not isinstance(obj_base, types.MethodType): 105 continue 106 107 # Get the args and their default values. 108 args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) 109 args_intern, varargs_intern, varkw_intern, defaults_intern = getargspec(obj_intern) 110 111 # Check the args. 112 if args_base != args_intern or varargs_base != varargs_intern or varkw_base != varkw_intern or defaults_base != defaults_intern: 113 # Get string representations of the methods. 114 doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) 115 doc_intern = self.format_method(name, args_intern, varargs_intern, varkw_intern, defaults_intern) 116 print(doc_base) 117 118 # Fail. 119 self.fail('The args of the method\n\t' + doc_intern + '\ndo not match those of the API method\n\t' + doc_base)
120 121
122 - def test_Internal_objects(self):
123 """Are the initial public objects of the Internal structural object all within the API base class?""" 124 125 # The base and internal objects. 126 base = Base_struct_API() 127 internal = Internal() 128 129 # The objects in the base class. 130 base_names = dir(base) 131 132 # Loop over the objects in the internal object. 133 for name in dir(internal): 134 # Skip anything starting with '_'. 135 if search('^_', name): 136 continue 137 138 # Get the object in the derived class. 139 obj = getattr(internal, name) 140 141 # Not present. 142 if name not in base_names: 143 self.fail('The object ' + repr(name) + ' ' + repr(type(obj)) + ' cannot be found in the structural API base class.')
144 145
147 """The args of the public methods of the Scientific structural object must be the same as the API base class.""" 148 149 # The base and Scientific objects. 150 base = Base_struct_API() 151 sci = Scientific_data() 152 153 # Loop over the objects in the Scientific object. 154 for name in dir(sci): 155 # Skip anything starting with '_'. 156 if search('^_', name): 157 continue 158 159 # Get the object in the two classes. 160 obj_base = getattr(base, name) 161 obj_sci = getattr(sci, name) 162 163 # Skip non-method objects. 164 if not isinstance(obj_base, types.MethodType): 165 continue 166 167 # Get the args and their default values. 168 args_base, varargs_base, varkw_base, defaults_base = getargspec(obj_base) 169 args_sci, varargs_sci, varkw_sci, defaults_sci = getargspec(obj_sci) 170 171 # Check the args. 172 if args_base != args_sci or varargs_base != varargs_sci or varkw_base != varkw_sci or defaults_base != defaults_sci: 173 # Get string representations of the methods. 174 doc_base = self.format_method(name, args_base, varargs_base, varkw_base, defaults_base) 175 doc_sci = self.format_method(name, args_sci, varargs_sci, varkw_sci, defaults_sci) 176 177 # Fail. 178 self.fail('The args of the method\n\t' + doc_sci + '\ndo not match those of the API method\n\t' + doc_base)
179 180
181 - def test_Scientific_objects(self):
182 """Are the initial public objects of the Scientific structural object all within the API base class?""" 183 184 # The base and Scientific objects. 185 base = Base_struct_API() 186 sci = Scientific_data() 187 188 # The objects in the base class. 189 base_names = dir(base) 190 191 # Loop over the objects in the Scientific object. 192 for name in dir(sci): 193 # Skip anything starting with '_'. 194 if search('^_', name): 195 continue 196 197 # Get the object in the derived class. 198 obj = getattr(sci, name) 199 200 # Not present. 201 if name not in base_names: 202 self.fail('The object ' + repr(name) + ' ' + repr(type(obj)) + ' cannot be found in the structural API base class.')
203