o
    ZhT                      @   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                   @      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   b/var/www/html/lang_env/lib/python3.10/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          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      |  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   r%   )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                   @   r   )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   r   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-   )r2   r   r1   )r   etypevaluetbr   r   r   __exit__y   s   zmock_input_helper.__exit__c                 C   sF   zt | jW S  ty   d| j_Y dS    t | _d| j_Y dS )NFr   )nextr.   StopIterationr   Zkeep_runningsysexc_infor/   r#   r   r   r   r3   |   s   
zmock_input_helper.fake_inputN)r   r   r   __doc__r0   r4   r8   r3   r   r   r   r   r,   j   s    r,   c                    s    fdd}|S )zDecorator 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 sw   Y  |jd ur7|j\}}}dd l}|j|tjd ~|d S )Nr   )file)r,   r   Zinteractr/   	tracebackprint_tbr;   stdout)r   r.   Zmihr5   r6   r7   r?   testfuncr   r   test_method   s   

zmock_input.<locals>.test_methodr   )rC   rD   r   rB   r   
mock_input   s   rE   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   rK   nr   rJ   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 w   Y  tjddd dV  W d    n1 s:w   Y  tjddd dV  W d    n1 sTw   Y  W |jt d S W |jt d S |jt w )	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	= w  d
kszJ 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   rZ   r[   r   r   r   r   Test   s    r\   c                   @   r   )z@InteractiveShellTestCase.test_repl_not_plain_text.<locals>.Test2c                 S   s    ddl m}m} ||d d S )Nr   )displayHTMLz<custom>)IPython.displayr]   r^   )r   r]   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   r   ra   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
   reprrT   
formattersZmime_renderersr]   )	r   r   	formatterr\   objrc   _ra   rg   r   re   r   test_repl_not_plain_text   s,   



z1InteractiveShellTestCase.test_repl_not_plain_textN)r   r   r   rQ   rE   rX   rq   r   r   r   r   rF      s
    
rF   c                 C   s<   | D ]}| 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"rM   )findSyntaxErrortextoffset)lineslineposer   r   r   rU      s   

rU   c                   @   r   )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_func6   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   rz      r   rz   )r=   r;   Zunittestr   Zprompt_toolkit.auto_suggestr   ZIPython.testingr   rV   ZIPython.terminal.ptutilsr   r   Z'IPython.terminal.shortcuts.auto_suggestr   ZTestCaser   r   r)   rj   r,   rE   rF   rU   rz   r   r   r   r   <module>   s    3I