
    Yf'h                        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mZ ddlmZ ddlmZ dd	lmZ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& ddl'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ej>        dd         dk    Z? G d de=e"          Z@ G d de$          ZAd jB        jC        ZD G d de$          ZEd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Tuplecast)XML)implementer)assert_thatequal_to)CancelledErrorDeferredgatherResultspassthrusucceed)Failure)XMLAssertionMixin)SynchronousTestCase)BUFFER_SIZE)FlattenerErrorUnfilledSlotUnsupportedType)IRenderableIRequestITemplateLoader)CDATACharRefCommentElementFlattenableTag	TagLoaderflattenflattenStringrendererslottags)FlattenTestCase   )      c                   h   e Zd ZdZd$dZd$dZd$dZd$dZd$dZd	e	e
gef         d
e	egef         ddfdZd$dZd$dZd$dZd$dZd$dZd$dZd
e	egef         ddfdZd$dZd$dZd$dZd$dZdeee                  f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 )%SerializationTestsz.
    Tests for flattening various things.
    returnNc                 ~    |                      t          j        t          j        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selfs    O/var/www/html/env/lib/python3.11/site-packages/twisted/web/test/test_flatten.pytest_nestedTagsz"SerializationTests.test_nestedTags;   sC     	&&Idioo'2226	
 	
 	
 	
 	
    c                 b    |                      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)r8   r;   s    r=   test_serializeStringz'SerializationTests.test_serializeStringD   s=     	&&uf5566&&|5PQQRRRRr?   c                 T    |                      t          j                    d           dS )zP
        The serialized form of a self-closing tag is C{'<tagName />'}.
        s   <img />Nr8   r,   imgr;   s    r=   test_serializeSelfClosingTagsz0SerializationTests.test_serializeSelfClosingTagsK   s&     	&&txzz:>>>>>r?   c                 X    |                      t          j        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" />NrD   r;   s    r=   test_serializeAttributez*SerializationTests.test_serializeAttributeQ   s.     	&&txE':':':<PQQQQQr?   c                     t          j                    }t          ddg          |_        |                     |d           dS )zg
        Multiple attributes are separated by a single space in their serialized
        form.
        )rJ   rH   )namebars   <img src="foo" name="bar" />N)r,   rE   r   
attributesr8   r<   tags     r=   !test_serializedMultipleAttributesz4SerializationTests.test_serializedMultipleAttributesW   s@    
 hjj$no%FGG&&s,KLLLLLr?   wrapDatawrapTagc           	      |    |                       |t          j         |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.
        <>&"rI   s!   <img src="&lt;&gt;&amp;&quot;" />NrD   )r<   rS   rT   s      r=   checkAttributeSanitizationz-SerializationTests.checkAttributeSanitization`   sM      	&&GDH&!1!1222330	
 	
 	
 	
 	
r?   c                 F    |                      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)rW   r   r;   s    r=   (test_serializedAttributeWithSanitizationz;SerializationTests.test_serializedAttributeWithSanitizationu   s      	''(;;;;;r?   c                 F    |                      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)rW   r   r   r;   s    r=   0test_serializedDeferredAttributeWithSanitizationzCSerializationTests.test_serializedDeferredAttributeWithSanitization}   s      	'':::::r?   c                     g dt           dt          ffd}dt          dt          ffd}|                     ||           dS )zS
        Like L{test_serializedAttributeWithSanitization} but with a slot.
        valuer3   c                 L                         |            t          d          S )Nstuff)appendr+   r]   tosss    r=   
insertSlotzWSerializationTests.test_serializedAttributeWithSlotWithSanitization.<locals>.insertSlot   s!    KK== r?   rQ   c                 T    |                                                                S )N)r_   )	fillSlotspop)rQ   rb   s    r=   fillSlotzUSerializationTests.test_serializedAttributeWithSlotWithSanitization.<locals>.fillSlot   s    ==txxzz=222r?   N)strr%   r&   rW   )r<   rc   rg   rb   s      @r=   0test_serializedAttributeWithSlotWithSanitizationzCSerializationTests.test_serializedAttributeWithSlotWithSanitization   s     	!c 	!k 	! 	! 	! 	! 	! 	!	3# 	3# 	3 	3 	3 	3 	3 	3 	''
H=====r?   c                 P    |                      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)rW   r,   transparentr   r;   s    r=   *test_serializedAttributeWithTransparentTagz=SerializationTests.test_serializedAttributeWithTransparentTag   s#     	''(8(CCCCCr?   c                      G d dt                     g dt          dt          ffd}dt          dt          ffd}|                     ||           dS )	z
        Like L{test_serializedAttributeWithTransparentTag}, but when the
        attribute is rendered by a renderer on an element.
        c                   h     e Zd Zdedee         ddf fdZedee         de	de
fd            Z xZS )	_SerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRendererr]   loaderr3   Nc                 X    || _         t                                          |           d S N)r]   super__init__)r<   r]   rp   	__class__s      r=   rt   zhSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.__init__   s(    "
  (((((r?   requestrQ   c                     | j         S rr   r]   )r<   rv   rQ   s      r=   r_   zeSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.stuff   s
    z!r?   )__name__
__module____qualname__rh   r   r    rt   r*   r   r&   r%   r_   __classcell__)ru   s   @r=   WithRendererro      s        )c )8O3L )QU ) ) ) ) ) ) "Xh%7 "c "k " " " X" " " " "r?   r}   r]   r3   c                 X                         |            t          j        d          S )Nr_   render)r`   r,   rk   ra   s    r=   insertRendererzaSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.insertRenderer   s)    KK#73333r?   rQ   c                 Z                                      t          |                     S rr   )rf   r'   )rQ   r}   rb   s    r=   r   zYSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.render   s#    <

IcNN;;;r?   N)r$   rh   r%   r&   rW   )r<   r   r   r}   rb   s      @@r=   6test_serializedAttributeWithTransparentTagWithRendererzISerializationTests.test_serializedAttributeWithTransparentTagWithRenderer   s    	" 	" 	" 	" 	"7 	" 	" 	" 	4# 	4+ 	4 	4 	4 	4 	4 	4	< 	< 	< 	< 	< 	< 	< 	< 	< 	''?????r?   c                     t          t                     G 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                   j    e Zd ZdeddfdZdee         defdZdede	ee         e
gef         fdZdS )	LSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitraryr]   r3   Nc                     || _         d S rr   rx   )r<   r]   s     r=   rt   zUSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.__init__   s    "


r?   rv   c                     | j         S rr   rx   )r<   rv   s     r=   r   zSSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.render   s
    z!r?   rM   c                      t          d          )NzUnexpected call)NotImplementedErrorr<   rM   s     r=   lookupRenderMethodz_SerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.lookupRenderMethod   s     **;<<<r?   )ry   rz   r{   r%   rt   r   r   r   rh   r   r&   r    r?   r=   	Arbitraryr      s        #k #d # # # #"hx&8 "[ " " " "==8H-s3[@A= = = = = =r?   r   N)r   r   rW   r   )r<   r   s     r=   &test_serializedAttributeWithRenderablez9SerializationTests.test_serializedAttributeWithRenderable   s]     
[	!	!
	= 
	= 
	= 
	= 
	= 
	= 
	= 
"	!
	= 	''	8<<<<<r?   c                    t          j        d          }t          j         ||                    }|                     |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
        rV   rI   s@   <img src="&lt;a&gt;&amp;lt;&amp;gt;&amp;amp;&quot;&lt;/a&gt;" />s   <a>&lt;&gt;&amp;"</a>rJ   N)r,   arE   r8   assertXMLEqualr   attrib)r<   rT   innerTagouterTagouterinners         r=   checkTagAttributeSerializationz1SerializationTests.checkTagAttributeSerialization   s     6&>>8 1 1222..O
 
 ..x9QRR 	CJJ-e4e<<<<<r?   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   r;   s    r=   test_serializedAttributeWithTagz2SerializationTests.test_serializedAttributeWithTag   s     	++H55555r?   c                 :    |                      t                     dS )z
        Like L{test_serializedAttributeWithTag}, but when the L{Tag} is in a
        L{Deferred <twisted.internet.defer.Deferred>}.
        N)r   r   r;   s    r=   'test_serializedAttributeWithDeferredTagz:SerializationTests.test_serializedAttributeWithDeferredTag   s    
 	++G44444r?   c                     |                      t          j        t          j        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.
        rV   )hrefrI   sV   <img src="&lt;a href=&quot;&amp;lt;&amp;gt;&amp;amp;&amp;quot;&quot;&gt;&lt;/a&gt;" />rJ   s"   <a href="&lt;&gt;&amp;&quot;"></a>N)r8   r,   rE   r   r   r   r   )r<   	flatteneds     r=   ,test_serializedAttributeWithTagWithAttributez?SerializationTests.test_serializedAttributeWithTagWithAttribute   ss     22HV,,,---
 
	 		NN!%(*O	
 	
 	
 	
 	
r?   c                 L    |                      t          d          d           dS )zI
        Test that comments are correctly flattened and escaped.
        foo bars   <!--foo bar-->N)r8   r#   r;   s    r=   test_serializeCommentz(SerializationTests.test_serializeComment  s)     	&&wy'9'9;LMMMMMr?   c                      dt           ddf fd}g }dD ]I}t          dt          |                    }|                    |           |                    |           Jt          |          S )a-  
        The data in a L{Comment} is escaped and mangled in the flattened output
        so that the result can be safely included in an HTML document.

        Test that C{>} is escaped when the sequence C{-->} is encountered
        within a comment, and that comments do not end with C{-}.
        cr3   Nc                                         |                     d          | d                                |                     d          | d                                t          |           dk    | d           | dd         }d	|v r                    d
|           n                    d
|           |r                    |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   foo   >   -)
assertTrue
startswithendswithlenassertInassertNotInassertNotEqual)r   contentr<   s     r=   verifyCommentz>SerializationTests.test_commentEscaping.<locals>.verifyComment  s   OOW%%???   OO

6""===   OOCFFaKA)T)T)TUUU"gG  dG,,,,  w/// 7##GBK666667 7r?   ) z	foo > barzabracadabra-znot-->magic)bytesr)   r#   addCallbackr`   r   )r<   r   resultsr   ds   `    r=   test_commentEscapingz'SerializationTests.test_commentEscaping  s    	7U 	7t 	7 	7 	7 	7 	7 	7( 
 	 	A dGAJJ//AMM-(((NN1W%%%r?   c                     |                      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)r8   r!   r;   s    r=   test_serializeCDATAz&SerializationTests.test_serializeCDATA6  sV     	&&uY'7'79OPPQQ&&-  "H	
 	
 	
 	
 	
r?   c                    d}|                      |d           |                      t          j        |          d           |                      t          |          d           |                      t	          |          d           |                     t          |          t                     |                     t          d|di          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   )rO   N)r8   r,   r   r#   r!   assertFlatteningRaisesr&   UnicodeEncodeError)r<   snowmans     r=   test_serializeUnicodez(SerializationTests.test_serializeUnicode?  s    
  &&w@@@&&tvg8NOOO&&ww'7'79OPPP&&uW~~7RSSS##CLL2DEEE##"...0B	
 	
 	
 	
 	
r?   c                 j    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"   ordr8   )r<   refs     r=   test_serializeCharRefz(SerializationTests.test_serializeCharRefN  s5    
 c-(())&&sJ77777r?   c                 L    |                      t          d          d           dS )zz
        Test that a deferred is substituted with the current value in the
        callback chain when flattened.
        twos   twoN)r8   r   r;   s    r=   test_serializeDeferredz)SerializationTests.test_serializeDeferredV  s&    
 	&&wu~~v>>>>>r?   c                 |    t          d          }|                     |d           |                     |d           dS )zE
        Test that the same deferred can be flattened twice.
        threes   threeN)r   r8   )r<   r   s     r=   test_serializeSameDeferredTwicez2SerializationTests.test_serializeSameDeferredTwice]  sC     G&&q(333&&q(33333r?   c                     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   execr8   r<   r   	namespacer   s       r=   test_serializeCoroutinez*SerializationTests.test_serializeCoroutinee  sv    
 	$#####-/	F  	
 	
 	
  &&ttF||W=====r?   c                     ddl m} t          t                    }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   r8   r   s       r=    test_serializeCoroutineWithAwaitz3SerializationTests.test_serializeCoroutineWithAwaitz  s    
 	$#####)))	F  	
 	
 	
  &&ttF||W=====r?   c                      t          t                     G  fdd                      }                      |            d           dS )zQ
        Test that flattening respects all of the IRenderable interface.
        c                   T    e Zd ZdedefdZdedeee	         ege
f         f fdZdS )ASerializationTests.test_serializeIRenderable.<locals>.FakeElementoredr3   c                 |    t          j        dt          j        d          dt          j        d                    S )Nzhello, testr   z - )r,   r   rk   )ignr   s     r=   r   zHSerializationTests.test_serializeIRenderable.<locals>.FakeElement.render  s>    v$F333$F333	  r?   rM   c                 6                         |d           d S )Nr   c                      |d          S )Nworldr   )r   nodes     r=   <lambda>zfSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethod.<locals>.<lambda>  s    g r?   )assertEqual)r   rM   r<   s     r=   r   zTSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethod  s$       v...666r?   N)ry   rz   r{   objectr&   r   rh   r   r   r   r%   r   r;   s   r=   FakeElementr     sy        & S    778H-s3[@A7 7 7 7 7 7 7 7r?   r   s   <p>hello, world - world</p>N)r   r   r8   )r<   r   s   ` r=   test_serializeIRenderablez,SerializationTests.test_serializeIRenderable  so    
 
[	!	!	7 	7 	7 	7 	7 	7 	7 	7 	7 
"	!	7 	&&{{}}6TUUUUUr?   c                 b    |                      t          j        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   r   N)r   r,   rk   
ValueErrorr;   s    r=   "test_serializeMissingRenderFactoryz5SerializationTests.test_serializeMissingRenderFactory  s.     	##D$4F$C$C$CZPPPPPr?   c                     t          j        t          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+   clonere   r   r   r8   )r<   t1t2s      r=   test_serializeSlotsz&SerializationTests.test_serializeSlots  sm     VDLL!!XXZZ
.)))##B555&&r+ABBBBBr?   c                     t          j        t          d                    }|                    t	          t          j        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+   re   r   emr8   )r<   ts     r=   test_serializeDeferredSlotsz.SerializationTests.test_serializeDeferredSlots  s\    
 F4<<  	!1!122333&&q*EFFFFFr?   c                 <    |                      dt                     dS )zT
        Test that flattening an unknown type of thing raises an exception.
        N)r   r   r;   s    r=   test_unknownTypeRaisesz)SerializationTests.test_unknownTypeRaises  s      	##D/:::::r?   r3   N))ry   rz   r{   __doc__r>   rB   rF   rK   rR   r   rh   r%   r&   rW   rY   r[   ri   rl   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r=   r2   r2   6   s        
 
 
 
S S S S? ? ? ?R R R RM M M M
C5+-.
 3%,-
 
	
 
 
 
*< < < <; ; ; ;> > > >D D D D@ @ @ @4= = = =,={ 23=	= = = =46 6 6 65 5 5 5
 
 
 
$N N N N'&htE{&; '& '& '& '&R
 
 
 

 
 
 
8 8 8 8? ? ? ?4 4 4 4> > > >*> > > >*V V V V.Q Q Q QC C C CG G G G; ; ; ; ; ;r?   r2   c                       e Zd ZdZd
dZd
dZdeegee	e         eg e
f         f         f         ddfdZd
dZd
d	ZdS )FlattenChunkingTestszi
    Tests for the way pieces of the result are chunked together in calls to
    the write function.
    r3   Nc                     g }|                      t          dg d|j                             t          |t	          dg                     dS )z
        If the entire value to be flattened is available synchronously and fits
        into the buffer it is all passed to a single call to the write
        function.
        N)123s   123)successResultOfr(   r`   r   r   )r<   outputs     r=   test_oneSmallChunkz'FlattenChunkingTests.test_oneSmallChunk  sO     !WT???FMJJKKKFHfX../////r?   c           	         dgt           z  }dgt           z  }dgt           z  }g }|                     t          d|||g|j                             t	          |t          dt           z  dt           z  dt           z  g                     dS )z
        If the entire value to be flattened is available synchronously but does
        not fit into the buffer then it is chunked into buffer-sized pieces
        and these are passed to the write function.
        xyzN   x   y   z)r   r  r(   r`   r   r   )r<   somesomeMoreevenMorer  s        r=   test_someLargeChunksz)FlattenChunkingTests.test_someLargeChunks  s     u{"5;&5;& WTD(H+Ev}UUVVVd[($*<d[>PQRR	
 	
 	
 	
 	
r?   startc                     |d          \  }} |d          \  }}dd|dd|ddg}g }t          d||j                  } |              |             |                     |           t          |t	          g d	                     dS )
a#  
        Assert that flattening with a L{Deferred} returned by C{start} results
        in the expected buffering behavior.

        The L{Deferred} need not have a result by it is returned by C{start}
        but must have a result after the callable returned along with it is
        called.

        The expected buffering behavior is that flattened values up to the
        L{Deferred} are written together and then the result of the
        L{Deferred} is written together with values following it up to the
        next L{Deferred}.
        zfirst-zsecond-zalready-availablez-chunkszchunks-already-computedzmore-chunks-N)s   already-available-chunkss   first-chunks-already-computeds$   second-more-chunks-already-available)r(   r`   r  r   r   )	r<   r  
first_waitfirst_finishsecond_waitsecond_finishr]   r  r   s	            r=   _chunksSeparatedByAsyncTestz0FlattenChunkingTests._chunksSeparatedByAsyncTest  s    & $)5?? 
L%*U9%5%5"]	
 !D%//Q   		
 		
 		
 		
 		
r?   c                     dt           dt          t          t                    t          g df         f         fd}|                     |           dS )a  
        When a fired L{Deferred} is encountered any buffered data is
        passed to the write function.  Then the L{Deferred}'s result is passed
        to another write along with following synchronous values.

        This exact buffering behavior should be considered an implementation
        detail and can be replaced by some other better behavior in the future
        if someone wants.
        vr3   Nc                 &    t          |           d fS )Nc                      d S rr   r   r   r?   r=   r   z^FlattenChunkingTests.test_chunksSeparatedByFiredDeferred.<locals>.sync_start.<locals>.<lambda>&  s     r?   r   )r  s    r=   
sync_startzLFlattenChunkingTests.test_chunksSeparatedByFiredDeferred.<locals>.sync_start#  s     AJJ--r?   r%   r   r   r   r  )r<   r  s     r=   #test_chunksSeparatedByFiredDeferredz8FlattenChunkingTests.test_chunksSeparatedByFiredDeferred  sZ    	.	.8K((2t8*<<=	. 	. 	. 	.
 	((44444r?   c                     dt           dt          t          t                    t          g df         f         fd}|                     |           dS )a   
        When an unfired L{Deferred} is encountered any buffered data is
        passed to the write function.  After the result of the L{Deferred} is
        available it is passed to another write along with following
        synchronous values.
        r  r3   Nc                 2     t                       fdfS )Nc                  .                                    S rr   )callback)r   r  s   r=   r   zaFlattenChunkingTests.test_chunksSeparatedByUnfiredDeferred.<locals>.async_start.<locals>.<lambda>6  s    qzz!}} r?   )r   )r  r   s   `@r=   async_startzOFlattenChunkingTests.test_chunksSeparatedByUnfiredDeferred.<locals>.async_start2  s'     (0zzA,,,,,--r?   r  )r<   r$  s     r=   %test_chunksSeparatedByUnfiredDeferredz:FlattenChunkingTests.test_chunksSeparatedByUnfiredDeferred*  sZ    	.	.8K((2t8*<<=	. 	. 	. 	. 	((55555r?   r   )ry   rz   r{   r   r  r  r   r%   r   r   r   r  r  r%  r   r?   r=   r   r     s         
0 0 0 0
 
 
 
"-
M5+!6V8L!LMM
-

 
-
 -
 -
 -
^5 5 5 5$6 6 6 6 6 6r?   r   c                      d S rr   r   r   r?   r=   r   r   C  s     r?   c                   B    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S )FlattenerErrorTestsz&
    Tests for L{FlattenerError}.
    r3   Nc           	          t          t                     G 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                       e Zd ZdefdZdS )7FlattenerErrorTests.test_renderable.<locals>.Renderabler3   c                     dS )Nzrenderable reprr   r;   s    r=   __repr__z@FlattenerErrorTests.test_renderable.<locals>.Renderable.__repr__T  s    ((r?   N)ry   rz   r{   rh   r-  r   r?   r=   
Renderabler+  R  s/        )# ) ) ) ) ) )r?   r.  reasonzCException while flattening:
  renderable repr
RuntimeError: reason
N)r   r   r   rh   r   RuntimeError)r<   r.  s     r=   test_renderablez#FlattenerErrorTests.test_renderableK  s     
[	!	!	) 	) 	) 	) 	) 	) 	) 
"	!	) 	|H55

~rJJKK%	
 	
 	
 	
 	
r?   c           	          t          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   rh   r   r0  rP   s     r=   test_tagzFlattenerErrorTests.test_tag^  sd     %"7BUWXXX|H55ubAABB%	
 	
 	
 	
 	
r?   c           
          |                      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   rh   r   r0  r&   r;   s    r=   test_tagWithoutLocationz+FlattenerErrorTests.test_tagWithoutLocationm  sP     	|H55F}bIIJJU	
 	
 	
 	
 	
r?   c           
         dfd}dt           fd	  |             |                     d           nL# t          $ r?}t          j        t          j                    d                   dd         }|}Y d}~nd}~ww xY wt          rd}nd	}|                     t          t          |g |                    d
t          |j        j        dz   |t          j        j        dz   fz             dS )z
        If a L{FlattenerError} is created with traceback frames, they are
        included in the string representation of the exception.
        r3   Nc                                     d S rr   r   )gs   r=   fz-FlattenerErrorTests.test_traceback.<locals>.f  s    ACCCCCr?   c                       t          d          )Nr/  )r0  r   r?   r=   r?  z-FlattenerErrorTests.test_traceback.<locals>.g  s    x(((r?   zf() must raise RuntimeErrorr.      z    ~^^
r   zException while flattening:
  File "%s", line %d, in f
    g()
%s  File "%s", line %d, in g
    raise RuntimeError("reason")
RuntimeError: reason
r   )r
   failr0  	traceback
extract_tbsysexc_infoIS_PYTHON_313r   rh   r   HERE__code__co_firstlineno)r<   r@  etbinfoexccolumn_markerr?  s         @r=   test_tracebackz"FlattenerErrorTests.test_tracebackx  s;   	 	 	 	 	 		)8 	) 	) 	) 	)	5ACCC II34444  	 	 	)#,..*;<<QRR@FCCCCCC	  	'MMMsB//00% 
)A-
)A-	
 	
 	
 	
 	
s   
4 
A=5A88A=c                    t                      t          t                     G fdd                      }t          d |            g          }|                     |           t          d          }                    |           |                     |t                    }t          rd}nd}| 
                    t          |j                  t          j        t          d          |z   t          j                             |                     d	t          |j                             |                     t
                     dS )
z
        When flattening a renderer which raises an exception asynchronously,
        the error is reported when it occurs.
        c                   p    e Zd ZdZdefdZdedeee         e	ge
f         fdZdee         de
f fdZdS )	PFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderablez#No methods provided; this will failr3   c                     dS )Nz<unrenderable>r   r;   s    r=   r-  zYFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.__repr__  s    ''r?   rM   c                     d S rr   r   r   s     r=   r   zcFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.lookupRenderMethod  s	     r?   rv   c                     S rr   r   )r<   rv   failings     r=   r   zWFlattenerErrorTests.test_asynchronousFlattenError.<locals>.NotActuallyRenderable.render  s    r?   N)ry   rz   r{   r   rh   r-  r   r   r   r&   r%   r   r   )rW  s   r=   NotActuallyRenderablerS    s        11(# ( ( ( (8H-s3[@A   
hx&8 [        r?   rX  Nexamplez[ ^]*
RuntimeError: example
r   av                      Exception while flattening:
                      \[<unrenderable>\]
                      <unrenderable>
                      <Deferred at .* current result: <twisted.python.failure.Failure builtins.RuntimeError: example>>
                      File ".*", line \d*, in _flattenTree
                        element = await element.*
                    )flagszRuntimeError: example)r   r   r   r)   assertNoResultr0  errbackfailureResultOfr   rH  assertRegexrh   r]   recompiler   	MULTILINEr   )r<   rX  
flatteningrN  failurerO  rW  s         @r=   test_asynchronousFlattenErrorz1FlattenerErrorTests.test_asynchronousFlattenError  sh   
 %-JJ	[	!	!	 	 	 	 	 	 	 	 	 
"	!	 #4*?*?*A*A)BCC
J'''9%%&&z>BB 	<MMMJ	 	  
  l  	
 	
 	
" 	-s7=/A/ABBB 	Wl33333r?   c                   	 dddt           t                   ddffd}d	dt          ddf	f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	cancelledr3   c                     | dz  d S )NrB  r   )rf  	cancelArgcancelCounts    r=   checkCancelz4FlattenerErrorTests.test_cancel.<locals>.checkCancel  s    !I1KKKr?   rc  c                     | d S rr   r   )rc  errs    r=   saveErrz0FlattenerErrorTests.test_cancel.<locals>.saveErr  s    CCCr?   rB  )r   r   r   r)   r[  
addErrbackcancelr]  r   r   assertIsassertIsInstancer   r]   r   args)
r<   rj  rm  r   rb  rc  rN  rh  ri  rl  s
          @@@r=   test_cancelzFlattenerErrorTests.test_cancel  su    		8F#3 	 	 	 	 	 	 	 	
 	W 	 	 	 	 	 	 	 '{33"4++
J'''	W &&z>BBa(((i###c7+++d7C006GGGm #c>22222r?   r   )
ry   rz   r{   r   r1  r9  r<  rP  rd  rs  r   r?   r=   r(  r(  F  s         
 
 
 
&
 
 
 
	
 	
 	
 	
,
 ,
 ,
 ,
\34 34 34 34j$3 $3 $3 $3 $3 $3r?   r(  )Fr   r_  rF  rD  collectionsr   r   r   typesr   typingr   r   r	   r
   r   r   r   xml.etree.ElementTreer   zope.interfacer   hamcrestr   r   twisted.internet.deferr   r   r   r   r   twisted.python.failurer   twisted.test.testutilsr   twisted.trial.unittestr   twisted.web._flattenr   twisted.web.errorr   r   r   twisted.web.iwebr   r   r    twisted.web.templater!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   twisted.web.test._utilr-   version_inforH  r2   r   rJ  co_filenamerI  r(  r   r?   r=   <module>r     s   
 
			 



     # # # # # #             H H H H H H H H H H H H H H H H H H % % % % % % & & & & & & * * * * * * * *              + * * * * * 4 4 4 4 4 4 6 6 6 6 6 6 , , , , , , K K K K K K K K K K C C C C C C C C C C                            3 2 2 2 2 2 !$/O; O; O; O; O;*; O; O; O;dp6 p6 p6 p6 p6. p6 p6 p6v 	*y3 y3 y3 y3 y3- y3 y3 y3 y3 y3r?   