[Zodb-checkins] CVS: Zope3/src/zope/interface/tests - test_advice.py:1.4.4.1 test_declarations.py:1.4.2.1 test_type.py:1.6.8.1 test_verify.py:1.3.8.1 test_flatten.py:NONE test_implements.py:NONE test_visitimplements.py:NONE

Grégoire Weber zope at i-con.ch
Sun Jun 22 11:24:17 EDT 2003


Update of /cvs-repository/Zope3/src/zope/interface/tests
In directory cvs.zope.org:/tmp/cvs-serv24874/src/zope/interface/tests

Modified Files:
      Tag: cw-mail-branch
	test_declarations.py test_type.py test_verify.py 
Added Files:
      Tag: cw-mail-branch
	test_advice.py 
Removed Files:
      Tag: cw-mail-branch
	test_flatten.py test_implements.py test_visitimplements.py 
Log Message:
Synced up with HEAD

=== Added File Zope3/src/zope/interface/tests/test_advice.py ===

##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Tests for advice

This module was adapted from 'protocols.tests.advice', part of the Python
Enterprise Application Kit (PEAK).  Please notify the PEAK authors
(pje at telecommunity.com and tsarna at sarna.org) if bugs are found or
Zope-specific changes are required, so that the PEAK version of this module
can be kept in sync.

PEAK is a Python application framework that interoperates with (but does
not require) Zope 3 and Twisted.  It provides tools for manipulating UML
models, object-relational persistence, aspect-oriented programming, and more.
Visit the PEAK home page at http://peak.telecommunity.com for more information.

$Id: test_advice.py,v 1.4.4.1 2003/06/22 14:23:45 gregweb Exp $
"""

from unittest import TestCase, makeSuite, TestSuite
from zope.interface.advice import *
from types import ClassType
import sys

def ping(log, value):

    def pong(klass):
        log.append((value,klass))
        return [klass]

    addClassAdvisor(pong)

class ClassicClass:
    __metaclass__ = ClassType
    classLevelFrameInfo = getFrameInfo(sys._getframe())

class NewStyleClass:
    __metaclass__ = type
    classLevelFrameInfo = getFrameInfo(sys._getframe())

moduleLevelFrameInfo = getFrameInfo(sys._getframe())

class FrameInfoTest(TestCase):

    classLevelFrameInfo = getFrameInfo(sys._getframe())

    def checkModuleInfo(self):
        kind, module, f_locals, f_globals = moduleLevelFrameInfo
        self.assertEquals(kind, "module")
        for d in module.__dict__, f_locals, f_globals:
            self.assert_(d is globals())

    def checkClassicClassInfo(self):
        kind, module, f_locals, f_globals = ClassicClass.classLevelFrameInfo
        self.assertEquals(kind, "class")

        self.assert_(f_locals is ClassicClass.__dict__)  # ???
        for d in module.__dict__, f_globals:
            self.assert_(d is globals())

    def checkNewStyleClassInfo(self):
        kind, module, f_locals, f_globals = NewStyleClass.classLevelFrameInfo
        self.assertEquals(kind, "class")

        for d in module.__dict__, f_globals:
            self.assert_(d is globals())

    def checkCallInfo(self):
        kind, module, f_locals, f_globals = getFrameInfo(sys._getframe())
        self.assertEquals(kind, "function call")
        self.assert_(f_locals is locals()) # ???
        for d in module.__dict__, f_globals:
            self.assert_(d is globals())


class AdviceTests(TestCase):

    def checkOrder(self):
        log = []
        class Foo:
            ping(log, 1)
            ping(log, 2)
            ping(log, 3)

        # Strip the list nesting
        for i in 1,2,3:
            self.assert_(isinstance(Foo, list))
            Foo, = Foo

        self.assertEquals(log, [(1, Foo), (2, [Foo]), (3, [[Foo]])])

    def XXXcheckOutside(self):
        # Disabled because the check does not work with doctest tests.
        try:
            ping([], 1)
        except SyntaxError:
            pass
        else:
            raise AssertionError(
                "Should have detected advice outside class body"
            )

    def checkDoubleType(self):
        if sys.hexversion >= 0x02030000:
            return  # you can't duplicate bases in 2.3
        class aType(type,type):
            ping([],1)
        aType, = aType
        self.assert_(aType.__class__ is type)

    def checkSingleExplicitMeta(self):

        class M(type):
            pass

        class C(M):
            __metaclass__ = M
            ping([],1)

        C, = C
        self.assert_(C.__class__ is M)


    def checkMixedMetas(self):

        class M1(type): pass
        class M2(type): pass

        class B1: __metaclass__ = M1
        class B2: __metaclass__ = M2

        try:
            class C(B1,B2):
                ping([],1)
        except TypeError:
            pass
        else:
            raise AssertionError("Should have gotten incompatibility error")

        class M3(M1,M2): pass

        class C(B1,B2):
            __metaclass__ = M3
            ping([],1)

        self.assert_(isinstance(C,list))
        C, = C
        self.assert_(isinstance(C,M3))

    def checkMetaOfClass(self):

        class metameta(type):
            pass

        class meta(type):
            __metaclass__ = metameta

        self.assertEquals(determineMetaclass((meta, type)), metameta)

TestClasses = (AdviceTests, FrameInfoTest)

def test_suite():
    return TestSuite([makeSuite(t,'check') for t in TestClasses])























=== Zope3/src/zope/interface/tests/test_declarations.py 1.4 => 1.4.2.1 ===
--- Zope3/src/zope/interface/tests/test_declarations.py:1.4	Sun May 18 13:53:06 2003
+++ Zope3/src/zope/interface/tests/test_declarations.py	Sun Jun 22 10:23:45 2003
@@ -152,11 +152,143 @@
                          [])
         
 
+def test_signature_w_no_class_interfaces():
+    """
+    >>> from zope.interface import *
+    >>> class C:
+    ...     pass
+    >>> c = C()
+    >>> providedBy(c).__signature__
+    ''
+    
+    >>> class I(Interface):
+    ...    pass
+    >>> directlyProvides(c, I)
+    >>> int(providedBy(c).__signature__
+    ...     == directlyProvidedBy(c).__signature__)
+    1
+    """
+
+def test_classImplement_on_deeply_nested_classes():
+    """This test is in response to a bug found, which is why it's a bit
+    contrived
+
+    >>> from zope.interface import *
+    >>> class B1:
+    ...     pass
+    >>> class B2(B1):
+    ...     pass
+    >>> class B3(B2):
+    ...     pass
+    >>> class D:
+    ...     implements()
+    >>> class S(B3, D):
+    ...     implements()
+
+    This failed due to a bug in the code for finding __providedBy__
+    descriptors for old-style classes.
+
+    """
+
+def test_computeSignature():
+    """Compute a specification signature
+
+    For example::
+
+      >>> from zope.interface import Interface
+      >>> class I1(Interface): pass
+      ...
+      >>> class I2(I1): pass
+      ...
+      >>> spec = InterfaceSpecification(I2)
+      >>> int(spec.__signature__ == "%s\\t%s\\t%s" % (
+      ...    I2.__identifier__, I1.__identifier__,
+      ...    Interface.__identifier__))
+      1
+
+    """
+
+def test_cant_pickle_plain_specs():
+    """
+    >>> from pickle import dumps
+    >>> dumps(InterfaceSpecification())
+    Traceback (most recent call last):
+    ...
+    TypeError: can't pickle InterfaceSpecification objects
+    >>> dumps(InterfaceSpecification(), 2)
+    Traceback (most recent call last):
+    ...
+    TypeError: can't pickle InterfaceSpecification objects
+    
+    """
+
+def test_pickle_provides_specs():
+    """
+    >>> from pickle import dumps, loads
+    >>> a = A()
+    >>> int(I2.isImplementedBy(a))
+    0
+    >>> directlyProvides(a, I2)
+    >>> int(I2.isImplementedBy(a))
+    1
+    >>> a2 = loads(dumps(a))
+    >>> int(I2.isImplementedBy(a2))
+    1
+    
+    """
+
+def test_pickle_implements_specs():
+    """
+    >>> from pickle import dumps, loads
+    >>> class A:
+    ...   implements(I1)
+    >>> class B(A):
+    ...   implements(I2)
+    >>> names =  [i.__name__ for i in implementedBy(B)]
+    >>> names
+    ['I2', 'I1']
+    >>> old = B.__dict__['__implements__']
+    >>> new = loads(dumps(old))
+    >>> names =  [i.__name__ for i in new]
+    >>> names
+    ['I2']
+    >>> classImplements(A, I3)
+    >>> B.__implements__ = new
+    >>> names =  [i.__name__ for i in implementedBy(B)]
+    >>> names
+    ['I2', 'I1', 'I3']
+    
+    """
+
+def test_pickle_only_specs():
+    """
+    >>> from pickle import dumps, loads
+    >>> class A:
+    ...   implements(I1)
+    >>> class B(A):
+    ...   implementsOnly(I2)
+    >>> names =  [i.__name__ for i in implementedBy(B)]
+    >>> names
+    ['I2']
+    >>> old = B.__dict__['__implements__']
+    >>> new = loads(dumps(old))
+    >>> names =  [i.__name__ for i in new]
+    >>> names
+    ['I2']
+    >>> classImplements(A, I3)
+    >>> B.__implements__ = new
+    >>> names =  [i.__name__ for i in implementedBy(B)]
+    >>> names
+    ['I2']
+    
+    """
+
 
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(Test))
     suite.addTest(DocTestSuite("zope.interface.declarations"))
+    suite.addTest(DocTestSuite())
     
     return suite
 


=== Zope3/src/zope/interface/tests/test_type.py 1.6 => 1.6.8.1 ===
--- Zope3/src/zope/interface/tests/test_type.py:1.6	Thu May  1 15:35:44 2003
+++ Zope3/src/zope/interface/tests/test_type.py	Sun Jun 22 10:23:45 2003
@@ -19,7 +19,7 @@
 
 import unittest
 from zope.interface.type import TypeRegistry
-from zope.interface import Interface
+from zope.interface import Interface, implements
 
 def getAllForObject(reg, ob):
     all = list(reg.getAllForObject(ob))
@@ -52,9 +52,9 @@
         self.assertEqual(getTypesMatching(reg, I2), [I2])
         self.assertEqual(getTypesMatching(reg, I3), [])
 
-        class C1: __implements__ = I1
-        class C2: __implements__ = I2
-        class C3: __implements__ = I3
+        class C1: implements(I1)
+        class C2: implements(I2)
+        class C3: implements(I3)
         class C: pass
 
         self.assertEqual(getAllForObject(reg, C1()), [])
@@ -131,11 +131,11 @@
         class I2(I1): pass
         class I3(I1): pass
         class I4(I2, I3): pass
-        class C1: __implements__ = I1
-        class C2: __implements__ = I2
-        class C3: __implements__ = I3
-        class C4: __implements__ = I4
-        class C5: __implements__ = I1, I2, I3, I4
+        class C1: implements(I1)
+        class C2: implements(I2)
+        class C3: implements(I3)
+        class C4: implements(I4)
+        class C5: implements(I1, I2, I3, I4)
         class C: pass
 
         reg = TypeRegistry()


=== Zope3/src/zope/interface/tests/test_verify.py 1.3 => 1.3.8.1 ===
--- Zope3/src/zope/interface/tests/test_verify.py:1.3	Thu May  1 15:35:44 2003
+++ Zope3/src/zope/interface/tests/test_verify.py	Sun Jun 22 10:23:45 2003
@@ -18,7 +18,7 @@
 """
 
 
-from zope.interface import Interface
+from zope.interface import Interface, implements, classImplements
 from zope.interface.verify import verifyClass, verifyObject
 from zope.interface.exceptions import DoesNotImplement, BrokenImplementation
 from zope.interface.exceptions import BrokenMethodImplementation
@@ -35,7 +35,7 @@
 
         self.assertRaises(DoesNotImplement, verifyClass, I, C)
 
-        C.__implements__=I
+        classImplements(C, I)
 
         verifyClass(I, C)
 
@@ -45,8 +45,7 @@
             def f(): pass
 
         class C:
-
-            __implements__=I
+            implements(I)
 
         self.assertRaises(BrokenImplementation, verifyClass, I, C)
 
@@ -65,7 +64,7 @@
 
         class C:
 
-            __implements__=I
+            implements(I)
 
         self.assertRaises(BrokenImplementation, verifyClass, I, C)
 
@@ -82,7 +81,7 @@
 
             def f(self, b): pass
 
-            __implements__=I
+            implements(I)
 
         # We no longer require names to match.
         #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
@@ -116,7 +115,7 @@
 
             def f(self, a, b): pass
 
-            __implements__=I
+            implements(I)
 
         self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
 
@@ -137,7 +136,7 @@
 
             def f(self, a): pass
 
-            __implements__=I
+            implements(I)
 
         self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
 
@@ -154,7 +153,7 @@
 
             def f(self, a): pass
 
-            __implements__=I
+            implements(I)
 
         self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)
 

=== Removed File Zope3/src/zope/interface/tests/test_flatten.py ===

=== Removed File Zope3/src/zope/interface/tests/test_implements.py ===

=== Removed File Zope3/src/zope/interface/tests/test_visitimplements.py ===




More information about the Zodb-checkins mailing list