o
    bp                     @   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)DummyRequesthPassing 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