U
    W[4                     @   s  d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
mZmZmZmZ ddlmZ ddlmZ d	d
 Ze ZG dd dejZG dd dejZG dd dejZG dd dZG dd de
ejeZG dd deejeZG dd deejeZG dd dejZdS )z%
Tests for L{twisted.conch.manhole}.
    N)unittest)errordefer)StringTransport)_TelnetMixin	_SSHMixin_StdioMixinstdiossh)manhole)insultsc                   C   s.   zdd  W n   t  d d  Y S X dS )z
    Return the string used by Python as the name for code objects which are
    compiled from interactive input or at the top-level of modules.
       r      N)	tracebackextract_stack r   r   A/usr/lib/python3/dist-packages/twisted/conch/test/test_manhole.pydetermineDefaultFunctionName   s    r   c                   @   s   e Zd ZdZdd ZdS )ManholeInterpreterTestsz2
    Tests for L{manhole.ManholeInterpreter}.
    c                 C   s2   t d}|jddg |  | |j dS )zR
        L{ManholeInterpreter.resetBuffer} should empty the input buffer.
        N12)r   ZManholeInterpreterbufferextendZresetBufferassertFalse)selfinterpreterr   r   r   test_resetBuffer*   s    
z(ManholeInterpreterTests.test_resetBufferN)__name__
__module____qualname____doc__r   r   r   r   r   r   &   s   r   c                   @   s   e Zd ZdZdd ZdS )ManholeProtocolTestsz'
    Tests for L{manhole.Manhole}.
    c                 C   sP   t  }ttj}|| |j}|j}|j	ddg |
  | |j dS )zn
        L{manhole.Manhole.handle_INT} should cause the interpreter input buffer
        to be reset.
        r   r   N)r   r   ZServerProtocolr   ZManholeZmakeConnectionZterminalProtocolr   r   r   Z
handle_INTr   )r   Z	transportZterminalZprotocolr   r   r   r   %test_interruptResetsInterpreterBuffer9   s    
z:ManholeProtocolTests.test_interruptResetsInterpreterBufferN)r   r   r    r!   r#   r   r   r   r   r"   5   s   r"   c                   @   s\   e 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S )WriterTestsc                 C   s   t d dS )z&
        Colorize an integer.
        r   Nr   lastColorizedLiner   r   r   r   test_IntegerJ   s    zWriterTests.test_Integerc                 C   s   t d dS )z7
        Colorize an integer in double quotes.
        z"1"Nr%   r'   r   r   r   test_DoubleQuoteStringQ   s    z"WriterTests.test_DoubleQuoteStringc                 C   s   t d dS )z7
        Colorize an integer in single quotes.
        z'1'Nr%   r'   r   r   r   test_SingleQuoteStringX   s    z"WriterTests.test_SingleQuoteStringc                 C   s   t d dS )z7
        Colorize an integer in triple quotes.
        z'''1'''Nr%   r'   r   r   r   test_TripleSingleQuotedString_   s    z)WriterTests.test_TripleSingleQuotedStringc                 C   s   t d dS )zB
        Colorize an integer in triple and double quotes.
        z"""1"""Nr%   r'   r   r   r   test_TripleDoubleQuotedStringf   s    z)WriterTests.test_TripleDoubleQuotedStringc                 C   s   t d dS )z1
        Colorize a function definition.
        z
def foo():Nr%   r'   r   r   r   test_FunctionDefinitionm   s    z#WriterTests.test_FunctionDefinitionc                 C   s   t d dS )z.
        Colorize a class definition.
        z
class foo:Nr%   r'   r   r   r   test_ClassDefinitiont   s    z WriterTests.test_ClassDefinitionc                 C   s   t d}| t|t dS )z,
        Colorize a Unicode string.
           иNr   r&   Z
assertTrue
isinstancebytesr   resr   r   r   test_unicode{   s    
zWriterTests.test_unicodec                 C   s   t d}| t|t dS )z/
        Colorize a UTF-8 byte string.
           иNr0   r3   r   r   r   
test_bytes   s    
zWriterTests.test_bytesc                 C   s   |  tdtd dS )zT
        The output of UTF-8 bytestrings and Unicode strings are identical.
        r6   r/   N)assertEqualr   r&   r'   r   r   r   test_identicalOutput   s    z WriterTests.test_identicalOutputN)r   r   r    r(   r)   r*   r+   r,   r-   r.   r5   r7   r9   r   r   r   r   r$   I   s   r$   c                   @   s   e Zd Zej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ejdd Zejdd Zdd Zdd Zejdd ZdS )ManholeLoopbackMixinc                 C   s
   t |S )N)r   ZwaitForDeferredr   dr   r   r   wfd   s    zManholeLoopbackMixin.wfdc                    s,    j d} d  fdd}||S )z-
        Evaluate simple expression.
           dones
   1 + 1
donec                    s     dddg d S )N	   >>> 1 + 1   2   >>> done_assertBufferignr'   r   r   finished   s    z<ManholeLoopbackMixin.test_SimpleExpression.<locals>.finishedrecvlineClientexpect
_testwriteaddCallbackr   ZdonerF   r   r'   r   test_SimpleExpression   s    z*ManholeLoopbackMixin.test_SimpleExpressionc                    s,    j d} d  fdd}||S )z>
        Evaluate line continuation in triple quotes.
        r>   s   '''
'''
donec                    s     ddddg d S )Ns   >>> '''s   ... '''s   '\n'rA   rB   rD   r'   r   r   rF      s    zGManholeLoopbackMixin.test_TripleQuoteLineContinuation.<locals>.finishedrG   rL   r   r'   r    test_TripleQuoteLineContinuation   s    z5ManholeLoopbackMixin.test_TripleQuoteLineContinuationc                    s,    j d} d  fdd}||S )z/
        Evaluate function definition.
        r>   s'   def foo(bar):
	print(bar)

foo(42)
donec                    s     ddddddg d S )Ns   >>> def foo(bar):s   ...     print(bar)   ... s   >>> foo(42)s   42rA   rB   rD   r'   r   r   rF      s    z>ManholeLoopbackMixin.test_FunctionDefinition.<locals>.finishedrG   rL   r   r'   r   r-      s    	z,ManholeLoopbackMixin.test_FunctionDefinitionc                    s,    j d} d  fdd}||S )z,
        Evaluate class definition.
        r>   sE   class Foo:
	def bar(self):
		print('Hello, world!')

Foo().bar()
donec              	      s     dddddddg d S )Ns   >>> class Foo:s   ...     def bar(self):s"   ...         print('Hello, world!')rO   s   >>> Foo().bar()s   Hello, world!rA   rB   rD   r'   r   r   rF      s    z;ManholeLoopbackMixin.test_ClassDefinition.<locals>.finishedrG   rL   r   r'   r   r.      s    
z)ManholeLoopbackMixin.test_ClassDefinitionc                    s,    j d} d  fdd}||S )z0
        Evaluate raising an exception.
        r>   s#   raise Exception('foo bar baz')
donec                    s"     dddtd ddg d S )Ns"   >>> raise Exception('foo bar baz')s"   Traceback (most recent call last):s     File "<console>", line 1, in zutf-8s   Exception: foo bar bazrA   )rC   defaultFunctionNameencoderD   r'   r   r   rF      s    z5ManholeLoopbackMixin.test_Exception.<locals>.finishedrG   rL   r   r'   r   test_Exception   s    	z#ManholeLoopbackMixin.test_Exceptionc                    s6    j d} dtj d   fdd}||S )z4
        Evaluate interrupting with CTRL-C.
        r>      cancelled linec                    s     dddg d S )N   >>> cancelled line   KeyboardInterruptrA   rB   rD   r'   r   r   rF     s    z4ManholeLoopbackMixin.test_ControlC.<locals>.finished)rH   rI   rJ   r   CTRL_CrK   rL   r   r'   r   test_ControlC
  s    z"ManholeLoopbackMixin.test_ControlCc                    sF    j d} d  fdd}||  fdd}|| |S )z
        Sending ^C to Manhole while in a state where more input is required to
        complete a statement should discard the entire ongoing statement and
        reset the input prompt to the non-continuation prompt.
        s   thingss   (
thingsc                    s*     ddg  jd} tj |S )N   >>> (
   ... things   >>> )rC   rH   rI   rJ   r   rV   )ignoredZinterruptedr'   r   r   gotContinuation'  s    zNManholeLoopbackMixin.test_interruptDuringContinuation.<locals>.gotContinuationc                    s     ddddg d S )NrX   rY   rU   rZ   rB   )r[   r'   r   r   gotInterruption0  s    zNManholeLoopbackMixin.test_interruptDuringContinuation.<locals>.gotInterruptionrG   )r   Z
continuingr\   r]   r   r'   r    test_interruptDuringContinuation  s    


z5ManholeLoopbackMixin.test_interruptDuringContinuationc                    s>     d  jd} fdd} fdd}|||S )z2
        Evaluate cancelling with CTRL-\.
        rS   c                    s.     dg  tj  jj} |tjS )NrT   )	rC   rJ   r   ZCTRL_BACKSLASHrH   onDisconnectionassertFailurer   ConnectionDone)rE   r<   r'   r   r   gotPartialLineA  s    zBManholeLoopbackMixin.test_ControlBackslash.<locals>.gotPartialLinec                    s     dg d S )N    rB   rD   r'   r   r   gotClearedLineI  s    zBManholeLoopbackMixin.test_ControlBackslash.<locals>.gotClearedLinerJ   rH   rI   rK   )r   ZpartialLinerb   rd   r   r'   r   test_ControlBackslash:  s    

z*ManholeLoopbackMixin.test_ControlBackslashc                 c   s   |  d | jdV  | dg |  tjd  | jdV  | dg |  d | jdV  |  tj | jj}| |tj	V  dS )	z~
        A CTRL+D in the middle of a line doesn't close a connection,
        but at the beginning of a line it does.
        s   1 + 1   \+ 1r?       + 1   >>> 1 + 1 + 1   
s   3
>>> N)
rJ   rH   rI   rC   r   ZCTRL_Dr_   r`   r   ra   r;   r   r   r   test_controlDQ  s    

z"ManholeLoopbackMixin.test_controlDc                 c   sT   |  d | jdV  | ddg |  tjd  | jdV  | dg dS )	a  
        CTRL+L is generally used as a redraw-screen command in terminal
        applications.  Manhole doesn't currently respect this usage of it,
        but it should at least do something reasonable in response to this
        event (rather than, say, eating your face).
        s   
1 + 1rg   rZ   r?   rh   s   1 \+ 1 \+ 1ri   N)rJ   rH   rI   rC   r   ZCTRL_Lr'   r   r   r   test_ControlLg  s    

z"ManholeLoopbackMixin.test_ControlLc                    s,     d  jd} fdd}||S )zl
        CTRL-A can be used as HOME - returning cursor to beginning of
        current line buffer.
        s   rint "hello"p   print "hello"c                    s     dg d S Ns   >>> print "hello"rB   ignorer'   r   r   cb  s    z.ManholeLoopbackMixin.test_controlA.<locals>.cbre   r   r<   rq   r   r'   r   test_controlAz  s    
z"ManholeLoopbackMixin.test_controlAc                    s,     d  jd} fdd}||S )zc
        CTRL-E can be used as END - setting cursor to end of current
        line buffer.
        s   rint "hellop"rm   c                    s     dg d S rn   rB   ro   r'   r   r   rq     s    z.ManholeLoopbackMixin.test_controlE.<locals>.cbre   rr   r   r'   r   test_controlE  s    
z"ManholeLoopbackMixin.test_controlEc              	   c   sZ   |  d | jdV  |  d | jdV  | jdV  | ddddd	d
dg dS )z
        When a deferred is returned to the manhole REPL, it is displayed with
        a sequence number, and when the deferred fires, the result is printed.
        sC   from twisted.internet import defer, reactor
d = defer.Deferred()
d
s   <Deferred #0>s.   c = reactor.callLater(0.1, d.callback, 'Hi!')
rZ   s#   Deferred #0 called back: 'Hi!'
>>> s/   >>> from twisted.internet import defer, reactors   >>> d = defer.Deferred()s   >>> ds1   >>> c = reactor.callLater(0.1, d.callback, 'Hi!')s   Deferred #0 called back: 'Hi!'N)rJ   rH   rI   rC   r'   r   r   r   test_deferred  s*    z"ManholeLoopbackMixin.test_deferredN)r   r   r    r   ZColoredManholeserverProtocolr=   rM   rN   r-   r.   rR   rW   r^   rf   r   ZinlineCallbacksrk   rl   rs   rt   ru   r   r   r   r   r:      s$   

r:   c                   @   s   e Zd ZdZdS )ManholeLoopbackTelnetTestsz,
    Test manhole loopback over Telnet.
    N)r   r   r    r!   r   r   r   r   rw     s   rw   c                   @   s   e Zd ZdZedkrdZdS )ManholeLoopbackSSHTestsz)
    Test manhole loopback over SSH.
    Nz!cryptography requirements missing)r   r   r    r!   r
   skipr   r   r   r   rx     s   rx   c                   @   s$   e Zd ZdZedkrdZnejZdS )ManholeLoopbackStdioTestsz1
    Test manhole loopback over standard IO.
    NTerminal requirements missing)r   r   r    r!   r	   ry   ZConsoleManholerv   r   r   r   r   rz     s   rz   c                   @   s$   e Zd ZdZedkrdZdd ZdS )ManholeMainTestsz=
    Test the I{main} method from the I{manhole} module.
    Nr{   c                 C   s*   | j ttjdgd}| d|jd  dS )z
        Will raise an exception when called with an argument which is a
        dotted patch which can not be imported..
        zno-such-class)argvzEmpty module namer   N)ZassertRaises
ValueErrorr	   mainr8   args)r   Z	exceptionr   r   r   test_mainClassNotFound  s     z'ManholeMainTests.test_mainClassNotFound)r   r   r    r!   r	   ry   r   r   r   r   r   r|     s   r|   )r!   r   Ztwisted.trialr   Ztwisted.internetr   r   Ztwisted.test.proto_helpersr   Z twisted.conch.test.test_recvliner   r   r   r	   r
   Ztwisted.conchr   Ztwisted.conch.insultsr   r   rP   ZTestCaser   r"   r$   r:   rw   rx   rz   r|   r   r   r   r   <module>   s2   K  	
