o
    ¯b…  ã                   @   sV   d Z ddlZddlmZ ddlmZ G dd„ dƒZdd„ Zd	d
„ ZG dd„ deƒZ	dS )zt
Tests for miscellaneous behaviors of the top-level L{twisted} package (ie, for
the code in C{twisted/__init__.py}.
é    N)Ú
ModuleType)ÚTestCasec                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚSetAsideModulezž
    L{SetAsideModule} is a context manager for temporarily removing a module
    from C{sys.modules}.

    @ivar name: The name of the module to remove.
    c                 C   s
   || _ d S ©N)Úname)Úselfr   © r   ú;/usr/lib/python3/dist-packages/twisted/test/test_twisted.pyÚ__init__   s   
zSetAsideModule.__init__c                    s2   ‡ fdd„t tj ¡ ƒD ƒ}|D ]}tj|= q|S )z¥
        Find the given module and all of its hierarchically inferior modules in
        C{sys.modules}, remove them from it, and return whatever was found.
        c                    s0   i | ]\}}|ˆ j ks| ˆ j d  ¡r||“qS )Ú.)r   Ú
startswith)Ú.0Ú
moduleNameÚmodule©r   r   r	   Ú
<dictcomp>"   s
    þz,SetAsideModule._unimport.<locals>.<dictcomp>)ÚlistÚsysÚmodulesÚitems)r   r   r   r   r   r	   Ú	_unimport   s   
þ
zSetAsideModule._unimportc                 C   s   |   | j¡| _d S r   )r   r   r   r   r   r   r	   Ú	__enter__+   s   zSetAsideModule.__enter__c                 C   s   |   | j¡ tj | j¡ d S r   )r   r   r   r   Úupdate)r   ÚexcTypeÚexcValueÚ	tracebackr   r   r	   Ú__exit__.   s   zSetAsideModule.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r   r	   r      s    r   c                 C   s    i }t d| |ƒ tj |¡ dS )a  
    Take a mapping defining a package and turn it into real C{ModuleType}
    instances in C{sys.modules}.

    Consider these example::

        a = {"foo": "bar"}
        b = {"twisted": {"__version__": "42.6"}}
        c = {"twisted": {"plugin": {"getPlugins": stub}}}

    C{_install(a)} will place an item into C{sys.modules} with C{"foo"} as the
    key and C{"bar" as the value.

    C{_install(b)} will place an item into C{sys.modules} with C{"twisted"} as
    the key.  The value will be a new module object.  The module will have a
    C{"__version__"} attribute with C{"42.6"} as the value.

    C{_install(c)} will place an item into C{sys.modules} with C{"twisted"} as
    the key.  The value will be a new module object with a C{"plugin"}
    attribute.  An item will also be placed into C{sys.modules} with the key
    C{"twisted.plugin"} which refers to that module object.  That module will
    have an attribute C{"getPlugins"} with a value of C{stub}.

    @param modules: A mapping from names to definitions of modules.  The names
        are native strings like C{"twisted"} or C{"unittest"}.  Values may be
        arbitrary objects.  Any value which is not a dictionary will be added to
        C{sys.modules} unmodified.  Any dictionary value indicates the value is
        a new module and its items define the attributes of that module.  The
        definition of this structure is recursive, so a value in the dictionary
        may be a dictionary to trigger another level of processing.

    @return: L{None}
    N)Ú_makePackagesr   r   r   )r   Úresultr   r   r	   Ú_install3   s   "r#   c                 C   s´   i }t | ¡ ƒD ]O\}}| du r-t|tƒr(t|ƒ}|j t|||ƒ¡ |||< q|||< qt|tƒrSt| jd | ƒ}|j t|||ƒ¡ ||| jd | < |||< q|||< q|S )aË  
    Construct module objects (for either modules or packages).

    @param parent: L{None} or a module object which is the Python package
        containing all of the modules being created by this function call.  Its
        name will be prepended to the name of all created modules.

    @param attributes: A mapping giving the attributes of the particular module
        object this call is creating.

    @param result: A mapping which is populated with all created module names.
        This is suitable for use in updating C{sys.modules}.

    @return: A mapping of all of the attributes created by this call.  This is
        suitable for populating the dictionary of C{parent}.

    @see: L{_install}.
    Nr   )	r   r   Ú
isinstanceÚdictr   Ú__dict__r   r!   r   )ÚparentÚ
attributesr"   Úattrsr   Úvaluer   r   r   r	   r!   Z   s   





r!   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚMakePackagesTestszh
    Tests for L{_makePackages}, a helper for populating C{sys.modules} with
    fictional modules.
    c                 C   s,   i }t dtdd|ƒ |  |tdd¡ dS )z‘
        A non-C{dict} value in the attributes dictionary passed to L{_makePackages}
        is preserved unchanged in the return value.
        NÚreactor)r,   )r!   r%   ÚassertEqual©r   r   r   r   r	   Útest_nonModule‡   s   z MakePackagesTests.test_nonModulec                 C   s`   i }t dttddd|ƒ |  |t¡ |  |d t¡ |  d|d j¡ |  d|d j¡ dS )zÑ
        A C{dict} value in the attributes dictionary passed to L{_makePackages}
        is turned into a L{ModuleType} instance with attributes populated from
        the items of that C{dict} value.
        NÚ123©Úversion©Útwistedr4   )r!   r%   ÚassertIsInstancer   r-   r   r2   r.   r   r   r	   Útest_moduleWithAttribute   s   z*MakePackagesTests.test_moduleWithAttributec                 C   sŽ   i }t dtttdddd|ƒ |  |t¡ |  |d t¡ |  d|d j¡ |  |d jt¡ |  d|d jj¡ |  d|d jj¡ dS )z¥
        Processing of the attributes dictionary is recursive, so a C{dict} value
        it contains may itself contain a C{dict} value to the same effect.
        NÚ321r1   )Úwebr3   r4   ztwisted.web)r!   r%   r5   r   r-   r   r8   r2   r.   r   r   r	   Útest_packageWithModule   s   z(MakePackagesTests.test_packageWithModuleN)r   r   r   r    r/   r6   r9   r   r   r   r	   r+      s
    	r+   )
r    r   Útypesr   Útwisted.trial.unittestr   r   r#   r!   r+   r   r   r   r	   Ú<module>   s   !''