U
    
W[
  ã                   @   s€   d Z ddlmZ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 ddlmZ d	d
„ ZG dd„ de	ƒZd
dgZdS )z0
General helpers for L{twisted.web} unit tests.
é    )ÚdivisionÚabsolute_import)Úsucceed)Úserver)ÚTestCase)ÚFailure)ÚflattenString)ÚFlattenerErrorc                 C   sb   |   |¡}t|tƒr.| |¡ | ¡  td ƒS |tjkrP|jrFtd ƒS | 	¡ S nt
d|f ƒ‚d S )NzUnexpected return value: %r)ZrenderÚ
isinstanceÚbytesÚwriteZfinishr   r   ZNOT_DONE_YETZfinishedZnotifyFinishÚ
ValueError)ÚresourceZrequestÚresult© r   ú8/usr/lib/python3/dist-packages/twisted/web/test/_util.pyÚ_render   s    




r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFlattenTestCasezH
    A test case that assists with testing L{twisted.web._flatten}.
    c                    s"   t d|ƒ}| ‡ ‡fdd„¡ |S )zS
        Assert that a root element, when flattened, is equal to a string.
        Nc                    s   ˆ   | ˆ¡S ©N)ZassertEqual)Ús©ÚselfÚtargetr   r   Ú<lambda>-   ó    z2FlattenTestCase.assertFlattensTo.<locals>.<lambda>)r   ÚaddCallback)r   Úrootr   Údr   r   r   ÚassertFlattensTo(   s    
z FlattenTestCase.assertFlattensToc                 C   sX   g }|   ||¡}| |j¡ |  |j¡ |s6|  d¡ |d }t|tƒrP| ¡  |d S )aÖ  
        Assert that a root element, when flattened, is equal to a string, and
        performs no asynchronus Deferred anything.

        This version is more convenient in tests which wish to make multiple
        assertions about flattening, since it can be called multiple times
        without having to add multiple callbacks.

        @return: the result of rendering L{root}, which should be equivalent to
            L{target}.
        @rtype: L{bytes}
        z'Rendering did not complete immediately.r   )	r   ZaddBothÚappendZ
addCleanupZcancelZfailr
   r   ZraiseException)r   r   r   ZresultsÚitr   r   r   r   ÚassertFlattensImmediately1   s    

z)FlattenTestCase.assertFlattensImmediatelyc                    s,   ˆ  ˆ |d¡t¡}| ‡ ‡fdd„¡ |S )zQ
        Assert flattening a root element raises a particular exception.
        r   c                    s   ˆ  | jˆ ¡S r   )ZassertIsInstanceZ
_exception)Úexc©Úexnr   r   r   r   P   r   z8FlattenTestCase.assertFlatteningRaises.<locals>.<lambda>)ZassertFailurer   r	   r   )r   r   r$   r   r   r#   r   ÚassertFlatteningRaisesK   s    z&FlattenTestCase.assertFlatteningRaisesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r%   r   r   r   r   r   $   s   	r   N)r)   Z
__future__r   r   Ztwisted.internet.deferr   Ztwisted.webr   Ztwisted.trial.unittestr   Ztwisted.python.failurer   Ztwisted.web._flattenr   Ztwisted.web.errorr	   r   r   Ú__all__r   r   r   r   Ú<module>   s   0