diff --git a/src/z3c/testing/__init__.py b/src/z3c/testing/__init__.py new file mode 100644 index 0000000..203cd51 --- /dev/null +++ b/src/z3c/testing/__init__.py @@ -0,0 +1,23 @@ +############################################################################## +# +# Copyright (c) 2005 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" +$Id$ +""" + +from z3c.testing.app import TestCase +from z3c.testing.app import InterfaceBaseTest +from z3c.testing.app import marker_pos +from z3c.testing.app import marker_kws +from z3c.testing.directive import registerDirective +from z3c.testing.directive import setUpContentMetaDirectives diff --git a/src/z3c/testing/app.py b/src/z3c/testing/app.py new file mode 100644 index 0000000..1e0756d --- /dev/null +++ b/src/z3c/testing/app.py @@ -0,0 +1,83 @@ +############################################################################## +# +# Copyright (c) 2005 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" +$Id$ +""" + +import sys +import unittest + + +############################################################################### +# +# TestCase +# +############################################################################### +marker_pos = object() +marker_kws = object() + +class TestCase(unittest.TestCase): + + def getTestInterface(self): + msg = 'Subclasses has to implement getTestInterface()' + raise NotImplementedError, msg + + def getTestClass(self): + raise NotImplementedError, 'Subclasses has to implement getTestClass()' + + def getTestPos(self): + return marker_pos + + def getTestKws(self): + return marker_kws + + def makeTestObject(self, object=None, *pos, **kws): + # provide default positional or keyword arguments + if self.getTestPos() is not marker_pos and not pos: + pos = self.getTestPos() + + if self.getTestKws() is not marker_kws and not kws: + kws = self.getTestKws() + + testclass = self.getTestClass() + + if object is None: + # a class instance itself is the object to be tested. + return testclass(*pos, **kws) + + else: + # an adapted instance is the object to be tested. + return testclass(object, *pos, **kws) + + +############################################################################### +# +# Public Base Tests +# +############################################################################### +from zope.interface.verify import verifyClass +from zope.interface.verify import verifyObject + + +class InterfaceBaseTest(TestCase): + """Base test for IContainer including interface test.""" + + def test_verifyClass(self): + # class test + self.assert_(verifyClass(self.getTestInterface(), self.getTestClass())) + + def test_verifyObject(self): + # object test + self.assert_(verifyObject(self.getTestInterface(), + self.makeTestObject())) diff --git a/src/z3c/testing/directive.py b/src/z3c/testing/directive.py new file mode 100644 index 0000000..74a8cfa --- /dev/null +++ b/src/z3c/testing/directive.py @@ -0,0 +1,47 @@ +############################################################################## +# +# Copyright (c) 2005 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" +$Id$ +""" + +from cStringIO import StringIO + +from zope.configuration.xmlconfig import xmlconfig +from zope.configuration.xmlconfig import XMLConfig + + + +# helper for directive testing +template = """ + %s + """ + + +def registerDirective(directive, template=template): + xmlconfig(StringIO(template % directive)) + + + +# Zope (content) Meta Directives +#----------------------------------------------------------------------------- +import zope.component +import zope.app.security + +def setUpContentMetaDirectives(): + XMLConfig('meta.zcml', zope.component)() + XMLConfig('meta.zcml', zope.app.security)() diff --git a/src/z3c/testing/tests.py b/src/z3c/testing/tests.py new file mode 100644 index 0000000..9208f81 --- /dev/null +++ b/src/z3c/testing/tests.py @@ -0,0 +1,48 @@ +############################################################################## +# +# Copyright (c) 2005 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" +$Id$ +""" + +import unittest +import zope.interface + +from z3c import testing + + +class ISample(zope.interface.Interface): + """Sample interface.""" + +class Sample(object): + """Sample object.""" + + zope.interface.implements(ISample) + + +class TestTestCase(testing.InterfaceBaseTest): + + def getTestClass(self): + return Sample + + def getTestInterface(self): + return ISample + + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(TestTestCase), + )) + +if __name__=='__main__': + unittest.main(defaultTest='test_suite')