U
    ¹~Hj”_  ã                   @   s†  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ G dd„ dƒZG d	d
„ d
eƒZG 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ƒZG dd„ deƒZG dd„ dƒ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ƒZ"G d,d-„ d-eƒZ#dS ).z6
Test cases for the L{twisted.python.reflect} module.
é    N)Údeque)Úreflect)ÚaccumulateMethodsÚaddMethodNamesToDictÚfullyQualifiedNameÚprefixedMethodNamesÚprefixedMethods)ÚSynchronousTestCasec                   @   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   ú=/tmp/pip-unpacked-wheel-ewod1tgb/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_method/   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_method4   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_ownClass@   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_baseClassK   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_prefixW   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_onlyObjecti   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    r   s    
z PrefixedMethodsTests.test_prefixN)r   r   r   r   r"   r    r   r   r   r   r!   c   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   sF   G dd„ dƒ}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)r   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   r/   r   r   r   r   r.   «   s   r.   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
namedClassr.   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.pythonr5   r2   r   ZnamedModule©r   r5   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)r7   r2   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   r4   r6   N)r7   r5   r2   r   r;   r8   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.
        r1   N)r2   r   r;   r.   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;   r.   r/   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)r2   r   r;   r.   r/   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)rD   Ú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 ¡ qŒd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)rD   r   ZModuleNotFoundr;   r   ÚstrZObjectNotFoundZInvalidName)r   ÚerrZinvalidNamer   r   r   Útest_invalidNames"  s8    
  ÿ  ÿ  ÿÿþ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)Úobjectr   ÚrequireModuler2   )r   ÚnamerQ   r-   r   r   r   Útest_requireModuleImportErrorG  s    z*LookupsTests.test_requireModuleImportErrorc                 C   s   t  d¡}|  |¡ dS )zI
        When module import fails it returns L{None} by default.
        rO   N)r   rS   ZassertIsNone)r   r-   r   r   r   Útest_requireModuleDefaultNoneS  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   r4   r6   rP   N)r7   r5   rR   r2   r   rS   )r   r5   rQ   r   r   r   Ú!test_requireModuleRequestedImport[  s    þz.LookupsTests.test_requireModuleRequestedImportN)r   r   r   r   r3   r9   r?   r@   rA   rB   rC   rH   rJ   rN   rU   rV   rW   r   r   r   r   r0   ¶   s   	%r0   c                   @   s0   e Zd ZdZdZedœdd„Zedœdd„ZdS )Ú	BreakableF)Úreturnc                 C   s   | j rtdƒ‚ndS d S )Nzstr!z<Breakable>)ÚbreakStrÚRuntimeErrorr   r   r   r   Ú__str__n  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__t  s    
zBreakable.__repr__N)r   r   r   r]   rZ   rL   r\   r^   r   r   r   r   rX   j  s   rX   c                   @   s   e Zd ZdZedd„ ƒZ dS )Ú
BrokenTypeFc                 C   s   | j rtdƒ‚dS )Nzno namer_   )Ú	breakNamer[   r   r   r   r   r   ~  s    zBrokenType.__name__N)r   r   r   r`   Úpropertyr   r   r   r   r_   {  s   r_   ÚBTBaseT)r]   rZ   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   ra   Ú	__class__r   r   r   r   rc   ˆ  s   rc   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.
        r,   é   é   ó   aN)r   ÚlistÚmapr   Ú	safe_reprÚrepr)r   Ú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)	rX   r]   r   rm   Ú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)rX   rZ   r   r   rm   rn   ©r   rx   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}   ©rb   r   rm   ©r   r}   r   r   r   Útest_brokenClassRepr¯  s    
z"SafeReprTests.test_brokenClassReprc                 C   sF   G dd„ dt ƒ}t |¡}dt|ƒd›d�}|  || 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 0xr   z with repr error:Ú
r   N)rb   r   rm   Ú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   rZ   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   rq   N)	rc   r]   r   rm   rr   rs   rt   ru   rv   rw   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   r`   r   r   r   r   r}   à  s   r}   ú<BROKEN CLASS AT 0xr   rq   N)rb   r   rm   rr   rs   rt   ru   rv   )r   r}   r„   r   r   r   Útest_brokenClassNameAttributeÙ  s
    z+SafeReprTests.test_brokenClassNameAttributeN)r   r   r   r   rp   ry   r{   r€   r…   r‡   r‰   rŒ   r   r   r   r   rg   Œ  s   rg   c                   @   s`   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S )ÚSafeStrTestsz1
    Tests for L{reflect.safe_str} function.
    c                 C   s$   dddg}|   t |¡t|ƒ¡ d S )Nr,   rh   ri   )r   r   Úsafe_strrL   ©r   r   r   r   r   Útest_workingStrî  s    
zSafeStrTests.test_workingStrc                 C   s   t ƒ }d|_t |¡ d S ©NT)rX   rZ   r   rŽ   rz   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¡¡ dS )zw
        L{safe_str} for C{bytes} with utf-8 encoded data should return
        the value decoded into C{str}.
        s   tÃ¼stúutf-8N)r   r   rŽ   Údecoder�   r   r   r   Útest_workingUtf8_3ÿ  s    zSafeStrTests.test_workingUtf8_3c                 C   s"   d}t  |¡}|  |t|ƒ¡ dS )z=
        Use str() for non-utf8 bytes: "b'non-utf8'"
        ó   ÿN)r   rŽ   r   rL   )r   r   ÚxStrr   r   r   Útest_brokenUtf8  s    
zSafeStrTests.test_brokenUtf8c                 C   s   t ƒ }d|_t |¡ d S r‘   )rX   r]   r   rŽ   rz   r   r   r   ry     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}     s   r}   ©rb   r   rŽ   r   r   r   r   r‡     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}     s   r}   rš   r   r   r   r   r€     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)	rc   rZ   r   rŽ   rr   rs   rt   ru   rv   )r   rx   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}   6  s   r}   r‹   r   r›   N)rb   r   rŽ   rr   rs   rt   ru   rv   )r   r}   r˜   r   r   r   rŒ   /  s
    z*SafeStrTests.test_brokenClassNameAttributeN)r   r   r   r   r�   r{   r“   r–   r™   ry   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fakepackageÚtestz__init__.pyÚwrK   )rs   rt   ÚjoinÚmktempÚmakedirsÚopenÚwriteÚdirname)r   Úfr   r   r   ÚsetUpD  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   ÚfilenameToModuleNamert   r   rs   Úsep©r   Úmoduler   r   r   Útest_directoryL  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§   rs   rt   rŸ   r   r©   r   r   r   Ú	test_fileV  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§   rs   rt   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œ   ?  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   ÚobjÚexpectedr   r   r   Ú_checkFullyQualifiedNameq  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   Nr>   r:   )r>   r³   r7   r<   r=   r   r   r   Útest_packagex  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)Ztwisted.python.compatr³   r<   Úcompatr=   r   r   r   Útest_module„  s    z#FullyQualifiedNameTests.test_modulec                 C   s   |   tt› d�¡ dS )zS
        L{fullyQualifiedName} returns the name of a class and its module.
        z.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t› d| jj› d�¡ dS )zs
        L{fullyQualifiedName} returns the name of a bound method inside its
        class and its module.
        Ú.z.test_boundMethodN)r³   Útest_boundMethodr   rf   r   r   r   r   rº   œ  s    þz(FullyQualifiedNameTests.test_boundMethodc                 C   s$   |   | jjt› d| jj› d�¡ dS )zv
        L{fullyQualifiedName} returns the name of an unbound method inside its
        class and its module.
        r¹   z.test_unboundMethodN)r³   rf   Ú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°   l  s   
r°   c                   @   sT   e 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S )ÚObjectGrepTestsc                 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}©rR   rr   r   ÚobjgrepÚisSame)r   ÚoÚ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)rR   rÀ   rr   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Úrefrr   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Ï   rr   r   r¾   Ú__self__r¿   rf   Ú__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Fooshri   ZfooÚbar)r   r,   rh   z"().__self__.attr[2][0][2]{'Foosh'})Úattrr   rÌ   rÍ   rr   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Æ   r,   )ZmaxDepthz[1][0]rh   z	[1][1][0]ri   N)r   r   r¾   r¿   )r   r’   rx   ÚcrÊ   r   r   r   Útest_depthLimit  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Ã   )rR   r   Úappendrr   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¼   ±  s   		r¼   c                   @   s   e Zd Zdd„ ZdS )ÚGetClassTestsc                 C   s@   G dd„ dƒ}|ƒ }|   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   ÚNewClass4  s   rÝ   Útype)r   r   ZgetClassr   )r   rÝ   Únewr   r   r   Útest_new3  s    zGetClassTests.test_newN)r   r   r   rà   r   r   r   r   rÜ   2  s   rÜ   )$r   rs   rÌ   Úcollectionsr   r7   r   Ztwisted.python.reflectr   r   r   r   r   Ztwisted.trial.unittestr	   ZTestCaser
   r   r   r   r!   r#   r(   r.   r0   rX   rÞ   r_   rb   rc   rg   r�   rœ   r°   r¼   rÜ   r   r   r   r   Ú<module>   s6   ) 5
]V-E 