Back to index

plone3  3.1.7
testInterfaceTool.py
Go to the documentation of this file.
00001 #
00002 # InterfaceTool tests
00003 #
00004 
00005 from Testing import ZopeTestCase
00006 from Products.CMFPlone.tests import PloneTestCase
00007 
00008 from Products.CMFCore.interfaces.DublinCore import DublinCore
00009 from Products.CMFCore.interfaces.Contentish import Contentish
00010 from Products.CMFCore.PortalContent import PortalContent
00011 from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
00012 from Products.CMFDefault.Document import Document
00013 
00014 from Products.CMFPlone.InterfaceTool import resolveInterface, getDottedName
00015 from Products.CMFPlone.InterfaceTool import InterfaceFinder
00016 from Products.CMFPlone.interfaces import PloneBaseTool
00017 from Products.CMFPlone.utils import classImplements
00018 
00019 
00020 class MyPortalContent(Contentish): pass
00021 
00022 class A(PortalContent, DefaultDublinCoreImpl):
00023     __implements__ = PortalContent.__implements__, \
00024                      DefaultDublinCoreImpl.__implements__
00025 classImplements(A, A.__implements__)
00026 
00027 class B(PortalContent, DefaultDublinCoreImpl):
00028     __implements__ = MyPortalContent, \
00029                      DefaultDublinCoreImpl.__implements__
00030 classImplements(B, B.__implements__)
00031 
00032 
00033 class TestInterfaceResolution(ZopeTestCase.ZopeTestCase):
00034 
00035     def testResolveDublinCore(self):
00036         # DublinCore should be resolved
00037         dotted_name = getDottedName(DublinCore)
00038         self.assertEqual(resolveInterface(dotted_name), DublinCore)
00039 
00040     def testResolveContentish(self):
00041         # Contentish should be resolved
00042         dotted_name = getDottedName(Contentish)
00043         self.assertEqual(resolveInterface(dotted_name), Contentish)
00044 
00045     def testResolveNonInterface(self):
00046         # Should raise ValueError when called with non-Interface
00047         dotted_name = getDottedName(PortalContent)
00048         self.assertRaises(ValueError, resolveInterface, dotted_name)
00049 
00050 
00051 class TestInterfaceFinder(ZopeTestCase.ZopeTestCase):
00052 
00053     def testAvailableInterfaces(self):
00054         # Should find available interfaces
00055         from Products.CMFPlone.interfaces import InterfaceTool
00056         ifs = InterfaceFinder().findInterfaces(module=InterfaceTool)
00057         self.failUnless(getDottedName(InterfaceTool.IInterfaceTool) in ifs)
00058         self.failUnless(getDottedName(PloneBaseTool.IPloneBaseTool) in ifs)
00059 
00060 
00061 class TestInterfaceTool(PloneTestCase.PloneTestCase):
00062 
00063     def afterSetUp(self):
00064         self.interface = self.portal.portal_interface
00065 
00066     def testContentImplements(self):
00067         content = PortalContent()
00068         self.failUnless(self.interface.objectImplements(content, getDottedName(Contentish)))
00069 
00070     def testDocumentImplements(self):
00071         document = Document(id='foo')
00072         self.failUnless(self.interface.objectImplements(document, getDottedName(Contentish)))
00073         self.failUnless(self.interface.objectImplements(document, getDottedName(DublinCore)))
00074 
00075     def testDCImplements(self):
00076         dc = DefaultDublinCoreImpl()
00077         self.failUnless(self.interface.objectImplements(dc, getDottedName(DublinCore)))
00078 
00079     def testAImplements(self):
00080         a = A()
00081         self.failUnless(self.interface.objectImplements(a, getDottedName(Contentish)))
00082         self.failUnless(self.interface.objectImplements(a, getDottedName(DublinCore)))
00083         self.failIf(self.interface.objectImplements(a, getDottedName(MyPortalContent)))
00084 
00085     def testBImplements(self):
00086         b = B()
00087         self.failUnless(self.interface.objectImplements(b, getDottedName(Contentish)))
00088         self.failUnless(self.interface.objectImplements(b, getDottedName(DublinCore)))
00089         self.failUnless(self.interface.objectImplements(b, getDottedName(MyPortalContent)))
00090 
00091     def testNamesAndDescriptions(self):
00092         from Products.CMFPlone.interfaces.InterfaceTool import IInterfaceTool
00093         nd = self.interface.namesAndDescriptions(getDottedName(IInterfaceTool))
00094         nd2 = IInterfaceTool.namesAndDescriptions()
00095         nd2 = [(n, d.getDoc()) for n, d in nd2]
00096         self.assertEquals(nd, nd2)
00097 
00098 
00099 def test_suite():
00100     # Normalize dotted names
00101     from Products.CMFPlone.tests.testInterfaceTool import TestInterfaceResolution
00102     from Products.CMFPlone.tests.testInterfaceTool import TestInterfaceFinder
00103     from Products.CMFPlone.tests.testInterfaceTool import TestInterfaceTool
00104 
00105     from unittest import TestSuite, makeSuite
00106     suite = TestSuite()
00107     suite.addTest(makeSuite(TestInterfaceResolution))
00108     suite.addTest(makeSuite(TestInterfaceFinder))
00109     suite.addTest(makeSuite(TestInterfaceTool))
00110     return suite