o
    ¯b©p  ã                   @   s,  d Z ddlmZ ddl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 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lmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ee3dd�Z4G dd„ deƒZ5G dd„ deƒZ6G dd„ deƒZ7G dd„ dƒZ8G dd„ dee8ƒZ9G d d!„ d!ee8ƒZ:G d"d#„ d#ee8ƒZ;G d$d%„ d%ee8ƒZ<G d&d'„ d'e0ƒZ=G d(d)„ d)e0ƒZ>G d*d+„ d+e&ƒZ?G d,d-„ d-e&ƒZ@G d.d/„ d/e&ƒZAG d0d1„ d1ƒZBee!ƒG d2d3„ d3e2ƒƒZCG d4d5„ d5eƒZDd6S )7z#
Tests for L{twisted.web.template}
é    )ÚStringIO)ÚListÚOptional)Úimplementer)ÚverifyObject)ÚDeferredÚsucceed)ÚglobalLogPublisher)ÚFailure)ÚFilePath)ÚEventLoggingObserver)ÚTestCase)Úsuppress)ÚUnexposedMethodError)ÚFlattenerErrorÚMissingRenderMethodÚMissingTemplateLoader)ÚIRequestÚITemplateLoader)ÚNOT_DONE_YET)	ÚElementÚFlattenableÚTagÚ	TagLoaderÚXMLFileÚ	XMLStringÚrenderElementÚrendererÚtags)ÚFlattenTestCase)ÚDummyRequestúhPassing filenames or file objects to XMLFile is deprecated since Twisted 12.1.  Pass a FilePath instead.)ÚcategoryÚmessagec                   @   sB   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S )ÚTagFactoryTestszO
    Tests for L{_TagFactory} through the publicly-exposed L{tags} object.
    ÚreturnNc                 C   ó   t j}|  |jd¡ dS )z=
        HTML tags can be retrieved through C{tags}.
        ÚaN)r   r'   ÚassertEqualÚtagName©ÚselfÚtag© r-   ú@/usr/lib/python3/dist-packages/twisted/web/test/test_template.pyÚtest_lookupTag4   s   zTagFactoryTests.test_lookupTagc                 C   r&   )zj
        Twisted supports the latest and greatest HTML tags from the HTML5
        specification.
        ÚvideoN)r   r0   r(   r)   r*   r-   r-   r.   Útest_lookupHTML5Tag;   s   z#TagFactoryTests.test_lookupHTML5Tagc                 C   r&   )zÏ
        To support transparent inclusion in templates, there is a special tag,
        the transparent tag, which has no name of its own but is accessed
        through the "transparent" attribute.
        Ú N)r   Útransparentr(   r)   r*   r-   r-   r.   Útest_lookupTransparentTagC   s   z)TagFactoryTests.test_lookupTransparentTagc                 C   ó   |   tttd¡ dS )zv
        Invalid tags which are not part of HTML cause AttributeErrors when
        accessed through C{tags}.
        ÚinvalidN©ÚassertRaisesÚAttributeErrorÚgetattrr   ©r+   r-   r-   r.   Útest_lookupInvalidTagL   ó   z%TagFactoryTests.test_lookupInvalidTagc                 C   r5   )z‘
        As a special case, the <xmp> tag is simply not available through
        C{tags} or any other part of the templating machinery.
        ÚxmpNr7   r;   r-   r-   r.   Útest_lookupXMPS   r=   zTagFactoryTests.test_lookupXMP©r%   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r1   r4   r<   r?   r-   r-   r-   r.   r$   /   s    



	r$   c                   @   s`   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dd„Z
ddd„ZdS )ÚElementTestsz5
    Tests for the awesome new L{Element} class.
    r%   Nc                 C   s(   t ƒ }|  t|jd¡}|  |j|¡ dS )zr
        L{Element.render} raises L{MissingTemplateLoader} if the C{loader}
        attribute is L{None}.
        N)r   r8   r   ÚrenderÚassertIdenticalÚelement©r+   rH   Úerrr-   r-   r.   Útest_missingTemplateLoader`   s   z'ElementTests.test_missingTemplateLoaderc                 C   s*   G dd„ dt ƒ}|  dtt|ƒ ƒƒ¡ dS )zT
        A L{MissingTemplateLoader} instance can be repr()'d without error.
        c                   @   ó   e Zd Zdefdd„ZdS )zFElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElementr%   c                 S   ó   dS ©NúPretty Repr Elementr-   r;   r-   r-   r.   Ú__repr__o   ó   zOElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElement.__repr__N©rA   rB   rC   ÚstrrP   r-   r-   r-   r.   ÚPrettyReprElementn   ó    rT   rO   N)r   ÚassertInÚreprr   )r+   rT   r-   r-   r.   Útest_missingTemplateLoaderRepri   s   ÿz+ElementTests.test_missingTemplateLoaderReprc                 C   s6   t ƒ }|  t|jd¡}|  |j|¡ |  |jd¡ dS )zž
        When called with the name which is not associated with a render method,
        L{Element.lookupRenderMethod} raises L{MissingRenderMethod}.
        ÚfooN)r   r8   r   ÚlookupRenderMethodrG   rH   r(   Ú
renderNamerI   r-   r-   r.   Útest_missingRendererMethodv   s   z'ElementTests.test_missingRendererMethodc                 C   s<   G dd„ dt ƒ}tt|ƒ dƒƒ}|  d|¡ |  d|¡ dS )zR
        A L{MissingRenderMethod} instance can be repr()'d without error.
        c                   @   rL   )zDElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElementr%   c                 S   rM   rN   r-   r;   r-   r-   r.   rP   †   rQ   zMElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElement.__repr__NrR   r-   r-   r-   r.   rT   …   rU   rT   ÚexpectedMethodrO   N)r   rW   r   rV   )r+   rT   Úsr-   r-   r.   Útest_missingRenderMethodRepr€   s   z)ElementTests.test_missingRenderMethodReprc                 C   s4   G dd„ dt ƒ}|ƒ  d¡}|  |dtjƒd¡ dS )z‰
        When called with the name of a defined render method,
        L{Element.lookupRenderMethod} returns that render method.
        c                   @   ó*   e Zd Zedee dedefdd„ƒZdS )zBElementTests.test_definedRenderer.<locals>.ElementWithRenderMethodÚrequestr,   r%   c                 S   rM   )NÚbarr-   ©r+   ra   r,   r-   r-   r.   rY   ”   ó   zFElementTests.test_definedRenderer.<locals>.ElementWithRenderMethod.fooN)	rA   rB   rC   r   r   r   r   r   rY   r-   r-   r-   r.   ÚElementWithRenderMethod“   ó     re   rY   Nrb   )r   rZ   r(   r   Úbr)r+   re   rY   r-   r-   r.   Útest_definedRenderer�   s   z!ElementTests.test_definedRendererc                    sH   t tƒG dd„ dƒƒ‰ G ‡ fdd„dtƒ}|ƒ }|  | d¡dg¡ dS )zi
        L{Element.render} loads a document from the C{loader} attribute and
        returns it.
        c                   @   s   e Zd Zdee fdd„ZdS )z0ElementTests.test_render.<locals>.TemplateLoaderr%   c                 S   s   dgS )NÚresultr-   r;   r-   r-   r.   Úload£   s   z5ElementTests.test_render.<locals>.TemplateLoader.loadN)rA   rB   rC   r   r   rj   r-   r-   r-   r.   ÚTemplateLoader¡   s    rk   c                       s   e Zd Z” ƒ ZdS )z-ElementTests.test_render.<locals>.StubElementN)rA   rB   rC   Úloaderr-   ©rk   r-   r.   ÚStubElement¦   s    
rn   Nri   )r   r   r   r(   rF   )r+   rn   rH   r-   rm   r.   Útest_render›   s
   zElementTests.test_renderc                 C   s    |   tt¡}|  t|ƒd¡ dS )z‚
        If the L{renderer} decorator  is called without any arguments, it will
        raise a comprehensible exception.
        z,expose() takes at least 1 argument (0 given)N)r8   Ú	TypeErrorr   r(   rS   )r+   Úter-   r-   r.   Útest_misuseRenderer¬   s   z ElementTests.test_misuseRendererc                 C   s   |   ttjdd¡ dS )zŒ
        Called directly, without a default, L{renderer.get} raises
        L{UnexposedMethodError} when it cannot find a renderer.
        NÚnotARenderer)r8   r   r   Úgetr;   r-   r-   r.   Útest_renderGetDirectlyError´   s   z(ElementTests.test_renderGetDirectlyErrorr@   )rA   rB   rC   rD   rK   rX   r\   r_   rh   ro   rr   ru   r-   r-   r-   r.   rE   [   s    

	





rE   c                   @   s>   e Zd ZdZd
dd„Zd
dd„Zege_d
dd	„Zege_dS )ÚXMLFileReprTestszB
    Tests for L{twisted.web.template.XMLFile}'s C{__repr__}.
    r%   Nc                 C   ó(   t dƒ}|  d|›d�tt|ƒƒ¡ dS )zK
        An L{XMLFile} with a L{FilePath} returns a useful repr().
        ú/tmp/fake.xmlú<XMLFile of ú>N)r   r(   rW   r   )r+   Úpathr-   r-   r.   Útest_filePathÁ   ó    zXMLFileReprTests.test_filePathc                 C   s$   d}|   d|›d�tt|ƒƒ¡ dS )zH
        An L{XMLFile} with a filename returns a useful repr().
        rx   ry   rz   N)r(   rW   r   )r+   Úfnamer-   r-   r.   Útest_filenameÈ   s    zXMLFileReprTests.test_filenamec                 C   rw   )zK
        An L{XMLFile} with a file object returns a useful repr().
        znot xmlry   rz   N)r   r(   rW   r   )r+   Úfobjr-   r-   r.   Ú	test_fileÑ   r}   zXMLFileReprTests.test_filer@   )	rA   rB   rC   rD   r|   r   Ú_xmlFileSuppressr   r�   r-   r-   r-   r.   rv   ¼   s    


rv   c                   @   sH   e Zd ZU eed< 	 dZ	 defdd„Zddd„Zdd	d
„Z	e
ge	_dS )ÚXMLLoaderTestsMixinÚdeprecatedUseú<p>Hello, world.</p>r%   c                 C   s   t ‚©N)ÚNotImplementedErrorr;   r-   r-   r.   ÚloaderFactoryè   rQ   z!XMLLoaderTestsMixin.loaderFactoryNc                 C   s°   t | tƒsJ ‚|  ¡ }| ¡ \}t |tƒsJ ‚| j| jgd�}| jr?|  t|ƒd¡ |  |d d t	¡ |  |d d d¡ n|  t|ƒd¡ |  |j
d¡ |  |jdg¡ d	S )
zQ
        Verify that the loader returns a tag with the correct children.
        )ÚoffendingFunctionsé   r   r"   r#   r!   ÚpúHello, world.N)Ú
isinstancer   rˆ   rj   r   ÚflushWarningsr„   r(   ÚlenÚDeprecationWarningr)   Úchildren)r+   rl   r,   Úwarningsr-   r-   r.   Ú	test_loadë   s   

þzXMLLoaderTestsMixin.test_loadc                 C   s6   t | tƒsJ ‚|  ¡ }| ¡ }| ¡ }|  ||¡ dS )zd
        If {load()} can be called on a loader twice the result should be the
        same.
        N)r�   r   rˆ   rj   r(   )r+   rl   Útags1Útags2r-   r-   r.   Útest_loadTwice  s
   z"XMLLoaderTestsMixin.test_loadTwicer@   )rA   rB   rC   ÚboolÚ__annotations__ÚtemplateStringr   rˆ   r“   r–   r‚   r   r-   r-   r-   r.   rƒ   Û   s   
 

rƒ   c                   @   ó"   e Zd ZdZdZdefdd„ZdS )ÚXMLStringLoaderTestsz5
    Tests for L{twisted.web.template.XMLString}
    Fr%   c                 C   s
   t | jƒS )zS
        @return: an L{XMLString} constructed with C{self.templateString}.
        )r   r™   r;   r-   r-   r.   rˆ     s   
z"XMLStringLoaderTests.loaderFactoryN©rA   rB   rC   rD   r„   r   rˆ   r-   r-   r-   r.   r›     ó    r›   c                   @   rš   )ÚXMLFileWithFilePathTestszJ
    Tests for L{twisted.web.template.XMLFile}'s L{FilePath} support.
    Fr%   c                 C   s&   t |  ¡ ƒ}| | j d¡¡ t|ƒS )zŽ
        @return: an L{XMLString} constructed with a L{FilePath} pointing to a
            file that contains C{self.templateString}.
        Úutf8)r   ÚmktempÚ
setContentr™   Úencoder   ©r+   Úfpr-   r-   r.   rˆ   &  s   z&XMLFileWithFilePathTests.loaderFactoryNrœ   r-   r-   r-   r.   rž     r�   rž   c                   @   rš   )ÚXMLFileWithFileTestszU
    Tests for L{twisted.web.template.XMLFile}'s deprecated file object support.
    Tr%   c                 C   s   t t| jƒƒS )z{
        @return: an L{XMLString} constructed with a file object that contains
            C{self.templateString}.
        )r   r   r™   r;   r-   r-   r.   rˆ   7  s   z"XMLFileWithFileTests.loaderFactoryNrœ   r-   r-   r-   r.   r¥   0  r�   r¥   c                   @   rš   )ÚXMLFileWithFilenameTestszR
    Tests for L{twisted.web.template.XMLFile}'s deprecated filename support.
    Tr%   c                 C   s(   t |  ¡ ƒ}| | j d¡¡ t|jƒS )z‹
        @return: an L{XMLString} constructed with a filename that points to a
            file containing C{self.templateString}.
        rŸ   )r   r    r¡   r™   r¢   r   r{   r£   r-   r-   r.   rˆ   F  s   
z&XMLFileWithFilenameTests.loaderFactoryNrœ   r-   r-   r-   r.   r¦   ?  r�   r¦   c                   @   sÄ   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(d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(dd„Zd(d d!„Zd(d"d#„Zd(d$d%„Zd(d&d'„ZdS ))ÚFlattenIntegrationTestsz[
    Tests for integration between L{Element} and
    L{twisted.web._flatten.flatten}.
    r%   Nc                 C   s,   g d¢}|D ]}|   tt|ƒd�|¡ qdS )z¾
        Given a series of parsable XML strings, verify that
        L{twisted.web._flatten.flatten} will flatten the L{Element} back to the
        input when sent on a round trip.
        )ó   <p>Hello, world.</p>s   <p><!-- hello, world --></p>s    <p><![CDATA[Hello, world.]]></p>sB   <test1 xmlns:test2="urn:test2"><test2:test3></test2:test3></test1>s0   <test1 xmlns="urn:test2"><test3></test3></test1>ó
   <p>â˜ƒ</p>©rl   N)ÚassertFlattensImmediatelyr   r   )r+   Ú	fragmentsÚxmlr-   r-   r.   Útest_roundTripV  s   ÿz&FlattenIntegrationTests.test_roundTripc                 C   ó   t tdƒd�}|  |d¡ dS )zx
        When flattening an HTML entity, it should flatten out to the utf-8
        representation if possible.
        z<p>&#9731;</p>rª   r©   N©r   r   r«   ©r+   rH   r-   r-   r.   Útest_entityConversiong  s   z-FlattenIntegrationTests.test_entityConversionc                 C   s   |   tƒ t¡ dS )zk
        Rendering an Element without a loader attribute raises the appropriate
        exception.
        N)ÚassertFlatteningRaisesr   r   r;   r-   r-   r.   rK   o  s   z2FlattenIntegrationTests.test_missingTemplateLoaderc                 C   s   t tdƒd�}|  |t¡ dS )zÒ
        Flattening an L{Element} with a C{loader} which has a tag with a render
        directive fails with L{FlattenerError} if there is no available render
        method to satisfy that directive.
        zy
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="unknownMethod" />
        rª   N)r   r   r³   r   r±   r-   r-   r.   Útest_missingRenderMethodv  s   ÿÿz0FlattenIntegrationTests.test_missingRenderMethodc                 C   r¯   )zw
        A C{transparent} element should be eliminated from the DOM and rendered as
        only its children.
        zk<t:transparent xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">Hello, world.</t:transparent>rª   ó   Hello, world.Nr°   r±   r-   r-   r.   Útest_transparentRendering†  s   ÿÿz1FlattenIntegrationTests.test_transparentRenderingc                 C   r¯   )z~
        An Element with an attr tag renders the vaule of its attr tag as an
        attribute of its containing tag.
        z‚<a xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"><t:attr name="href">http://example.com</t:attr>Hello, world.</a>rª   s.   <a href="http://example.com">Hello, world.</a>Nr°   r±   r-   r-   r.   Útest_attrRendering•  s   ÿÿÿz*FlattenIntegrationTests.test_attrRenderingc                 C   s   |   dd„ tdƒD ƒd¡ dS )z–
        When rendering a large number of already-fired Deferreds we should not
        encounter any recursion errors or stack-depth issues.
        c                 S   s   g | ]}t d ƒ‘qS )Úx©r   )Ú.0Úir-   r-   r.   Ú
<listcomp>«  s    zMFlattenIntegrationTests.test_synchronousDeferredRecursion.<locals>.<listcomp>éú   sú   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxN)r«   Úranger;   r-   r-   r.   Ú!test_synchronousDeferredRecursion¦  s   z9FlattenIntegrationTests.test_synchronousDeferredRecursionc                 C   ó   |   ttd¡ dS )z{
        A template with a toplevel C{attr} tag will not load; it will raise
        L{AssertionError} if you try.
        z˜<t:attr
            xmlns:t='http://twistedmatrix.com/ns/twisted.web.template/0.1'
            name='something'
            >hello</t:attr>
            N©r8   ÚAssertionErrorr   r;   r-   r-   r.   Útest_errorToplevelAttr­  ó
   ýz.FlattenIntegrationTests.test_errorToplevelAttrc                 C   rÀ   )z�
        A template with an C{attr} tag with no C{name} attribute will not load;
        it will raise L{AssertionError} if you try.
        z{<html><t:attr
            xmlns:t='http://twistedmatrix.com/ns/twisted.web.template/0.1'
            >hello</t:attr></html>NrÁ   r;   r-   r-   r.   Útest_errorUnnamedAttr¼  rÄ   z-FlattenIntegrationTests.test_errorUnnamedAttrc                 C   s(   d}t t|ƒd�}|  || d¡¡ dS )zˆ
        If the parser sees a prefix it doesn't recognize on an attribute, it
        will pass it on through to serialization.
        z}<hello:world hello:sample="testing" xmlns:hello="http://made-up.example.com/ns/not-real">This is a made-up tag.</hello:world>rª   rŸ   N)r   r   ÚassertFlattensTor¢   )r+   ÚtheInputrH   r-   r-   r.   Útest_lenientPrefixBehaviorÉ  s   ÿz2FlattenIntegrationTests.test_lenientPrefixBehaviorc                 C   ó.   G dd„ dt ƒ}|tdƒd�}|  |d¡ dS )zi
        An Element with a render method which returns a Deferred will render
        correctly.
        c                   @   r`   )zHFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElementra   r,   r%   c                 S   s   t dƒS ©NrŒ   r¹   rc   r-   r-   r.   ÚrenderMethodÝ  s   zUFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElement.renderMethodN©	rA   rB   rC   r   r   r   r   r   rË   r-   r-   r-   r.   ÚRenderfulElementÜ  ó    ÿÿþrÍ   úŸ
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="renderMethod">
            Goodbye, world.
        </p>
        rª   rµ   Nr°   ©r+   rÍ   rH   r-   r-   r.   Útest_deferredRenderingÖ  ó   ÿÿ
z.FlattenIntegrationTests.test_deferredRenderingc                 C   s"   G dd„ dt ƒ}|  |ƒ d¡ dS )z£
        If there is a non-None loader attribute on the class of an Element
        instance but none on the instance itself, the class attribute is used.
        c                   @   s   e Zd ZedƒZdS )zEFlattenIntegrationTests.test_loaderClassAttribute.<locals>.SubElementr…   N)rA   rB   rC   r   rl   r-   r-   r-   r.   Ú
SubElementõ  s    rÓ   r¨   N)r   r«   )r+   rÓ   r-   r-   r.   Útest_loaderClassAttributeï  s   z1FlattenIntegrationTests.test_loaderClassAttributec                    s6   g ‰ G ‡ fdd„dt ƒ}|tdƒd�}|  |d¡ dS )z�
        An Element with a valid render directive has that directive invoked and
        the result added to the output.
        c                       ó.   e Zd Zedee dedef‡ fdd„ƒZdS )zIFlattenIntegrationTests.test_directiveRendering.<locals>.RenderfulElementra   r,   r%   c                    s   ˆ   | |f¡ |dƒS rÊ   )Úappendrc   ©Úrendersr-   r.   rË     s   zVFlattenIntegrationTests.test_directiveRendering.<locals>.RenderfulElement.renderMethodNrÌ   r-   r×   r-   r.   rÍ     s    ÿÿþrÍ   úx
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="renderMethod" />
        rª   r¨   Nr°   rÐ   r-   r×   r.   Útest_directiveRenderingú  s   ÿÿz/FlattenIntegrationTests.test_directiveRenderingc                 C   rÉ   )z‡
        An Element with a render method which omits the containing tag
        successfully removes that tag from the output.
        c                   @   r`   )zTFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElementra   r,   r%   c                 S   rM   rÊ   r-   rc   r-   r-   r.   rË     s   zaFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElement.renderMethodNrÌ   r-   r-   r-   r.   rÍ     rÎ   rÍ   rÏ   rª   rµ   Nr°   rÐ   r-   r-   r.   Ú"test_directiveRenderingOmittingTag  rÒ   z:FlattenIntegrationTests.test_directiveRenderingOmittingTagc                 C   rÉ   )út
        An Element which is returned by the render method of another Element is
        rendered properly.
        c                   @   r`   )zUFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElementra   r,   r%   c                 S   s   |t tdƒd�ƒS )Nz<em>Hello, world.</em>rª   )r   r   rc   r-   r-   r.   rË   3  s   zbFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElement.renderMethodNrÌ   r-   r-   r-   r.   rÍ   2  rÎ   rÍ   rÙ   rª   s   <p><em>Hello, world.</em></p>Nr°   rÐ   r-   r-   r.   Ú#test_elementContainingStaticElement,  s   ÿÿz;FlattenIntegrationTests.test_elementContainingStaticElementc                 C   rÉ   )rÜ   c                   @   r`   )zHFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElementra   r,   r%   c                 S   s   |j dd�S )Nzworld.)Útest2)Ú	fillSlotsrc   r-   r-   r.   rË   J  ó   zUFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElement.renderMethodNrÌ   r-   r-   r-   r.   rÍ   I  rÎ   rÍ   zž<p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1" t:render="renderMethod"><t:slot name="test1" default="Hello, " /><t:slot name="test2" /></p>rª   r¨   Nr°   rÐ   r-   r-   r.   Útest_elementUsingSlotsC  s   ÿÿ	z.FlattenIntegrationTests.test_elementUsingSlotsc                    sB   G ‡ fdd„dt ƒ}G dd„ dt ƒ‰ |tdƒd�}|  |d¡ dS )	z¿
        Directives in the document factory of an Element returned from a render
        method of another Element are satisfied from the correct object: the
        "inner" Element.
        c                       rÕ   )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElementra   r,   r%   c                    s   |ˆ t dƒd�ƒS )Nz©
                <t:ignored
                  xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
                  t:render="innerMethod" />
                rª   )r   rc   ©ÚInnerElementr-   r.   ÚouterMethodc  s   ÿÿÿz^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElement.outerMethodN)	rA   rB   rC   r   r   r   r   r   rä   r-   râ   r-   r.   ÚOuterElementb  s    $rå   c                   @   r`   )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElementra   r,   r%   c                 S   rM   rÊ   r-   rc   r-   r-   r.   ÚinnerMethodr  rd   z^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElement.innerMethodN)	rA   rB   rC   r   r   r   r   r   ræ   r-   r-   r-   r.   rã   q  rf   rã   zw
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="outerMethod" />
        rª   r¨   Nr°   )r+   rå   rH   r-   râ   r.   Ú$test_elementContainingDynamicElement[  s   ÿÿz<FlattenIntegrationTests.test_elementContainingDynamicElementc                    sR   t dƒ‰G ‡ ‡fdd„dtƒ‰ ˆ ƒ }ˆ ƒ }|  |d¡ |  |d¡ |  |d¡ dS )z
        Rendering the output of a loader, or even the same element, should
        return different output each time.
        zœ<p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"><t:transparent t:render="classCounter" /> <t:transparent t:render="instanceCounter" /></p>c                       sX   e Zd ZdZdZ”Zedee de	de
f‡ fdd„ƒZedee de	de
fdd„ƒZd	S )
zHFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElementr   ra   r,   r%   c                    s   ˆ  j d7  _ |tˆ j ƒƒS ©NrŠ   )ÚcountrS   rc   )ÚDestructiveElementr-   r.   ÚclassCounter‘  ó   zUFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.classCounterc                 S   s   |  j d7  _ |t| j ƒƒS rè   )ÚinstanceCountrS   rc   r-   r-   r.   ÚinstanceCounter˜  rì   zXFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.instanceCounterN)rA   rB   rC   ré   rí   rl   r   r   r   r   r   rë   rî   r-   ©rê   ÚsharedLoaderr-   r.   rê   Œ  s(    ÿÿþÿÿþrê   s
   <p>1 1</p>s
   <p>2 2</p>s
   <p>3 1</p>N)r   r   r«   )r+   Úe1Úe2r-   rï   r.   Útest_sameLoaderTwice€  s   ÿz,FlattenIntegrationTests.test_sameLoaderTwicer@   )rA   rB   rC   rD   r®   r²   rK   r´   r¶   r·   r¿   rÃ   rÅ   rÈ   rÑ   rÔ   rÚ   rÛ   rÝ   rá   rç   ró   r-   r-   r-   r.   r§   P  s(    
















%r§   c                   @   s8   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„ZdS )ÚTagLoaderTestsz!
    Tests for L{TagLoader}.
    r%   Nc                 C   s   t t d¡ƒ| _d S )NÚtest)r   r   r»   rl   r;   r-   r-   r.   ÚsetUp«  s   zTagLoaderTests.setUpc                 C   s   |   tt| jƒ¡ dS )zJ
        An instance of L{TagLoader} provides L{ITemplateLoader}.
        N)Ú
assertTruer   r   rl   r;   r-   r-   r.   Útest_interface®  ó   zTagLoaderTests.test_interfacec                 C   s   |   | j ¡ t¡ dS )zK
        L{TagLoader.load} returns a list, per L{ITemplateLoader}.
        N)ÚassertIsInstancerl   rj   Úlistr;   r-   r-   r.   Útest_loadsList´  rù   zTagLoaderTests.test_loadsListc                 C   s   t | jƒ}|  |d¡ dS )zŽ
        L{TagLoader} can be used in an L{Element}, and flattens as the tag used
        to construct the L{TagLoader} would flatten.
        s   <i>test</i>N)r   rl   r«   )r+   Úer-   r-   r.   Útest_flattenº  s   
zTagLoaderTests.test_flattenr@   )rA   rB   rC   rD   rö   rø   rü   rþ   r-   r-   r-   r.   rô   ¦  s    


rô   c                   @   s   e Zd ZdZedƒZdS )ÚTestElementz:
    An L{Element} that can be rendered successfully.
    zS<p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">Hello, world.</p>N)rA   rB   rC   rD   r   rl   r-   r-   r-   r.   rÿ   Ã  s
    ÿrÿ   c                   @   s,   e Zd ZdZedƒZddedefdd„ZdS )	ÚTestFailureElementzˆ
    An L{Element} that can be used in place of L{FailureElement} to verify
    that L{renderElement} can render failures properly.
    zO<p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">I failed.</p>NÚfailurerl   c                 C   s
   || _ d S r†   )r  )r+   r  rl   r-   r-   r.   Ú__init__Û  s   
zTestFailureElement.__init__r†   )	rA   rB   rC   rD   r   rl   r
   Úobjectr  r-   r-   r-   r.   r   Ï  s    ÿr   c                   @   s&   e Zd ZdZdee ddfdd„ZdS )ÚFailingElementz<
    An element that raises an exception when rendered.
    ra   r%   r   c                 C   s   d}d}|| › S )Né*   r   r-   )r+   ra   r'   Úbr-   r-   r.   rF   ä  s   
zFailingElement.renderN)rA   rB   rC   rD   r   r   rF   r-   r-   r-   r.   r  ß  s    r  c                   @   s   e Zd ZdZdZdS )ÚFakeSitezL
    A minimal L{Site} object that we can use to test displayTracebacks
    FN)rA   rB   rC   rD   ÚdisplayTracebacksr-   r-   r-   r.   r  ê  s    r  c                       s"   e Zd ZdZd‡ fdd„Z‡  ZS )ÚDummyRenderRequestz¢
    A dummy request object that has a C{site} attribute.

    This does not implement the full IRequest interface, but enough of it
    for this test suite.
    r%   Nc                    s   t ƒ  dg¡ tƒ | _d S )Nr2   )Úsuperr  r  Úsiter;   ©Ú	__class__r-   r.   r  û  s   zDummyRenderRequest.__init__r@   )rA   rB   rC   rD   r  Ú__classcell__r-   r-   r  r.   r	  ò  s    r	  c                   @   st   e Zd ZdZddd„Zded fdd„Zded fdd	„Zded fd
d„Zded fdd„Z	ded fdd„Z
dS )ÚRenderElementTestsz
    Test L{renderElement}
    r%   Nc                 C   s   t ƒ | _dS )z8
        Set up a common L{DummyRenderRequest}.
        N)r	  ra   r;   r-   r-   r.   rö     rà   zRenderElementTests.setUpc                    óH   t ƒ }ˆ j ¡ }dtddf‡ fdd„}| |¡ ˆ  ttˆ j|ƒ¡ |S )z¢
        L{renderElement} returns NOT_DONE_YET and eventually
        writes the rendered L{Element} to the request before finishing the
        request.
        Ú_r%   Nc                    s(   ˆ   d ˆ jj¡d¡ ˆ  ˆ jj¡ d S )Nó    s$   <!DOCTYPE html>
<p>Hello, world.</p>)r(   Újoinra   Úwrittenr÷   Úfinished©r  r;   r-   r.   Úcheck  s
   þz3RenderElementTests.test_simpleRender.<locals>.check)rÿ   ra   ÚnotifyFinishr  ÚaddCallbackrG   r   r   ©r+   rH   Údr  r-   r;   r.   Útest_simpleRender  s   

z$RenderElementTests.test_simpleRenderc                    r  )z
        L{renderElement} handles failures by writing a minimal
        error message to the request and finishing it.
        r  r%   Nc                    sB   ˆ   t¡}ˆ  t|ƒd¡ ˆ  d ˆ jj¡d¡ ˆ  ˆ jj¡ d S )NrŠ   r  s‚   <!DOCTYPE html>
<div style="font-size:800%;background-color:#FFF;color:#F00">An error occurred while rendering the response.</div>)	ÚflushLoggedErrorsr   r(   r�   r  ra   r  r÷   r  )r  Úflushedr;   r-   r.   r  +  s   
ý
z4RenderElementTests.test_simpleFailure.<locals>.check)r  ra   r  r  r  rG   r   r   r  r-   r;   r.   Útest_simpleFailure"  s   

z%RenderElementTests.test_simpleFailurec                    s\   t  ˆt¡‰ dˆjj_tƒ }ˆj ¡ }dtddf‡ ‡fdd„}| 	|¡ t
ˆj|td� |S )z˜
        L{renderElement} will render a traceback when rendering of
        the element fails and our site is configured to display tracebacks.
        Tr  r%   Nc                    sl   ˆ  dtˆ ƒ¡ ˆ d d }ˆ |jt¡ ˆ t¡}ˆ t|ƒd¡ ˆ d ˆjj	¡d¡ ˆ 
ˆjj¡ d S )NrŠ   r   Úlog_failurer  s    <!DOCTYPE html>
<p>I failed.</p>)ÚassertEqualsr�   rú   Úvaluer   r  r(   r  ra   r  r÷   r  )r  Úfr  ©ÚlogObserverr+   r-   r.   r  L  s   
ÿzARenderElementTests.test_simpleFailureWithTraceback.<locals>.check)Ú_failElement)r   ÚcreateWithCleanupr	   ra   r  r  r  r  r  r  r   r   r  r-   r$  r.   Útest_simpleFailureWithTraceback@  s   


z2RenderElementTests.test_simpleFailureWithTracebackc                    sD   t ƒ }ˆ j ¡ }dtddf‡ fdd„}| |¡ tˆ j|dd� |S )zs
        L{renderElement} will write the doctype string specified by the
        doctype keyword argument.
        r  r%   Nc                    ó   ˆ   d ˆ jj¡d¡ d S )Nr  s‚   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<p>Hello, world.</p>©r(   r  ra   r  r  r;   r-   r.   r  f  s   ýz8RenderElementTests.test_nonDefaultDoctype.<locals>.checksm   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">©Údoctype©rÿ   ra   r  r  r  r   r  r-   r;   r.   Útest_nonDefaultDoctype]  s   


ü	z)RenderElementTests.test_nonDefaultDoctypec                    sD   t ƒ }ˆ j ¡ }dtddf‡ fdd„}| |¡ tˆ j|dd� |S )zs
        L{renderElement} will not write out a doctype if the doctype keyword
        argument is L{None}.
        r  r%   Nc                    r)  )Nr  r¨   r*  r  r;   r-   r.   r  †  s   z2RenderElementTests.test_noneDoctype.<locals>.checkr+  r-  r  r-   r;   r.   Útest_noneDoctype}  s   

z#RenderElementTests.test_noneDoctyper@   )rA   rB   rC   rD   rö   r   r  r  r(  r.  r/  r-   r-   r-   r.   r     s    
 r  N)ErD   Úior   Útypingr   r   Úzope.interfacer   Úzope.interface.verifyr   Útwisted.internet.deferr   r   Útwisted.loggerr	   Útwisted.python.failurer
   Útwisted.python.filepathr   Útwisted.test.proto_helpersr   Útwisted.trial.unittestr   Útwisted.trial.utilr   ÚSUPPRESSÚtwisted.web._elementr   Útwisted.web.errorr   r   r   Útwisted.web.iwebr   r   Útwisted.web.serverr   Útwisted.web.templater   r   r   r   r   r   r   r   r   Útwisted.web.test._utilr   Útwisted.web.test.test_webr    r�   r‚   r$   rE   rv   rƒ   r›   rž   r¥   r¦   r§   rô   rÿ   r   r  r  r	  r  r-   r-   r-   r.   Ú<module>   sT   ,þ,a6  X