o
    ¯brV  ã                   @   sR  d Z ddl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mZmZmZmZ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 dd
lmZ ddlmZ ddlm Z  ddl!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 G dd„ de6eƒZ7dd„ j8j9Z:G dd„ de ƒZ;dS )zf
Tests for the flattening portion of L{twisted.web.template}, implemented in
L{twisted.web._flatten}.
é    N)ÚOrderedDict©Údedent)ÚFunctionType)ÚCallableÚDictÚListÚNoReturnÚOptionalÚcast)ÚXML)Úimplementer)ÚCancelledErrorÚDeferredÚgatherResultsÚpassthruÚsucceed)ÚFailure)ÚXMLAssertionMixin)ÚSynchronousTestCase)ÚFlattenerErrorÚUnfilledSlotÚUnsupportedType)ÚIRenderableÚIRequestÚITemplateLoader)ÚCDATAÚCharRefÚCommentÚElementÚFlattenableÚTagÚ	TagLoaderÚflattenStringÚrendererÚslotÚtags)ÚFlattenTestCasec                   @   sz  e Zd ZdZdBdd„ZdBdd„ZdBdd	„ZdBd
d„ZdBdd„Zde	e
gef de	egef ddfdd„ZdBdd„ZdBdd„ZdBdd„ZdBdd„ZdBdd„ZdBdd„Zde	egef ddfdd„ZdBd d!„ZdBd"d#„ZdBd$d%„ZdBd&d'„Zdeee  fd(d)„ZdBd*d+„ZdBd,d-„ZdBd.d/„ZdBd0d1„Z dBd2d3„Z!dBd4d5„Z"dBd6d7„Z#dBd8d9„Z$dBd:d;„Z%dBd<d=„Z&dBd>d?„Z'dBd@dA„Z(dS )CÚSerializationTestsz.
    Tests for flattening various things.
    ÚreturnNc                 C   s    |   tjt d¡dd�d¡ dS )z:
        Test that nested tags flatten correctly.
        Ú42Úthere)Úhis'   <html hi="there"><body>42</body></html>N)ÚassertFlattensImmediatelyr&   ÚhtmlÚbody©Úself© r2   ú?/usr/lib/python3/dist-packages/twisted/web/test/test_flatten.pyÚtest_nestedTags5   s   þz"SerializationTests.test_nestedTagsc                 C   s    |   dd¡f |   dd¡f dS )zL
        Test that strings will be flattened and escaped correctly.
        Úones   onez
<abc&&>123s   &lt;abc&amp;&amp;&gt;123N)r-   r0   r2   r2   r3   Útest_serializeString>   s   z'SerializationTests.test_serializeStringc                 C   s   |   t ¡ d¡ dS )zP
        The serialized form of a self-closing tag is C{'<tagName />'}.
        s   <img />N©r-   r&   Úimgr0   r2   r2   r3   Útest_serializeSelfClosingTagsE   ó   z0SerializationTests.test_serializeSelfClosingTagsc                 C   s   |   tjdd�d¡ dS )zV
        The serialized form of attribute I{a} with value I{b} is C{'a="b"'}.
        Úfoo©Úsrcs   <img src="foo" />Nr7   r0   r2   r2   r3   Útest_serializeAttributeK   s   z*SerializationTests.test_serializeAttributec                 C   s&   t  ¡ }tddgƒ|_|  |d¡ dS )zg
        Multiple attributes are separated by a single space in their serialized
        form.
        )r=   r;   )ÚnameÚbars   <img src="foo" name="bar" />N)r&   r8   r   Ú
attributesr-   ©r1   Útagr2   r2   r3   Ú!test_serializedMultipleAttributesQ   s   z4SerializationTests.test_serializedMultipleAttributesÚwrapDataÚwrapTagc                 C   s    |   |tj|dƒd�ƒd¡ dS )aÏ  
        Common implementation of L{test_serializedAttributeWithSanitization}
        and L{test_serializedDeferredAttributeWithSanitization},
        L{test_serializedAttributeWithTransparentTag}.

        @param wrapData: A 1-argument callable that wraps around the
            attribute's value so other tests can customize it.

        @param wrapTag: A 1-argument callable that wraps around the outer tag
            so other tests can customize it.
        ú<>&"r<   s!   <img src="&lt;&gt;&amp;&quot;" />Nr7   )r1   rE   rF   r2   r2   r3   ÚcheckAttributeSanitizationZ   s   þz-SerializationTests.checkAttributeSanitizationc                 C   s   |   tt¡ dS )zË
        Attribute values containing C{"<"}, C{">"}, C{"&"}, or C{'"'} have
        C{"&lt;"}, C{"&gt;"}, C{"&amp;"}, or C{"&quot;"} substituted for those
        bytes in the serialized output.
        N)rH   r   r0   r2   r2   r3   Ú(test_serializedAttributeWithSanitizationo   ó   z;SerializationTests.test_serializedAttributeWithSanitizationc                 C   s   |   tt¡ dS )z²
        Like L{test_serializedAttributeWithSanitization}, but when the contents
        of the attribute are in a L{Deferred
        <twisted.internet.defer.Deferred>}.
        N)rH   r   r   r0   r2   r2   r3   Ú0test_serializedDeferredAttributeWithSanitizationw   rJ   zCSerializationTests.test_serializedDeferredAttributeWithSanitizationc                    s@   g ‰ dt dtf‡ fdd„}dtdtf‡ fdd„}|  ||¡ dS )	zS
        Like L{test_serializedAttributeWithSanitization} but with a slot.
        Úvaluer)   c                    s   ˆ   | ¡ tdƒS )NÚstuff)Úappendr%   ©rL   ©Útossr2   r3   Ú
insertSlot…   s   
zWSerializationTests.test_serializedAttributeWithSlotWithSanitization.<locals>.insertSlotrC   c                    s   | j ˆ  ¡ d�S )N)rM   )Ú	fillSlotsÚpop©rC   rP   r2   r3   ÚfillSlot‰   s   zUSerializationTests.test_serializedAttributeWithSlotWithSanitization.<locals>.fillSlotN)Ústrr    r!   rH   )r1   rR   rV   r2   rP   r3   Ú0test_serializedAttributeWithSlotWithSanitization   s   zCSerializationTests.test_serializedAttributeWithSlotWithSanitizationc                 C   s   |   tjt¡ dS )z³
        Attribute values which are supplied via the value of a C{t:transparent}
        tag have the same substitution rules to them as values supplied
        directly.
        N)rH   r&   Útransparentr   r0   r2   r2   r3   Ú*test_serializedAttributeWithTransparentTagŽ   s   z=SerializationTests.test_serializedAttributeWithTransparentTagc                    sR   G dd„ dt ƒ‰ g ‰dtdtf‡fdd„}dtdtf‡ ‡fdd	„}|  ||¡ d
S )z�
        Like L{test_serializedAttributeWithTransparentTag}, but when the
        attribute is rendered by a renderer on an element.
        c                       sH   e Zd Zdedee f‡ fdd„Zedee de	de
fdd	„ƒZ‡  ZS )
z_SerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRendererrL   Úloaderc                    s   || _ tƒ  |¡ d S ©N)rL   ÚsuperÚ__init__)r1   rL   r[   ©Ú	__class__r2   r3   r^   �   s   zhSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.__init__ÚrequestrC   r)   c                 S   ó   | j S r\   rO   )r1   ra   rC   r2   r2   r3   rM   ¡   s   zeSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.stuff)Ú__name__Ú
__module__Ú__qualname__rW   r
   r   r^   r$   r   r!   r    rM   Ú__classcell__r2   r2   r_   r3   ÚWithRendererœ   s    $rg   rL   r)   c                    s   ˆ   | ¡ tjdd�S )NrM   ©Úrender)rN   r&   rY   rO   rP   r2   r3   ÚinsertRenderer§   s   
zaSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.insertRendererrC   c                    s   ˆ ˆ  ¡ t| ƒƒS r\   )rT   r"   rU   ©rg   rQ   r2   r3   ri   «   s   zYSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.renderN)r   rW   r    r!   rH   )r1   rj   ri   r2   rk   r3   Ú6test_serializedAttributeWithTransparentTagWithRenderer–   s
   	zISerializationTests.test_serializedAttributeWithTransparentTagWithRendererc                 C   s&   t tƒG dd„ dƒƒ}|  |t¡ dS )z«
        Like L{test_serializedAttributeWithTransparentTag}, but when the
        attribute is a provider of L{IRenderable} rather than a transparent
        tag.
        c                   @   sR   e Zd Zdefdd„Zdee defdd„Zdede	ee e
gef fd	d
„ZdS )zLSerializationTests.test_serializedAttributeWithRenderable.<locals>.ArbitraryrL   c                 S   s
   || _ d S r\   rO   )r1   rL   r2   r2   r3   r^   ¹   ó   
zUSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.__init__ra   r)   c                 S   rb   r\   rO   ©r1   ra   r2   r2   r3   ri   ¼   s   zSSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.renderr?   c                 S   ó   t dƒ‚)NzUnexpected call)ÚNotImplementedError©r1   r?   r2   r2   r3   ÚlookupRenderMethod¿   s   z_SerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.lookupRenderMethodN)rc   rd   re   r    r^   r
   r   ri   rW   r   r!   rr   r2   r2   r2   r3   Ú	Arbitrary·   s    ÿþrs   N)r   r   rH   r   )r1   rs   r2   r2   r3   Ú&test_serializedAttributeWithRenderable°   s   z9SerializationTests.test_serializedAttributeWithRenderablec                 C   sL   t  d¡}t j||ƒd�}|  |d¡}|  |d¡}|  t|ƒjd |¡ dS )ag  
        Common implementation of L{test_serializedAttributeWithTag} and
        L{test_serializedAttributeWithDeferredTag}.

        @param wrapTag: A 1-argument callable that wraps around the attribute's
            value so other tests can customize it.
        @type wrapTag: callable taking L{Tag} and returning something
            flattenable
        rG   r<   s@   <img src="&lt;a&gt;&amp;lt;&amp;gt;&amp;amp;&quot;&lt;/a&gt;" />s   <a>&lt;&gt;&amp;"</a>r=   N)r&   Úar8   r-   ÚassertXMLEqualr   Úattrib)r1   rF   ÚinnerTagÚouterTagÚouterÚinnerr2   r2   r3   ÚcheckTagAttributeSerializationÆ   s   
þz1SerializationTests.checkTagAttributeSerializationc                 C   ó   |   t¡ dS )zÆ
        L{Tag} objects which are serialized within the context of an attribute
        are serialized such that the text content of the attribute may be
        parsed to retrieve the tag.
        N)r|   r   r0   r2   r2   r3   Útest_serializedAttributeWithTagà   s   z2SerializationTests.test_serializedAttributeWithTagc                 C   r}   )z�
        Like L{test_serializedAttributeWithTag}, but when the L{Tag} is in a
        L{Deferred <twisted.internet.defer.Deferred>}.
        N)r|   r   r0   r2   r2   r3   Ú'test_serializedAttributeWithDeferredTagè   s   z:SerializationTests.test_serializedAttributeWithDeferredTagc                 C   s6   |   tjtjdd�d�d¡}|  t|ƒjd d¡ dS )zì
        Similar to L{test_serializedAttributeWithTag}, but for the additional
        complexity where the tag which is the attribute value itself has an
        attribute value which contains bytes which require substitution.
        rG   )Úhrefr<   sV   <img src="&lt;a href=&quot;&amp;lt;&amp;gt;&amp;amp;&amp;quot;&quot;&gt;&lt;/a&gt;" />r=   s"   <a href="&lt;&gt;&amp;&quot;"></a>N)r-   r&   r8   ru   rv   r   rw   )r1   Ú	flattenedr2   r2   r3   Ú,test_serializedAttributeWithTagWithAttributeï   s   þÿz?SerializationTests.test_serializedAttributeWithTagWithAttributec                 C   ó   |   tdƒd¡ dS )zI
        Test that comments are correctly flattened and escaped.
        úfoo bars   <!--foo bar-->N)r-   r   r0   r2   r2   r3   Útest_serializeComment  r:   z(SerializationTests.test_serializeCommentc                    sN   dt ddf‡ fdd„}g }dD ]}tdt|ƒƒ}| |¡ | |¡ qt|ƒS )aÖ  
        The data in a L{Comment} is escaped and mangled in the flattened output
        so that the result is a legal SGML and XML comment.

        SGML comment syntax is complicated and hard to use. This rule is more
        restrictive, and more compatible:

        Comments start with <!-- and end with --> and never contain -- or >.

        Also by XML syntax, a comment may not end with '-'.

        @see: U{http://www.w3.org/TR/REC-xml/#sec-comments}
        Úcr)   Nc                    sŠ   ˆ   |  d¡| ›d�¡ ˆ   |  d¡| ›d�¡ ˆ   t| ƒdk| ›d�¡ | dd… }ˆ  d	|¡ ˆ  d
|¡ |rCˆ  |d d¡ d S d S )Ns   <!--z' does not start with the comment prefixs   -->z% does not end with the comment suffixé   z# is too short to be a legal commenté   éýÿÿÿs   --ó   >éÿÿÿÿó   -)Ú
assertTrueÚ
startswithÚendswithÚlenÚassertNotInÚassertNotEqual)r†   Úcontentr0   r2   r3   ÚverifyComment  s   þþÿz>SerializationTests.test_commentEscaping.<locals>.verifyComment)Ú z	foo---barz
foo---bar-zfoo>barz	foo-->barz----------------)Úbytesr#   r   ÚaddCallbackrN   r   )r1   r”   Úresultsr†   Údr2   r0   r3   Útest_commentEscaping  s   
z'SerializationTests.test_commentEscapingc                 C   s&   |   tdƒd¡f |   tdƒd¡ dS )zE
        Test that CDATA is correctly flattened and escaped.
        r„   s   <![CDATA[foo bar]]>zfoo ]]> bars#   <![CDATA[foo ]]]]><![CDATA[> bar]]>N)r-   r   r0   r2   r2   r3   Útest_serializeCDATA6  s   ÿz&SerializationTests.test_serializeCDATAc                 C   sn   d}|   |d¡ |   t |¡d¡ |   t|ƒd¡ |   t|ƒd¡ |  t|ƒt¡ |  td|did�t¡ d	S )
z–
        Test that unicode is encoded correctly in the appropriate places, and
        raises an error when it occurs in inappropriate place.
        õ   â˜ƒs   â˜ƒs
   <p>â˜ƒ</p>s
   <!--â˜ƒ-->s   <![CDATA[â˜ƒ]]>Úpr•   )rA   N)r-   r&   r�   r   r   ÚassertFlatteningRaisesr!   ÚUnicodeEncodeError)r1   Úsnowmanr2   r2   r3   Útest_serializeUnicode?  s   ÿz(SerializationTests.test_serializeUnicodec                 C   s   t tdƒƒ}|  |d¡ dS )ze
        A character reference is flattened to a string using the I{&#NNNN;}
        syntax.
        rœ   s   &#9731;N)r   Úordr-   )r1   Úrefr2   r2   r3   Útest_serializeCharRefN  s   z(SerializationTests.test_serializeCharRefc                 C   rƒ   )zz
        Test that a deferred is substituted with the current value in the
        callback chain when flattened.
        Útwos   twoN)r-   r   r0   r2   r2   r3   Útest_serializeDeferredV  s   z)SerializationTests.test_serializeDeferredc                 C   s$   t dƒ}|  |d¡ |  |d¡ dS )zE
        Test that the same deferred can be flattened twice.
        Úthrees   threeN)r   r-   )r1   r™   r2   r2   r3   Útest_serializeSameDeferredTwice]  s   z2SerializationTests.test_serializeSameDeferredTwicec                 C   s:   ddl m} i }t|dƒ|ƒ |d }|  |dƒd¡ dS )zt
        Test that a coroutine returning a value is substituted with the that
        value when flattened.
        r   r   zE
            async def coro(x):
                return x
            ÚcoroÚfouró   fourN)Útextwrapr   Úexecr-   ©r1   r   Ú	namespacer©   r2   r2   r3   Útest_serializeCoroutinee  s   ÿù	z*SerializationTests.test_serializeCoroutinec                 C   s@   ddl m} ttd�}t|dƒ|ƒ |d }|  |dƒd¡ dS )	z‚
        Test that a coroutine returning an awaited deferred value is
        substituted with that value when flattened.
        r   r   )r   zT
            async def coro(x):
                return await succeed(x)
            r©   rª   r«   N)r¬   r   Údictr   r­   r-   r®   r2   r2   r3   Ú test_serializeCoroutineWithAwaitz  s   
ÿù	z3SerializationTests.test_serializeCoroutineWithAwaitc                    s,   t tƒG ‡ fdd„dƒƒ}ˆ  |ƒ d¡ dS )zQ
        Test that flattening respects all of the IRenderable interface.
        c                       sD   e Zd Zdedefdd„Zdedeee	 ege
f f‡ fdd„ZdS )	zASerializationTests.test_serializeIRenderable.<locals>.FakeElementÚoredr)   c                 S   s    t  dt jdd�dt jdd�¡S )Nzhello, Útestrh   z - )r&   r�   rY   )Úignr³   r2   r2   r3   ri   –  s   

üzHSerializationTests.test_serializeIRenderable.<locals>.FakeElement.renderr?   c                    s   ˆ   |d¡ dd„ S )Nr´   c                 S   s   |dƒS )NÚworldr2   )rµ   Únoder2   r2   r3   Ú<lambda>¢  s    zfSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethod.<locals>.<lambda>)ÚassertEqual)rµ   r?   r0   r2   r3   rr   ž  s   zTSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethodN)rc   rd   re   Úobjectr!   ri   rW   r   r
   r   r    rr   r2   r0   r2   r3   ÚFakeElement”  s    ÿþr»   s   <p>hello, world - world</p>N)r   r   r-   )r1   r»   r2   r0   r3   Útest_serializeIRenderable�  s   z,SerializationTests.test_serializeIRenderablec                 C   s   |   tjdd�t¡ dS )z—
        Test that flattening a tag with a C{render} attribute when no render
        factory is available in the context raises an exception.
        r´   rh   N)rž   r&   rY   Ú
ValueErrorr0   r2   r2   r3   Ú"test_serializeMissingRenderFactory¦  s   z5SerializationTests.test_serializeMissingRenderFactoryc                 C   s>   t  tdƒ¡}| ¡ }|jdd� |  |t¡ |  |d¡ dS )zS
        Test that flattening a slot will use the slot value from the tag.
        r´   zhello, world©r´   s   <p>hello, world</p>N)r&   r�   r%   ÚclonerS   rž   r   r-   )r1   Út1Út2r2   r2   r3   Útest_serializeSlots®  s
   z&SerializationTests.test_serializeSlotsc                 C   s4   t  tdƒ¡}|jtt  d¡ƒd� |  |d¡ dS )z|
        Test that a slot with a deferred as its value will be flattened using
        the value from the deferred.
        r´   zfour>r¿   s   <p><em>four&gt;</em></p>N)r&   r�   r%   rS   r   Úemr-   )r1   Útr2   r2   r3   Útest_serializeDeferredSlots¸  s   z.SerializationTests.test_serializeDeferredSlotsc                 C   s   |   dt¡ dS )zT
        Test that flattening an unknown type of thing raises an exception.
        N)rž   r   r0   r2   r2   r3   Útest_unknownTypeRaisesÁ  s   z)SerializationTests.test_unknownTypeRaises©r)   N))rc   rd   re   Ú__doc__r4   r6   r9   r>   rD   r   rW   r    r!   rH   rI   rK   rX   rZ   rl   rt   r|   r~   r   r‚   r…   r   r   r–   rš   r›   r¡   r¤   r¦   r¨   r°   r²   r¼   r¾   rÃ   rÆ   rÇ   r2   r2   r2   r3   r(   0   sT    

	


	þý
ü





ÿ
þ




/
	









	r(   c                   C   ó   d S r\   r2   r2   r2   r2   r3   r¸   Ð  s    r¸   c                   @   sL   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	dS )ÚFlattenerErrorTestsz&
    Tests for L{FlattenerError}.
    r)   Nc                 C   s:   t tƒG dd„ dƒƒ}|  tttdƒ|ƒ gg ƒƒd¡ dS )z¼
        If a L{FlattenerError} is created with an L{IRenderable} provider root,
        the repr of that object is included in the string representation of the
        exception.
        c                   @   s   e Zd Zdefdd„ZdS )z7FlattenerErrorTests.test_renderable.<locals>.Renderabler)   c                 S   ó   dS )Nzrenderable reprr2   r0   r2   r2   r3   Ú__repr__á  ó   z@FlattenerErrorTests.test_renderable.<locals>.Renderable.__repr__N)rc   rd   re   rW   rÍ   r2   r2   r2   r3   Ú
Renderableß  s    rÏ   ÚreasonzCException while flattening:
  renderable repr
RuntimeError: reason
N)r   r   r¹   rW   r   ÚRuntimeError)r1   rÏ   r2   r2   r3   Útest_renderableØ  s   þz#FlattenerErrorTests.test_renderablec                 C   s2   t ddddd�}|  tttdƒ|gg ƒƒd¡ dS )	zË
        If a L{FlattenerError} is created with a L{Tag} instance with source
        location information, the source location is included in the string
        representation of the exception.
        Údivz/foo/filename.xhtmlé   é   )ÚfilenameÚ
lineNumberÚcolumnNumberrÐ   zlException while flattening:
  File "/foo/filename.xhtml", line 17, column 12, in "div"
RuntimeError: reason
N)r!   r¹   rW   r   rÑ   rB   r2   r2   r3   Útest_tagë  s
   þzFlattenerErrorTests.test_tagc                 C   s&   |   tttdƒtdƒgg ƒƒd¡ dS )zË
        If a L{FlattenerError} is created with a L{Tag} instance without source
        location information, only the tagName is included in the string
        representation of the exception.
        rÐ   Úspanz>Exception while flattening:
  Tag <span>
RuntimeError: reason
N)r¹   rW   r   rÑ   r!   r0   r2   r2   r3   Útest_tagWithoutLocationú  s   þz+FlattenerErrorTests.test_tagWithoutLocationc              
      s¬   d‡ fdd„}dt fdd„‰ z|ƒ  W n! ty4 } zt t ¡ d ¡dd… }|}W Y d}~n
d}~ww |  d	¡ |  tt	|g |ƒƒd
t
|jjd t
ˆ jjd f ¡ dS )z“
        If a L{FlattenerError} is created with traceback frames, they are
        included in the string representation of the exception.
        r)   Nc                      s
   ˆ ƒ  d S r\   r2   r2   ©Úgr2   r3   Úf  rm   z-FlattenerErrorTests.test_traceback.<locals>.fc                   S   ro   )NrÐ   )rÑ   r2   r2   r2   r3   rÝ     s   z-FlattenerErrorTests.test_traceback.<locals>.gé   é   zf() must raise RuntimeErrorz�Exception while flattening:
  File "%s", line %d, in f
    g()
  File "%s", line %d, in g
    raise RuntimeError("reason")
RuntimeError: reason
rÈ   )r	   rÑ   Ú	tracebackÚ
extract_tbÚsysÚexc_infoÚfailr¹   rW   r   ÚHEREÚ__code__Úco_firstlineno)r1   rÞ   ÚeÚtbinfoÚexcr2   rÜ   r3   Útest_traceback  s(   
€ý


üúþz"FlattenerErrorTests.test_tracebackc                    sˆ   t ƒ ‰ ttƒG ‡ fdd„dƒƒ}td|ƒ gƒ}|  |¡ tdƒ}ˆ  |¡ |  |t¡}|  	t
|jƒtjtdƒtjd�¡ |  ˆ t¡ dS )z„
        When flattening a renderer which raises an exception asynchronously,
        the error is reported when it occurs.
        c                       sZ   e Zd ZdZdefdd„Zdedeee e	ge
f fdd„Zdee de
f‡ fd	d
„ZdS )zPFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderablez#No methods provided; this will failr)   c                 S   rÌ   )Nz<unrenderable>r2   r0   r2   r2   r3   rÍ   6  rÎ   zYFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.__repr__r?   c                 S   rÊ   r\   r2   rq   r2   r2   r3   rr   9  s   zcFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.lookupRenderMethodra   c                    s   ˆ S r\   r2   rn   ©Úfailingr2   r3   ri   >  rÎ   zWFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.renderN)rc   rd   re   rÉ   rW   rÍ   r   r
   r   r!   r    rr   ri   r2   rí   r2   r3   ÚNotActuallyRenderable2  s    ÿ
þrï   NÚexamplea@                      Exception while flattening:
                      \[<unrenderable>\]
                      <unrenderable>
                      .*
                      File ".*", line \d*, in _flattenTree
                        element = await element
                    RuntimeError: example
                    )Úflags)r   r   r   r#   ÚassertNoResultrÑ   ÚerrbackÚfailureResultOfr   ÚassertRegexrW   rL   ÚreÚcompiler   Ú	MULTILINE)r1   rï   Ú
flatteningrë   Úfailurer2   rí   r3   Útest_asynchronousFlattenError+  s$   

ÿôþz1FlattenerErrorTests.test_asynchronousFlattenErrorc                    sÌ   d‰d‰ dt t ddf‡ ‡fdd„}d‰dtddf‡fdd	„}t |ƒ}td|ƒ}|  |¡ | |¡ | ¡  |  |t¡}|  	ˆd
¡ |  
ˆ |¡ |  ˆt¡ |  ttˆƒjt¡ |jjd }|  |t¡ dS )z@
        The flattening of a Deferred can be cancelled.
        r   NÚ	cancelledr)   c                    s   | ‰ ˆd7 ‰d S )Nrà   r2   )rü   )Ú	cancelArgÚcancelCountr2   r3   ÚcheckCancelb  s   z4FlattenerErrorTests.test_cancel.<locals>.checkCancelrú   c                    s   | ‰ d S r\   r2   )rú   )Úerrr2   r3   ÚsaveErri  s   z0FlattenerErrorTests.test_cancel.<locals>.saveErrrà   )r   rº   r   r#   rò   Ú
addErrbackÚcancelrô   r   r¹   ÚassertIsÚassertIsInstancer   rL   r   Úargs)r1   rÿ   r  r™   rù   rú   rë   r2   )rý   rþ   r   r3   Útest_cancel[  s"   


zFlattenerErrorTests.test_cancelrÈ   )
rc   rd   re   rÉ   rÒ   rÙ   rÛ   rì   rû   r  r2   r2   r2   r3   rË   Ó  s    




&0rË   )<rÉ   rö   rã   rá   Úcollectionsr   r¬   r   Útypesr   Útypingr   r   r   r	   r
   r   Úxml.etree.ElementTreer   Úzope.interfacer   Útwisted.internet.deferr   r   r   r   r   Útwisted.python.failurer   Útwisted.test.testutilsr   Útwisted.trial.unittestr   Útwisted.web.errorr   r   r   Útwisted.web.iwebr   r   r   Útwisted.web.templater   r   r   r   r    r!   r"   r#   r$   r%   r&   Útwisted.web.test._utilr'   r(   rç   Úco_filenameræ   rË   r2   r2   r2   r3   Ú<module>   s0    4   #