a
    ø£BeT   ã                   @   sÎ   d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlm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eƒZdd„ ZG dd„ dejƒZdd„ ZG dd„ dejƒZdS )z:Tests for the TerminalInteractiveShell and related pieces.é    N)ÚAutoSuggestFromHistory)Útools)Ú_elideÚ(_adjust_completion_text_based_on_context)ÚNavigableAutoSuggestFromHistoryc                   @   s   e Zd Zdd„ ZdS )ÚTestAutoSuggestc                 C   sF   t ƒ }d |_|  |jd ¡ d|_|  |jt¡ d|_|  |jt¡ d S )Nr   r   )Úget_ipythonZautosuggestions_providerÚassertEqualZauto_suggestZassertIsInstancer   r   ©ÚselfÚip© r   ún/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/terminal/tests/test_interactivshell.pyÚtest_changing_provider   s    z&TestAutoSuggest.test_changing_providerN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú	TestElidec                 C   sh   t ddƒ t ddƒ |  t ddƒd¡ tj g d¢¡}tjd d d	 tj d
 }|  t |dƒ|¡ d S )Nzconcatenate((a1, a2, ...), axisÚ z concatenate((a1, a2, ..), . axisz0aaaa.bbbb.ccccc.dddddd.eeeee.fffff.gggggg.hhhhhhu   aaaa.bâ€¦g.hhhhhh)r   Z
aaaaaaaaaaZ
bbbbbbbbbbÚ
ccccccccccr   Úau   â€¦Úbr   )r   r	   ÚosÚsepÚjoin)r   Útest_stringZexpect_stringr   r   r   Ú
test_elide   s    

þÿzTestElide.test_elidec                 C   s   |   tdddd�d¡ d S )Nú,the quick brown fox jumped over the lazy dogzthe quick brown foxé
   ©Z	min_elideu"   theâ€¦fox jumped over the lazy dog©r	   r   ©r   r   r   r   Útest_elide_typed_normal-   s    ýúz!TestElide.test_elide_typed_normalc                 C   s   |   tdddd�d¡ dS )úY
        if the match is too short we don't elide.
        avoid the "the...the"
        r   Zther   r   Nr    r!   r   r   r   Útest_elide_typed_short_match7   s    þz&TestElide.test_elide_typed_short_matchc                 C   s   |   tdddd�d¡ dS )r#   r   zthe quick red foxr   r   Nr    r!   r   r   r   Útest_elide_typed_no_matchA   s    ýúz#TestElide.test_elide_typed_no_matchN)r   r   r   r   r"   r$   r%   r   r   r   r   r      s   

r   c                   @   s   e Zd Zdd„ ZdS )ÚTestContextAwareCompletionc                 C   sh   |   tdddƒd¡ |   tdddƒd¡ |   tdddƒd¡ |   tdddƒd¡ |   tdddƒd¡ d S )	Nzarg1=z	func1(a=)é   Zarg1zfunc1(a)zfunc1(az%magicZfunc2)r	   r   r!   r   r   r   Ú,test_adjust_completion_text_based_on_contextR   s    ÿÿÿÿÿzGTestContextAwareCompletion.test_adjust_completion_text_based_on_contextN)r   r   r   r(   r   r   r   r   r&   Q   s   r&   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Úmock_input_helperzZMachinery for tests of the main interact loop.

    Used by the mock_input decorator.
    c                 C   s   || _ d | _tƒ | _d S ©N)ÚtestgenÚ	exceptionr   r   )r   r+   r   r   r   Ú__init__o   s    zmock_input_helper.__init__c                 C   s   | j j| _| j| j _| S r*   )r   Úprompt_for_codeÚorig_prompt_for_codeÚ
fake_inputr!   r   r   r   Ú	__enter__t   s    

zmock_input_helper.__enter__c                 C   s   | j | j_d S r*   )r/   r   r.   )r   ÚetypeÚvalueÚtbr   r   r   Ú__exit__y   s    zmock_input_helper.__exit__c                 C   sL   zt | jƒW S  ty(   d| j_Y dS    t ¡ | _d| j_Y dS 0 d S )NFr   )Únextr+   ÚStopIterationr   Zkeep_runningÚsysÚexc_infor,   r!   r   r   r   r0   |   s    
zmock_input_helper.fake_inputN)r   r   r   Ú__doc__r-   r1   r5   r0   r   r   r   r   r)   j   s
   r)   c                    s   ‡ fdd„}|S )z·Decorator for tests of the main interact loop.

    Write the test as a generator, yield-ing the input strings, which IPython
    will see as if they were typed in at the prompt.
    c                    sr   ˆ | ƒ}t |ƒ�}|j ¡  W d   ƒ n1 s00    Y  |jd urn|j\}}}dd l}|j|tjd� ~|‚d S )Nr   )Úfile)r)   r   Zinteractr,   Ú	tracebackÚprint_tbr8   Ústdout)r   r+   Zmihr2   r3   r4   r<   ©Útestfuncr   r   Útest_method�   s    
(
zmock_input.<locals>.test_methodr   )r@   rA   r   r?   r   Ú
mock_input‡   s    rB   c                   @   s(   e Zd Zdd„ Zedd„ ƒZdd„ ZdS )ÚInteractiveShellTestCasec                    s   ‡ fdd„t |d ddƒD ƒS )z-Get last n readline history entries as a listc                    s   g | ]}ˆ   ˆ  ¡ | ¡‘qS r   )Zget_history_itemÚget_current_history_length)Ú.0Úx©Úrlr   r   Ú
<listcomp>¡   s   ÿz<InteractiveShellTestCase.rl_hist_entries.<locals>.<listcomp>é   éÿÿÿÿ)Úrange)r   rH   Únr   rG   r   Úrl_hist_entriesŸ   s    
ÿz(InteractiveShellTestCase.rl_hist_entriesc              	   c   sÒ   t ƒ }|j t¡ z¬tjddd�� dV  W d   ƒ n1 s>0    Y  tjddd�� dV  W d   ƒ n1 sr0    Y  tjddd�� dV  W d   ƒ n1 s¦0    Y  W |j t¡ n|j t¡ 0 d S )	NÚ4F)Úsuppressz
print(2*2)zSyntaxError: input containszprint(2345) # syntaxerrorZ16z
print(4*4))r   Zinput_transformers_postÚappendÚsyntax_error_transformerÚttZAssertPrintsÚremover
   r   r   r   Ú!test_inputtransformer_syntaxerror¤   s    $$&z:InteractiveShellTestCase.test_inputtransformer_syntaxerrorc           	   	      sø   t ƒ }|j}|jdgksJ ‚|jjs(J ‚G dd„ dtƒ}|ƒ }| |¡\}}|  |dt|ƒi¡ G dd„ d|ƒ}d‰ ‡ fdd„}|jj 	d	¡ d
|jj
d	 _||jd	< z(|ƒ }t|ƒ W d|jj
d	 _|jd	= nd|jj
d	 _|jd	= 0 ˆ d
ksôJ ‚d S )Nz
text/plainc                   @   s   e Zd Zdd„ Zdd„ ZdS )z?InteractiveShellTestCase.test_repl_not_plain_text.<locals>.Testc                 S   s   dt | ƒ S )Nz	<Test %i>)Úidr!   r   r   r   Ú__repr__Á   s    zHInteractiveShellTestCase.test_repl_not_plain_text.<locals>.Test.__repr__c                 S   s   dS )Nz<html>r   r!   r   r   r   Ú_repr_html_Ä   s    zKInteractiveShellTestCase.test_repl_not_plain_text.<locals>.Test._repr_html_N)r   r   r   rW   rX   r   r   r   r   ÚTestÀ   s   rY   c                   @   s   e Zd Zdd„ ZdS )z@InteractiveShellTestCase.test_repl_not_plain_text.<locals>.Test2c                 S   s    ddl m}m} ||dƒƒ d S )Nr   )ÚdisplayÚHTMLz<custom>)ÚIPython.displayrZ   r[   )r   rZ   r[   r   r   r   Ú_ipython_display_Í   s    zRInteractiveShellTestCase.test_repl_not_plain_text.<locals>.Test2._ipython_display_N)r   r   r   r]   r   r   r   r   ÚTest2Ì   s   r^   Fc                    s   t dƒ d‰ d S )NzHandler calledT)Úprint)ÚdataÚmetadata©Úcalledr   r   ÚhandlerÕ   s    zBInteractiveShellTestCase.test_repl_not_plain_text.<locals>.handlerz	text/htmlT)r   Údisplay_formatterZactive_typesZipython_display_formatterÚenabledÚobjectÚformatr	   ÚreprrQ   Ú
formattersZmime_renderersrZ   )	r   r   Ú	formatterrY   Úobjr`   Ú_r^   rd   r   rb   r   Útest_repl_not_plain_text·   s,    


ÿ
z1InteractiveShellTestCase.test_repl_not_plain_textN)r   r   r   rN   rB   rU   rn   r   r   r   r   rC   ž   s   
rC   c                 C   s<   | D ]2}|  d¡}|dkrtdƒ}||_|d |_|‚q| S )zDTransformer that throws SyntaxError if 'syntaxerror' is in the code.Zsyntaxerrorr   zinput contains "syntaxerror"rJ   )ÚfindÚSyntaxErrorÚtextÚoffset)ÚlinesÚlineÚposÚer   r   r   rR   ç   s    

rR   c                   @   s   e Zd Zdd„ ZdS )ÚTerminalMagicsTestCasec                 C   s>   t ƒ }d}|jjd }|j|dd� |  |jd dƒd¡ dS )z=Test that code with a blank line doesn't get split (gh-3246).z-def pasted_func(a):
    b = a+1

    return bZTerminalMagicsN)ÚnameZpasted_funcé6   é7   )r   Zmagics_managerÚregistryZstore_or_executer	   Zuser_ns)r   r   ÚsÚtmr   r   r   Útest_paste_magics_blanklineô   s
    z2TerminalMagicsTestCase.test_paste_magics_blanklineN)r   r   r   r~   r   r   r   r   rw   ó   s   rw   )r:   r8   Zunittestr   Zprompt_toolkit.auto_suggestr   ZIPython.testingr   rS   ZIPython.terminal.ptutilsr   r   Z'IPython.terminal.shortcuts.auto_suggestr   ZTestCaser   r   r&   rg   r)   rB   rC   rR   rw   r   r   r   r   Ú<module>   s   3I