
    VhK                         d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d dej                        Z
 G d d	      Z G d
 deej                        Z G d deej                        Z G d dej                        Zy)zF
Tests for L{twisted.words.xish.domish}, a DOM-like library for XMPP.
    )verifyObject)requireModule)unittest)domishc                   p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)ElementTestsz&
    Tests for L{domish.Element}.
    c                 ^    t        t        j                  t        j                  d             y)zB
        L{domish.Element} implements L{domish.IElement}.
        NfooN)r   r   IElementElementselfs    N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/words/test/test_domish.pytest_interfacezElementTests.test_interface   s     	V__fnn]&CD    c                     d}| j                  t        j                  |      d       | j                  t        j                  |d      d       y)zF
        The built-in entity references are properly encoded.
        z&<>'"z&amp;&lt;&gt;'"   z&amp;&lt;&gt;&apos;&quot;N)assertEqualr   escapeToXml)r   ss     r   test_escapingzElementTests.test_escaping   sC     ++A.0BC++Aq13NOr   c                 f    t        j                  d      }| j                  |j                  d       y)zN
        An attribute on L{domish.Namespace} yields a qualified name.
        testnsr   r   N)r   	Namespacer   r   )r   nss     r   test_namespacezElementTests.test_namespace#   s)     h'!23r   c                    t        j                  d      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       t        j                  d      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       t        j                  d      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       t        j                  dd      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       y)	z?
        Basic L{domish.Element} initialization tests.
        r
   r   N r   r!   r   r   test2ns)r   r   r   nameuri
defaultUriparentr   es     r   test_elementInitzElementTests.test_elementInit*   sl    NN=)'%t,4(NN;''#r*4(NN,-')x04(NN,i8')y1r   c                 Z   t        j                  d      }|j                  d       |j                  d      }d|d<   d|d<   |j                  d       |j                  d       |j                  d	       |j                  d
       | j	                  |j
                  d   d       | j	                  |j                  |       |j                  j                  d       d|j                  d<   | j	                  |j
                  d   |j                         | j	                  |j
                  d   |j                         | j	                  |d   d       |d= | j	                  |j                  d      d       | j	                  |j                  d      d       | j	                  |d   d       y)z6
        Basic L{domish.Element} child tests.
        r   somecontent)testns2bar2value1attrib1value2)r,   attrib2barabc123abc123
subcontent	somevalue	bar2valuer      r   r1   N)	r   r   
addContent
addElementr   childrenr-   r2   hasAttribute)r   r(   b2s      r   test_childOpszElementTests.test_childOpsE   sa    NN,-	]#\\-.)$,
 !	U	U	U	U 	B2 	$	,'){ 	A/A. 	9x0iL	2A6	2A612H=r   c                     t        j                  d      }|j                  d       t        |      }| j	                  d|       | j                  |t               y)6
        Extract character data using L{str}.
        r   r+   Nr   r   r;   strr   assertIsInstancer   elementtexts      r   test_characterDatazElementTests.test_characterDataf   J     ..!23=)7|-dC(r   c                     t        j                  d      }|j                  d       t        |      }| j	                  d|       | j                  |t               y)z<
        Extract ascii character data using L{str}.
        r   r+   NrC   rF   s      r   test_characterDataNativeStringz+ElementTests.test_characterDataNativeStringq   rJ   r   c                     t        j                  d      }|j                  d       t        |      }| j	                  d|       | j                  |t               y)rB   r      ☃NrC   rF   s      r   test_characterDataUnicodez&ElementTests.test_characterDataUnicode|   rJ   r   c                     t        j                  d      }|j                  d       t        |      }| j	                  dj                         |       | j                  |t               y)zA
        Extract character data as UTF-8 using L{bytes}.
        r   rN   N)r   r   r;   bytesr   encoderE   rF   s      r   test_characterDataBytesz$ElementTests.test_characterDataBytes   sS     ..!23=)W~--/6dE*r   c                     t        j                  d      }|j                  d       |j                  d       |j                  d       | j	                  dt        |             y)zV
        Mixing addChild with cdata and element, the first cdata is returned.
        r   r3   r2   defN)r   r   addChildr<   r   rD   r   rG   s     r   test_characterDataMixedz$ElementTests.test_characterDataMixed   sQ     ..!235!G-r   c                     t        j                  d      }|j                  d       | j                  dt	        |             y)zT
        Unicode strings passed to C{addContent} become the character data.
        r   unicodeNr   r   r;   r   rD   rW   s     r   test_addContentzElementTests.test_addContent   s5     ..!239%CL1r   c                     t        j                  d      }|j                  d       | j                  dt	        |             y)zY
        ASCII native strings passed to C{addContent} become the character data.
        r   nativeNr[   rW   s     r    test_addContentNativeStringASCIIz-ElementTests.test_addContentNativeStringASCII   s5     ..!238$3w<0r   c                 p    t        j                  d      }| j                  t        |j                  d       y)zV
        Byte strings passed to C{addContent} are not acceptable on Python 3.
        r   s   bytesN)r   r   assertRaises	TypeErrorr;   rW   s     r   test_addContentBytesz!ElementTests.test_addContentBytes   s+     ..!23)W%7%7Br   c                     t        j                  d      }|j                  dd      }| j                  dt	        |             y)zW
        Content passed to addElement becomes character data on the new child.
        r   r2   r3   )contentN)r   r   r<   r   rD   )r   rG   childs      r   test_addElementContentz#ElementTests.test_addElementContent   s<     ..!23""5%"8E
+r   c                 x   t        j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        |j	                               }| j                  ||       | j                  ||       | j                  ||       | j                  ||       y)z
        Calling C{elements} without arguments on a L{domish.Element} returns
        all child elements, whatever the qualified name.
        r   r#   r,   bazquuxr   r#   N)r   r   r<   listelementsassertInr   r(   c1c2c3c4eltss          r   test_elementszElementTests.test_elements   s    
 NN,-\\&!\\,-\\&!\\,-AJJL!b$b$b$b$r   c                 |   t        j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        |j	                  dd            }| j                  ||       | j                  ||       | j                  ||       | j                  ||       y)z
        Calling C{elements} with a namespace and local name on a
        L{domish.Element} returns all child elements with that qualified name.
        r   r#   ri   rk   rl   r   N)r   r   r<   rm   rn   ro   assertNotInrp   s          r   test_elementsWithQNz ElementTests.test_elementsWithQN   s    
 NN,-\\&!\\,-\\&!\\,-AJJx01b$T"T"b$r   N)__name__
__module____qualname____doc__r   r   r   r)   r@   rI   rL   rO   rS   rX   r\   r_   rc   rg   rv   ry    r   r   r   r      s[    EP426>B	)	)	)	+.21C, $ r   r   c                   d    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)DomishStreamTestsMixinz
    Mixin defining tests for different stream implementations.

    @ivar streamClass: A no-argument callable which will be used to create an
        XML parser which can produce a stream of elements from incremental
        input.
    c                    d| _         d| _        d | _        g | _        | j	                         | _        | j                  | j
                  _        | j                  j                  | j
                  _	        | j                  | j
                  _        y )NF)doc_started	doc_endedrootrn   streamClassstream_docStartedDocumentStartEventappendElementEvent	_docEndedDocumentEndEventr   s    r   setUpzDomishStreamTestsMixin.setUp   sg     	&&()-)9)9&#'==#7#7 '+~~$r   c                      || _         d| _        y NT)r   r   )r   r   s     r   r   z"DomishStreamTestsMixin._docStarted   s    	r   c                     d| _         y r   )r   r   s    r   r   z DomishStreamTestsMixin._docEnded   s	    r   c                 :    | j                   j                  |       y )N)r   parser   xmls     r   doTestzDomishStreamTestsMixin.doTest   s    #r   c                    d}| j                   j                  |       | j                  | j                  d       | j                  | j                  j
                  d       | j                  | j                  d   j
                  d       | j                  | j                  d   j
                  d       | j                  | j                  d       y )Ns   <root><child/><child2/></root>Tr   r   rf   r   child2)r   r   r   r   r   r#   rn   r   r   s     r   testHarnessz"DomishStreamTestsMixin.testHarness   s    /#))400q)..8q)..9.r   c                    d}| j                   j                  |       | j                  | j                  j                  d       | j                  | j                  j
                  d       | j                  | j                  d   j                  d       | j                  | j                  d   j
                  d       | j                  | j                  d   d   d       | j                  | j                  d   j                  j
                  d	       | j                  t        | j                  d   j                        d
       y )Ns   <stream:stream xmlns:stream='etherx' xmlns='jabber'>
  <message to='bar'>    <x xmlns='xdelay'>some&amp;data&gt;</x>  </message></stream:stream>r   etherxr   messagejabbertor2   xdelayz
some&data>)	r   r   r   r   r#   r$   rn   xrD   r   s     r   	testBasicz DomishStreamTestsMixin.testBasic  s    " 	 	#21q)..	:q)--x8q)$/7q)++//:T]]1-//0,?r   c                     d}| j                   j                  |       | j                  | j                  j                  d       | j                  | j
                  d   j                  d       y )Ns(   <stream><error xmlns='etherx'/></stream>r!   r   r   r   r   r   r   r$   rn   r   s     r   testNoRootNSz#DomishStreamTestsMixin.testNoRootNS  sP    9#+q)--x8r   c                 z   d}| j                   j                  |       | j                  | j                  j                  d       | j                  | j                  j
                  d       | j                  | j                  d   j                  d       | j                  | j                  d   j
                  d       y )Ns=   <stream:stream xmlns:stream='etherx'><error/></stream:stream>r   r!   r   )r   r   r   r   r$   r%   rn   r   s     r   testNoDefaultNSz&DomishStreamTestsMixin.testNoDefaultNS  s    N#1--r2q)--r2q)44b9r   c                     d}| j                   j                  |       | j                  | j                  j                  d       | j                  | j
                  d   j                  d       y )Ns$   <root xmlns='testns'><child/></root>r   r   r   r   s     r   testChildDefaultNSz)DomishStreamTestsMixin.testChildDefaultNS(  sP    5#1q)--x8r   c                     d}| j                   j                  |       | j                  | j                  d   j                  j
                  d       y )Nsg   <root xmlns='testns'>
                    <child1><child2 xmlns=''/></child1>
                  </root>r   r!   )r   r   r   rn   r   r$   r   s     r   testEmptyChildNSz'DomishStreamTestsMixin.testEmptyChildNS/  sB     	#q)0044b9r   c                     d}| j                   j                  |       | j                  | j                  d   j                  d       y)z
        Attributes with namespace are parsed without Exception.
        (https://twistedmatrix.com/trac/ticket/9730 regression test)
        s~   <root xmlns:test='http://example.org' xml:lang='en'>
                    <test:test>test</test:test>
                  </root>r   zhttp://example.orgN)r   r   r   rn   r$   r   s     r   test_attributesWithNamespacesz4DomishStreamTestsMixin.test_attributesWithNamespaces7  s=     	#q)--/CDr   c                     d}| j                   j                  |       | j                  | j                  j                  d   d       | j                  | j
                  d   j                  d       y )Ns<   <root xmlns='testns' xmlns:foo='testns2'><foo:child/></root>r   r,   r   )r   r   r   r   localPrefixesrn   r$   r   s     r   testChildPrefixz&DomishStreamTestsMixin.testChildPrefixG  sW    M#007Cq)--y9r   c                 n    | j                  t        j                  | j                  j                  d       y )Ns   <root><error></root>)ra   r   ParserErrorr   r   r   s    r   testUnclosedElementz*DomishStreamTestsMixin.testUnclosedElementN  s(     1 13J	
r   c                    d}| j                   j                  |       | j                  d| j                  d   j                         | j                  d| j                  d   j
                         | j                  d| j                  d   j                         | j                  ddi| j                  d   j                         | j                  d| j                  d   j                         | j                  d| j                  d   j
                         | j                  d| j                  d   j                         | j                  i | j                  d   j                         y	)
at  
        Test that reuse of namespaces does affect an element's serialization.

        When one element uses a prefix for a certain namespace, this is
        stored in the C{localPrefixes} attribute of the element. We want
        to make sure that elements created after such use, won't have this
        prefix end up in their C{localPrefixes} attribute, too.
        s   <root>
                    <foo:child1 xmlns:foo='testns'/>
                    <child2 xmlns='testns'/>
                  </root>child1r   r   r!   r   r   r   N)r   r   r   rn   r#   r$   r%   r   r   s     r   test_namespaceReusez*DomishStreamTestsMixin.test_namespaceReuseS  s   
 	#4==#3#8#894==#3#7#78T]]1-889%*DMM!,<,J,JK4==#3#8#894==#3#7#784==#3#>#>?T]]1-;;<r   N)rz   r{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r   r   r   r      sO    6 /@$9:9:E :

=r   r   c                   B    e Zd ZdZej
                  Z edd      dZyy)DomishExpatStreamTestszd
    Tests for L{domish.ExpatElementStream}, the expat-based element stream
    implementation.
    pyexpatN)defaultz1pyexpat is required for ExpatElementStream tests.)	rz   r{   r|   r}   r   ExpatElementStreamr   r   skipr~   r   r   r   r   m  s+    
 ++KY-5B 6r   r   c                   (    e Zd ZdZej
                  Zy)DomishSuxStreamTestszo
    Tests for L{domish.SuxElementStream}, the L{twisted.web.sux}-based element
    stream implementation.
    N)rz   r{   r|   r}   r   SuxElementStreamr   r~   r   r   r   r   y  s    
 ))Kr   r   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)SerializerTestsc                     t        j                  d      }| j                  |j                         d       | j                  |j                  d      d       y )Nr
   z<foo/>r   )closeElementz<foo>r   r   r   toXmlr'   s     r   testNoNamespacezSerializerTests.testNoNamespace  sA    NN=)H-a0':r   c                 n    t        j                  d      }| j                  |j                         d       y )Nr   z<foo xmlns='testns'/>r   r'   s     r   testDefaultNamespacez$SerializerTests.testDefaultNamespace  s(    NN,-$;<r   c                 v    t        j                  dd      }| j                  |j                  ddi      d       y )Nr   r,   r   r2   z-<bar:foo xmlns:bar='testns' xmlns='testns2'/>r   r'   s     r   testOtherNamespacez"SerializerTests.testOtherNamespace  s5    NN,i8GGXu%&(W	
r   c                     t        j                  d      }|j                  d       | j                  |j	                         d       y )Nr   r2    <foo xmlns='testns'><bar/></foo>r   r   r<   r   r   r'   s     r   testChildDefaultNamespacez)SerializerTests.testChildDefaultNamespace  s4    NN,-	U$FGr   c                     t        j                  d      }|j                  d       | j                  |j	                         d       y )Nr   r   r2   r   r   r'   s     r   testChildSameNamespacez&SerializerTests.testChildSameNamespace  s5    NN,-	&'$FGr   c                     t        j                  d      }|j                  dd       | j                  |j	                         d       y )Nr   r2   r   r   r   r'   s     r   testChildSameDefaultNamespacez-SerializerTests.testChildSameDefaultNamespace  s6    NN,-	UH%$FGr   c                     t        j                  d      }|j                  dd       | j                  |j	                         d       y )Nr   r,   r2   r,   z0<foo xmlns='testns'><bar xmlns='testns2'/></foo>r   r'   s     r   testChildOtherDefaultNamespacez.SerializerTests.testChildOtherDefaultNamespace  s7    NN,-	'3$VWr   c                     t        j                  d      }|j                  dd       | j                  |j	                         d       y )Nr
   )ns2r2   r   z<foo><bar xmlns='ns2'/></foo>r   r'   s     r   testOnlyChildDefaultNamespacez-SerializerTests.testOnlyChildDefaultNamespace  s5    NN=)	^U+$CDr   c                     t        j                  d      }|j                  d       | j                  |j	                         d       y )Nr
   r2   z<foo><bar/></foo>r   r'   s     r   testOnlyChildDefaultNamespace2z.SerializerTests.testOnlyChildDefaultNamespace2  s3    NN=)	U$78r   c                     t        j                  dd      }|j                  d       | j                  |j	                         d       y )Nr   r,   r   z<<xn0:foo xmlns:xn0='testns' xmlns='testns2'><bar/></xn0:foo>r   r'   s     r   testChildInDefaultNamespacez+SerializerTests.testChildInDefaultNamespace  s:    NN,i8	'(GGIU	
r   c                 v    t        j                  dddi      }| j                  |j                         d       y )Nr
   r   rj   attribsz(<foo xmlns:xn0='testns2' xn0:bar='baz'/>r   r'   s     r   testQualifiedAttributez&SerializerTests.testQualifiedAttribute  s/    NN=3Eu2MN$NOr   c                 v    t        j                  dddi      }| j                  |j                         d       y )Nr   r   rj   r   z6<foo xmlns='testns' xmlns:xn0='testns' xn0:bar='baz'/>r   r'   s     r   testQualifiedAttributeDefaultNSz/SerializerTests.testQualifiedAttributeDefaultNS  s3    NN,7H%6PQGGIO	
r   c                     t        j                  d      }|j                  dd      }|j                  d       |j                  dd      }|j                  d       | j                  |j	                         d       y )	Nr    r   r,   )r,   rk   )testns3rj   testns4)r   rk   z<foo><xn0:bar xmlns:xn0='testns' xmlns='testns2'><quux/></xn0:bar><xn1:baz xmlns:xn1='testns3' xmlns='testns4'><xn0:quux xmlns:xn0='testns'/></xn1:baz></foo>r   )r   r(   r   r   s       r   testTwoChildszSerializerTests.testTwoChilds  sm    NN;'/;-.0)<,-GGI l	
r   c                 v    t        j                  dddi      }| j                  |j                         d       y )Nr
   )z$http://www.w3.org/XML/1998/namespacelangen_USr   z<foo xml:lang='en_US'/>r   r'   s     r   testXMLNamespacez SerializerTests.testXMLNamespace  s4    NNEwO
 	$=>r   c                 |    t        j                  dddi      }| j                  |j                  ddi      d       y )Nr
   r   rj   r   r,   quxz(<foo xmlns:qux='testns2' qux:bar='baz'/>r   r'   s     r   )testQualifiedAttributeGivenListOfPrefixesz9SerializerTests.testQualifiedAttributeGivenListOfPrefixes  s:    NN=3Eu2MNGGY&')S	
r   c                     t        j                  dddi      }|j                  d      }d|d<   | j                  |j	                         d       y )Nr
   r   rj   r   r,   r   rk   zF<foo xmlns:xn0='testns2' xn0:bar='baz'><xn0:qux xn0:bar='quux'/></foo>r   r   r(   cs      r   testNSPrefixzSerializerTests.testNSPrefix  sL    NN=3Eu2MNLL+, &
GGIT	
r   c                     t        j                  dddi      }|j                  d      }d|d<   |j                  d       | j                  |j	                         d       y )	Nr
   r   rj   r   r   rk   r   zY<foo xmlns:xn0='testns2' xn0:bar='baz'><xn0:qux xn0:bar='quux'><xn0:foo/></xn0:qux></foo>r   r   s      r   testDefaultNSPrefixz#SerializerTests.testDefaultNSPrefix  sX    NN=3Eu2MNLL+, &
	UGGIg	
r   c                 z    t        j                  d      }| j                  |j                  ddidg      d       y )Nr   r   r2   )prefixesprefixesInScopez
<bar:foo/>r   r'   s     r   testPrefixScopezSerializerTests.testPrefixScope  s8    NN,-GGh.GH,	
r   c                 v    t        j                  dddi      }| j                  |j                         d       y )Nr   r2   r   r   z<bar:foo xmlns:bar='testns'/>r   r'   s     r   testLocalPrefixesz!SerializerTests.testLocalPrefixes  s/    NN,UH<MN$CDr   c                     t        j                  dddi      }|j                  d       | j                  |j                  j
                  d        | j                  |j                         d       y )Nr   r2   r   r   rj   z,<bar:foo xmlns:bar='testns'><baz/></bar:foo>)r   r   r<   assertIdenticalrj   r%   r   r   r'   s     r   testLocalPrefixesWithChildz*SerializerTests.testLocalPrefixesWithChild  sU    NN,UH<MN	UQUU--t4$RSr   c                 T   ddi}t        j                  |      }| j                  ||j                         t        j                  dddi      }| j                  d|j                  |             t        j                  d      }| j                  d	|j                  |             y
)aQ  
        Test that prefixes passed to serialization are not modified.

        This test makes sure that passing a dictionary of prefixes repeatedly
        to C{toXml} of elements does not cause serialization errors. A
        previous implementation changed the passed in dictionary internally,
        causing havoc later on.
        r   r   )r   )r,   r   rk   r,   r   z <quux:foo xmlns:quux='testns2'/>z<foo xmlns='testns2'/>N)r   SerializerClassassertNotIdenticalr   r   r   r   )r   r   r   r(   s       r   test_prefixesReusez"SerializerTests.test_prefixesReuse  s     e$ ""H5!**5 NN-fi=PQ;QWWhW=WXNN-.1177H73MNr   c                     t        j                  d      }|j                  d       | j                  |j	                         d       y )Nr
   z<abc123>z<foo><abc123></foo>r   r   	addRawXmlr   r   r'   s     r   testRawXMLSerializationz'SerializerTests.testRawXMLSerialization  s5    NN=)	J 	$9:r   c                     t        j                  d      }|j                  d       | j                  |j	                         d       y )Nr
   u   <degree>°</degree>u   <foo><degree>°</degree></foo>r  r'   s     r   "testRawXMLWithUnicodeSerializationz2SerializerTests.testRawXMLWithUnicodeSerialization  s4    NN=)	-.$HIr   c                     t        j                  d      }d|d<   |j                  d       | j                  |j	                         d       y )Nr
   u   my valueȡetestu   A degree symbol...°u2   <foo test='my valueȡe'>A degree symbol...°</foo>)r   r   r;   r   r   r'   s     r   testUnicodeSerializationz(SerializerTests.testUnicodeSerialization!  s@    NN=)%&		/0GGIV	
r   N)rz   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r~   r   r   r   r     s    ;
=
H
H
H
X
E
9

P
	
?

	

ETO*;J

r   r   N)r}   zope.interface.verifyr   twisted.python.reflectr   twisted.trialr   twisted.words.xishr   TestCaser   r   r   r   r   r~   r   r   <module>r     sz   
 / 0 " %M 8$$ M `J= J=Z	C3X5F5F 	C*183D3D *e
h'' e
r   