U
    ˆj†q  ã                   @   s4  d Z ddl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l0m1Z1 ddl2m3Z3 ee4dd�Z5G dd„ deƒZ6G dd„ deƒZ7G dd„ deƒZ8G dd„ dƒZ9G dd „ d ee9ƒZ:G d!d"„ d"ee9ƒZ;G d#d$„ d$ee9ƒZ<G d%d&„ d&ee9ƒZ=G d'd(„ d(e1ƒZ>G d)d*„ d*e1ƒZ?G d+d,„ d,e'ƒZ@G d-d.„ d.e'ƒZAG d/d0„ d0e'ƒZBG d1d2„ d2ƒZCee"ƒG d3d4„ d4e3ƒƒZDG d5d6„ d6eƒZEdS )7z#
Tests for L{twisted.web.template}
é    N)ÚStringIO)ÚListÚOptional)Úimplementer)ÚverifyObject)ÚDeferredÚsucceed)ÚEventLoggingObserver)ÚglobalLogPublisher)ÚFailure)ÚFilePath)Ú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                   @   sV   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„ZdS )ÚTagFactoryTestszO
    Tests for L{_TagFactory} through the publicly-exposed L{tags} object.
    N©Úreturnc                 C   s   t j}|  |jd¡ dS )z=
        HTML tags can be retrieved through C{tags}.
        ÚaN)r   r'   ÚassertEqualÚtagName©ÚselfÚtag© r-   úB/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/web/test/test_template.pyÚtest_lookupTag4   s    zTagFactoryTests.test_lookupTagc                 C   s   t j}|  |jd¡ dS )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   s   t j}|  |jd¡ dS )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   Ztransparentr(   r)   r*   r-   r-   r.   Útest_lookupTransparentTagC   s    z)TagFactoryTests.test_lookupTransparentTagc                 C   s   |   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   s    z%TagFactoryTests.test_lookupInvalidTagc                 C   s   |   tttd¡ dS )z‘
        As a special case, the <xmp> tag is simply not available through
        C{tags} or any other part of the templating machinery.
        ÚxmpNr5   r9   r-   r-   r.   Útest_lookupXMPS   s    zTagFactoryTests.test_lookupXMP)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r1   r3   r:   r<   r-   r-   r-   r.   r$   /   s   	r$   c                   @   s€   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„ZdS )ÚElementTestsz5
    Tests for the awesome new L{Element} class.
    Nr%   c                 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   r6   r   ÚrenderÚassertIdenticalÚelement©r+   rD   Ú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                   @   s   e Zd Zedœdd„ZdS )zFElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElementr%   c                 S   s   dS ©NúPretty Repr Elementr-   r9   r-   r-   r.   Ú__repr__o   s    zOElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElement.__repr__N©r=   r>   r?   ÚstrrJ   r-   r-   r-   r.   ÚPrettyReprElementn   s   rM   rI   N)r   ÚassertInÚreprr   )r+   rM   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   r6   r   ÚlookupRenderMethodrC   rD   r(   Z
renderNamerE   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                   @   s   e Zd Zedœdd„ZdS )zDElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElementr%   c                 S   s   dS rH   r-   r9   r-   r-   r.   rJ   †   s    zMElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElement.__repr__NrK   r-   r-   r-   r.   rM   …   s   rM   ZexpectedMethodrI   N)r   rO   r   rN   )r+   rM   Ú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                   @   s&   e Zd Zeee eedœdd„ƒZdS )zBElementTests.test_definedRenderer.<locals>.ElementWithRenderMethod©Úrequestr,   r&   c                 S   s   dS )NÚbarr-   ©r+   rW   r,   r-   r-   r.   rQ   ”   s    zFElementTests.test_definedRenderer.<locals>.ElementWithRenderMethod.fooN)	r=   r>   r?   r   r   r   r   r   rQ   r-   r-   r-   r.   ÚElementWithRenderMethod“   s   rZ   rQ   NrX   )r   rR   r(   r   Úbr)r+   rZ   rQ   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ee dœdd„ZdS )z0ElementTests.test_render.<locals>.TemplateLoaderr%   c                 S   s   dgS )NÚresultr-   r9   r-   r-   r.   Úload£   s    z5ElementTests.test_render.<locals>.TemplateLoader.loadN)r=   r>   r?   r   r   r^   r-   r-   r-   r.   ÚTemplateLoader¡   s   r_   c                       s   e Zd Z” ƒ ZdS )z-ElementTests.test_render.<locals>.StubElementN)r=   r>   r?   Úloaderr-   ©r_   r-   r.   ÚStubElement¦   s   rb   Nr]   )r   r   r   r(   rB   )r+   rb   rD   r-   ra   r.   Útest_render›   s
    zElementTests.test_renderc                 C   s<   |   tt¡}tjdkr(|  t|ƒd¡ n|  t|ƒd¡ dS )z‚
        If the L{renderer} decorator  is called without any arguments, it will
        raise a comprehensible exception.
        )é   é
   z=Expose.__call__() missing 1 required positional argument: 'f'z6__call__() missing 1 required positional argument: 'f'N)r6   Ú	TypeErrorr   ÚsysÚversion_infor(   rL   )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.
        NZnotARenderer)r6   r   r   Úgetr9   r-   r-   r.   Útest_renderGetDirectlyError»   s    z(ElementTests.test_renderGetDirectlyError)r=   r>   r?   r@   rG   rP   rS   rU   r\   rc   rj   rl   r-   r-   r-   r.   rA   [   s   	
rA   c                   @   sJ   e Zd ZdZddœdd„Zddœdd„Zege_ddœdd	„Zege_dS )
ÚXMLFileReprTestszB
    Tests for L{twisted.web.template.XMLFile}'s C{__repr__}.
    Nr%   c                 C   s(   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(   rO   r   )r+   Úpathr-   r-   r.   Útest_filePathÈ   s    zXMLFileReprTests.test_filePathc                 C   s$   d}|   d|›d�tt|ƒƒ¡ dS )zH
        An L{XMLFile} with a filename returns a useful repr().
        rn   ro   rp   N)r(   rO   r   )r+   Úfnamer-   r-   r.   Útest_filenameÏ   s    zXMLFileReprTests.test_filenamec                 C   s(   t dƒ}|  d|›d�tt|ƒƒ¡ dS )zK
        An L{XMLFile} with a file object returns a useful repr().
        znot xmlro   rp   N)r   r(   rO   r   )r+   Zfobjr-   r-   r.   Ú	test_fileØ   s    zXMLFileReprTests.test_file)	r=   r>   r?   r@   rr   rt   Ú_xmlFileSuppressr   ru   r-   r-   r-   r.   rm   Ã   s   rm   c                   @   sL   e Zd ZU eed< dZedœdd„Zddœdd„Zddœd	d
„Z	e
ge	_dS )ÚXMLLoaderTestsMixinÚdeprecatedUseú<p>Hello, world.</p>r%   c                 C   s   t ‚d S ©N)ÚNotImplementedErrorr9   r-   r-   r.   ÚloaderFactoryî   s    z!XMLLoaderTestsMixin.loaderFactoryNc                 C   s°   t | tƒst‚|  ¡ }| ¡ \}t |tƒs.t‚| 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.
        )ZoffendingFunctionsé   r   r"   r#   r!   ÚpúHello, world.N)Ú
isinstancer   ÚAssertionErrorr|   r^   r   ZflushWarningsrx   r(   ÚlenÚDeprecationWarningr)   Úchildren)r+   r`   r,   Úwarningsr-   r-   r.   Ú	test_loadñ   s    

þzXMLLoaderTestsMixin.test_loadc                 C   s6   t | tƒst‚|  ¡ }| ¡ }| ¡ }|  ||¡ dS )zd
        If {load()} can be called on a loader twice the result should be the
        same.
        N)r€   r   r�   r|   r^   r(   )r+   r`   Ztags1Ztags2r-   r-   r.   Útest_loadTwice	  s
    z"XMLLoaderTestsMixin.test_loadTwice)r=   r>   r?   ÚboolÚ__annotations__ÚtemplateStringr   r|   r†   r‡   rv   r   r-   r-   r-   r.   rw   â   s   
rw   c                   @   s"   e Zd ZdZdZedœ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Š   r9   r-   r-   r.   r|     s    z"XMLStringLoaderTests.loaderFactoryN©r=   r>   r?   r@   rx   r   r|   r-   r-   r-   r.   r‹     s   r‹   c                   @   s"   e Zd ZdZdZedœdd„ZdS )Ú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�   %  s   r�   c                   @   s"   e Zd ZdZdZedœdd„ZdS )Ú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Š   r9   r-   r-   r.   r|   =  s    z"XMLFileWithFileTests.loaderFactoryNrŒ   r-   r-   r-   r.   r”   6  s   r”   c                   @   s"   e Zd ZdZdZedœdd„ZdS )Ú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   rq   r’   r-   r-   r.   r|   L  s    z&XMLFileWithFilenameTests.loaderFactoryNrŒ   r-   r-   r-   r.   r•   E  s   r•   c                   @   s  e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„ZdS )(ÚFlattenIntegrationTestsz[
    Tests for integration between L{Element} and
    L{twisted.web._flatten.flatten}.
    Nr%   c                 C   s4   ddddddg}|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>©r`   N)ÚassertFlattensImmediatelyr   r   )r+   Z	fragmentsÚxmlr-   r-   r.   Útest_roundTrip\  s    úz&FlattenIntegrationTests.test_roundTripc                 C   s   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+   rD   r-   r-   r.   Útest_entityConversionm  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   r9   r-   r-   r.   rG   u  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_missingRenderMethod|  s    ÿÿz0FlattenIntegrationTests.test_missingRenderMethodc                 C   s   t tdƒd�}|  |d¡ dS )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   s   t tdƒd�}|  |d¡ dS )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š   Úranger9   r-   r-   r.   Ú!test_synchronousDeferredRecursion¬  s    z9FlattenIntegrationTests.test_synchronousDeferredRecursionc                 C   s   |   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©r6   r�   r   r9   r-   r-   r.   Útest_errorToplevelAttr³  s
    ýz.FlattenIntegrationTests.test_errorToplevelAttrc                 C   s   |   ttd¡ dS )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­   r9   r-   r-   r.   Útest_errorUnnamedAttrÂ  s
    ý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   ZassertFlattensTor‘   )r+   ZtheInputrD   r-   r-   r.   Útest_lenientPrefixBehaviorÏ  s    ÿz2FlattenIntegrationTests.test_lenientPrefixBehaviorc                 C   s.   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                   @   s&   e Zd Zeee eedœdd„ƒZdS )zHFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElementrV   c                 S   s   t dƒS ©Nr   r¦   rY   r-   r-   r.   ÚrenderMethodã  s    zUFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElement.renderMethodN©	r=   r>   r?   r   r   r   r   r   r²   r-   r-   r-   r.   ÚRenderfulElementâ  s
    þr´   úŸ
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="renderMethod">
            Goodbye, world.
        </p>
        r™   r¢   Nr�   ©r+   r´   rD   r-   r-   r.   Útest_deferredRenderingÜ  s    ÿÿ
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>.SubElementry   N)r=   r>   r?   r   r`   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                       s*   e Zd Zeee eedœ‡ fdd„ƒZdS )zIFlattenIntegrationTests.test_directiveRendering.<locals>.RenderfulElementrV   c                    s   ˆ   | |f¡ |dƒS r±   )ÚappendrY   ©Z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   s.   G dd„ dt ƒ}|tdƒd�}|  |d¡ dS )z‡
        An Element with a render method which omits the containing tag
        successfully removes that tag from the output.
        c                   @   s&   e Zd Zeee eedœdd„ƒZdS )zTFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElementrV   c                 S   s   dS r±   r-   rY   r-   r-   r.   r²      s    zaFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElement.renderMethodNr³   r-   r-   r-   r.   r´     s
    þr´   rµ   r™   r¢   Nr�   r¶   r-   r-   r.   Ú"test_directiveRenderingOmittingTag  s    ÿÿ
z:FlattenIntegrationTests.test_directiveRenderingOmittingTagc                 C   s.   G dd„ dt ƒ}|tdƒd�}|  |d¡ dS )út
        An Element which is returned by the render method of another Element is
        rendered properly.
        c                   @   s&   e Zd Zeee eedœdd„ƒZdS )zUFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElementrV   c                 S   s   |t tdƒd�ƒS )Nz<em>Hello, world.</em>r™   )r   r   rY   r-   r-   r.   r²   9  s    zbFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElement.renderMethodNr³   r-   r-   r-   r.   r´   8  s
    þr´   r¼   r™   s   <p><em>Hello, world.</em></p>Nr�   r¶   r-   r-   r.   Ú#test_elementContainingStaticElement2  s    ÿÿz;FlattenIntegrationTests.test_elementContainingStaticElementc                 C   s.   G dd„ dt ƒ}|tdƒd�}|  |d¡ dS )r¿   c                   @   s&   e Zd Zeee eedœdd„ƒZdS )zHFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElementrV   c                 S   s   |j dd�S )Nzworld.)Ztest2)Z	fillSlotsrY   r-   r-   r.   r²   P  s    zUFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElement.renderMethodNr³   r-   r-   r-   r.   r´   O  s
    þ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_elementUsingSlotsI  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                       s*   e Zd Zeee eedœ‡ fdd„ƒZdS )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElementrV   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   rY   ©ÚInnerElementr-   r.   ÚouterMethodi  s    ÿÿÿz^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElement.outerMethodN)	r=   r>   r?   r   r   r   r   r   rÄ   r-   rÂ   r-   r.   ÚOuterElementh  s   rÅ   c                   @   s&   e Zd Zeee eedœdd„ƒZdS )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElementrV   c                 S   s   dS r±   r-   rY   r-   r-   r.   ÚinnerMethodx  s    z^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElement.innerMethodN)	r=   r>   r?   r   r   r   r   r   rÆ   r-   r-   r-   r.   rÃ   w  s   rÃ   zw
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="outerMethod" />
        r™   r—   Nr�   )r+   rÅ   rD   r-   rÂ   r.   Ú$test_elementContainingDynamicElementa  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                       sP   e Zd ZdZdZ”Zeee e	e
dœ‡ fdd„ƒZeee e	e
dœdd„ƒZdS )zHFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElementr   rV   c                    s   ˆ  j d7  _ |tˆ j ƒƒS ©Nr}   )ÚcountrL   rY   )ÚDestructiveElementr-   r.   ÚclassCounter—  s    zUFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.classCounterc                 S   s   |  j d7  _ |t| j ƒƒS rÈ   )ÚinstanceCountrL   rY   r-   r-   r.   ÚinstanceCounterž  s    zXFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.instanceCounterN)r=   r>   r?   rÉ   rÌ   r`   r   r   r   r   r   rË   rÍ   r-   ©rÊ   Z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_sameLoaderTwice)r=   r>   r?   r@   rœ   rŸ   rG   r¡   r£   r¤   r¬   r®   r¯   r°   r·   r¹   r½   r¾   rÀ   rÁ   rÇ   rÑ   r-   r-   r-   r.   r–   V  s&   %r–   c                   @   sH   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„ZdS )ÚTagLoaderTestsz!
    Tests for L{TagLoader}.
    Nr%   c                 C   s   t t d¡ƒ| _d S )NÚtest)r   r   r¨   r`   r9   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   r`   r9   r-   r-   r.   Útest_interface´  s    zTagLoaderTests.test_interfacec                 C   s   |   | j ¡ t¡ dS )zK
        L{TagLoader.load} returns a list, per L{ITemplateLoader}.
        N)ÚassertIsInstancer`   r^   Úlistr9   r-   r-   r.   Útest_loadsListº  s    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   r`   rš   )r+   Úer-   r-   r.   Útest_flattenÀ  s    
zTagLoaderTests.test_flatten)r=   r>   r?   r@   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)r=   r>   r?   r@   r   r`   r-   r-   r-   r.   rÜ   É  s   ÿrÜ   c                   @   s,   e Zd ZdZedƒZdeeddœ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)Úfailurer`   r&   c                 C   s
   || _ d S rz   )rÞ   )r+   rÞ   r`   r-   r-   r.   Ú__init__á  s    zTestFailureElement.__init__)N)	r=   r>   r?   r@   r   r`   r   Úobjectrß   r-   r-   r-   r.   rÝ   Õ  s
   ÿrÝ   c                   @   s$   e Zd ZdZee ddœdd„ZdS )ÚFailingElementz<
    An element that raises an exception when rendered.
    r   )rW   r&   c                 C   s   d}d}|| › S )Né*   r   r-   )r+   rW   r'   Úbr-   r-   r.   rB   ê  s    zFailingElement.renderN)r=   r>   r?   r@   r   r   rB   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)r=   r>   r?   r@   ÚdisplayTracebacksr-   r-   r-   r.   rä   ð  s   rä   c                       s&   e Zd ZdZd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.
    Nr%   c                    s   t ƒ  dg¡ tƒ | _d S )Nó    )Úsuperrß   rä   Úsiter9   ©Ú	__class__r-   r.   rß     s    zDummyRenderRequest.__init__)r=   r>   r?   r@   rß   Ú__classcell__r-   r-   rê   r.   ræ   ø  s   ræ   c                   @   sx   e Zd ZdZddœdd„Zed dœdd„Zed dœdd	„Zed dœd
d„Zed dœdd„Z	ed dœdd„Z
dS )ÚRenderElementTestsz
    Test L{renderElement}
    Nr%   c                 C   s   t ƒ | _dS )z8
        Set up a common L{DummyRenderRequest}.
        N)ræ   rW   r9   r-   r-   r.   rÔ     s    zRenderElementTests.setUpc                    sF   t ƒ }ˆ j ¡ }tddœ‡ 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.
        N©Ú_r&   c                    s(   ˆ   d ˆ jj¡d¡ ˆ  ˆ jj¡ d S )Nrç   s$   <!DOCTYPE html>
<p>Hello, world.</p>)r(   ÚjoinrW   ÚwrittenrÕ   Úfinished©rï   r9   r-   r.   Úcheck  s
    þz3RenderElementTests.test_simpleRender.<locals>.check)rÜ   rW   ÚnotifyFinishrà   ÚaddCallbackrC   r   r   ©r+   rD   Údrô   r-   r9   r.   Útest_simpleRender  s    

z$RenderElementTests.test_simpleRenderc                    sF   t ƒ }ˆ j ¡ }tddœ‡ fdd„}| |¡ ˆ  ttˆ j|ƒ¡ |S )z
        L{renderElement} handles failures by writing a minimal
        error message to the request and finishing it.
        Nrî   c                    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ð   rW   rñ   rÕ   rò   )rï   Úflushedr9   r-   r.   rô   1  s    
ý
z4RenderElementTests.test_simpleFailure.<locals>.check)rá   rW   rõ   rà   rö   rC   r   r   r÷   r-   r9   r.   Útest_simpleFailure(  s    

z%RenderElementTests.test_simpleFailurec                    sZ   t  ˆt¡‰ dˆjj_tƒ }ˆj ¡ }tddœ‡ ‡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.
        TNrî   c                    sl   ˆ  dtˆ ƒ¡ ˆ d d }ˆ |jt¡ ˆ t¡}ˆ t|ƒd¡ ˆ d ˆjj	¡d¡ ˆ 
ˆjj¡ d S )Nr}   r   Zlog_failurerç   s    <!DOCTYPE html>
<p>I failed.</p>)ZassertEqualsr‚   r×   Úvaluer   rú   r(   rð   rW   rñ   rÕ   rò   )rï   Úfrû   ©ZlogObserverr+   r-   r.   rô   R  s    
 ÿzARenderElementTests.test_simpleFailureWithTraceback.<locals>.check)Z_failElement)r	   ZcreateWithCleanupr
   rW   ré   rå   rá   rõ   rà   rö   r   rÝ   r÷   r-   rÿ   r.   Útest_simpleFailureWithTracebackF  s    


z2RenderElementTests.test_simpleFailureWithTracebackc                    sB   t ƒ }ˆ j ¡ }tddœ‡ fdd„}| |¡ tˆ j|dd� |S )zs
        L{renderElement} will write the doctype string specified by the
        doctype keyword argument.
        Nrî   c                    s   ˆ   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ð   rW   rñ   ró   r9   r-   r.   rô   l  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Ü   rW   rõ   rà   rö   r   r÷   r-   r9   r.   Útest_nonDefaultDoctypec  s    


ü	z)RenderElementTests.test_nonDefaultDoctypec                    sB   t ƒ }ˆ j ¡ }tddœ‡ 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}.
        Nrî   c                    s   ˆ   d ˆ jj¡d¡ d S )Nrç   r—   r  ró   r9   r-   r.   rô   Œ  s    z2RenderElementTests.test_noneDoctype.<locals>.checkr  r  r÷   r-   r9   r.   Útest_noneDoctypeƒ  s    

z#RenderElementTests.test_noneDoctype)r=   r>   r?   r@   rÔ   r   rù   rü   r   r  r  r-   r-   r-   r.   rí     s    rí   )Fr@   rg   Úior   Útypingr   r   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.deferr   r   Ztwisted.internet.testingr	   Ztwisted.loggerr
   Ztwisted.python.failurer   Ztwisted.python.filepathr   Ztwisted.trial.unittestr   Ztwisted.trial.utilr   ÚSUPPRESSZtwisted.web._elementr   Ztwisted.web.errorr   r   r   Ztwisted.web.iwebr   r   Ztwisted.web.serverr   Ztwisted.web.templater   r   r   r   r   r   r   r   r   Ztwisted.web.test._utilr   Ztwisted.web.test.test_webr    rƒ   rv   r$   rA   rm   rw   r‹   r�   r”   r•   r–   rÒ   rÜ   rÝ   rá   rä   ræ   rí   r-   r-   r-   r.   Ú<module>   sT   ,þ,h5  X