HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_flatten.cpython-38.pyc
U


W[SG�@sdZddlZddlZddlmZddlmZddlmZddl	m
Z
ddlmZddl
mZdd	lmZmZmZdd
lmZddlmZmZmZddlmZmZmZmZmZmZdd
lm Z m!Z!m"Z"m#Z#ddl$m%Z%Gdd�de%e�Z&dd�j'j(Z)Gdd�de�Z*dS)zf
Tests for the flattening portion of L{twisted.web.template}, implemented in
L{twisted.web._flatten}.
�N)�XML)�OrderedDict)�implementer)�	_PY35PLUS)�TestCase)�XMLAssertionMixin)�passthru�succeed�
gatherResults)�IRenderable)�UnfilledSlot�UnsupportedType�FlattenerError)�tags�Tag�Comment�CDATA�CharRef�slot)�Element�renderer�	TagLoader�
flattenString)�FlattenTestCasec@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zes�d2e_d3d4�Zes�d2e_d5d6�Zd7d8�Z d9d:�Z!d;d<�Z"d=S)>�SerializationTestsz.
    Tests for flattening various things.
    cCs|�tjt�d�dd�d�S)z:
        Test that nested tags flatten correctly.
        Z42Zthere)�his'<html hi="there"><body>42</body></html>)�assertFlattensTorZhtmlZbody��self�r�?/usr/lib/python3/dist-packages/twisted/web/test/test_flatten.py�test_nestedTags's�z"SerializationTests.test_nestedTagscCst|�dd�|�dd�g�S)zL
        Test that strings will be flattened and escaped correctly.
        Zonesonez
<abc&&>123s&lt;abc&amp;&amp;&gt;123)r
rrrrr �test_serializeString0s

�z'SerializationTests.test_serializeStringcCs|�t��d�S)zP
        The serialized form of a self-closing tag is C{'<tagName />'}.
        s<img />)rr�imgrrrr �test_serializeSelfClosingTags:sz0SerializationTests.test_serializeSelfClosingTagscCs|�tjdd�d�dS)zV
        The serialized form of attribute I{a} with value I{b} is C{'a="b"'}.
        �foo��srcs<img src="foo" />N��assertFlattensImmediatelyrr#rrrr �test_serializeAttributeAs�z*SerializationTests.test_serializeAttributecCs&t��}tddg�|_|�|d�dS)zg
        Multiple attributes are separated by a single space in their serialized
        form.
        )r'r%)�nameZbars<img src="foo" name="bar" />N)rr#r�
attributesr)�r�tagrrr �!test_serializedMultipleAttributesIsz4SerializationTests.test_serializedMultipleAttributescCs |�|tj|d�d��d�dS)au
        Common implementation of L{test_serializedAttributeWithSanitization}
        and L{test_serializedDeferredAttributeWithSanitization},
        L{test_serializedAttributeWithTransparentTag}.

        @param wrapData: A 1-argument callable that wraps around the
            attribute's value so other tests can customize it.
        @param wrapData: callable taking L{bytes} and returning something
            flattenable

        @param wrapTag: A 1-argument callable that wraps around the outer tag
            so other tests can customize it.
        @type wrapTag: callable taking L{Tag} and returning L{Tag}.
        �<>&"r&s!<img src="&lt;&gt;&amp;&quot;" />Nr()rZwrapData�wrapTagrrr �checkAttributeSanitizationSs�z-SerializationTests.checkAttributeSanitizationcCs|�tt�dS)z�
        Attribute values containing C{"<"}, C{">"}, C{"&"}, or C{'"'} have
        C{"&lt;"}, C{"&gt;"}, C{"&amp;"}, or C{"&quot;"} substituted for those
        bytes in the serialized output.
        N)r2rrrrr �(test_serializedAttributeWithSanitizationgsz;SerializationTests.test_serializedAttributeWithSanitizationcCs|�tt�dS)z�
        Like L{test_serializedAttributeWithSanitization}, but when the contents
        of the attribute are in a L{Deferred
        <twisted.internet.defer.Deferred>}.
        N)r2r	rrrrr �0test_serializedDeferredAttributeWithSanitizationpszCSerializationTests.test_serializedDeferredAttributeWithSanitizationcs$g�|��fdd��fdd��dS)zS
        Like L{test_serializedAttributeWithSanitization} but with a slot.
        cs��|�ptd�S)N�stuff)�appendr��value��tossrr �<lambda>�zUSerializationTests.test_serializedAttributeWithSlotWithSanitization.<locals>.<lambda>cs|j���d�S)N)r5)�	fillSlots�pop�r.r9rr r;�r<N)r2rrr9r �0test_serializedAttributeWithSlotWithSanitizationys


�zCSerializationTests.test_serializedAttributeWithSlotWithSanitizationcCs|�tjt�dS)z�
        Attribute values which are supplied via the value of a C{t:transparent}
        tag have the same substitution rules to them as values supplied
        directly.
        N)r2r�transparentrrrrr �*test_serializedAttributeWithTransparentTag�sz=SerializationTests.test_serializedAttributeWithTransparentTagcs:G�fdd�dt��g�|��fdd���fdd��dS)z�
        Like L{test_serializedAttributeWithTransparentTag}, but when the
        attribute is rendered by a renderer on an element.
        cs*eZdZ��fdd�Zedd��Z�ZS)z_SerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderercs||_t�|��|�dS�N)r8�super�__init__)rr8�loader)�WithRenderer�	__class__rr rE�szhSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.__init__cSs|jSrCr7)r�requestr.rrr r5�szeSerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.WithRenderer.stuff)�__name__�
__module__�__qualname__rErr5�
__classcell__r)rG)rHr rG�srGcs��|�ptjdd�S)Nr5��render)r6rrAr7r9rr r;�s
z[SerializationTests.test_serializedAttributeWithTransparentTagWithRenderer.<locals>.<lambda>cs����t|��SrC)r>rr?�rGr:rr r;�r<N)rr2rrrPr �6test_serializedAttributeWithTransparentTagWithRenderer�s
�zISerializationTests.test_serializedAttributeWithTransparentTagWithRenderercCs(tt�Gdd�dt��}|�|t�dS)z�
        Like L{test_serializedAttributeWithTransparentTag}, but when the
        attribute is a provider of L{IRenderable} rather than a transparent
        tag.
        c@seZdZdd�Zdd�ZdS)zLSerializationTests.test_serializedAttributeWithRenderable.<locals>.ArbitrarycSs
||_dSrCr7)rr8rrr rE�szUSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.__init__cSs|jSrCr7)rrIrrr rO�szSSerializationTests.test_serializedAttributeWithRenderable.<locals>.Arbitrary.renderN)rJrKrLrErOrrrr �	Arbitrary�srRN)rr�objectr2r)rrRrrr �&test_serializedAttributeWithRenderable�sz9SerializationTests.test_serializedAttributeWithRenderablecCsLt�d�}tj||�d�}|�|d�}|�|d�}|�t|�jd|�dS)ah
        Common implementation of L{test_serializedAttributeWithTag} and
        L{test_serializedAttributeWithDeferredTag}.

        @param wrapTag: A 1-argument callable that wraps around the attribute's
            value so other tests can customize it.
        @param wrapTag: callable taking L{Tag} and returning something
            flattenable
        r0r&s@<img src="&lt;a&gt;&amp;lt;&amp;gt;&amp;amp;&quot;&lt;/a&gt;" />s<a>&lt;&gt;&amp;"</a>r'N)r�ar#r)�assertXMLEqualr�attrib)rr1ZinnerTagZouterTagZouter�innerrrr �checkTagAttributeSerialization�s

��z1SerializationTests.checkTagAttributeSerializationcCs|�t�dS)z�
        L{Tag} objects which are serialized within the context of an attribute
        are serialized such that the text content of the attribute may be
        parsed to retrieve the tag.
        N)rYrrrrr �test_serializedAttributeWithTag�sz2SerializationTests.test_serializedAttributeWithTagcCs|�t�dS)z�
        Like L{test_serializedAttributeWithTag}, but when the L{Tag} is in a
        L{Deferred <twisted.internet.defer.Deferred>}.
        N)rYr	rrrr �'test_serializedAttributeWithDeferredTag�sz:SerializationTests.test_serializedAttributeWithDeferredTagcCs6|�tjtjdd�d�d�}|�t|�jdd�dS)z�
        Similar to L{test_serializedAttributeWithTag}, but for the additional
        complexity where the tag which is the attribute value itself has an
        attribute value which contains bytes which require substitution.
        r0)Zhrefr&sV<img src="&lt;a href=&quot;&amp;lt;&amp;gt;&amp;amp;&amp;quot;&quot;&gt;&lt;/a&gt;" />r's"<a href="&lt;&gt;&amp;&quot;"></a>N)r)rr#rUrVrrW)rZ	flattenedrrr �,test_serializedAttributeWithTagWithAttribute�s��z?SerializationTests.test_serializedAttributeWithTagWithAttributecCs|�td�d�fS)zI
        Test that comments are correctly flattened and escaped.
        �foo bars<!--foo bar-->)rrrrrr �test_serializeComment�sz(SerializationTests.test_serializeCommentcsD�fdd�}g}dD]&}tdt|��}|�|�|�|�qt|�S)a�
        The data in a L{Comment} is escaped and mangled in the flattened output
        so that the result is a legal SGML and XML comment.

        SGML comment syntax is complicated and hard to use. This rule is more
        restrictive, and more compatible:

        Comments start with <!-- and end with --> and never contain -- or >.

        Also by XML syntax, a comment may not end with '-'.

        @see: U{http://www.w3.org/TR/REC-xml/#sec-comments}
        cs���|�d�d|f���|�d�d|f���t|�dkd|f�|dd�}��d	|���d
|�|r���|dd�dS)
Ns<!--z)%r does not start with the comment prefixs-->z'%r does not end with the comment suffix�z%%r is too short to be a legal comment����s--�>����-)Z
assertTrue�
startswith�endswith�lenZassertNotInZassertNotEqual)�cZcontentrrr �
verifyComments"��
�z>SerializationTests.test_commentEscaping.<locals>.verifyComment)�z	foo---barz
foo---bar-zfoo>barz	foo-->barz----------------N)rrZaddCallbackr6r
)rriZresultsrh�drrr �test_commentEscaping�s
z'SerializationTests.test_commentEscapingcCs$t|�td�d�|�td�d�g�S)zE
        Test that CDATA is correctly flattened and escaped.
        r]s<![CDATA[foo bar]]>zfoo ]]> bars#<![CDATA[foo ]]]]><![CDATA[> bar]]>)r
rrrrrr �test_serializeCDATA!s��z&SerializationTests.test_serializeCDATAcCsfd}t|�|d�|�t�|�d�|�t|�d�|�t|�d�|�t|�t�|�td|did�t�g�S)	z�
        Test that unicode is encoded correctly in the appropriate places, and
        raises an error when it occurs in inappropriate place.
        �☃s☃s
<p>☃</p>s
<!--☃-->s<![CDATA[☃]]>�prj)r,)	r
rrrorr�assertFlatteningRaisesr�UnicodeEncodeError)rZsnowmanrrr �test_serializeUnicode-s
���z(SerializationTests.test_serializeUnicodecCsttd��}|�|d�S)ze
        A character reference is flattened to a string using the I{&#NNNN;}
        syntax.
        rns&#9731;)r�ordr)r�refrrr �test_serializeCharRef?sz(SerializationTests.test_serializeCharRefcCs|�td�d�S)zz
        Test that a deferred is substituted with the current value in the
        callback chain when flattened.
        Ztwostwo)rr	rrrr �test_serializeDeferredHsz)SerializationTests.test_serializeDeferredcCs$td�}t|�|d�|�|d�g�S)zE
        Test that the same deferred can be flattened twice.
        Zthreesthree)r	r
r)rrkrrr �test_serializeSameDeferredTwicePs


�z2SerializationTests.test_serializeSameDeferredTwicecCs6ddlm}i}t|d�|�|d}|�|d�d�S)zt
        Test that a coroutine returning a value is substituted with the that
        value when flattened.
        r��dedentzE
            async def coro(x):
                return x
            �coro�four�four)�textwrapry�execr�rry�	namespacerzrrr �test_serializeCoroutine[s��z*SerializationTests.test_serializeCoroutinez*coroutines not available before Python 3.5cCs<ddlm}ttd�}t|d�|�|d}|�|d�d�S)z�
        Test that a coroutine returning an awaited deferred value is
        substituted with that value when flattened.
        rrx)r	zT
            async def coro(x):
                return await succeed(x)
            rzr{r|)r}ry�dictr	r~rrrrr � test_serializeCoroutineWithAwaitrs
��z3SerializationTests.test_serializeCoroutineWithAwaitcs0tt�G�fdd�dt��}t��|�d�g�S)zQ
        Test that flattening respects all of the IRenderable interface.
        cs eZdZdd�Z�fdd�ZdS)zASerializationTests.test_serializeIRenderable.<locals>.FakeElementcSs t�dtjdd�dtjdd��S)Nzhello, �testrNz - )rrorA)�ignZoredrrr rO�s

�zHSerializationTests.test_serializeIRenderable.<locals>.FakeElement.rendercs��|d�dd�S)Nr�cSs|d�S)NZworldr)r�Znoderrr r;�r<zfSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethod.<locals>.<lambda>)�assertEqual)r�r+rrr �lookupRenderMethod�szTSerializationTests.test_serializeIRenderable.<locals>.FakeElement.lookupRenderMethodN)rJrKrLrOr�rrrr �FakeElement�sr�s<p>hello, world - world</p>)rrrSr
r)rr�rrr �test_serializeIRenderable�s

�z,SerializationTests.test_serializeIRenderablecCs>t�td��}|��}|jdd�t|�|t�|�|d�g�S)zS
        Test that flattening a slot will use the slot value from the tag.
        r�zhello, world�r�s<p>hello, world</p>)	rrorZcloner=r
rprr)r�t1�t2rrr �test_serializeSlots�s

�z&SerializationTests.test_serializeSlotscCs0t�td��}|jtt�d��d�|�|d�S)z|
        Test that a slot with a deferred as its value will be flattened using
        the value from the deferred.
        r�zfour>r�s<p><em>four&gt;</em></p>)rrorr=r	Zemr)r�trrr �test_serializeDeferredSlots�sz.SerializationTests.test_serializeDeferredSlotscCs|�dt�S)zT
        Test that flattening an unknown type of thing raises an exception.
        N)rpr
rrrr �test_unknownTypeRaises�sz)SerializationTests.test_unknownTypeRaisesN)#rJrKrL�__doc__r!r"r$r*r/r2r3r4r@rBrQrTrYrZr[r\r^rlrmrrrurvrwr�r�skipr�r�r�r�r�rrrr r#sH	

				/	��

rcCsdSrCrrrrr r;�r<r;c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�FlattenerErrorTestsz&
    Tests for L{FlattenerError}.
    cCs<tt�Gdd�dt��}|�tttd�|�gg��d�dS)z�
        If a L{FlattenerError} is created with an L{IRenderable} provider root,
        the repr of that object is included in the string representation of the
        exception.
        c@seZdZdd�ZdS)z7FlattenerErrorTests.test_renderable.<locals>.RenderablecSsdS)Nzrenderable reprrrrrr �__repr__�sz@FlattenerErrorTests.test_renderable.<locals>.Renderable.__repr__N)rJrKrLr�rrrr �
Renderable�sr��reasonzCException while flattening:
  renderable repr
RuntimeError: reason
N)rrrSr��strr�RuntimeError)rr�rrr �test_renderable�s��z#FlattenerErrorTests.test_renderablecCs2tddddd�}|�tttd�|gg��d�dS)	z�
        If a L{FlattenerError} is created with a L{Tag} instance with source
        location information, the source location is included in the string
        representation of the exception.
        Zdivz/foo/filename.xhtml��)�filenameZ
lineNumberZcolumnNumberr�zlException while flattening:
  File "/foo/filename.xhtml", line 17, column 12, in "div"
RuntimeError: reason
N)rr�r�rr�r-rrr �test_tag�s��zFlattenerErrorTests.test_tagcCs&|�tttd�td�gg��d�dS)z�
        If a L{FlattenerError} is created with a L{Tag} instance without source
        location information, only the tagName is included in the string
        representation of the exception.
        r��spanz>Exception while flattening:
  Tag <span>
RuntimeError: reason
N)r�r�rr�rrrrr �test_tagWithoutLocation�s�z+FlattenerErrorTests.test_tagWithoutLocationc
s��fdd�}dd��z
|�Wn@tk
r^}z"t�t��d�dd�}|}W5d}~XYnX|�d�|�tt|g|��d	t	|j
jdt	�j
jdf�dS)
z�
        If a L{FlattenerError} is created with traceback frames, they are
        included in the string representation of the exception.
        cs
��dSrCrr��grr �fsz-FlattenerErrorTests.test_traceback.<locals>.fcSstd��dS)Nr�)r�rrrr r�sz-FlattenerErrorTests.test_traceback.<locals>.g��Nzf() must raise RuntimeErrorz�Exception while flattening:
  File "%s", line %d, in f
    g()
  File "%s", line %d, in g
    raise RuntimeError("reason")
RuntimeError: reason
)r��	traceback�
extract_tb�sys�exc_infoZfailr�r�r�HERE�__code__�co_firstlineno)rr��eZtbinfo�excrr�r �test_traceback�s$



���z"FlattenerErrorTests.test_tracebackN)rJrKrLr�r�r�r�r�rrrr r��s

r�)+r�r�r�Zxml.etree.cElementTreer�collectionsrZzope.interfacerZtwisted.python.compatrZtwisted.trial.unittestrZtwisted.test.testutilsrZtwisted.internet.deferrr	r
Ztwisted.web.iwebrZtwisted.web.errorrr
rZtwisted.web.templaterrrrrrrrrrZtwisted.web.test._utilrrr��co_filenamer�r�rrrr �<module>s( #