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 ddlmZ G dd	„ d	ƒZG d
d„ dƒZG dd„ dejƒZG dd„ dejƒZdS )z+
Test cases for twisted.words.xish.utility
é    )Úabsolute_importÚdivision)ÚOrderedDict)Úunittest)Úutility)ÚElement)ÚEventDispatcherc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCallbackTrackerzŒ
    Test helper for tracking callbacks.

    Increases a counter on each call to L{call} and stores the object
    passed in the call.
    c                 C   s   d| _ d | _d S )Nr   ©ÚcalledÚobj©Úself© r   úB/usr/lib/python3/dist-packages/twisted/words/test/test_xishutil.pyÚ__init__   s    zCallbackTracker.__init__c                 C   s   | j d | _ || _d S )Né   r
   )r   r   r   r   r   Úcall   s    zCallbackTracker.callN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r	      s   r	   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚOrderedCallbackTrackerz=
    Test helper for tracking callbacks and their order.
    c                 C   s
   g | _ d S ©N)ÚcallListr   r   r   r   r   *   s    zOrderedCallbackTracker.__init__c                 C   s   | j  | j¡ d S r   )r   ÚappendÚcall1©r   Úobjectr   r   r   r   .   s    zOrderedCallbackTracker.call1c                 C   s   | j  | j¡ d S r   )r   r   Úcall2r   r   r   r   r   2   s    zOrderedCallbackTracker.call2c                 C   s   | j  | j¡ d S r   )r   r   Úcall3r   r   r   r   r    6   s    zOrderedCallbackTracker.call3N)r   r   r   r   r   r   r   r    r   r   r   r   r   %   s
   r   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚEventDispatcherTestsz'
    Tests for L{EventDispatcher}.
    c                 C   s<  t ƒ }tƒ }tƒ }tƒ }| d|j¡ | d|j¡ | d|j¡ | d|j¡ tdƒ}| d¡ tdƒ}| d¡ | |¡ |  |jd	¡ |  |j	|¡ |  |jd
¡ | |¡ |  |jd	¡ |  |jd¡ |  |j	|¡ |  |jd
¡ | |d¡ |  |jd¡ |  |j	|¡ | 
d|j¡ | |¡ |  |jd¡ d S )Nú/message/bodyú/messageú	/presenceú//event/testevent©ÚnsÚmessageÚbody©r'   Úpresencer+   é   r   r   )r   r	   ÚaddObserverr   r   Ú
addElementÚdispatchÚassertEqualr   r   ÚremoveObserver)r   ÚdÚcb1Úcb2Zcb3ÚmsgÚpresr   r   r   Ú	testStuff@   s6    




zEventDispatcherTests.testStuffc                 C   sv   t ƒ }tƒ }tƒ }| d|j¡ | d|j¡ | |d¡ |  |jd¡ |  |j|¡ |  |jd¡ |  |j|¡ dS )zŽ
        Test adding two observers for the same query.

        When the event is dispatched both of the observers need to be called.
        r%   r   N)r   r	   r-   r   r/   r0   r   r   )r   r2   r3   r4   r   r   r   Útest_addObserverTwicee   s    z*EventDispatcherTests.test_addObserverTwicec                    sz   t ƒ ‰tdƒ}tƒ ‰ ‡ ‡fdd„}ˆ d|¡ ˆ |¡ |  ˆ jd¡ ˆ |¡ |  ˆ jd¡ ˆ |¡ |  ˆ jd¡ dS )	zG
        Test for registration of an observer during dispatch.
        r&   c                    s   ˆ  dˆ j¡ d S ©Nr#   )r-   r   ©Ú_©Úcbr2   r   r   Ú	onMessage   s    zBEventDispatcherTests.test_addObserverInDispatch.<locals>.onMessager#   r   r   r,   N©r   r   r	   ÚaddOnetimeObserverr/   r0   r   ©r   r5   r>   r   r<   r   Útest_addObserverInDispatchy   s    


z/EventDispatcherTests.test_addObserverInDispatchc                    sz   t ƒ ‰tdƒ}tƒ ‰ ‡ ‡fdd„}ˆ d|¡ ˆ |¡ |  ˆ jd¡ ˆ |¡ |  ˆ jd¡ ˆ |¡ |  ˆ jd¡ dS )zN
        Test for registration of a onetime observer during dispatch.
        r&   c                    s   ˆ  dˆ j¡ d S r9   )r@   r   )r5   r<   r   r   r>   ˜   s    zIEventDispatcherTests.test_addOnetimeObserverInDispatch.<locals>.onMessager#   r   r   Nr?   rA   r   r<   r   Ú!test_addOnetimeObserverInDispatch   s    


z6EventDispatcherTests.test_addOnetimeObserverInDispatchc                 C   sV   t ƒ }tdƒ}tƒ }| d|j¡ | |¡ |  |jd¡ | |¡ |  |jd¡ d S )Nr&   r#   r   )r   r   r	   r@   r   r/   r0   r   )r   r2   r5   r=   r   r   r   ÚtestOnetimeDispatch§   s    

z(EventDispatcherTests.testOnetimeDispatchc                 C   sZ   t ƒ }tdƒ}tdƒ}tƒ }| d|j¡ | |¡}|  d|¡ | |¡}|  d|¡ d S )Nr&   r*   r$   FT)r   r   r	   r-   r   r/   r0   )r   r2   r5   r6   r=   Úresultr   r   r   ÚtestDispatcherResult³   s    

z)EventDispatcherTests.testDispatcherResultc                 C   sŠ   t ƒ }tƒ }| d|j¡ | d|jd¡ | d|jd¡ tdƒ}| d¡ | |¡ |  	|j
|j|j|jgdtdd	„ |j
D ƒƒ ¡ d S )
Nr"   r#   éÿÿÿÿr   r&   r)   zCalls out of order: %sc                 S   s   g | ]
}|j ‘qS r   )r   )Ú.0Úcr   r   r   Ú
<listcomp>Í   s     zAEventDispatcherTests.testOrderedXPathDispatch.<locals>.<listcomp>)r   r   r-   r   r    r   r   r.   r/   r0   r   Úrepr©r   r2   r=   r5   r   r   r   ÚtestOrderedXPathDispatchÁ   s    

ÿÿz-EventDispatcherTests.testOrderedXPathDispatchc                 C   s^   t ƒ }tƒ }| d|j¡ | dd¡ |  d|j¡ | d|j¡ |  dt|j	 
d¡ƒ¡ dS )zO
        Test observer clean-up after removeObserver for named events.
        ú//event/testNr   r   )r   r	   r-   r   r/   r0   r   r1   ÚlenÚ_eventObserversÚpop©r   r2   r=   r   r   r   Útest_cleanUpRemoveEventObserverÖ   s    z4EventDispatcherTests.test_cleanUpRemoveEventObserverc                 C   sd   t ƒ }tƒ }tdƒ}| d|j¡ | |¡ |  d|j¡ | d|j¡ |  dt	|j
 d¡ƒ¡ dS )zO
        Test observer clean-up after removeObserver for XPath events.
        ©Nr(   r#   r   r   N)r   r	   r   r-   r   r/   r0   r   r1   rO   Ú_xpathObserversrQ   rL   r   r   r   Útest_cleanUpRemoveXPathObserverå   s    
z4EventDispatcherTests.test_cleanUpRemoveXPathObserverc                 C   sP   t ƒ }tƒ }| d|j¡ | dd¡ |  d|j¡ |  dt|j 	d¡ƒ¡ dS )zD
        Test observer clean-up after onetime named events.
        rN   Nr   r   )
r   r	   r@   r   r/   r0   r   rO   rP   rQ   rR   r   r   r   Ú test_cleanUpOnetimeEventObserverõ   s    z5EventDispatcherTests.test_cleanUpOnetimeEventObserverc                 C   sV   t ƒ }tƒ }tdƒ}| d|j¡ | |¡ |  d|j¡ |  dt|j	 
d¡ƒ¡ dS )zD
        Test observer clean-up after onetime XPath events.
        rT   r#   r   r   N)r   r	   r   r@   r   r/   r0   r   rO   rU   rQ   rL   r   r   r   Ú test_cleanUpOnetimeXPathObserver  s    
z5EventDispatcherTests.test_cleanUpOnetimeXPathObserverc                    sÆ   G dd„ dt jƒ}G dd„ dtƒ‰ ‡ fdd„}tƒ }tƒ }t j}zx|t _| d|¡ | d|j¡ z| dd¡ W n ˆ k
r   |  d	¡ Y nX |  	d
t
|  ˆ ¡ƒ¡ |  	d
|j¡ W 5 |t _X dS )zÓ
        Test that exceptions in observers do not bubble up to dispatch.

        The exceptions raised in observers should be logged and other
        observers should be called as if nothing happened.
        c                   @   s   e Zd Zdd„ ZdS )zOEventDispatcherTests.test_observerRaisingException.<locals>.OrderedCallbackListc                 S   s   t ƒ | _d S r   )r   Z	callbacksr   r   r   r   r     s    zXEventDispatcherTests.test_observerRaisingException.<locals>.OrderedCallbackList.__init__N)r   r   r   r   r   r   r   r   ÚOrderedCallbackList  s   rY   c                   @   s   e Zd ZdS )zEEventDispatcherTests.test_observerRaisingException.<locals>.TestErrorN)r   r   r   r   r   r   r   Ú	TestError  s   rZ   c                    s
   ˆ ƒ ‚d S r   r   r:   ©rZ   r   r   Ú
raiseError!  s    zFEventDispatcherTests.test_observerRaisingException.<locals>.raiseErrorrN   Nz2TestError raised. Should have been logged instead.r   )r   ZCallbackListÚ	Exceptionr   r	   r-   r   r/   Zfailr0   rO   ZflushLoggedErrorsr   )r   rY   r\   r2   r=   ZoriginalCallbackListr   r[   r   Útest_observerRaisingException  s"    z2EventDispatcherTests.test_observerRaisingExceptionN)r   r   r   r   r7   r8   rB   rC   rD   rF   rM   rS   rV   rW   rX   r^   r   r   r   r   r!   ;   s   %r!   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚXmlPipeTestsz:
    Tests for L{twisted.words.xish.utility.XmlPipe}.
    c                 C   s   t  ¡ | _d S r   )r   ZXmlPipeÚpiper   r   r   r   ÚsetUp?  s    zXmlPipeTests.setUpc                    sH   ‡ fdd„}g ‰ | j j d|¡ tdƒ}| j j |¡ |  |gˆ ¡ dS )zO
        Send an element from the source and observe it from the sink.
        c                    s   ˆ   | ¡ d S r   ©r   ©r   ©r   r   r   r=   G  s    z,XmlPipeTests.test_sendFromSource.<locals>.cbú/test[@xmlns="testns"]©ZtestnsZtestN)r`   Úsinkr-   r   ÚsourceÚsendr0   ©r   r=   Úelementr   rd   r   Útest_sendFromSourceC  s    z XmlPipeTests.test_sendFromSourcec                    sH   ‡ fdd„}g ‰ | j j d|¡ tdƒ}| j j |¡ |  |gˆ ¡ dS )zO
        Send an element from the sink and observe it from the source.
        c                    s   ˆ   | ¡ d S r   rb   rc   rd   r   r   r=   U  s    z*XmlPipeTests.test_sendFromSink.<locals>.cbre   rf   N)r`   rh   r-   r   rg   ri   r0   rj   r   rd   r   Útest_sendFromSinkQ  s    zXmlPipeTests.test_sendFromSinkN)r   r   r   r   ra   rl   rm   r   r   r   r   r_   :  s   r_   N)r   Z
__future__r   r   Úcollectionsr   Ztwisted.trialr   Ztwisted.words.xishr   Ztwisted.words.xish.domishr   Ztwisted.words.xish.utilityr   r	   r   ZTestCaser!   r_   r   r   r   r   Ú<module>   s     