[Checkins] SVN: zope.interface/branches/tseaver-better_unit_tests/src/zope/interface/tests/test_verify.py Tighten up the tests for zope.interface.verify.

Tres Seaver tseaver at palladion.com
Mon Apr 19 11:18:13 EDT 2010


Log message for revision 111111:
  Tighten up the tests for zope.interface.verify.

Changed:
  U   zope.interface/branches/tseaver-better_unit_tests/src/zope/interface/tests/test_verify.py

-=-
Modified: zope.interface/branches/tseaver-better_unit_tests/src/zope/interface/tests/test_verify.py
===================================================================
--- zope.interface/branches/tseaver-better_unit_tests/src/zope/interface/tests/test_verify.py	2010-04-19 15:18:12 UTC (rev 111110)
+++ zope.interface/branches/tseaver-better_unit_tests/src/zope/interface/tests/test_verify.py	2010-04-19 15:18:13 UTC (rev 111111)
@@ -11,192 +11,417 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Interface Verify tests
-
-$Id$
+""" zope.interface.verify unit tests
 """
-import doctest
 import unittest
 
-from zope.interface import Interface, implements, classImplements, Attribute
-from zope.interface.verify import verifyClass, verifyObject
-from zope.interface.exceptions import DoesNotImplement, BrokenImplementation
-from zope.interface.exceptions import BrokenMethodImplementation
 
-class Test(unittest.TestCase):
+class Test_verifyClass(unittest.TestCase):
 
-    def testNotImplemented(self):
+    def _callFUT(self, iface, klass):
+        from zope.interface.verify import verifyClass
+        return verifyClass(iface, klass)
 
-        class C(object): pass
+    def test_class_doesnt_implement(self):
+        from zope.interface import Interface
+        from zope.interface.exceptions import DoesNotImplement
 
-        class I(Interface): pass
+        class ICurrent(Interface):
+            pass
 
-        self.assertRaises(DoesNotImplement, verifyClass, I, C)
+        class Current(object):
+            pass
 
-        classImplements(C, I)
+        self.assertRaises(DoesNotImplement, self._callFUT, ICurrent, Current)
 
-        verifyClass(I, C)
+    def test_class_doesnt_implement_but_classImplements_later(self):
+        from zope.interface import Interface
+        from zope.interface import classImplements
 
-    def testMissingAttr(self):
+        class ICurrent(Interface):
+            pass
 
-        class I(Interface):
-            def f(): pass
+        class Current(object):
+            pass
 
-        class C(object):
-            implements(I)
+        classImplements(Current, ICurrent)
 
-        self.assertRaises(BrokenImplementation, verifyClass, I, C)
+        self._callFUT(ICurrent, Current)
 
-        C.f=lambda self: None
+    def test_class_doesnt_have_required_method_simple(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenImplementation
 
-        verifyClass(I, C)
+        class ICurrent(Interface):
+            def method(): pass
 
-    def testMissingAttr_with_Extended_Interface(self):
+        class Current(object):
+            implements(ICurrent)
 
-        class II(Interface):
-            def f():
+        self.assertRaises(BrokenImplementation,
+                          self._callFUT, ICurrent, Current)
+
+    def test_class_has_required_method_simple(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+
+        class ICurrent(Interface):
+            def method(): pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self):
                 pass
 
-        class I(II):
+        self._callFUT(ICurrent, Current)
+
+    def test_class_doesnt_have_required_method_derived(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenImplementation
+
+        class IBase(Interface):
+            def method():
+                pass
+
+        class IDerived(IBase):
             pass
 
-        class C(object):
-            implements(I)
+        class Current(object):
+            implements(IDerived)
 
-        self.assertRaises(BrokenImplementation, verifyClass, I, C)
+        self.assertRaises(BrokenImplementation,
+                          self._callFUT, IDerived, Current)
 
-        C.f=lambda self: None
+    def test_class_has_required_method_derived(self):
+        from zope.interface import Interface
+        from zope.interface import implements
 
-        verifyClass(I, C)
+        class IBase(Interface):
+            def method():
+                pass
 
-    def testWrongArgs(self):
+        class IDerived(IBase):
+            pass
 
-        class I(Interface):
-            def f(a): pass
+        class Current(object):
+            implements(IDerived)
 
-        class C(object):
-            def f(self, b): pass
+            def method(self):
+                pass
 
-            implements(I)
+        self._callFUT(IDerived, Current)
 
+    def test_method_takes_wrong_arg_names_but_OK(self):
         # We no longer require names to match.
-        #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+        from zope.interface import Interface
+        from zope.interface import implements
 
-        C.f=lambda self, a: None
+        class ICurrent(Interface):
 
-        verifyClass(I, C)
+            def method(a):
+                pass
 
-        C.f=lambda self, **kw: None
+        class Current(object):
+            implements(ICurrent)
 
-        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+            def method(self, b):
+                pass
 
-        C.f=lambda self, a, *args: None
+        self._callFUT(ICurrent, Current)
 
-        verifyClass(I, C)
+    def test_method_takes_not_enough_args(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
 
-        C.f=lambda self, a, *args, **kw: None
+        class ICurrent(Interface):
 
-        verifyClass(I, C)
+            def method(a):
+                pass
 
-        C.f=lambda self, *args: None
+        class Current(object):
+            implements(ICurrent)
 
-        verifyClass(I, C)
+            def method(self):
+                pass
 
-    def testExtraArgs(self):
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
 
-        class I(Interface):
-            def f(a): pass
+    def test_method_takes_extra_arg(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
 
-        class C(object):
-            def f(self, a, b): pass
+        class ICurrent(Interface):
 
-            implements(I)
+            def method(a):
+                pass
 
-        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+        class Current(object):
+            implements(ICurrent)
 
-        C.f=lambda self, a: None
+            def method(self, a, b):
+                pass
 
-        verifyClass(I, C)
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
 
-        C.f=lambda self, a, b=None: None
+    def test_method_takes_extra_arg_with_default(self):
+        from zope.interface import Interface
+        from zope.interface import implements
 
-        verifyClass(I, C)
+        class ICurrent(Interface):
 
-    def testNoVar(self):
+            def method(a):
+                pass
 
-        class I(Interface):
-            def f(a, *args): pass
+        class Current(object):
+            implements(ICurrent)
 
-        class C(object):
-            def f(self, a): pass
+            def method(self, a, b=None):
+                pass
 
-            implements(I)
+        self._callFUT(ICurrent, Current)
 
-        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+    def test_method_takes_only_positional_args(self):
+        from zope.interface import Interface
+        from zope.interface import implements
 
-        C.f=lambda self, a, *foo: None
+        class ICurrent(Interface):
 
-        verifyClass(I, C)
+            def method(a):
+                pass
 
-    def testNoKW(self):
+        class Current(object):
+            implements(ICurrent)
 
-        class I(Interface):
-            def f(a, **args): pass
+            def method(self, *args):
+                pass
 
-        class C(object):
-            def f(self, a): pass
+        self._callFUT(ICurrent, Current)
 
-            implements(I)
+    def test_method_takes_only_kwargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
 
-        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+        class ICurrent(Interface):
 
-        C.f=lambda self, a, **foo: None
+            def method(a):
+                pass
 
-        verifyClass(I, C)
+        class Current(object):
+            implements(ICurrent)
 
-    def testModule(self):
+            def method(self, **kw):
+                pass
 
-        from zope.interface.tests.ifoo import IFoo
-        from zope.interface.tests import dummy
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
 
-        verifyObject(IFoo, dummy)
+    def test_method_takes_extra_starargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
 
-    def testMethodForAttr(self):
-        
-        class IFoo(Interface):
-             foo = Attribute("The foo Attribute")
+        class ICurrent(Interface):
 
+            def method(a):
+                pass
 
-        class Foo:
-             implements(IFoo)
+        class Current(object):
+            implements(ICurrent)
 
-             def foo(self):
-                 pass
+            def method(self, a, *args):
+                pass
 
-        verifyClass(IFoo, Foo)
+        self._callFUT(ICurrent, Current)
 
-    def testNonMethodForMethod(self):
+    def test_method_takes_extra_starargs_and_kwargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
 
-        class IBar(Interface):
-             def foo():
-                 pass
+        class ICurrent(Interface):
 
-        class Bar:
-            implements(IBar)
+            def method(a):
+                pass
 
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, a, *args, **kw):
+                pass
+
+        self._callFUT(ICurrent, Current)
+
+    def test_method_doesnt_take_required_positional_and_starargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
+
+        class ICurrent(Interface):
+
+            def method(a, *args):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, a):
+                pass
+
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
+
+    def test_method_takes_required_positional_and_starargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+
+        class ICurrent(Interface):
+
+            def method(a, *args):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, a, *args):
+                pass
+
+        self._callFUT(ICurrent, Current)
+
+    def test_method_takes_only_starargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+
+        class ICurrent(Interface):
+
+            def method(a, *args):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, *args):
+                pass
+
+        self._callFUT(ICurrent, Current)
+
+    def test_method_takes_required_kwargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+
+        class ICurrent(Interface):
+
+            def method(**kwargs):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, **kw):
+                pass
+
+        self._callFUT(ICurrent, Current)
+
+    def test_method_takes_positional_plus_required_starargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
+
+        class ICurrent(Interface):
+
+            def method(*args):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, a, *args):
+                pass
+
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
+
+
+    def test_method_doesnt_take_required_kwargs(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
+
+        class ICurrent(Interface):
+
+            def method(**kwargs):
+                pass
+
+        class Current(object):
+            implements(ICurrent)
+
+            def method(self, a):
+                pass
+
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
+
+
+    def test_class_has_method_for_iface_attr(self):
+        from zope.interface import Attribute
+        from zope.interface import Interface
+        from zope.interface import implements
+
+        class ICurrent(Interface):
+            foo = Attribute("The foo Attribute")
+
+        class Current:
+            implements(ICurrent)
+
+            def foo(self):
+                pass
+
+        self._callFUT(ICurrent, Current)
+
+    def test_class_has_nonmethod_for_method(self):
+        from zope.interface import Interface
+        from zope.interface import implements
+        from zope.interface.exceptions import BrokenMethodImplementation
+
+        class ICurrent(Interface):
+            def foo():
+                pass
+
+        class Current:
+            implements(ICurrent)
             foo = 1
 
-        self.assertRaises(BrokenMethodImplementation, verifyClass, IBar, Bar)
-        
+        self.assertRaises(BrokenMethodImplementation,
+                          self._callFUT, ICurrent, Current)
 
+class Test_verifyObject(Test_verifyClass):
+
+    def _callFUT(self, iface, target):
+        from zope.interface.verify import verifyObject
+        if isinstance(target, (type, type(OldSkool))):
+            target = target()
+        return verifyObject(iface, target)
+
+    def testModule(self):
+        from zope.interface.tests.ifoo import IFoo
+        from zope.interface.tests import dummy
+
+        self._callFUT(IFoo, dummy)
+
+class OldSkool:
+    pass
+
 def test_suite():
-    loader=unittest.TestLoader()
     return unittest.TestSuite((
-        doctest.DocFileSuite(
-            '../verify.txt',
-            optionflags=doctest.NORMALIZE_WHITESPACE),
-        loader.loadTestsFromTestCase(Test),
-        ))
-
-if __name__=='__main__':
-    unittest.TextTestRunner().run(test_suite())
+        unittest.makeSuite(Test_verifyClass),
+        unittest.makeSuite(Test_verifyObject),
+    #   This one needs to turn into just docs.
+    #   import doctest
+    #   doctest.DocFileSuite('../verify.txt',
+    #                        optionflags=doctest.NORMALIZE_WHITESPACE),
+    ))



More information about the checkins mailing list