o
    ¯b±  ã                   @   sš   d 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
 G dd„ dejƒZG dd	„ d	eje	jƒZG d
d„ dejƒZG dd„ deƒZG dd„ deƒZdS )z,
Tests for L{twisted.words.xish.xmlstream}.
é    )Úprotocol)Úfailure)Úunittest)ÚdomishÚutilityÚ	xmlstreamc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚXmlStreamTestsc                 C   s0   d| _ g | _t ¡ | _| | j_| jj| jj_d S )Nz	no reason)ÚconnectionLostMsgÚoutlistr   Ú	XmlStreamÚ	transportÚappendÚwrite©Úself© r   úC/usr/lib/python3/dist-packages/twisted/words/test/test_xmlstream.pyÚsetUp   s
   
zXmlStreamTests.setUpc                 C   s   | j  t t| jƒ¡¡ dS )zA
        Stub loseConnection because we are a transport.
        N)r   ÚconnectionLostr   ÚFailureÚ	Exceptionr	   r   r   r   r   ÚloseConnection   s   ÿzXmlStreamTests.loseConnectionc                 C   s,   | j  ¡  | j  d¡ |  | jd d¡ dS )zq
        Calling L{xmlstream.XmlStream.send} results in the data being written
        to the transport.
        s   <root>r   N)r   ÚconnectionMadeÚsendÚassertEqualr
   r   r   r   r   Ú	test_send   s   
zXmlStreamTests.test_sendc                    sJ   g ‰ ‡ fdd„}| j  t j|¡ | j  ¡  | j  d¡ |  dtˆ ƒ¡ dS )zU
        Receiving the starttag of the root element results in stream start.
        c                    ó   ˆ   d ¡ d S ©N©r   )Úrootelem©ÚstreamStartedr   r   ÚstreamStartEvent.   ó   z9XmlStreamTests.test_receiveRoot.<locals>.streamStartEventú<root>é   N)r   ÚaddObserverÚSTREAM_START_EVENTr   ÚdataReceivedr   Úlen)r   r"   r   r    r   Útest_receiveRoot(   s   
zXmlStreamTests.test_receiveRootc                    s¼   g ‰g ‰ ‡fdd„}‡ fdd„}| j  t j|¡ | j  t j|¡ | j  ¡  | j  d¡ |  dtˆƒ¡ |  dtˆ ƒ¡ | j  d¡ |  dtˆƒ¡ |  ˆd  	t
j¡¡ |  dtˆ ƒ¡ d	S )
zN
        Receiving malformed XML results in an L{STREAM_ERROR_EVENT}.
        c                    ó   ˆ   | ¡ d S r   r   ©Úreason)ÚstreamErrorr   r   ÚstreamErrorEvent=   r#   z;XmlStreamTests.test_receiveBadXML.<locals>.streamErrorEventc                    r   r   r   )Ú_©Ú	streamEndr   r   ÚstreamEndEvent@   r#   z9XmlStreamTests.test_receiveBadXML.<locals>.streamEndEventr$   r   z<child><unclosed></child>r%   N)r   r&   ÚSTREAM_ERROR_EVENTÚSTREAM_END_EVENTr   r(   r   r)   Ú
assertTrueÚcheckr   ÚParserError)r   r/   r3   r   )r2   r.   r   Útest_receiveBadXML6   s   
z!XmlStreamTests.test_receiveBadXMLc                    sn   g ‰ ‡ fdd„}| j  t j|¡ | j  ¡  |  ¡  |  dtˆ ƒ¡ |  ˆ d tj	¡ |  ˆ d  
¡ | j¡ dS )z@
        Ending the stream fires a L{STREAM_END_EVENT}.
        c                    r+   r   r   r,   r1   r   r   r3   V   r#   z5XmlStreamTests.test_streamEnd.<locals>.streamEndEventr%   r   N)r   r&   r5   r   r   r   r)   ÚassertIsInstancer   r   ÚgetErrorMessager	   )r   r3   r   r1   r   Útest_streamEndP   s   
zXmlStreamTests.test_streamEndN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r*   r9   r<   r   r   r   r   r      s    	r   c                   @   s   e Zd ZdZdd„ ZdS )ÚDummyProtocolzÝ
    I am a protocol with an event dispatcher without further processing.

    This protocol is only used for testing XmlStreamFactoryMixin to make
    sure the bootstrap observers are added to the protocol instance.
    c                 O   s"   || _ || _g | _tj | ¡ d S r   )ÚargsÚkwargsÚ	observersr   ÚEventDispatcherÚ__init__)r   rA   rB   r   r   r   rE   i   s   zDummyProtocol.__init__N)r=   r>   r?   Ú__doc__rE   r   r   r   r   r@   a   s    r@   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBootstrapMixinTestszq
    Tests for L{xmlstream.BootstrapMixin}.

    @ivar factory: Instance of the factory or mixin under test.
    c                 C   ó   t  ¡ | _d S r   )r   ÚBootstrapMixinÚfactoryr   r   r   r   r   x   r#   zBootstrapMixinTests.setUpc                    sP   g ‰ ‡ fdd„}t ƒ }| j d|¡ | j |¡ | dd¡ |  dtˆ ƒ¡ dS )zL
        Dispatching an event fires registered bootstrap observers.
        c                    r+   r   r   ©Údata©Úcalledr   r   Úcb�   r#   z6BootstrapMixinTests.test_installBootstraps.<locals>.cbú//event/myeventNr%   )r@   rK   ÚaddBootstrapÚinstallBootstrapsÚdispatchr   r)   ©r   rP   Ú
dispatcherr   rN   r   Útest_installBootstraps{   s   z*BootstrapMixinTests.test_installBootstrapsc                    sX   g ‰ ‡ fdd„}| j  d|¡ | j  d|¡ tƒ }| j  |¡ | dd¡ |  ˆ ¡ dS )zI
        Test addition and removal of a bootstrap event handler.
        c                    r+   r   r   rL   rN   r   r   rP   ’   r#   z:BootstrapMixinTests.test_addAndRemoveBootstrap.<locals>.cbrQ   N)rK   rR   ÚremoveBootstrapr@   rS   rT   ÚassertFalserU   r   rN   r   Útest_addAndRemoveBootstrap‹   s   z.BootstrapMixinTests.test_addAndRemoveBootstrapN)r=   r>   r?   rF   r   rW   rZ   r   r   r   r   rH   q   s
    rH   c                   @   rG   )	Ú!GenericXmlStreamFactoryTestsMixinz3
    Generic tests for L{XmlStream} factories.
    c                 C   rI   r   )r   ÚXmlStreamFactoryrK   r   r   r   r   r   ¤   r#   z'GenericXmlStreamFactoryTestsMixin.setUpc                    sJ   g ‰ ‡ fdd„}| j  d|¡ | j  d¡}| dd¡ |  dtˆ ƒ¡ dS )zY
        The protocol factory installs bootstrap event handlers on the protocol.
        c                    r+   r   r   rL   rN   r   r   rP   ­   r#   zRGenericXmlStreamFactoryTestsMixin.test_buildProtocolInstallsBootstraps.<locals>.cbrQ   Nr%   )rK   rR   ÚbuildProtocolrT   r   r)   )r   rP   Úxsr   rN   r   Ú$test_buildProtocolInstallsBootstraps§   s   zFGenericXmlStreamFactoryTestsMixin.test_buildProtocolInstallsBootstrapsc                 C   s    | j  d¡}|  | j |j ¡ dS )z@
        The protocol factory is saved in the protocol.
        N)rK   r]   ÚassertIdentical©r   r^   r   r   r   Útest_buildProtocolStoresFactory·   s   zAGenericXmlStreamFactoryTestsMixin.test_buildProtocolStoresFactoryN)r=   r>   r?   rF   r   r_   rb   r   r   r   r   r[   Ÿ   s
    r[   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚXmlStreamFactoryMixinTestsz7
    Tests for L{xmlstream.XmlStreamFactoryMixin}.
    c                 C   s   t jd d d�| _t| j_d S )N)Útest)r   ÚXmlStreamFactoryMixinrK   r@   r   r   r   r   r   r   Ä   s   z XmlStreamFactoryMixinTests.setUpc                 C   s0   | j  d¡}|  d|j¡ |  ddi|j¡ dS )zb
        Arguments passed to the factory are passed to protocol on
        instantiation.
        Nr   rd   )rK   r]   r   rA   rB   ra   r   r   r   Ú"test_buildProtocolFactoryArgumentsÈ   s   z=XmlStreamFactoryMixinTests.test_buildProtocolFactoryArgumentsN)r=   r>   r?   rF   r   rf   r   r   r   r   rc   ¿   s    rc   N)rF   Útwisted.internetr   Útwisted.pythonr   Útwisted.trialr   Útwisted.words.xishr   r   r   ÚTestCaser   ÚProtocolrD   r@   rH   r[   rc   r   r   r   r   Ú<module>   s   R. 