U
    
W[e                     @   s  d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZmZmZmZmZ G d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd de!e"Z#e#d e!fd!d!d"Z$G d#d$ d$e!Z%G d%d& d&eZ&G d'd( d(eZ'G d)d* d*eZ(G d+d, d,eZ)G d-d. d.ejZ*G d/d0 d0ejZ+dS )1z6
Test cases for the L{twisted.python.reflect} module.
    )divisionabsolute_importN)deque)_PY3)unittest)SynchronousTestCase)reflect)accumulateMethodsprefixedMethodsprefixedMethodNamesaddMethodNamesToDictfullyQualifiedNamec                   @   s   e Zd ZdZdd ZdS )Basez`
    A no-op class which can be used to verify the behavior of
    method-discovering APIs.
    c                 C   s   dS )z9
        A no-op method which can be discovered.
        N selfr   r   ;/usr/lib/python3/dist-packages/twisted/test/test_reflect.pymethod   s    zBase.methodN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )SubzF
    A subclass of a class with a method which can be discovered.
    N)r   r   r   r   r   r   r   r   r   $   s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )Separatez=
    A no-op class with methods with differing prefixes.
    c                 C   s   dS )zJ
        A no-op method which a matching prefix to be discovered.
        Nr   r   r   r   r   good_method0   s    zSeparate.good_methodc                 C   s   dS )zO
        A no-op method with a mismatched prefix to not be discovered.
        Nr   r   r   r   r   
bad_method6   s    zSeparate.bad_methodN)r   r   r   r   r   r   r   r   r   r   r   +   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AccumulateMethodsTestszt
    Tests for L{accumulateMethods} which finds methods on a class hierarchy and
    adds them to a dictionary.
    c                 C   s*   t  }i }t|| | d|ji| dS )z
        If x is and instance of Base and Base defines a method named method,
        L{accumulateMethods} adds an item to the given dictionary with
        C{"method"} as the key and a bound method object for Base.method value.
        r   N)r   r	   assertEqualr   r   xoutputr   r   r   test_ownClassC   s    
z$AccumulateMethodsTests.test_ownClassc                 C   s*   t  }i }t|| | d|ji| dS )a  
        If x is an instance of Sub and Sub is a subclass of Base and Base
        defines a method named method, L{accumulateMethods} adds an item to the
        given dictionary with C{"method"} as the key and a bound method object
        for Base.method as the value.
        r   N)r   r	   r   r   r   r   r   r   test_baseClassO   s    
z%AccumulateMethodsTests.test_baseClassc                 C   s,   t  }i }t||d | d|ji| dS )z
        If a prefix is given, L{accumulateMethods} limits its results to
        methods beginning with that prefix.  Keys in the resulting dictionary
        also have the prefix removed from them.
        good_r   N)r   r	   r   r   r   r   r   r   test_prefix\   s    z"AccumulateMethodsTests.test_prefixN)r   r   r   r   r!   r"   r$   r   r   r   r   r   =   s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )PrefixedMethodsTestszr
    Tests for L{prefixedMethods} which finds methods on a class hierarchy and
    adds them to a dictionary.
    c                 C   s"   t  }t|}| |jg| dS )zc
        L{prefixedMethods} returns a list of the methods discovered on an
        object.
        N)r   r
   r   r   r   r   r   r   test_onlyObjecto   s    z$PrefixedMethodsTests.test_onlyObjectc                 C   s$   t  }t|d}| |jg| dS )zo
        If a prefix is given, L{prefixedMethods} returns only methods named
        with that prefix.
        r#   N)r   r
   r   r   r   r   r   r   r$   y   s    
z PrefixedMethodsTests.test_prefixN)r   r   r   r   r&   r$   r   r   r   r   r%   i   s   
r%   c                   @   s    e Zd ZdZdd Zdd ZdS )PrefixedMethodNamesTestsz+
    Tests for L{prefixedMethodNames}.
    c                 C   s   |  dgttd dS )z
        L{prefixedMethodNames} returns a list including methods with the given
        prefix defined on the class passed to it.
        r   r#   N)r   r   r   r   r   r   r   test_method   s    z$PrefixedMethodNamesTests.test_methodc                 C   s(   G dd dt }| dgt|d dS )z
        L{prefixedMethodNames} returns a list included methods with the given
        prefix defined on base classes of the class passed to it.
        c                   @   s   e Zd ZdS )z<PrefixedMethodNamesTests.test_inheritedMethod.<locals>.ChildNr   r   r   r   r   r   r   Child   s   r*   r   r#   N)r   r   r   )r   r*   r   r   r   test_inheritedMethod   s    z-PrefixedMethodNamesTests.test_inheritedMethodN)r   r   r   r   r(   r+   r   r   r   r   r'      s   r'   c                   @   s   e Zd ZdZdd ZdS )AddMethodNamesToDictTestsz,
    Tests for L{addMethodNamesToDict}.
    c                 C   sH   G dd dt }G dd dt|}i }t||d| | ddi| dS )	z
        If C{baseClass} is passed to L{addMethodNamesToDict}, only methods which
        are a subclass of C{baseClass} are added to the result dictionary.
        c                   @   s   e Zd ZdS )z;AddMethodNamesToDictTests.test_baseClass.<locals>.AlternateNr)   r   r   r   r   	Alternate   s   r-   c                   @   s   e Zd Zdd ZdS )z7AddMethodNamesToDictTests.test_baseClass.<locals>.Childc                 S   s   d S Nr   r   r   r   r   good_alternate   s    zFAddMethodNamesToDictTests.test_baseClass.<locals>.Child.good_alternateN)r   r   r   r/   r   r   r   r   r*      s   r*   r#   Z	alternate   N)objectr   r   r   )r   r-   r*   resultr   r   r   r"      s
    z(AddMethodNamesToDictTests.test_baseClassN)r   r   r   r   r"   r   r   r   r   r,      s   r,   c                   @   s   e Zd ZdZdd ZdS )Summerz9
    A class we look up as part of the LookupsTests.
    c                 C   s   dS )z
        Do something.
        Nr   r   r   r   r   	reallySet   s    zSummer.reallySetN)r   r   r   r   r4   r   r   r   r   r3      s   r3   c                   @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )LookupsTestszC
    Tests for L{namedClass}, L{namedModule}, and L{namedAny}.
    c                 C   s   |  tdt dS )zY
        L{namedClass} should return the class object for the name it is passed.
         twisted.test.test_reflect.SummerN)assertIsr   Z
namedClassr3   r   r   r   r   test_namedClassLookup   s    z"LookupsTests.test_namedClassLookupc                 C   s"   ddl m} | td| dS )zc
        L{namedModule} should return the module object for the name it is
        passed.
        r   monkeytwisted.python.monkeyN)twisted.pythonr:   r7   r   ZnamedModuler   r:   r   r   r   test_namedModuleLookup   s
     z#LookupsTests.test_namedModuleLookupc                 C   s    ddl }| td|j dS )zY
        L{namedAny} should return the package object for the name it is passed.
        r   Ntwisted.python)r<   r7   r   namedAnypythonr   twistedr   r   r   test_namedAnyPackageLookup   s
     z'LookupsTests.test_namedAnyPackageLookupc                 C   s"   ddl m} | td| dS )zX
        L{namedAny} should return the module object for the name it is passed.
        r   r9   r;   N)r<   r:   r7   r   r@   r=   r   r   r   test_namedAnyModuleLookup   s
     z&LookupsTests.test_namedAnyModuleLookupc                 C   s   |  tdt dS )zW
        L{namedAny} should return the class object for the name it is passed.
        r6   N)r7   r   r@   r3   r   r   r   r   test_namedAnyClassLookup   s    z%LookupsTests.test_namedAnyClassLookupc                 C   s   |  tdtj dS )z
        L{namedAny} should return the object an attribute of a non-module,
        non-package object is bound to for the name it is passed.
        z*twisted.test.test_reflect.Summer.reallySetN)r   r   r@   r3   r4   r   r   r   r   test_namedAnyAttributeLookup   s    z)LookupsTests.test_namedAnyAttributeLookupc                 C   s   |  tdtjj dS )z
        L{namedAny} should return the object an attribute of an object which
        itself was an attribute of a non-module, non-package object is bound to
        for the name it is passed.
        z2twisted.test.test_reflect.Summer.reallySet.__doc__N)r7   r   r@   r3   r4   r   r   r   r   r   "test_namedAnySecondAttributeLookup   s    z/LookupsTests.test_namedAnySecondAttributeLookupc                 C   sD   |  ttjd |  ttjd |  ttjd |  ttjd dS )z
        Exceptions raised by modules which L{namedAny} causes to be imported
        should pass through L{namedAny} to the caller.
        ztwisted.test.reflect_helper_ZDEztwisted.test.reflect_helper_VEztwisted.test.reflect_helper_IEN)assertRaisesZeroDivisionErrorr   r@   
ValueErrorImportErrorr   r   r   r   test_importExceptions  s(        z"LookupsTests.test_importExceptionsc                 C   s4   |  ttjd |  ttjd |  ttjd dS )z
        If segments on the end of a fully-qualified Python name represents
        attributes which aren't actually present on the object represented by
        the earlier segments, L{namedAny} should raise an L{AttributeError}.
        ztwisted.nosuchmoduleintheworldz twisted.nosuch.modulein.theworldz0twisted.test.test_reflect.Summer.nosuchattributeN)rI   AttributeErrorr   r@   r   r   r   r   test_attributeExceptions!  s      z%LookupsTests.test_attributeExceptionsc                 C   s   |  tjtjd}| t|d |  tjtjd}| t|d |  tjtjd}| t|d |  tjtjd}| t|d d	D ],}|  tjtj|}| t|d
|f  qdS )a  
        Passing a name which isn't a fully-qualified Python name to L{namedAny}
        should result in one of the following exceptions:
         - L{InvalidName}: the name is not a dot-separated list of Python
           objects
         - L{ObjectNotFound}: the object doesn't exist
         - L{ModuleNotFound}: the object doesn't exist and there is only one
           component in the name
        Znosuchmoduleintheworldz(No module named 'nosuchmoduleintheworld'z@#$@(#.!@(#!@#z('@#$@(#.!@(#!@#' does not name an objectztcelfer.nohtyp.detsiwtz0'tcelfer.nohtyp.detsiwt' does not name an object zEmpty module name)z.twistedztwisted.ztwisted..pythonzOname must be a string giving a '.'-separated list of Python identifiers, not %rN)rI   r   ZModuleNotFoundr@   r   strZObjectNotFoundZInvalidName)r   errZinvalidNamer   r   r   test_invalidNames6  s:    
  zLookupsTests.test_invalidNamesc                 C   s.   dD ]$}t  }tj||d}| || qdS )zk
        When module import fails with ImportError it returns the specified
        default value.
        )Znosuchmtopoduleno.such.moduledefaultN)r1   r   requireModuler7   )r   namerV   r2   r   r   r   test_requireModuleImportError[  s    z*LookupsTests.test_requireModuleImportErrorc                 C   s   t d}| | dS )zI
        When module import fails it returns L{None} by default.
        rT   N)r   rW   ZassertIsNone)r   r2   r   r   r   test_requireModuleDefaultNoneh  s    
z*LookupsTests.test_requireModuleDefaultNonec                 C   s,   ddl m} t }| tjd|d| dS )ze
        When module import succeed it returns the module and not the default
        value.
        r   r9   r;   rU   N)r<   r:   r1   r7   r   rW   )r   r:   rV   r   r   r   !test_requireModuleRequestedImportq  s    z.LookupsTests.test_requireModuleRequestedImportN)r   r   r   r   r8   r>   rD   rE   rF   rG   rH   rM   rO   rS   rY   rZ   r[   r   r   r   r   r5      s   	
			%	r5   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )	BreakableFc                 C   s   | j rtdndS d S )Nzstr!z<Breakable>)breakStrRuntimeErrorr   r   r   r   __str__  s    
zBreakable.__str__c                 C   s   | j rtdndS d S )Nzrepr!zBreakable())	breakReprr^   r   r   r   r   __repr__  s    
zBreakable.__repr__N)r   r   r   r`   r]   r_   ra   r   r   r   r   r\     s   r\   c                   @   s    e Zd ZdZdd ZeeZ dS )
BrokenTypeFc                 C   s   | j rtddS )Nzno namerb   )	breakNamer^   r   r   r   r   get___name__  s    zBrokenType.get___name__N)r   r   r   rc   rd   propertyr   r   r   r   rb     s   rb   BTBaseT)r`   r]   c                   @   s   e Zd Zedd ZdS )NoClassAttrc                 C   s   | j S r.   )Z	not_class)r   r   r   r   <lambda>      zNoClassAttr.<lambda>N)r   r   r   re   	__class__r   r   r   r   rg     s   rg   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )SafeReprTestsz2
    Tests for L{reflect.safe_repr} function.
    c                 C   s4   dddgdf}|  tttj|ttt| dS )zg
        L{reflect.safe_repr} produces the same output as C{repr} on a working
        object.
        r0            aN)r   listmapr   	safe_reprrepr)r   Zxsr   r   r   test_workingRepr  s    zSafeReprTests.test_workingReprc                 C   sJ   t  }d|_t|}| d| | tjtd | | d| dS )z
        L{reflect.safe_repr} returns a string with class name, address, and
        traceback when the repr call failed.
        TzBreakable instance at 0xr   RuntimeError: repr!N)	r\   r`   r   rq   assertInospathsplitext__file__r   bZbReprr   r   r   test_brokenRepr  s    
zSafeReprTests.test_brokenReprc                 C   s&   t  }d|_| t|t| dS )zT
        L{reflect.safe_repr} isn't affected by a broken C{__str__} method.
        TN)r\   r]   r   r   rq   rr   r   r{   r   r   r   test_brokenStr  s    zSafeReprTests.test_brokenStrc                 C   s*   G dd dt }t| t|  d S )Nc                   @   s   e Zd ZdZdS )z-SafeReprTests.test_brokenClassRepr.<locals>.XTNr   r   r   r`   r   r   r   r   X  s   r   rf   r   rq   r   r   r   r   r   test_brokenClassRepr  s    
z"SafeReprTests.test_brokenClassReprc                 C   sB   G dd dt }t|}dt|f }| ||dd  dS )z
        C{id} is used to print the ID of the object in case of an error.

        L{safe_repr} includes a traceback after a newline, so we only check
        against the first line of the repr.
        c                   @   s   e Zd ZdZdS )z2SafeReprTests.test_brokenReprIncludesID.<locals>.XTNr   r   r   r   r   r     s   r   z-<BrokenType instance at 0x%x with repr error:
r   N)rf   r   rq   idr   split)r   r   xReprZxReprExpectedr   r   r   test_brokenReprIncludesID  s    
z'SafeReprTests.test_brokenReprIncludesIDc                 C   s*   G dd dt }t| t|  d S )Nc                   @   s   e Zd ZdZdS )z,SafeReprTests.test_brokenClassStr.<locals>.XTNr   r   r   r]   r   r   r   r   r     s   r   r   r   r   r   r   test_brokenClassStr  s    
z!SafeReprTests.test_brokenClassStrc                 C   sJ   t  }d|_t|}| d| | tjtd | | d| dS )z
        If an object raises an exception when accessing its C{__class__}
        attribute, L{reflect.safe_repr} uses C{type} to retrieve the class
        object.
        TNoClassAttr instance at 0xr   rt   N)	rg   r`   r   rq   ru   rv   rw   rx   ry   rz   r   r   r   test_brokenClassAttribute  s    
z'SafeReprTests.test_brokenClassAttributec                 C   sP   G dd dt }t| }| d| | tjtd | | d| dS )z
        If a class raises an exception when accessing its C{__name__} attribute
        B{and} when calling its C{__str__} implementation, L{reflect.safe_repr}
        returns 'BROKEN CLASS' instead of the class name.
        c                   @   s   e Zd ZdZdS )z6SafeReprTests.test_brokenClassNameAttribute.<locals>.XTNr   r   r   rc   r   r   r   r   r     s   r   <BROKEN CLASS AT 0xr   rt   N)rf   r   rq   ru   rv   rw   rx   ry   )r   r   r   r   r   r   test_brokenClassNameAttribute  s
    z+SafeReprTests.test_brokenClassNameAttributeN)r   r   r   r   rs   r|   r~   r   r   r   r   r   r   r   r   r   rk     s   		rk   c                   @   sz   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	r@de_
nde_
dd Zdd Zdd Zdd Zdd Zdd ZdS )SafeStrTestsz1
    Tests for L{reflect.safe_str} function.
    c                 C   s$   dddg}|  t|t| d S )Nr0   rl   rm   )r   r   safe_strrQ   r   r   r   r   r   test_workingStr  s    
zSafeStrTests.test_workingStrc                 C   s   t  }d|_t| d S NT)r\   r]   r   r   r}   r   r   r   r~     s    zSafeStrTests.test_brokenStrc                 C   s   d}|  t|d dS )zh
        L{safe_str} for C{str} with ascii-only data should return the
        value unchanged.
        aNr   r   r   r   r   r   r   test_workingAscii  s    zSafeStrTests.test_workingAsciic                 C   s   d}|  t|| dS )zk
        L{safe_str} for C{str} with utf-8 encoded data should return the
        value unchanged.
           tüstNr   r   r   r   r   test_workingUtf8_2$  s    zSafeStrTests.test_workingUtf8_2c                 C   s    d}|  t||d dS )zw
        L{safe_str} for C{bytes} with utf-8 encoded data should return
        the value decoded into C{str}.
        r   utf-8N)r   r   r   decoder   r   r   r   test_workingUtf8_3-  s    zSafeStrTests.test_workingUtf8_3z)Skip Python 2 specific test for utf-8 strz+Skip Python 3 specific test for utf-8 bytesc                 C   s"   d}t |}| |t| dS )z=
        Use str() for non-utf8 bytes: "b'non-utf8'"
           N)r   r   r   rQ   )r   r   xStrr   r   r   test_brokenUtf8@  s    
zSafeStrTests.test_brokenUtf8c                 C   s   t  }d|_t| d S r   )r\   r`   r   r   r}   r   r   r   r|   I  s    zSafeStrTests.test_brokenReprc                 C   s*   G dd dt }t| t|  d S )Nc                   @   s   e Zd ZdZdS )z+SafeStrTests.test_brokenClassStr.<locals>.XTNr   r   r   r   r   r   P  s   r   rf   r   r   r   r   r   r   r   O  s    
z SafeStrTests.test_brokenClassStrc                 C   s*   G dd dt }t| t|  d S )Nc                   @   s   e Zd ZdZdS )z,SafeStrTests.test_brokenClassRepr.<locals>.XTNr   r   r   r   r   r   W  s   r   r   r   r   r   r   r   V  s    
z!SafeStrTests.test_brokenClassReprc                 C   sJ   t  }d|_t|}| d| | tjtd | | d| dS )z
        If an object raises an exception when accessing its C{__class__}
        attribute, L{reflect.safe_str} uses C{type} to retrieve the class
        object.
        Tr   r   RuntimeError: str!N)	rg   r]   r   r   ru   rv   rw   rx   ry   )r   r{   ZbStrr   r   r   r   ]  s    
z&SafeStrTests.test_brokenClassAttributec                 C   sP   G dd dt }t| }| d| | tjtd | | d| dS )z
        If a class raises an exception when accessing its C{__name__} attribute
        B{and} when calling its C{__str__} implementation, L{reflect.safe_str}
        returns 'BROKEN CLASS' instead of the class name.
        c                   @   s   e Zd ZdZdS )z5SafeStrTests.test_brokenClassNameAttribute.<locals>.XTNr   r   r   r   r   r   q  s   r   r   r   r   N)rf   r   r   ru   rv   rw   rx   ry   )r   r   r   r   r   r   r   k  s
    z*SafeStrTests.test_brokenClassNameAttributeN)r   r   r   r   r   r~   r   r   r   r   skipr   r|   r   r   r   r   r   r   r   r   r     s    			r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )FilenameToModuleTestsz1
    Test L{filenameToModuleName} detection.
    c              	   C   s   t j|  dd| _t | j tt j| jdd}|d W 5 Q R X tt jt j| jdd}|d W 5 Q R X d S )NZfakepackageZtestz__init__.pywrP   )rv   rw   joinZmktempmakedirsopenwritedirname)r   fr   r   r   setUp  s    zFilenameToModuleTests.setUpc                 C   s<   t | j}| |d t | jtjj }| |d dS )zk
        L{filenameToModuleName} returns the correct module (a package) given a
        directory.
        zfakepackage.testN)r   filenameToModuleNamerw   r   rv   sepr   moduler   r   r   test_directory  s    z$FilenameToModuleTests.test_directoryc                 C   s&   t tj| jd}| |d dS )zh
        L{filenameToModuleName} returns the correct module given the path to
        its file.
        ztest_reflect.pyfakepackage.test.test_reflectN)r   r   rv   rw   r   r   r   r   r   r   	test_file  s    zFilenameToModuleTests.test_filec                 C   s,   t tj| jdd}| |d dS )zo
        L{filenameToModuleName} returns the correct module given a C{bytes}
        path to its file.
        r   s   test_reflect.pyr   N)r   r   rv   rw   r   encoder   r   r   r   r   
test_bytes  s    z FilenameToModuleTests.test_bytesN)r   r   r   r   r   r   r   r   r   r   r   r   r   z  s
   

r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )FullyQualifiedNameTestsz)
    Test for L{fullyQualifiedName}.
    c                 C   s   |  t|| dS )ze
        Helper to check that fully qualified name of C{obj} results to
        C{expected}.
        N)r   r   )r   objZexpectedr   r   r   _checkFullyQualifiedName  s    z0FullyQualifiedNameTests._checkFullyQualifiedNamec                 C   s.   ddl }| |d ddl}| |jd dS )zd
        L{fullyQualifiedName} returns the full name of a package and a
        subpackage.
        r   NrC   r?   )rC   r   r<   rA   rB   r   r   r   test_package  s    z$FullyQualifiedNameTests.test_packagec                 C   s   ddl }| |jjd dS )zV
        L{fullyQualifiedName} returns the name of a module inside a package.
        r   Nztwisted.python.compat)twisted.python.compatr   rA   compatrB   r   r   r   test_module  s
     z#FullyQualifiedNameTests.test_modulec                 C   s   |  tdtf  dS )zS
        L{fullyQualifiedName} returns the name of a class and its module.
        z%s.FullyQualifiedNameTestsN)r   r   r   r   r   r   r   
test_class  s    z"FullyQualifiedNameTests.test_classc                 C   s   |  td dS )zY
        L{fullyQualifiedName} returns the name of a function inside its module.
        z)twisted.python.reflect.fullyQualifiedNameN)r   r   r   r   r   r   test_function  s     z%FullyQualifiedNameTests.test_functionc                 C   s   |  | jdt| jjf  dS )zs
        L{fullyQualifiedName} returns the name of a bound method inside its
        class and its module.
        z%s.%s.test_boundMethodN)r   test_boundMethodr   rj   r   r   r   r   r     s    z(FullyQualifiedNameTests.test_boundMethodc                 C   s    |  | jjdt| jjf  dS )zv
        L{fullyQualifiedName} returns the name of an unbound method inside its
        class and its module.
        z%s.%s.test_unboundMethodN)r   rj   test_unboundMethodr   r   r   r   r   r     s    z*FullyQualifiedNameTests.test_unboundMethodN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   		
r   c                   @   s\   e Zd ZerdZdd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd ZdS )ObjectGrepTestsz=twisted.python.reflect.objgrep hasn't been ported to Python 3c                 C   sJ   t  }d|i}|di}| dt||tj | dt||tj dS )zV
        Test references search through a dictionary, as a key or as a value.
        Nz[None]z{None}r1   ru   r   objgrepisSame)r   oZd1Zd2r   r   r   test_dictionary  s
    zObjectGrepTests.test_dictionaryc                 C   s*   t  }d|g}| dt||tj dS )z8
        Test references search through a list.
        N[1]r   )r   r   Lr   r   r   	test_list  s    zObjectGrepTests.test_listc                 C   s*   t  }|df}| dt||tj dS )z9
        Test references search through a tuple.
        N[0]r   )r   r   Tr   r   r   
test_tuple
  s    zObjectGrepTests.test_tuplec                 C   s<   G dd d}t  }| }||_| dt||tj dS )zE
        Test references search through an object attribute.
        c                   @   s   e Zd ZdS )z,ObjectGrepTests.test_instance.<locals>.DummyNr)   r   r   r   r   Dummy  s   r   z.oN)r1   r   ru   r   r   r   )r   r   r   dr   r   r   test_instance  s
    zObjectGrepTests.test_instancec                 C   s:   G dd d}| }t |}| dt||tj dS )zB
        Test references search through a weakref object.
        c                   @   s   e Zd ZdS )z+ObjectGrepTests.test_weakref.<locals>.DummyNr)   r   r   r   r   r   #  s   r   z()N)weakrefrefru   r   r   r   )r   r   r   Zw1r   r   r   test_weakref  s    
zObjectGrepTests.test_weakrefc                 C   sn   G dd d}| }|j }| dt||jtj | dt||jjtj | dt||jtj dS )zK
        Test references search through method special attributes.
        c                   @   s   e Zd Zdd ZdS )z/ObjectGrepTests.test_boundMethod.<locals>.Dummyc                 S   s   d S r.   r   r   r   r   r   dummy/  s    z5ObjectGrepTests.test_boundMethod.<locals>.Dummy.dummyN)r   r   r   r   r   r   r   r   r   .  s   r   z	.__self__z.__self__.__class__z	.__func__N)r   ru   r   r   __self__r   rj   __func__)r   r   r   mr   r   r   r   *  s    z ObjectGrepTests.test_boundMethodc           
      C   s   G dd d}| }dddd|di}dd|dg}|i | f}d	d
|d}| }||_ |j}t|}	| dt|	|tj dS )zF
        Test references search using complex set of objects.
        c                   @   s   e Zd Zdd ZdS )z.ObjectGrepTests.test_everything.<locals>.Dummyc                 S   s   d S r.   r   r   r   r   r   r   @  s    z5ObjectGrepTests.test_everything.<locals>.Dummy.methodN)r   r   r   r   r   r   r   r   r   ?  s   r   r   ZbazNZQuuxZFooshrm   ZfooZbar)r   r0   rl   z"().__self__.attr[2][0][2]{'Foosh'})attrr   r   r   ru   r   r   r   )
r   r   r   ZD1r   r   ZD2ir   r   r   r   r   test_everything;  s    
zObjectGrepTests.test_everythingc              	   C   s~   g }|g}||g}||g}|  dgtj||tjdd |  ddgtj||tjdd |  dddgtj||tjdd dS )	z6
        Test the depth of references search.
        r   r0   )ZmaxDepthz[1][0]rl   z	[1][1][0]rm   N)r   r   r   r   )r   r   r{   cr   r   r   r   test_depthLimitP  s     zObjectGrepTests.test_depthLimitc                 C   s<   t  }t }|d || | dt||tj dS )z@
        Test references search through a deque object.
        Nr   )r1   r   appendru   r   r   r   )r   r   Dr   r   r   
test_deque^  s
    

zObjectGrepTests.test_dequeN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   		r   c                   @   s0   e Zd ZerdgZnddgZdd Zdd ZdS )	GetClassTeststypeclassZclassobjc                 C   sB   G dd d}| }|  t|j| j | t|jd d S )Nc                   @   s   e Zd ZdS )z(GetClassTests.test_old.<locals>.OldClassNr)   r   r   r   r   OldClassq  s   r   )ru   r   getClassr   oldClassNamesr   )r   r   oldr   r   r   test_oldp  s    zGetClassTests.test_oldc                 C   sB   G dd dt }| }| t|jd | t|jd d S )Nc                   @   s   e Zd ZdS )z(GetClassTests.test_new.<locals>.NewClassNr)   r   r   r   r   NewClassx  s   r   r   )r1   r   r   r   r   )r   r   newr   r   r   test_neww  s    zGetClassTests.test_newN)r   r   r   r   r   r   r   r   r   r   r   r   j  s
   r   ),r   Z
__future__r   r   rv   r   collectionsr   r   r   Ztwisted.trialr   Ztwisted.trial.unittestr   ZTestCaser<   r   Ztwisted.python.reflectr	   r
   r   r   r   r1   r   r   r   r   r%   r'   r,   r3   r5   r\   r   rb   rf   rg   rk   r   r   r   r   r   r   r   r   r   <module>   sB   , Dao0Fz