U
    
W[ye                     @   s  d Z ddlmZ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mZmZmZmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z( ddl)m*Z* ddl+m,Z, ee-ddZ.G dd deZ/G dd deZ0G dd deZ1G dd de2Z3G dd dee3Z4G dd  d ee3Z5G d!d" d"ee3Z6G d#d$ d$ee3Z7G d%d& d&e!Z8G d'd( d(e!Z9G d)d* d*eZ:G d+d, d,eZ;G d-d. d.eZ<G d/d0 d0e2Z=G d1d2 d2eZ>d3S )4z#
Tests for L{twisted.web.template}
    )divisionabsolute_import)verifyObject)succeedgatherResults)FilePath)TestCase)suppress)Element	TagLoaderrenderertagsXMLFile	XMLString)ITemplateLoader)FlattenerErrorMissingTemplateLoaderMissingRenderMethod)renderElement)UnexposedMethodError)FlattenTestCase)DummyRequest)NOT_DONE_YET)NativeStringIO)globalLogPublisher)EventLoggingObserverhPassing filenames or file objects to XMLFile is deprecated since Twisted 12.1.  Pass a FilePath instead.)categorymessagec                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TagFactoryTestszO
    Tests for L{_TagFactory} through the publicly-exposed L{tags} object.
    c                 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&   @/usr/lib/python3/dist-packages/twisted/web/test/test_template.pytest_lookupTag+   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   r)   r!   r"   r#   r&   r&   r'   test_lookupHTML5Tag3   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_lookupTransparentTag<   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}.
        ZinvalidNassertRaisesAttributeErrorgetattrr   r$   r&   r&   r'   test_lookupInvalidTagF   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.
        ZxmpNr-   r1   r&   r&   r'   test_lookupXMPN   s    zTagFactoryTests.test_lookupXMPN)	__name__
__module____qualname____doc__r(   r*   r,   r2   r3   r&   r&   r&   r'   r   '   s   	
r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )ElementTestsz5
    Tests for the awesome new L{Element} class.
    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
   r.   r   renderassertIdenticalelementr$   r;   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dd ZdS )zFElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElementc                 S   s   dS NPretty Repr Elementr&   r1   r&   r&   r'   __repr__j   s    zOElementTests.test_missingTemplateLoaderRepr.<locals>.PrettyReprElement.__repr__Nr4   r5   r6   rA   r&   r&   r&   r'   PrettyReprElementi   s   rC   r@   N)r
   assertInreprr   )r$   rC   r&   r&   r'   test_missingTemplateLoaderRepre   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
   r.   r   lookupRenderMethodr:   r;   r!   Z
renderNamer<   r&   r&   r'   test_missingRendererMethodp   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dd ZdS )zDElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElementc                 S   s   dS r?   r&   r1   r&   r&   r'   rA      s    zMElementTests.test_missingRenderMethodRepr.<locals>.PrettyReprElement.__repr__NrB   r&   r&   r&   r'   rC      s   rC   ZexpectedMethodr@   N)r
   rE   r   rD   )r$   rC   sr&   r&   r'   test_missingRenderMethodRepr|   s    z)ElementTests.test_missingRenderMethodReprc                 C   s2   G dd dt }| d}| |dd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dd ZdS )zBElementTests.test_definedRenderer.<locals>.ElementWithRenderMethodc                 S   s   dS )Nbarr&   r$   requestr%   r&   r&   r'   rG      s    zFElementTests.test_definedRenderer.<locals>.ElementWithRenderMethod.fooN)r4   r5   r6   r   rG   r&   r&   r&   r'   ElementWithRenderMethod   s   rO   rG   NrL   )r
   rH   r!   )r$   rO   rG   r&   r&   r'   test_definedRenderer   s    z!ElementTests.test_definedRendererc                    s@   G dd dt  G  fdddt}| }| |dd dS )zi
        L{Element.render} loads a document from the C{loader} attribute and
        returns it.
        c                   @   s   e Zd Zdd ZdS )z0ElementTests.test_render.<locals>.TemplateLoaderc                 S   s   dS )Nresultr&   r1   r&   r&   r'   load   s    z5ElementTests.test_render.<locals>.TemplateLoader.loadN)r4   r5   r6   rR   r&   r&   r&   r'   TemplateLoader   s   rS   c                       s   e Zd Z  ZdS )z-ElementTests.test_render.<locals>.StubElementN)r4   r5   r6   loaderr&   rS   r&   r'   StubElement   s   rV   NrQ   )objectr
   r!   r9   )r$   rV   r;   r&   rU   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)r.   	TypeErrorr   r!   str)r$   Z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)r.   r   r   getr1   r&   r&   r'   test_renderGetDirectlyError   s    z(ElementTests.test_renderGetDirectlyErrorN)r4   r5   r6   r7   r>   rF   rI   rK   rP   rX   r[   r]   r&   r&   r&   r'   r8   W   s   

r8   c                   @   s8   e Zd ZdZdd Zdd Zege_dd Zege_dS )	XMLFileReprTestszB
    Tests for L{twisted.web.template.XMLFile}'s C{__repr__}.
    c                 C   s&   t d}| d|f tt| dS )zK
        An L{XMLFile} with a L{FilePath} returns a useful repr().
        /tmp/fake.xml<XMLFile of %r>N)r   r!   rE   r   )r$   pathr&   r&   r'   test_filePath   s    zXMLFileReprTests.test_filePathc                 C   s"   d}|  d|f tt| dS )zH
        An L{XMLFile} with a filename returns a useful repr().
        r_   r`   N)r!   rE   r   )r$   fnamer&   r&   r'   test_filename   s    zXMLFileReprTests.test_filenamec                 C   s&   t d}| d|f tt| dS )zK
        An L{XMLFile} with a file object returns a useful repr().
        znot xmlr`   N)StringIOr!   rE   r   )r$   Zfobjr&   r&   r'   	test_file   s    zXMLFileReprTests.test_fileN)	r4   r5   r6   r7   rb   rd   _xmlFileSuppressr	   rf   r&   r&   r&   r'   r^      s   r^   c                   @   s0   e Zd ZdZdZdZdd Zdd Zege_	dS )XMLLoaderTestsMixinz
    @ivar templateString: Simple template to use to exercise the loaders.

    @ivar deprecatedUse: C{True} if this use of L{XMLFile} is deprecated and
        should emit a C{DeprecationWarning}.
    N<p>Hello, world.</p>c                 C   s   |   }| \}| j| j gd}| jrb| 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)	loaderFactoryrR   ZflushWarningsdeprecatedUser!   lenDeprecationWarningr"   Zchildren)r$   rT   r%   warningsr&   r&   r'   	test_load   s    

zXMLLoaderTestsMixin.test_loadc                 C   s(   |   }| }| }| || dS )zd
        If {load()} can be called on a loader twice the result should be the
        same.
        N)rm   rR   r!   )r$   rT   Ztags1Ztags2r&   r&   r'   test_loadTwice   s    z"XMLLoaderTestsMixin.test_loadTwice)
r4   r5   r6   r7   rm   templateStringrr   rs   rg   r	   r&   r&   r&   r'   rh      s   	rh   c                   @   s   e Zd ZdZdZdd ZdS )XMLStringLoaderTestsz5
    Tests for L{twisted.web.template.XMLString}
    Fc                 C   s
   t | jS )zS
        @return: an L{XMLString} constructed with C{self.templateString}.
        )r   rt   r1   r&   r&   r'   rm     s    z"XMLStringLoaderTests.loaderFactoryNr4   r5   r6   r7   rn   rm   r&   r&   r&   r'   ru     s   ru   c                   @   s   e Zd ZdZdZdd ZdS )XMLFileWithFilePathTestszJ
    Tests for L{twisted.web.template.XMLFile}'s L{FilePath} support.
    Fc                 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
setContentrt   encoder   r$   fpr&   r&   r'   rm     s    z&XMLFileWithFilePathTests.loaderFactoryNrv   r&   r&   r&   r'   rw     s   rw   c                   @   s   e Zd ZdZdZdd ZdS )XMLFileWithFileTestszU
    Tests for L{twisted.web.template.XMLFile}'s deprecated file object support.
    Tc                 C   s   t t| jS )z{
        @return: an L{XMLString} constructed with a file object that contains
            C{self.templateString}.
        )r   re   rt   r1   r&   r&   r'   rm   (  s    z"XMLFileWithFileTests.loaderFactoryNrv   r&   r&   r&   r'   r~   #  s   r~   c                   @   s   e Zd ZdZdZdd ZdS )XMLFileWithFilenameTestszR
    Tests for L{twisted.web.template.XMLFile}'s deprecated filename support.
    Tc                 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}.
        rx   )r   ry   rz   rt   r{   r   ra   r|   r&   r&   r'   rm   6  s    z&XMLFileWithFilenameTests.loaderFactoryNrv   r&   r&   r&   r'   r   1  s   r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$S )%FlattenIntegrationTestsz[
    Tests for integration between L{Element} and
    L{twisted.web._flatten.flatten}.
    c                    s*   ddddddg} fdd|D }t |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>c                    s"   g | ]}  tt|d |qS )rT   )assertFlattensTor
   r   ).0Zxmlr1   r&   r'   
<listcomp>V  s   z:FlattenIntegrationTests.test_roundTrip.<locals>.<listcomp>)r   )r$   Z	fragmentsZ	deferredsr&   r1   r'   test_roundTripG  s    	
z&FlattenIntegrationTests.test_roundTripc                 C   s   t t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   r
   r   r   r$   r;   r&   r&   r'   test_entityConversion\  s    z-FlattenIntegrationTests.test_entityConversionc                 C   s   |  t tS )zk
        Rendering an Element without a loader attribute raises the appropriate
        exception.
        )assertFlatteningRaisesr
   r   r1   r&   r&   r'   r>   e  s    z2FlattenIntegrationTests.test_missingTemplateLoaderc                 C   s   t tdd}| |t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   )r
   r   r   r   r   r&   r&   r'   test_missingRenderMethodm  s    z0FlattenIntegrationTests.test_missingRenderMethodc                 C   s   t t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.r   r   r&   r&   r'   test_transparentRenderingz  s    z1FlattenIntegrationTests.test_transparentRenderingc                 C   s   t t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>r   r   r&   r&   r'   test_attrRendering  s    z*FlattenIntegrationTests.test_attrRenderingc                 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r.   AssertionErrorr   r1   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   r1   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   rx   N)r
   r   r   r{   )r$   ZtheInputr;   r&   r&   r'   test_lenientPrefixBehavior  s    z2FlattenIntegrationTests.test_lenientPrefixBehaviorc                 C   s*   G dd dt }|t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dd ZdS )zHFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElementc                 S   s   t dS Nrl   )r   rM   r&   r&   r'   renderMethod  s    zUFlattenIntegrationTests.test_deferredRendering.<locals>.RenderfulElement.renderMethodNr4   r5   r6   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   r   r$   r   r;   r&   r&   r'   test_deferredRendering  s    z.FlattenIntegrationTests.test_deferredRenderingc                 C   s   G dd dt }| | 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>.SubElementri   N)r4   r5   r6   r   rT   r&   r&   r&   r'   
SubElement  s   r   r   )r
   r   )r$   r   r&   r&   r'   test_loaderClassAttribute  s    z1FlattenIntegrationTests.test_loaderClassAttributec                    s2   g  G  fdddt }|t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 fddZdS )zIFlattenIntegrationTests.test_directiveRendering.<locals>.RenderfulElementc                    s     | |f |dS r   )appendrM   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   r   r   r&   r   r'   test_directiveRendering  s    z/FlattenIntegrationTests.test_directiveRenderingc                 C   s*   G dd dt }|t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dd ZdS )zTFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElementc                 S   s   dS r   r&   rM   r&   r&   r'   r     s    zaFlattenIntegrationTests.test_directiveRenderingOmittingTag.<locals>.RenderfulElement.renderMethodNr   r&   r&   r&   r'   r     s   r   r   r   r   r   r   r&   r&   r'   "test_directiveRenderingOmittingTag  s    z:FlattenIntegrationTests.test_directiveRenderingOmittingTagc                 C   s*   G dd dt }|t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dd ZdS )zUFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElementc                 S   s   |t tddS )Nz<em>Hello, world.</em>r   )r
   r   rM   r&   r&   r'   r     s    zbFlattenIntegrationTests.test_elementContainingStaticElement.<locals>.RenderfulElement.renderMethodNr   r&   r&   r&   r'   r     s   r   r   r   s   <p><em>Hello, world.</em></p>r   r   r&   r&   r'   #test_elementContainingStaticElement   s    z;FlattenIntegrationTests.test_elementContainingStaticElementc                 C   s*   G dd dt }|tdd}| |dS )r   c                   @   s   e Zd Zedd ZdS )zHFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElementc                 S   s   |j ddS )Nzworld.)Ztest2)Z	fillSlotsrM   r&   r&   r'   r     s    zUFlattenIntegrationTests.test_elementUsingSlots.<locals>.RenderfulElement.renderMethodNr   r&   r&   r&   r'   r     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   r   r   r&   r&   r'   test_elementUsingSlots  s
    z.FlattenIntegrationTests.test_elementUsingSlotsc                    s>   G  fdddt }G dd dt  |t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 fddZdS )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElementc                    s   | t ddS )Nz
                <t:ignored
                  xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
                  t:render="innerMethod" />
                r   )r   rM   InnerElementr&   r'   outerMethod+  s    z^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.OuterElement.outerMethodN)r4   r5   r6   r   r   r&   r   r&   r'   OuterElement*  s   r   c                   @   s   e Zd Zedd ZdS )zRFlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElementc                 S   s   dS r   r&   rM   r&   r&   r'   innerMethod3  s    z^FlattenIntegrationTests.test_elementContainingDynamicElement.<locals>.InnerElement.innerMethodN)r4   r5   r6   r   r   r&   r&   r&   r'   r   2  s   r   zw
        <p xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
          t:render="outerMethod" />
        r   r   r   )r$   r   r;   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                       s4   e Zd ZdZdZZe fddZedd ZdS )zHFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElementr   c                    s     j d7  _ |t j S Nrj   )countrZ   rM   )DestructiveElementr&   r'   classCounterM  s    zUFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.classCounterc                 S   s   |  j d7  _ |t| j S r   )instanceCountrZ   rM   r&   r&   r'   instanceCounterQ  s    zXFlattenIntegrationTests.test_sameLoaderTwice.<locals>.DestructiveElement.instanceCounterN)	r4   r5   r6   r   r   rT   r   r   r   r&   r   ZsharedLoaderr&   r'   r   H  s   r   s
   <p>1 1</p>s
   <p>2 2</p>s
   <p>3 1</p>N)r   r
   assertFlattensImmediately)r$   Ze1Ze2r&   r   r'   test_sameLoaderTwice=  s    z,FlattenIntegrationTests.test_sameLoaderTwiceN)r4   r5   r6   r7   r   r   r>   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r   A  s$   	
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TagLoaderTestsz!
    Tests for L{TagLoader}.
    c                 C   s   t td| _d S )NZtest)r   r   irT   r1   r&   r&   r'   setUpb  s    zTagLoaderTests.setUpc                 C   s   |  tt| j dS )zJ
        An instance of L{TagLoader} provides L{ITemplateLoader}.
        N)
assertTruer   r   rT   r1   r&   r&   r'   test_interfacef  s    zTagLoaderTests.test_interfacec                 C   s   |  | j t dS )zK
        L{TagLoader.load} returns a list, per L{ITemplateLoader}.
        N)assertIsInstancerT   rR   listr1   r&   r&   r'   test_loadsListm  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
   rT   r   )r$   er&   r&   r'   test_flattent  s    
zTagLoaderTests.test_flattenN)r4   r5   r6   r7   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)r4   r5   r6   r7   r   rT   r&   r&   r&   r'   r   ~  s   r   c                   @   s"   e Zd ZdZedZ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>Nc                 C   s
   || _ d S )N)failure)r$   r   rT   r&   r&   r'   __init__  s    zTestFailureElement.__init__)N)r4   r5   r6   r7   r   rT   r   r&   r&   r&   r'   r     s
   r   c                   @   s   e Zd ZdZdd ZdS )FailingElementz<
    An element that raises an exception when rendered.
    c                 C   s   d}d}|| S )N*   r   r&   )r$   rN   r    br&   r&   r'   r9     s    zFailingElement.renderN)r4   r5   r6   r7   r9   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)r4   r5   r6   r7   displayTracebacksr&   r&   r&   r'   r     s   r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )RenderElementTestsz
    Test L{renderElement}
    c                 C   s   t dg| _t | j_dS )zB
        Set up a common L{DummyRequest} and L{FakeSite}.
        r+   N)r   rN   r   siter1   r&   r&   r'   r     s    zRenderElementTests.setUpc                    s>   t  } j } 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.
        c                    s(     d jjd   jj d S )N    s$   <!DOCTYPE html>
<p>Hello, world.</p>)r!   joinrN   writtenr   finished_r1   r&   r'   check  s
    z3RenderElementTests.test_simpleRender.<locals>.check)r   rN   notifyFinishaddCallbackr:   r   r   r$   r;   dr   r&   r1   r'   test_simpleRender  s    

z$RenderElementTests.test_simpleRenderc                    s>   t  } j } fdd}||  tt j| |S )z
        L{renderElement} handles failures by writing a minimal
        error message to the request and finishing it.
        c                    sB     t} t|d  d jjd   jj d S )Nrj   r   s   <!DOCTYPE html>
<div style="font-size:800%;background-color:#FFF;color:#F00">An error occurred while rendering the response.</div>)	flushLoggedErrorsr   r!   ro   r   rN   r   r   r   )r   flushedr1   r&   r'   r     s    
z4RenderElementTests.test_simpleFailure.<locals>.check)r   rN   r   r   r:   r   r   r   r&   r1   r'   test_simpleFailure  s    

z%RenderElementTests.test_simpleFailurec                    sR   t t djj_t }j } 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.
        Tc                    sl    dt   d d }|jt t}t|d djj	d 
jj d S )Nrj   r   Zlog_failurer   s    <!DOCTYPE html>
<p>I failed.</p>)ZassertEqualsro   r   valuer   r   r!   r   rN   r   r   r   )r   fr   ZlogObserverr$   r&   r'   r     s    
zARenderElementTests.test_simpleFailureWithTraceback.<locals>.check)Z_failElement)r   ZcreateWithCleanupr   rN   r   r   r   r   r   r   r   r   r&   r   r'   test_simpleFailureWithTraceback  s    


z2RenderElementTests.test_simpleFailureWithTracebackc                    s:   t  } j } fdd}|| t j|dd |S )zs
        L{renderElement} will write the doctype string specified by the
        doctype keyword argument.
        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   rN   r   r   r1   r&   r'   r     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">Zdoctyper   rN   r   r   r   r   r&   r1   r'   test_nonDefaultDoctype  s    

z)RenderElementTests.test_nonDefaultDoctypec                    s:   t  } j } 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}.
        c                    s     d jjd d S )Nr   r   r   r   r1   r&   r'   r   2  s    z2RenderElementTests.test_noneDoctype.<locals>.checkNr   r   r   r&   r1   r'   test_noneDoctype)  s    

z#RenderElementTests.test_noneDoctypeN)
r4   r5   r6   r7   r   r   r   r   r   r   r&   r&   r&   r'   r     s   !r   N)?r7   Z
__future__r   r   Zzope.interface.verifyr   Ztwisted.internet.deferr   r   Ztwisted.python.filepathr   Ztwisted.trial.unittestr   Ztwisted.trial.utilr	   ZSUPPRESSZtwisted.web.templater
   r   r   r   r   r   Ztwisted.web.iwebr   Ztwisted.web.errorr   r   r   r   Ztwisted.web._elementr   Ztwisted.web.test._utilr   Ztwisted.web.test.test_webr   Ztwisted.web.serverr   Ztwisted.python.compatr   re   Ztwisted.loggerr   Ztwisted.test.proto_helpersr   rp   rg   r   r8   r^   rW   rh   ru   rw   r~   r   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   sJ    0c-   