o
    Óó·i±  ã                   @   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   Z	XmlStreamÚ	transportÚappendÚwrite©Úself© r   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   Z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   )Zrootelem©ZstreamStartedr   r   ÚstreamStartEvent.   ó   z9XmlStreamTests.test_receiveRoot.<locals>.streamStartEventú<root>é   N)r   ÚaddObserverZ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"   ZSTREAM_ERROR_EVENTÚSTREAM_END_EVENTr   r#   r   r$   Ú
assertTrueÚcheckr   ZParserError)r   r*   r.   r   )r-   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'   r,   r   r   r.   V   r   z5XmlStreamTests.test_streamEnd.<locals>.streamEndEventr!   r   N)r   r"   r/   r   r   r   r$   ZassertIsInstancer   r   ZgetErrorMessager	   )r   r.   r   r,   r   Útest_streamEndP   s   
zXmlStreamTests.test_streamEndN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r%   r2   r3   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ÚkwargsZ	observersr   ÚEventDispatcherÚ__init__)r   r8   r9   r   r   r   r;   i   s   zDummyProtocol.__init__N)r4   r5   r6   Ú__doc__r;   r   r   r   r   r7   a   s    r7   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   Z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!   )r7   r@   ÚaddBootstrapÚinstallBootstrapsÚdispatchr   r$   ©r   rE   Ú
dispatcherr   rC   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   rA   rC   r   r   rE   ’   r   z:BootstrapMixinTests.test_addAndRemoveBootstrap.<locals>.cbrF   N)r@   rG   ZremoveBootstrapr7   rH   rI   ZassertFalserJ   r   rC   r   Útest_addAndRemoveBootstrap‹   s   z.BootstrapMixinTests.test_addAndRemoveBootstrapN)r4   r5   r6   r<   r   rL   rM   r   r   r   r   r>   q   s
    r>   c                   @   r=   )	Ú!GenericXmlStreamFactoryTestsMixinz3
    Generic tests for L{XmlStream} factories.
    c                 C   r?   r   )r   ZXmlStreamFactoryr@   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   rA   rC   r   r   rE   ­   r   zRGenericXmlStreamFactoryTestsMixin.test_buildProtocolInstallsBootstraps.<locals>.cbrF   Nr!   )r@   rG   ÚbuildProtocolrI   r   r$   )r   rE   Úxsr   rC   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)r@   rO   ZassertIdentical©r   rP   r   r   r   Útest_buildProtocolStoresFactory·   s   zAGenericXmlStreamFactoryTestsMixin.test_buildProtocolStoresFactoryN)r4   r5   r6   r<   r   rQ   rS   r   r   r   r   rN   Ÿ   s
    rN   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   ZXmlStreamFactoryMixinr@   r7   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   rU   )r@   rO   r   r8   r9   rR   r   r   r   Ú"test_buildProtocolFactoryArgumentsÈ   s   z=XmlStreamFactoryMixinTests.test_buildProtocolFactoryArgumentsN)r4   r5   r6   r<   r   rV   r   r   r   r   rT   ¿   s    rT   N)r<   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.trialr   Ztwisted.words.xishr   r   r   ZTestCaser   ÚProtocolr:   r7   r>   rN   rT   r   r   r   r   Ú<module>   s   R. 