U
    
W[‚  ã                   @   sª   d Z ddlmZm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}.
é    )Úabsolute_importÚdivision)Ú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	XmlStreamZ	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	   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   ÚstreamStartEvent0   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   ÚstreamErrorEventA   s    z;XmlStreamTests.test_receiveBadXML.<locals>.streamErrorEventc                    s   ˆ   d ¡ d S r   r   )Ú_©Ú	streamEndr   r   ÚstreamEndEventD   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#   Z
assertTrueZcheckr   ZParserError)r   r(   r,   r   )r+   r'   r   Útest_receiveBadXML:   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,   ]   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_streamEndW   s    
ÿ
ÿzXmlStreamTests.test_streamEndN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r$   r.   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ÚkwargsZ	observersr   ÚEventDispatcherÚ__init__)r   r4   r5   r   r   r   r7   s   s    zDummyProtocol.__init__N)r0   r1   r2   Ú__doc__r7   r   r   r   r   r3   k   s   r3   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   ƒ   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©Zcalledr   r   Úcb   s    z6BootstrapMixinTests.test_installBootstraps.<locals>.cbú//event/myeventNr    )r3   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>.cbr?   N)r:   r@   ZremoveBootstrapr3   rA   rB   ZassertFalserC   r   r=   r   Útest_addAndRemoveBootstrap˜   s    z.BootstrapMixinTests.test_addAndRemoveBootstrapN)r0   r1   r2   r8   r   rE   rF   r   r   r   r   r9   |   s   r9   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>.cbr?   Nr    )r:   r@   ÚbuildProtocolrB   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:   rH   ZassertIdentical©r   rI   r   r   r   Útest_buildProtocolStoresFactoryÇ   s    zAGenericXmlStreamFactoryTestsMixin.test_buildProtocolStoresFactoryN)r0   r1   r2   r8   r   rJ   rL   r   r   r   r   rG   ­   s   rG   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:   r3   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   rN   )r:   rH   r   r4   r5   rK   r   r   r   Ú"test_buildProtocolFactoryArgumentsÚ   s    z=XmlStreamFactoryMixinTests.test_buildProtocolFactoryArgumentsN)r0   r1   r2   r8   r   rO   r   r   r   r   rM   Ð   s   rM   N)r8   Z
__future__r   r   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.trialr   Ztwisted.words.xishr   r   r	   ZTestCaser
   ZProtocolr6   r3   r9   rG   rM   r   r   r   r   Ú<module>   s   \1#