U
    -e8                     @   s  d dl Z d dlmZmZmZmZmZmZmZm	Z	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 d dlmZ d d	lmZmZ d
d Zdd Ze jdddgdd Ze jdddgdd Z e jddddgdd Z!e jdddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5gd6d7 Z"e jdd8d9d:d!gd;d< Z#e jdddd=d!d>gd?d@ Z$e jdAdBdCgdDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+e j,dRdS Z-dTdU Z.dVdW Z/dXdY Z0dZd[ Z1d\d] Z2d^d_ Z3d`da Z4dbdc Z5ddde Z6dS )f    N)
acceptaccept_or_jump_to_endaccept_tokenaccept_characteraccept_wordaccept_and_keep_cursordiscardNavigableAutoSuggestFromHistoryswap_autosuggestion_upswap_autosuggestion_down)	skip_over)create_ipython_shortcuts)InMemoryHistory)Buffer)Document)AutoSuggestFromHistory)patchMockc               	   C   s<   dd l m  m  m}  tjtdd | j W 5 Q R X d S )Nr   z8\.12.+accept_or_jump_to_endmatch)'IPython.terminal.shortcuts.auto_suggestZterminal	shortcutsauto_suggestpytestZwarnsDeprecationWarningZaccept_in_vi_insert_mode)Ziptsa r   f/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/IPython/terminal/tests/test_shortcuts.pytest_deprected   s    r   c                 C   sF   t  }t  |_t  |j_| |j_||j_||jj_t| |d|j_|S )N)textcursor_position)r   current_buffer
suggestionr   r   r   document)r   cursorr!   eventr   r   r   
make_event    s    

r%   ztext, suggestion, expected) def out(tag: str, n=50):r'   )def out(tag: str, n=50):r)   c                 C   sL   t | t| |}|j}t |_t| |jjs2t|jjd |fksHtd S Nr   )	r%   lenr    r   insert_textr   calledAssertionError	call_args)r   r!   expectedr$   bufferr   r   r   test_accept+   s    r2   ztext, suggestion)r&   r'   )r(   r)   c                 C   sD   t | t| |}|j}t |_t| |jjr2t|jd ks@td S N)	r%   r+   r    r   r,   r   r-   r.   r!   )r   r!   r$   r1   r   r   r   test_discard;   s    r4   z text, cursor, suggestion, called)123456   	123456789T)r5      r7   F)z123456   
789r6   r7   Tc                 C   s@   t | ||}t |j_t| |r0|jj  n|jj  dS )z?
    test that autosuggest is only applied at end of line.
    N)r%   r   r    r,   r   Zassert_calledZassert_not_called)r   r#   r!   r-   r$   r   r   r   test_autosuggest_at_EOLK   s    
r9   )r&   r'   r(   )def out(tag: str, n=50):zef )de f out(tag: str, n=50):f )defz out(tag: str, n=50): )r(   r)   zout()zdef ozut(tag: str, n=50):zut()zdef ouzt(tag: str, n=50):zt()zdef outz(tag: str, n=50):()zdef out(ztag: str, n=50):ztag: )z	def out(tzag: str, n=50):zag: )z
def out(tazg: str, n=50):zg: )zdef out(tagz: str, n=50):z: )zdef out(tag:z str, n=50):r@   )zdef out(tag: zstr, n=50):zstr, )zdef out(tag: sz
tr, n=50):ztr, )zdef out(tag: stz	r, n=50):zr, )zdef out(tag: strz, n=50):z, n)zdef out(tag: str,z n=50):z n)zdef out(tag: str, zn=50):zn=)zdef out(tag: str, nz=50):=)zdef out(tag: str, n=z50):z50))zdef out(tag: str, n=5z0):z0))zdef out(tag: str, n=50):rC   )zdef out(tag: str, n=50):rD   c                 C   sL   t | t| |}t |j_t| |jjjs0t|jjjd |fksHtd S r*   )	r%   r+   r   r    r,   r   r-   r.   r/   r   r!   r0   r$   r   r   r   test_autosuggest_tokenb   s
    
rF   )r&   r'   r:   )r:   r;   e)r<   r=   fc                 C   sL   t | t| |}t |j_t| |jjjs0t|jjjd |fksHtd S r*   )	r%   r+   r   r    r,   r   r-   r.   r/   rE   r   r   r   test_accept_character   s
    

rI   )der=   r>   )r(   r)   z	out(tag: c                 C   sL   t | t| |}t |j_t| |jjjs0t|jjjd |fksHtd S r*   )	r%   r+   r   r    r,   r   r-   r.   r/   rE   r   r   r   test_accept_word   s
    
rK   z"text, suggestion, expected, cursor)r&   r'   r'   r   )r(   r)   r)      c                 C   sV   t | ||}|j}t |_t| |jjs.t|jjd |fksDt|j|ksRtd S r*   )	r%   r    r   r,   r   r-   r.   r/   r   )r   r!   r0   r#   r$   r1   r   r   r   test_accept_and_keep_cursor   s    rM   c               	   C   sV   d} t | t| d}t |j_td&}t| |jjjr>t|jsHtW 5 Q R X d S )Nr'   r&   z?prompt_toolkit.key_binding.bindings.named_commands.forward_word)	r%   r+   r   r    r,   r   r   r-   r.   )fullr$   Zforward_wordr   r   r   test_autosuggest_token_empty   s    
rO   c                  C   sD   t  } t }| |_t }t |_t|dks0tt|dks@tdS )zHEnsure that swapping autosuggestions does not break with other providersN)	r   get_ipythonr   r   r   r    r
   r.   r   )provideripr$   r   r   r   test_other_providers   s    rS   c                     s4  t  tddddgd} t| d t }|_|  2 z3 d H W }q66 d _d _t}t	}t
 } |_ fdd	}| jd
kst|| | jdkst|| | jdkst|| | jd
kst|| | jdkst|| | jdkst|| | jd
kst|| | jdks0td S )NZvery_averyZvery_bvery_cZhistory_stringshistory   c                     s      j} |  _| S r3   get_suggestionr"   r!   r!   r1   rQ   r   r   r[      s    z/test_navigable_provider.<locals>.get_suggestion_c_b_ar	   r   r   rP   r   loadr   r   r
   r   r   r    r.   rX   rR   _upZdownr$   r[   r   r]   r   test_navigable_provider   s:    
rf   c                     s   t  tddgd} t| d t }|_|  2 z3 d H W }q26 d _d _t}t	}t
 } |_ fdd}| jd	kst|| | jd
kst|| | jdkst|| | jd
kst|| | jd	kstd S )Nzvery_a
very_brU   rV   rW   rY   rT   c                     s      j} |  _| S r3   rZ   r\   r]   r   r   r[     s    zAtest_navigable_provider_multiline_entries.<locals>.get_suggestionr^   r_   r`   ra   rc   r   r]   r   )test_navigable_provider_multiline_entries  s.    
rg   c                  C   s   t  } t | _| S r3   )r   r   default_buffer)sessionr   r   r   create_session_mock.  s    rj   c                  C   s   t  } d| _t }| | | jdks*t|jj  | jdksDtt }| | d| _| jdksht|jj  | jdkstd| _|   |jj  |jj  | jdkstd S )N   r      r8   )	r	   Z
skip_linesrj   connectr.   rh   Zon_text_insertfireZ
disconnect)rQ   Z	session_1Z	session_2r   r   r   "test_navigable_provider_connection4  s$    

ro   c                  c   s2   t  } t | _t| | j_z
| V  W 5 d | _X d S r3   )rP   r   pt_appr   key_bindings)rR   r   r   r   ipython_with_promptN  s    
rr   c                    s   t  } fdd|jjjD S )Nc                    s   g | ]}|j  kr|qS r   )handler).0Zbindingcommandr   r   
<listcomp>[  s   
z,find_bindings_by_command.<locals>.<listcomp>)rP   rp   rq   Zbindings)rv   rR   r   ru   r   find_bindings_by_commandY  s    
rx   c                 C   s  t t}t|dkstddddgdg| _t t}t|dksDtt|d jdddgks`tt|d jt|d jkst|d j|d jkstddd	g| _t t}t|dkstt|d jdddgkstt|d jt|d jkst|d j|d jkstd S )
Nrk   !IPython:auto_suggest.accept_tokenabcrv   new_keysr   always)rv   Z
new_filter)rx   r   r+   r.   r   listkeysfilter)rr   originalmatchedr   r   r   test_modify_unique_shortcutb  s       r   c                 C   s`   t t}t|dkstdg dg| _t t}t|dks>tg | _t t}t|dks\td S )Nrk   ry   r}   r   )rx   r   r+   r.   r   rr   r   r   r   r   test_disable_shortcuty  s    r   c              	   C   s   t t}dd |D }|dddddhks,ttjtdd	 d
dgdg| _W 5 Q R X d
dgddg| _t t}dd |D }|dddddhkstd S )Nc                 S   s   h | ]}|j d  qS r   r   rt   mr   r   r   	<setcomp>  s     z4test_modify_shortcut_with_filters.<locals>.<setcomp>)]}'"zMultiple shortcuts matchingr   IPython:auto_match.skip_overxr}   z6focused_insert & auto_match & followed_by_single_quote)rv   r~   Zmatch_filterc                 S   s   h | ]}|j d  qS r   r   r   r   r   r   r     s     )rx   r   r.   r   raises
ValueErrorr   )rr   r   Zmatched_keysr   r   r   !test_modify_shortcut_with_filters  s    
r   c                   C   s   d S r3   r   r   r   r   r   example_command  s    r   c              	   C   s^   t t}t|dksttjtdd ddgdg| _W 5 Q R X t t}t|dksZtd S )Nr   zexample_command is not a knownr   r   r   r}   )rx   r   r+   r.   r   r   r   r   r   r   r   r   !test_add_shortcut_for_new_command  s    
r   c              	   C   s2   t jtdd ddgdgdg| _W 5 Q R X d S )NzNo shortcuts matchingr   r   r   y)rv   Z
match_keysr~   )r   r   r   r   )rr   r   r   r   test_modify_shortcut_failure  s    r   c              	   C   s   t t}t|dksttjtdd dg ddg| _W 5 Q R X ddgddg| _t t}t|dksltg | _t t}t|dkstd S )	NrY   z"Cannot add a shortcut without keysr   r   Trv   r~   creater   r6   )rx   r   r+   r.   r   r   r   r   r   r   r   r   &test_add_shortcut_for_existing_command  s    
r   c                  C   s<   t  } | jd kstddgddg}|| _| j|ks8td S )Nr   r   Tr   )rP   rp   r.   r   )ipythonr   r   r   r   )test_setting_shortcuts_before_pt_app_init  s    r   )7r   r   r   r   r   r   r   r   r   r	   r
   r   Z%IPython.terminal.shortcuts.auto_matchr   ZIPython.terminal.shortcutsr   Zprompt_toolkit.historyr   Zprompt_toolkit.bufferr   Zprompt_toolkit.documentr   Zprompt_toolkit.auto_suggestr   Zunittest.mockr   r   r   r%   markZparametrizer2   r4   r9   rF   rI   rK   rM   rO   rS   rf   rg   rj   ro   Zfixturerr   rx   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   0
	
	

	



4&

	