U
    /Wj±  ã                   @   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   úlc:\users\psant\downloads\hotstarbackend\school\venv38\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                    s   ˆ   d ¡ d S ©N©r   )Zrootelem©ZstreamStartedr   r   ÚstreamStartEvent.   s    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                    s   ˆ   | ¡ d S r   r   ©Úreason)ÚstreamErrorr   r   ÚstreamErrorEvent=   s    z;XmlStreamTests.test_receiveBadXML.<locals>.streamErrorEventc                    s   ˆ   d ¡ d S r   r   )Ú_©Ú	streamEndr   r   ÚstreamEndEvent@   s    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                    s   ˆ   | ¡ d S r   r   r$   r)   r   r   r+   V   s    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#   r/   r0   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   r5   r6   r   r   r   r8   i   s    zDummyProtocol.__init__N)r1   r2   r3   Ú__doc__r8   r   r   r   r   r4   a   s   r4   c                   @   s(   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   s   t  ¡ | _d S r   )r   ZBootstrapMixinÚfactoryr   r   r   r   r   x   s    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                    s   ˆ   | ¡ d S r   r   ©Údata©Úcalledr   r   Úcb�   s    z6BootstrapMixinTests.test_installBootstraps.<locals>.cbú//event/myeventNr   )r4   r;   ÚaddBootstrapÚinstallBootstrapsÚdispatchr   r"   ©r   r@   Ú
dispatcherr   r>   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                    s   ˆ   | ¡ d S r   r   r<   r>   r   r   r@   ’   s    z:BootstrapMixinTests.test_addAndRemoveBootstrap.<locals>.cbrA   N)r;   rB   ZremoveBootstrapr4   rC   rD   ZassertFalserE   r   r>   r   Útest_addAndRemoveBootstrap‹   s    z.BootstrapMixinTests.test_addAndRemoveBootstrapN)r1   r2   r3   r9   r   rG   rH   r   r   r   r   r:   q   s   r:   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú!GenericXmlStreamFactoryTestsMixinz3
    Generic tests for L{XmlStream} factories.
    c                 C   s   t  ¡ | _d S r   )r   ZXmlStreamFactoryr;   r   r   r   r   r   ¤   s    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                    s   ˆ   | ¡ d S r   r   r<   r>   r   r   r@   ­   s    zRGenericXmlStreamFactoryTestsMixin.test_buildProtocolInstallsBootstraps.<locals>.cbrA   Nr   )r;   rB   ÚbuildProtocolrD   r   r"   )r   r@   Úxsr   r>   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;   rJ   ZassertIdentical©r   rK   r   r   r   Útest_buildProtocolStoresFactory·   s    zAGenericXmlStreamFactoryTestsMixin.test_buildProtocolStoresFactoryN)r1   r2   r3   r9   r   rL   rN   r   r   r   r   rI   Ÿ   s   rI   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;   r4   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   rP   )r;   rJ   r   r5   r6   rM   r   r   r   Ú"test_buildProtocolFactoryArgumentsÈ   s    z=XmlStreamFactoryMixinTests.test_buildProtocolFactoryArgumentsN)r1   r2   r3   r9   r   rQ   r   r   r   r   rO   ¿   s   rO   N)r9   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.trialr   Ztwisted.words.xishr   r   r   ZTestCaser   ÚProtocolr7   r4   r:   rI   rO   r   r   r   r   Ú<module>   s   R. 