[Zope3-checkins] CVS: Packages3/Interface/tests - IFoo.py:1.4 __init__.py:1.4 dummy.py:1.4 testDocument.py:1.8 testImplements.py:1.5 testInterface.py:1.9 testVerify.py:1.6 testVisitImplements.py:1.4 unitfixtures.py:1.6

Jim Fulton jim@zope.com
Fri, 18 Apr 2003 06:03:21 -0400


Update of /cvs-repository/Packages3/Interface/tests
In directory cvs.zope.org:/tmp/cvs-serv27994/tests

Added Files:
	IFoo.py __init__.py dummy.py testDocument.py testImplements.py 
	testInterface.py testVerify.py testVisitImplements.py 
	unitfixtures.py 
Log Message:
committing z2 sources in preparation for z3 compatability layer over zope.interface

=== Packages3/Interface/tests/IFoo.py 1.3 => 1.4 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/IFoo.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,24 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+from Interface import Interface
+
+class IFoo( Interface ):
+    """
+        Dummy interface for unit tests.
+    """
+
+    def bar( baz ):
+        """
+            Just a note.
+        """


=== Packages3/Interface/tests/__init__.py 1.3 => 1.4 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/__init__.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,14 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+""" Packagize. """


=== Packages3/Interface/tests/dummy.py 1.3 => 1.4 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/dummy.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,19 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+from Interface.tests.IFoo import IFoo
+
+__implements__ = IFoo
+
+def bar( baz ):
+    pass


=== Packages3/Interface/tests/testDocument.py 1.7 => 1.8 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/testDocument.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,75 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from Interface import Interface
+from Interface.Attribute import Attribute
+
+class Test(TestCase):
+
+    def testBlech(self):
+        from Interface.Document import asStructuredText
+
+        self.assertEqual(asStructuredText(I2), '''\
+I2
+
+ I2 doc
+
+ This interface extends:
+
+  o _I1
+
+ Attributes:
+
+  a1 -- no documentation
+
+  a2 -- a2 doc
+
+ Methods:
+
+  f21() -- f21 doc
+
+  f22() -- no documentation
+
+  f23() -- f23 doc
+
+''')
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+class _I1(Interface):
+    def f11(): pass
+    def f12(): pass
+
+class I2(_I1):
+    "I2 doc"
+
+    a1 = Attribute('a1')
+    a2 = Attribute('a2', 'a2 doc')
+
+    def f21(): "f21 doc"
+    def f22(): pass
+    def f23(): "f23 doc"
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')


=== Packages3/Interface/tests/testImplements.py 1.4 => 1.5 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/testImplements.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,70 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+from __future__ import nested_scopes
+from Interface import Interface
+from Interface.Implements import implements
+from Interface.Exceptions import DoesNotImplement, BrokenImplementation
+from Interface.Exceptions import BrokenMethodImplementation
+
+import unittest, sys
+
+class Test(unittest.TestCase):
+
+    def testSimple(self):
+
+        class I(Interface):
+            def f(): pass
+
+        class C: pass
+
+        self.assertRaises(BrokenImplementation, implements, C, I)
+
+        C.f=lambda self: None
+
+        implements(C, I)
+
+        self.assertEqual(C.__implements__, I)
+
+    def testAdd(self):
+
+        class I(Interface):
+            def f(): pass
+
+        class I2(Interface):
+            def g(): pass
+
+        class C:
+            __implements__=I2
+
+        self.assertRaises(BrokenImplementation, implements, C, I)
+        self.assertRaises(BrokenImplementation, implements, C, I2)
+
+        C.f=lambda self: None
+
+        implements(C, I)
+
+        self.assertEqual(C.__implements__, (I2, I))
+        self.assertRaises(BrokenImplementation, implements, C, I2)
+
+        C.g=C.f
+        implements(C, I)
+        implements(C, I2)
+
+
+def test_suite():
+    loader=unittest.TestLoader()
+    return loader.loadTestsFromTestCase(Test)
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())


=== Packages3/Interface/tests/testInterface.py 1.8 => 1.9 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/testInterface.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,146 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+
+import unittest
+import Interface
+from unitfixtures import *  # hehehe
+from Interface.Exceptions import BrokenImplementation
+from Interface.Implements import instancesOfObjectImplements
+from Interface.Implements import objectImplements
+from Interface import Interface
+from Interface.Attribute import Attribute
+
+class InterfaceTests(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def testClassImplements(self):
+        assert IC.isImplementedByInstancesOf(C)
+
+        assert I1.isImplementedByInstancesOf(A)
+        assert I1.isImplementedByInstancesOf(B)
+        assert not I1.isImplementedByInstancesOf(C)
+        assert I1.isImplementedByInstancesOf(D)
+        assert I1.isImplementedByInstancesOf(E)
+
+        assert not I2.isImplementedByInstancesOf(A)
+        assert I2.isImplementedByInstancesOf(B)
+        assert not I2.isImplementedByInstancesOf(C)
+        assert not I2.isImplementedByInstancesOf(D)
+        assert not I2.isImplementedByInstancesOf(E)
+
+    def testUtil(self):
+        f = instancesOfObjectImplements
+        assert IC in f(C)
+        assert I1 in f(A)
+        assert not I1 in f(C)
+        assert I2 in f(B)
+        assert not I2 in f(C)
+
+        f = objectImplements
+        assert IC in f(C())
+        assert I1 in f(A())
+        assert not I1 in f(C())
+        assert I2 in f(B())
+        assert not I2 in f(C())
+
+
+    def testObjectImplements(self):
+        assert IC.isImplementedBy(C())
+
+        assert I1.isImplementedBy(A())
+        assert I1.isImplementedBy(B())
+        assert not I1.isImplementedBy(C())
+        assert I1.isImplementedBy(D())
+        assert I1.isImplementedBy(E())
+
+        assert not I2.isImplementedBy(A())
+        assert I2.isImplementedBy(B())
+        assert not I2.isImplementedBy(C())
+        assert not I2.isImplementedBy(D())
+        assert not I2.isImplementedBy(E())
+
+    def testDeferredClass(self):
+        a = A()
+        self.assertRaises(BrokenImplementation, a.ma)
+
+
+    def testInterfaceExtendsInterface(self):
+        assert BazInterface.extends(BobInterface)
+        assert BazInterface.extends(BarInterface)
+        assert BazInterface.extends(FunInterface)
+        assert not BobInterface.extends(FunInterface)
+        assert not BobInterface.extends(BarInterface)
+        assert BarInterface.extends(FunInterface)
+        assert not BarInterface.extends(BazInterface)
+
+    def testVerifyImplementation(self):
+        from Interface.Verify import verifyClass
+        assert verifyClass(FooInterface, Foo)
+        assert Interface.isImplementedBy(I1)
+
+    def test_names(self):
+        names = list(_I2.names()); names.sort()
+        self.assertEqual(names, ['f21', 'f22', 'f23'])
+        names = list(_I2.names(1)); names.sort()
+        self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23'])
+
+    def test_namesAndDescriptions(self):
+        names = [nd[0] for nd in _I2.namesAndDescriptions()]; names.sort()
+        self.assertEqual(names, ['f21', 'f22', 'f23'])
+        names = [nd[0] for nd in _I2.namesAndDescriptions(1)]; names.sort()
+        self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23'])
+
+        for name, d in _I2.namesAndDescriptions(1):
+            self.assertEqual(name, d.__name__)
+
+    def test_getDescriptionFor(self):
+        self.assertEqual(_I2.getDescriptionFor('f11').__name__, 'f11')
+        self.assertEqual(_I2.getDescriptionFor('f22').__name__, 'f22')
+        self.assertEqual(_I2.queryDescriptionFor('f33', self), self)
+        self.assertRaises(KeyError, _I2.getDescriptionFor, 'f33')
+
+    def testAttr(self):
+        description = _I2.getDescriptionFor('a1')
+        self.assertEqual(description.__name__, 'a1')
+        self.assertEqual(description.__doc__, 'This is an attribute')
+
+
+class _I1(Interface):
+
+    a1 = Attribute("This is an attribute")
+
+    def f11(): pass
+    def f12(): pass
+
+class __I1(_I1): pass
+class ___I1(__I1): pass
+
+class _I2(___I1):
+    def f21(): pass
+    def f22(): pass
+    def f23(): pass
+
+def test_suite():
+    return unittest.makeSuite(InterfaceTests)
+
+def main():
+    unittest.TextTestRunner().run(test_suite())
+
+if __name__=="__main__":
+    main()


=== Packages3/Interface/tests/testVerify.py 1.5 => 1.6 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/testVerify.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,180 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""
+
+Revision information:
+$Id$
+"""
+from __future__ import nested_scopes
+
+
+from Interface import Interface
+from Interface.Verify import verifyClass, verifyObject
+from Interface.Exceptions import DoesNotImplement, BrokenImplementation
+from Interface.Exceptions import BrokenMethodImplementation
+
+import unittest, sys
+
+class Test(unittest.TestCase):
+
+    def testNotImplemented(self):
+
+        class C: pass
+
+        class I(Interface): pass
+
+        self.assertRaises(DoesNotImplement, verifyClass, I, C)
+
+        C.__implements__=I
+
+        verifyClass(I, C)
+
+    def testMissingAttr(self):
+
+        class I(Interface):
+            def f(): pass
+
+        class C:
+
+            __implements__=I
+
+        self.assertRaises(BrokenImplementation, verifyClass, I, C)
+
+        C.f=lambda self: None
+
+        verifyClass(I, C)
+
+    def testMissingAttr_with_Extended_Interface(self):
+       
+        class II(Interface):
+            def f():
+                pass
+
+        class I(II):
+            pass
+
+        class C:
+
+            __implements__=I
+
+        self.assertRaises(BrokenImplementation, verifyClass, I, C)
+
+        C.f=lambda self: None
+
+        verifyClass(I, C)
+
+    def testWrongArgs(self):
+
+        class I(Interface):
+            def f(a): pass
+
+        class C:
+
+            def f(self, b): pass
+
+            __implements__=I
+
+        # We no longer require names to match.
+        #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+
+        C.f=lambda self, a: None
+
+        verifyClass(I, C)
+
+        C.f=lambda self, **kw: None
+
+        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+
+        C.f=lambda self, a, *args: None
+
+        verifyClass(I, C)
+
+        C.f=lambda self, a, *args, **kw: None
+
+        verifyClass(I, C)
+
+        C.f=lambda self, *args: None
+
+        verifyClass(I, C)
+
+    def testExtraArgs(self):
+
+        class I(Interface):
+            def f(a): pass
+
+        class C:
+
+            def f(self, a, b): pass
+
+            __implements__=I
+
+        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+
+        C.f=lambda self, a: None
+
+        verifyClass(I, C)
+
+        C.f=lambda self, a, b=None: None
+
+        verifyClass(I, C)
+
+    def testNoVar(self):
+
+        class I(Interface):
+            def f(a, *args): pass
+
+        class C:
+
+            def f(self, a): pass
+
+            __implements__=I
+
+        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+
+        C.f=lambda self, a, *foo: None
+
+        verifyClass(I, C)
+
+    def testNoKW(self):
+
+        class I(Interface):
+            def f(a, **args): pass
+
+        class C:
+
+            def f(self, a): pass
+
+            __implements__=I
+
+        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
+
+        C.f=lambda self, a, **foo: None
+
+        verifyClass(I, C)
+
+    def testModule(self):
+
+        from Interface.tests.IFoo import IFoo
+        from Interface.tests import dummy
+
+        verifyObject(IFoo, dummy)
+
+
+
+def test_suite():
+    loader=unittest.TestLoader()
+    return loader.loadTestsFromTestCase(Test)
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())


=== Packages3/Interface/tests/testVisitImplements.py 1.3 => 1.4 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/testVisitImplements.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,54 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+import unittest, sys
+
+from Interface.Implements import visitImplements
+from Interface import Interface
+from Interface.Exceptions import BadImplements
+
+class I1(Interface): pass
+class I2(Interface): pass
+class I3(Interface): pass
+
+class Test(unittest.TestCase):
+
+    def testSimpleImplements(self):
+        data=[]
+        visitImplements(I1, None, data.append)
+        self.assertEqual(data, [I1])
+
+    def testSimpleBadImplements(self):
+        data=[]
+        self.assertRaises(BadImplements,
+                          visitImplements, unittest, None, data.append)
+
+    def testComplexImplements(self):
+        data=[]
+        visitImplements((I1, (I2, I3)), None, data.append)
+        data = map(lambda i: i.__name__, data)
+        self.assertEqual(data, ['I1', 'I2', 'I3'])
+
+    def testComplexBadImplements(self):
+        data=[]
+        self.assertRaises(BadImplements,
+                          visitImplements, (I1, (I2, unittest)),
+                          None, data.append)
+
+
+def test_suite():
+    loader=unittest.TestLoader()
+    return loader.loadTestsFromTestCase(Test)
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())


=== Packages3/Interface/tests/unitfixtures.py 1.5 => 1.6 ===
--- /dev/null	Fri Apr 18 06:03:21 2003
+++ Packages3/Interface/tests/unitfixtures.py	Fri Apr 18 06:03:20 2003
@@ -0,0 +1,117 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+from Interface import Interface
+from Interface.Attribute import Attribute
+
+class mytest(Interface):
+    pass
+
+class C:
+    def m1(self, a, b):
+        "return 1"
+        return 1
+
+    def m2(self, a, b):
+        "return 2"
+        return 2
+
+# testInstancesOfClassImplements
+
+
+
+
+#  YAGNI IC=Interface.impliedInterface(C)
+class IC(Interface):
+    def m1(a, b):
+        "return 1"
+
+    def m2(a, b):
+        "return 2"
+
+
+
+C.__implements__=IC
+
+class I1(Interface):
+    def ma():
+        "blah"
+
+class I2(I1): pass
+
+class I3(Interface): pass
+
+class I4(Interface): pass
+
+class A(I1.deferred()):
+    __implements__=I1
+
+class B:
+    __implements__=I2, I3
+
+class D(A, B): pass
+
+class E(A, B):
+    __implements__ = A.__implements__, C.__implements__
+
+
+class FooInterface(Interface):
+    """ This is an Abstract Base Class """
+
+    foobar = Attribute("fuzzed over beyond all recognition")
+
+    def aMethod(foo, bar, bingo):
+        """ This is aMethod """
+
+    def anotherMethod(foo=6, bar="where you get sloshed", bingo=(1,3,)):
+        """ This is anotherMethod """
+
+    def wammy(zip, *argues):
+        """ yadda yadda """
+
+    def useless(**keywords):
+        """ useless code is fun! """
+
+class Foo:
+    """ A concrete class """
+
+    __implements__ = FooInterface,
+
+    foobar = "yeah"
+
+    def aMethod(self, foo, bar, bingo):
+        """ This is aMethod """
+        return "barf!"
+
+    def anotherMethod(self, foo=6, bar="where you get sloshed", bingo=(1,3,)):
+        """ This is anotherMethod """
+        return "barf!"
+
+    def wammy(self, zip, *argues):
+        """ yadda yadda """
+        return "barf!"
+
+    def useless(self, **keywords):
+        """ useless code is fun! """
+        return "barf!"
+
+foo_instance = Foo()
+
+class Blah:
+    pass
+
+new = Interface.__class__
+FunInterface = new('FunInterface')
+BarInterface = new('BarInterface', [FunInterface])
+BobInterface = new('BobInterface')
+BazInterface = new('BazInterface', [BobInterface, BarInterface])