Back to index

plone3  3.1.7
testInterface.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 ## GroupUserFolder
00003 ## Copyright (C)2006 Ingeniweb
00004 
00005 ## This program is free software; you can redistribute it and/or modify
00006 ## it under the terms of the GNU General Public License as published by
00007 ## the Free Software Foundation; either version 2 of the License, or
00008 ## (at your option) any later version.
00009 
00010 ## This program is distributed in the hope that it will be useful,
00011 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ## GNU General Public License for more details.
00014 
00015 ## You should have received a copy of the GNU General Public License
00016 ## along with this program; see the file COPYING. If not, write to the
00017 ## Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 """
00019 
00020 """
00021 __version__ = "$Revision:  $"
00022 # $Source:  $
00023 # $Id: testInterface.py 30098 2006-09-08 12:35:01Z encolpe $
00024 __docformat__ = 'restructuredtext'
00025 
00026 
00027 import os, sys
00028 if __name__ == '__main__':
00029     execfile(os.path.join(sys.path[0], 'framework.py'))
00030 
00031 
00032 from Log import *
00033 
00034 # Load fixture
00035 from Testing import ZopeTestCase
00036 from Interface import Verify
00037 import string
00038 
00039 
00040 def flattenList(lst):
00041     """
00042     flattenList => transform a (deep) sequence into a simple sequence
00043     """
00044     ret = []
00045 
00046     if type(lst) not in (type(()), type([])):
00047         return (lst, )
00048 
00049     for item in lst:
00050         if type(item) in (type(()), type([]), ):
00051             ret.extend(flattenList(item))
00052         else:
00053             ret.append(item)
00054     return ret
00055 
00056 def flattenInterfaces(lst):
00057     """
00058     flattenInterfaces => fetch interfaces and inherited ones
00059     """
00060     ret = []
00061     lst = flattenList(lst)
00062     for intf in lst:
00063         bases = intf.getBases()
00064         ret.extend(flattenInterfaces(bases))
00065         if not intf in ret:
00066             ret.append(intf)
00067     return ret
00068 
00069 
00070 # Products and global vars
00071 from Products.GroupUserFolder import GroupUserFolder, GRUFUser
00072 ##klasses = (        # tell which classes to check
00073 ##    GroupUserFolder.GroupUserFolder,
00074 ##    GRUFUser.GRUFUser,
00075 ##    GRUFUser.GRUFGroup,
00076 ##    )
00077 
00078 
00079 class TestInterface(ZopeTestCase.ZopeTestCase):
00080 
00081     def test01Interfaces(self,):
00082         """
00083         Test that interfaces are okay
00084         """
00085         # Check interface for each and every class
00086         ignore = getattr(self, "ignore_interfaces", [])
00087         for klass in self.klasses:
00088             intfs = getattr(klass, "__implements__", None)
00089             self.failUnless(intfs, "'%s' class doesn't implement an interface!" % (klass.__name__, ))
00090 
00091             # Flatten interfaces
00092             intfs = flattenList(intfs)
00093 
00094             # Check each and everyone
00095             for intf in intfs:
00096                 if intf in ignore:
00097                     continue
00098                 self.failUnless(
00099                     Verify.verifyClass(
00100                     intf,
00101                     klass,
00102                     ),
00103                     "'%s' class doesn't implement '%s' interface correctly." % (klass.__name__, intf.__name__, ),
00104                     )
00105 
00106 
00107     def test02TestCaseCompletude(self,):
00108         """
00109         Check that the test case is complete : each interface entry xxx must be associated
00110         to a test_xxx method in the test class.
00111         """
00112         not_defined = []
00113         tests = dir(self)
00114         count = 0
00115         
00116         # Check interface for each and every class
00117         ignore = getattr(self, "ignore_interfaces", [])
00118         for klass in self.klasses:
00119             intfs = getattr(klass, "__implements__", None)
00120             self.failUnless(intfs, "'%s' class doesn't implement an interface!" % (klass.__name__, ))
00121 
00122             # Flatten interfaces
00123             intfs = flattenInterfaces(intfs)
00124 
00125             # Check each and every interface
00126             for intf in intfs:
00127                 if intf in ignore:
00128                     continue
00129                 for name in intf.names():
00130                     count += 1
00131                     if not "test_%s" % (name,) in tests:
00132                         not_defined.append("%s.%s" % (klass.__name__, name))
00133 
00134 
00135         # Raise in case some tests are missing
00136         if not_defined:
00137             raise RuntimeError, "%d (over %d) MISSING TESTS:\n%s do not have a test associated." % (
00138                 len(not_defined),
00139                 count,
00140                 string.join(not_defined, ", "),
00141                 )
00142         
00143         
00144     def test03ClassSecurityInfo(self):
00145         """
00146         This method tests that each and every method has a ClassSecurityInfo() declaration
00147         XXX This doesn't walk through inheritance :(
00148         """
00149         not_defined = []
00150         count = 0
00151         
00152         # Check interface for each and every class
00153         ignore = getattr(self, "ignore_interfaces", [])
00154         for klass in self.klasses:
00155             dict = dir(klass)
00156             intfs = getattr(klass, "__implements__", None)
00157             self.failUnless(intfs, "'%s' class doesn't implement an interface!" % (klass.__name__, ))
00158 
00159             # Flatten interfaces
00160             intfs = flattenInterfaces(intfs)
00161             
00162             # Now check the resulting class to see if the mapping was made
00163             # correctly. Note that this uses carnal knowledge of the internal
00164             # structures used to store this information!
00165             # Check each method of every interface
00166             for intf in intfs:
00167                 if intf in ignore:
00168                     continue
00169                 for name in intf.names():
00170                     count += 1
00171                     if not "%s__roles__" % (name,) in dict:
00172                         not_defined.append("%s.%s" % (klass.__name__, name))
00173 
00174         # Raise in case some tests are missing
00175         if not_defined:
00176             raise RuntimeError, "%d (over %d) MISSING SECURITY DECLARATIONS:\n%s do not have a security declaration associated." % (
00177                 len(not_defined),
00178                 count,
00179                 string.join(not_defined, ", "),
00180                 )
00181         
00182 
00183 def test_suite():
00184     from unittest import TestSuite, makeSuite
00185     suite = TestSuite()
00186     #suite.addTest(makeSuite(TestInterface))
00187     return suite
00188 
00189 if __name__ == '__main__':
00190     framework()