ó
ÔËÞKc           @   s  d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z m Z d d l m Z m	 Z	 m
 Z
 d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d e j f d  „  ƒ  YZ d! „  Z d" „  Z d# „  Z d$ „  Z d% „  Z  d& „  Z! d' „  Z" d( „  Z# d) „  Z$ d* „  Z% d+ „  Z& d, „  Z' d S(-   s   Test the new API for making and checking interface declarations

$Id: test_declarations.py 110827 2010-04-13 20:33:31Z tseaver $
iÿÿÿÿN(   t	   Interfacet
   implements(   t   directlyProvidest
   providedBy(   t   classImplementst   implementedByt   implementsOnlyt   I1c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR      s    t   I2c           B   s   e  Z RS(    (   R   R	   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR
      s    t   I3c           B   s   e  Z RS(    (   R   R	   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR      s    t   I4c           B   s   e  Z RS(    (   R   R	   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR      s    t   I5c           B   s   e  Z RS(    (   R   R	   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR      s    t   Ac           B   s   e  Z e e ƒ RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR      s   t   Bc           B   s   e  Z e e ƒ RS(    (   R   R	   R   R
   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   !   s   t   Cc           B   s   e  Z e e ƒ RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   #   s   t   COnlyc           B   s   e  Z e e ƒ RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   &   s   t	   COnly_oldc           B   s   e  Z e Z RS(    (   R   R	   R   t   __implemented__(    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   )   s   t   Dc           B   s   e  Z e e ƒ RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   ,   s   c           C   s   d S(   s~   
    >>> c = C()
    >>> directlyProvides(c, I4)
    >>> [i.__name__ for i in providedBy(c)]
    ['I4', 'I3', 'I1', 'I2']
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_ObjectSpecification_Simple/   s    c           C   s   d S(   sv   
    >>> c = COnly()
    >>> directlyProvides(c, I4)
    >>> [i.__name__ for i in providedBy(c)]
    ['I4', 'I3']
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt&   test_ObjectSpecification_Simple_w_only7   s    c           C   s   d S(   sz   
    >>> c = COnly_old()
    >>> directlyProvides(c, I4)
    >>> [i.__name__ for i in providedBy(c)]
    ['I4', 'I3']
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt)   test_ObjectSpecification_Simple_old_style?   s    t   Testc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c            sz  d t  f d „  ƒ  Y} d | f d „  ƒ  Y‰  d ˆ  f ‡  f d †  ƒ  Y} |  j | j j t k ƒ |  j g  t | ƒ  ƒ D] } | j ƒ  ^ qw d d d	 g ƒ d
 | f d „  ƒ  Y} |  j g  t | ƒ  ƒ D] } | j ƒ  ^ qÈ d d d d	 g ƒ |  j g  | j D] } | j ƒ  ^ q d d d d	 g ƒ |  j g  | j D] } | j ƒ  ^ q8d d d	 g ƒ |  j | j j t k ƒ d  S(   Nt   C1c           B   s   e  Z e Z RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   O   s    t   C2c           B   s   e  Z e e f Z RS(    (   R   R	   R
   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   P   s    t   C3c              s   e  Z e ˆ  j f Z RS(    (   R   R	   R   R   (    (   R   (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   Q   s    R   R
   R   t   C4c           B   s   e  Z e e ƒ RS(    (   R   R	   R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   Z   s   R   (	   t   objectt   assert_R   t	   __class__t   tuplet   assertEqualR   t   getNamet   failIf(   t   selfR   R   t   iR   (    (   R   sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_backward_compatM   s$    %%c         C   sX   d d l  m } m } t | | j | j ƒ |  j t t | ƒ ƒ t t | ƒ ƒ ƒ d  S(   Niÿÿÿÿ(   t   m1t   m2(	   t   zope.interface.testsR'   R(   R   R   R
   R!   t   listR   (   R$   R'   R(   (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_modulen   s    
c         C   s  t  t ƒ } | j } t t t ƒ d t f d „  ƒ  Y} d } |  j g  t | ƒ D] } | j ƒ  ^ qQ d g ƒ | d ƒ } t | t	 ƒ |  j g  t | ƒ D] } | j ƒ  ^ qœ d d d g ƒ | | _ t t ƒ d } |  j g  t | ƒ D] } | j ƒ  ^ qí g  ƒ d  S(   Nt   myintc           B   s   e  Z e e ƒ RS(    (   R   R	   R   R
   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR,   €   s   i*   R   R   R
   (
   R   t   intt   declaredR   R   R!   R   R"   R   R   (   R$   t   intspect   olddeclaredR,   t   xR%   (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_builtinsy   s     	(
(	
((   R   R	   R&   R+   R2   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyR   H   s   	!	c           C   s   d S(   s  
    >>> from zope.interface import *
    >>> class C(object):
    ...     pass
    >>> c = C()
    >>> list(providedBy(c))
    []

    >>> class I(Interface):
    ...    pass
    >>> directlyProvides(c, I)
    >>> list(providedBy(c))  == list(directlyProvidedBy(c))
    1
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt$   test_signature_w_no_class_interfaces•   s    c           C   s   d S(   sÂ  This test is in response to a bug found, which is why it's a bit
    contrived

    >>> from zope.interface import *
    >>> class B1(object):
    ...     pass
    >>> class B2(B1):
    ...     pass
    >>> class B3(B2):
    ...     pass
    >>> class D(object):
    ...     implements()
    >>> class S(B3, D):
    ...     implements()

    This failed due to a bug in the code for finding __providedBy__
    descriptors for old-style classes.

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt,   test_classImplement_on_deeply_nested_classes¥   s    c           C   s   d S(   sÙ   
    >>> from pickle import dumps, loads
    >>> a = A()
    >>> I2.providedBy(a)
    0
    >>> directlyProvides(a, I2)
    >>> I2.providedBy(a)
    1
    >>> a2 = loads(dumps(a))
    >>> I2.providedBy(a2)
    1

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_pickle_provides_specsº   s    c           C   s   d S(   s2  
    >>> from zope.interface import classProvides
    >>> class X(object):
    ...     classProvides(I1)
    >>> class Y(X):
    ...     pass
    >>> [i.__name__ for i in X.__provides__]
    ['I1']
    >>> Y.__provides__
    Traceback (most recent call last):
    ...
    AttributeError: __provides__

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt(   test_that_we_dont_inherit_class_providesÉ   s    c           C   s   d S(   s½  

    When we make a declaration for a class, we install a __provides__
    descriptors that provides a default for instances that don't have
    instance-specific declarations:

    >>> class A(object):
    ...     implements(I1)

    >>> class B(object):
    ...     implements(I2)

    >>> [i.__name__ for i in A().__provides__]
    ['I1']
    >>> [i.__name__ for i in B().__provides__]
    ['I2']

    But it's important that we don't use this for subclasses without
    declarations.  This would cause incorrect results:

    >>> class X(A, B):
    ...     pass

    >>> X().__provides__
    Traceback (most recent call last):
    ...
    AttributeError: __provides__

    However, if we "induce" a declaration, by calling implementedBy
    (even indirectly through providedBy):

    >>> [i.__name__ for i in providedBy(X())]
    ['I1', 'I2']


    then the optimization will work:

    >>> [i.__name__ for i in X().__provides__]
    ['I1', 'I2']

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt0   test_that_we_dont_inherit_provides_optimizationsÙ   s    c           C   s   d S(   s©  Special descriptor for class __provides__

    The descriptor caches the implementedBy info, so that
    we can get declarations for objects without instance-specific
    interfaces a bit quicker.

        For example::

          >>> from zope.interface import Interface, classProvides
          >>> class IFooFactory(Interface):
          ...     pass
          >>> class IFoo(Interface):
          ...     pass
          >>> class C(object):
          ...     classProvides(IFooFactory)
          ...     implements(IFoo)
          >>> [i.getName() for i in C.__provides__]
          ['IFooFactory']

          >>> [i.getName() for i in C().__provides__]
          ['IFoo']
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt$   test_classProvides_before_implements  s    c           C   s   d S(   s_  

    In general, we should be able to get a spec
    for a proxied class if someone has declared or
    asked for a spec before.

    We don't want to depend on proxies in this (zope.interface)
    package, but we do want to work with proxies.  Proxies have the
    effect that a class's __dict__ cannot be gotten. Further, for
    built-in classes, we can't save, and thus, cannot get, any class
    attributes.  We'll emulate this by treating a plain object as a class:

      >>> cls = object()

    We'll create an implements specification:

      >>> import zope.interface.declarations
      >>> impl = zope.interface.declarations.Implements(I1, I2)

    Now, we'll emulate a declaration for a built-in type by putting
    it in BuiltinImplementationSpecifications:

      >>> zope.interface.declarations.BuiltinImplementationSpecifications[
      ...   cls] = impl

    Now, we should be able to get it back:

      >>> implementedBy(cls) is impl
      True

    Of course, we don't want to leave it there. :)

      >>> del zope.interface.declarations.BuiltinImplementationSpecifications[
      ...   cls]

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt+   test_getting_spec_for_proxied_builtin_class  s    c           C   s   d S(   s«  
    We can get definitions from a declaration:

        >>> import zope.interface
        >>> class I1(zope.interface.Interface):
        ...    a11 = zope.interface.Attribute('a11')
        ...    a12 = zope.interface.Attribute('a12')
        >>> class I2(zope.interface.Interface):
        ...    a21 = zope.interface.Attribute('a21')
        ...    a22 = zope.interface.Attribute('a22')
        ...    a12 = zope.interface.Attribute('a212')
        >>> class I11(I1):
        ...    a11 = zope.interface.Attribute('a111')

        >>> decl = zope.interface.Declaration(I11, I2)
        >>> decl.get('a11') is I11.get('a11')
        True
        >>> decl.get('a12') is I1.get('a12')
        True
        >>> decl.get('a21') is I2.get('a21')
        True
        >>> decl.get('a22') is I2.get('a22')
        True
        >>> decl.get('a')
        >>> decl.get('a', 42)
        42

    We get None even with no interfaces:

        >>> decl = zope.interface.Declaration()
        >>> decl.get('a11')
        >>> decl.get('a11', 42)
        42

    We get new data if e change interface bases:

        >>> decl.__bases__ = I11, I2
        >>> decl.get('a11') is I11.get('a11')
        True
    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_declaration_getB  s    c           C   s   d S(   sX  http://www.zope.org/Collectors/Zope3-dev/402

>>> from zope.interface import *
>>> class I1(Interface):
...     pass
>>> class I2(Interface):
...     pass
>>> class C:
...     implements(I1)
>>> class C2:
...     implementsOnly(I2)
>>> class I3(Interface):
...     pass

>>> [i.__name__ for i in providedBy(C2()).__iro__]
['I2', 'Interface']

>>> classImplements(C2, I3)
>>> [i.__name__ for i in providedBy(C2()).__iro__]
['I2', 'I3', 'Interface']

>>> class I4(Interface):
...     pass
>>> classImplements(C2, I4)
>>> [i.__name__ for i in providedBy(C2()).__iro__]
['I2', 'I3', 'I4', 'Interface']


N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt8   test_classImplements_after_classImplementsOnly_issue_402l  s    c           C   s   d S(   sç   

    Sometimes, we need to pickle implements specs.  We should be able
    to do so as long as the class is picklable.

    >>> import pickle
    >>> pickle.loads(pickle.dumps(implementedBy(C))) is implementedBy(C)
    True


    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt.   test_picklability_of_implements_specifications‹  s    c           C   s   d S(   s­  

    This is an edge case: if the __slots__ of a class contain '__provides__',
    using providedBy() on that class should still work (this occurs, for
    example, when providing an adapter for a concrete class.)

    >>> import zope.interface
    >>> class Slotted(object):
    ...     __slots__ = ('__provides__')
    >>> class IFoo(zope.interface.Interface):
    ...     pass
    >>> IFoo.providedBy(Slotted)
    False

    N(    (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   test_provided_by_with_slots˜  s    c           C   s.   t  j t  j t ƒ t j d ƒ t j ƒ  f ƒ S(   Ns   zope.interface.declarations(   t   unittestt	   TestSuitet	   makeSuiteR   t   doctestt   DocTestSuite(    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt
   test_suite©  s    ((   t   __doc__RA   R>   t   zope.interfaceR    R   R   R   R   R   R   R   R
   R   R   R   R   R   R   R   R   R   R   R   R   R   t   TestCaseR   R3   R4   R5   R6   R7   R8   R9   R:   R;   R<   R=   RC   (    (    (    sJ   /usr/lib/python2.7/dist-packages/zope/interface/tests/test_declarations.pyt   <module>   s@   				M					+		&	*			