a
    åzetN  ã                   @   sü   d Z ddlmZ ddlmZmZ ddl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l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 ejZdd„ ZG dd„ dejƒZ G dd„ dejƒZ!G dd„ dejƒZ"e#dkrøej$dd dS )zTest squeezer, coverage 95%é    )Údedent)ÚTextÚTkN)ÚMockÚNonCallableMagicMockÚpatchÚsentinelÚANY)Úrequires)ÚidleConf)Úcount_lines_with_wrappingÚExpandingButtonÚSqueezer)Úmacosx)Ú	view_text)ÚHovertip)ÚPyShellc                    s0   t dƒ tƒ ‰ ˆ  ¡  ‡ fdd„}|  |¡ ˆ S )z*Helper for tests: Create a root Tk object.Zguic                      s   ˆ   ¡  ˆ  ¡  d S ©N)Zupdate_idletasksZdestroy© ©Úrootr   ú5/usr/lib/python3.9/idlelib/idle_test/test_squeezer.pyÚcleanup_root   s    z&get_test_tk_root.<locals>.cleanup_root)r
   r   ZwithdrawÚ
addCleanup)Ztest_instancer   r   r   r   Úget_test_tk_root   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S )ÚCountLinesTestz1Tests for the count_lines_with_wrapping function.c                 C   s   |   |t||ƒ¡S r   ©ÚassertEqualr   )ÚselfÚexpectedÚtextÚ	linewidthr   r   r   Úcheck%   s    þzCountLinesTest.checkc                 C   s   |   tdƒd¡ dS )zTest with an empty string.Ú r   Nr   ©r   r   r   r   Útest_count_empty+   s    zCountLinesTest.test_count_emptyc                 C   s   |   tdƒd¡ dS )z/Test with a string which begins with a newline.z
texté   Nr   r$   r   r   r   Ú!test_count_begins_with_empty_line/   s    z0CountLinesTest.test_count_begins_with_empty_linec                 C   s   |   tdƒd¡ dS )z-Test with a string which ends with a newline.ztext
é   Nr   r$   r   r   r   Útest_count_ends_with_empty_line3   s    z.CountLinesTest.test_count_ends_with_empty_linec                 C   s   |   tdƒd¡ dS )z Test with several lines of text.z1
2
3
é   Nr   r$   r   r   r   Útest_count_several_lines7   s    z'CountLinesTest.test_count_several_linesc                 C   s4   | j dddd | j dddd | j dddd d S )	Nr(   Ú
éP   ©r   r    r!   r&   ú

é
   z










©r"   r$   r   r   r   Útest_empty_lines;   s    zCountLinesTest.test_empty_linesc                 C   s$   | j dddd | j dddd d S )Nr*   ZÈaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar-   r.   zÉaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
r1   r$   r   r   r   Útest_long_line@   s    zCountLinesTest.test_long_linec                 C   s`   t dƒ}| jd|dd | jd|d dd | jd|dd | jd|d	d | jd
|dd d S )Nz€            13 characters
            43 is the number of characters on this line

            7 chars
            13 charactersé   r-   r.   r,   é   é(   é   é   é   r0   )r   r"   )r   r    r   r   r   Ú$test_several_lines_different_lengthsD   s    z3CountLinesTest.test_several_lines_different_lengthsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r%   r'   r)   r+   r2   r3   r:   r   r   r   r   r   #   s   r   c                   @   sŽ   e Zd ZdZd"dd„Zd#dd„Zd$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 d!„ ZdS )%ÚSqueezerTestzTests for the Squeezer class.Fc                 C   s:   t ƒ }d|_|r6t| ƒ|_| j|jd}| |_|j_|S )z$Create a mock EditorWindow instance.r-   r   )r   Úwidthr   r   Úmake_text_widgetr    ZperZbottom)r   Úwith_text_widgetÚeditwinÚtext_widgetr   r   r   Úmake_mock_editor_windowT   s    
z$SqueezerTest.make_mock_editor_windowNc                 C   s   |du r|   ¡ }t|ƒ}|S )z<Create an actual Squeezer instance with a mock EditorWindow.N)rE   r   )r   Zeditor_windowÚsqueezerr   r   r   Úmake_squeezer_instance`   s    z#SqueezerTest.make_squeezer_instancec                 C   s0   |d u rt | ƒ}t|ƒ}d|d< | dd¡ |S )N)ZCourierr0   ZfontZiomarkú1.0)r   r   Úmark_set)r   r   rD   r   r   r   rA   g   s    zSqueezerTest.make_text_widgetc                 C   s6   t  |||¡}t  ||||¡ |  t j||||¡ d S r   )r   Z	GetOptionZ	SetOptionr   )r   Z
configTypeÚsectionÚoptionÚvalueZprev_valr   r   r   Ú set_idleconf_option_with_cleanupo   s
    ÿz-SqueezerTest.set_idleconf_option_with_cleanupc              
   C   sž   |   ¡ }|  |¡}dD ]‚\}}}| j|||dX t|ƒ}t |d|¡" |  | |¡|¡ W d  ƒ n1 sp0    Y  W d  ƒ q1 sŽ0    Y  qdS )z0Test Squeezer.count_lines() with various inputs.))z'\n'r-   r(   )z'\n' * 3r-   r*   )z'a' * 40 + '\n'r-   r(   )z'a' * 80 + '\n'r-   r(   )z'a' * 200 + '\n'r-   r*   )ú'aa\t' * 20r-   r&   )z'aa\t' * 21r-   r*   )rN   r6   é   )Ú	text_codeÚ
line_widthr   r@   N)rE   rG   ZsubTestÚevalr   Úobjectr   Úcount_lines)r   rC   rF   rP   rQ   r   r    r   r   r   Útest_count_linesu   s    

þzSqueezerTest.test_count_linesc                 C   s2   |   ¡ }|  |¡}|  |j|¡ |  |jg ¡ dS )z(Test the creation of Squeezer instances.N)rE   rG   ZassertIsrC   r   Úexpandingbuttons)r   rC   rF   r   r   r   Ú	test_init‹   s    
zSqueezerTest.test_initc                 C   s~   |   ¡ }dddd dfD ]`}ttd |_}|  |¡}|  |j |d¡t¡ |  |jd¡ | |d¡ |  t	|j
ƒd	¡ qd
S )ú@Test Squeezer's overriding of the EditorWindow's write() method.r#   ÚTEXTú	LONG TEXTéè  áL  MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
MANY_LINES
©Úreturn_valuer   r(   r   N)rE   r   ÚSENTINEL_VALUEÚwriterG   r   rC   Ú
call_countÚassert_called_withÚlenrV   ©r   rC   r    Ú
orig_writerF   r   r   r   Útest_write_no_tags’   s    
zSqueezerTest.test_write_no_tagsc                 C   s|   dddd dfD ]f}|   ¡ }t|j_|j}|  |¡}|  |j |d¡t¡ |  |jd¡ | |d¡ |  t	|j
ƒd¡ qd	S )
rX   r#   rY   rZ   r[   r\   Ústderrr(   r   N)rE   r_   r`   r^   rG   r   rC   ra   rb   rc   rV   )r   r    rC   re   rF   r   r   r   Útest_write_not_stdoutž   s    
ÿz"SqueezerTest.test_write_not_stdoutc                 C   sà   |   ¡ }dD ]f}ttd |_}|  |¡}d|_|  |j |d¡t¡ |  |jd¡ | 	|d¡ |  t
|jƒd¡ qdd d	fD ]Z}ttd |_}|  |¡}d|_|  |j |d¡d
¡ |  |jd¡ |  t
|jƒd¡ q€d
S )rX   )r#   rY   r]   é2   Ústdoutr(   r   rZ   r[   r\   N)rE   r   r_   r`   rG   Úauto_squeeze_min_linesr   rC   ra   rb   rc   rV   rd   r   r   r   Útest_write_stdout¬   s$    
ÿ
zSqueezerTest.test_write_stdoutc                 C   sd   | j dd}|j}|  |¡}d|_tdd|_| dd¡ |  | dd	¡d
¡ |  t	|j
ƒd¡ dS )zATest that the auto-squeezing creates an ExpandingButton properly.T©rB   r4   r5   r]   zTEXT
TEXT
TEXT
TEXT
TEXT
TEXT
rj   rH   Úendr,   r(   N)rE   r    rG   rk   r   rT   r`   r   Úgetrc   rV   )r   rC   rD   rF   r   r   r   Útest_auto_squeezeÄ   s    
zSqueezerTest.test_auto_squeezec                 C   s  dD ]ú}| j dd}|j}|  |¡}tdd|_| dd|¡ | dd¡ |  | dd	¡d
¡ |  t	|j
ƒd¡ |jtƒ d}|  |d¡ |  | dd	¡d¡ |  t	|j
ƒd¡ |  |j
d jd¡ |j
d jtƒ d |  | dd	¡d
¡ |  t	|j
ƒd¡ qdS )z$Test the squeeze_current_text event.)rj   rg   Trm   r5   r]   rH   ú
SOME
TEXT
Úinsertrn   úSOME
TEXT

r   ©ÚeventÚbreakr/   r(   z	SOME
TEXTN)rE   r    rG   r   rT   rr   rI   r   ro   rc   rV   Úsqueeze_current_text_eventÚsÚexpand)r   Ztag_namerC   rD   rF   Úretvalr   r   r   Útest_squeeze_current_text_eventÐ   s"    
z,SqueezerTest.test_squeeze_current_text_eventc                 C   s¬   | j dd}|j}|  |¡}tdd|_| ddd¡ | dd¡ |  | dd	¡d
¡ |  t	|j
ƒd¡ |jtƒ d}|  |d¡ |  | dd	¡d
¡ |  t	|j
ƒd¡ dS )z@Test that the event doesn't squeeze text without a relevant tag.Trm   r5   r]   rH   rq   ZTAGrr   rn   rs   r   rt   rv   N)rE   r    rG   r   rT   rr   rI   r   ro   rc   rV   rw   ©r   rC   rD   rF   rz   r   r   r   Ú/test_squeeze_current_text_event_no_allowed_tagsí   s    
z<SqueezerTest.test_squeeze_current_text_event_no_allowed_tagsc                 C   sà   | j dd}|j}|  |¡}tdd|_| ddd¡ | dd¡ |jtƒ d	 |  t	|j
ƒd
¡ | ddd¡ | dd¡ |jtƒ d	}|  |d¡ |  | dd¡d¡ |  t	|j
ƒd¡ |  | |j
d d|j
d
 ¡¡ dS )z2Test squeezing text before existing squeezed text.Trm   r5   r]   rH   rq   rj   rr   rt   r(   zMORE
STUFF
rv   rn   z


r&   r   ú<N)rE   r    rG   r   rT   rr   rI   rw   r   rc   rV   ro   Ú
assertTrueZcomparer|   r   r   r   Ú/test_squeeze_text_before_existing_squeezed_text  s&    
ýz<SqueezerTest.test_squeeze_text_before_existing_squeezed_textc                 C   sR   | j dd}|  |¡}|j}|d }|  dddt|ƒ¡ t ¡  |  |j|¡ dS )zTest the reload() class-method.Trm   r0   Úmainr   zauto-squeeze-min-linesN)rE   rG   rk   rM   Ústrr   Úreloadr   )r   rC   rF   Zorig_auto_squeeze_min_linesZnew_auto_squeeze_min_linesr   r   r   Útest_reload  s    
þÿzSqueezerTest.test_reloadc                 C   s   t  ¡  dS )z@Test that Squeezer.reload() runs without any instances existing.N)r   rƒ   r$   r   r   r   Ú!test_reload_no_squeezer_instances,  s    z.SqueezerTest.test_reload_no_squeezer_instances)F)N)N)r;   r<   r=   r>   rE   rG   rA   rM   rU   rW   rf   rh   rl   rp   r{   r}   r€   r„   r…   r   r   r   r   r?   R   s    


r?   c                   @   sT   e Zd ZdZdd„ Zededdd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚExpandingButtonTestz$Tests for the ExpandingButton class.c                 C   s$   t | ƒ}tƒ }t|ƒ|j_d|_|S )z0Helper for tests: Create a mock Squeezer object.ri   )r   r   r   rC   r    rk   )r   r   rF   r   r   r   Úmake_mock_squeezer5  s
    z&ExpandingButtonTest.make_mock_squeezerzidlelib.squeezer.Hovertip©Zautospecc                 C   sÖ   |   ¡ }|jj}tddd|ƒ}|  |jd¡ |  |j|¡ |  d| d¡v ¡ |  | 	dd¡d¡ |  
d	| ¡ ¡ d
t ¡ r€dnd }|  
|| ¡ ¡ |  |jd¡ |j|ttd |jd d }|  
d| ¡ ¡ dS )z1Test the simplest creation of an ExpandingButton.rY   ÚTAGSri   z50 linesr    rH   rn   r,   z<Double-Button-1>z<Button-%s>Ú2Ú3r(   )Zhover_delayr   zright-clickN)r‡   rC   r    r   r   rx   Zmasterr   Zcgetro   ZassertInÚbindr   ZisAquaTkra   rb   r	   Ú	call_argsÚlower)r   ZMockHovertiprF   rD   ÚexpandingbuttonZright_button_codeZtooltip_textr   r   r   rW   ?  s    zExpandingButtonTest.test_initc                 C   sº   |   ¡ }tddd|ƒ}|j}|jd|d |j|_|jtƒ d}|  |d¡ |  | dd¡d	¡ | 	d
¡}|  | d|¡d¡ |  | 
dd¡d|f¡ |  |jjjd¡ |jj |¡ dS )zTest the expand event.rY   r‰   ri   rH   ©Zwindowrt   Nrn   zTEXT
úend-1cr(   )r‡   r   r    Úwindow_createÚ	base_textry   r   r   ro   ÚindexZtag_nextrangerV   Úremovera   rb   )r   rF   r   rD   rz   Ztext_end_indexr   r   r   Útest_expand\  s    
ÿzExpandingButtonTest.test_expandc                 C   s"  |   ¡ }dd }t|dd|ƒ}| ¡  |  |j¡ |j}|jd|d |j|_tdƒ.}d|j	_
d|j_
|jtƒ d	}W d
  ƒ n1 sŠ0    Y  |  |d¡ |  |j dd¡d¡ tdƒ.}d|j	_
d|j_
|jtƒ d	}W d
  ƒ n1 sò0    Y  |  |d
¡ |  |j dd¡|¡ d
S )z@Test that expanding very long output asks user for confirmation.Úai † r‰   ri   rH   r   zidlelib.squeezer.messageboxFrt   Nrv   r‘   r#   T)r‡   r   Zset_is_dangerousr   Zis_dangerousr    r’   r“   r   Zaskokcancelr^   Zaskyesnory   r   r   ro   )r   rF   r    r   rD   Zmock_msgboxrz   r   r   r   Útest_expand_dangerous_oupput|  s(    
,
,z0ExpandingButtonTest.test_expand_dangerous_oupputc                 C   sp   |   ¡ }tddd|ƒ}tƒ |_tƒ |_|jtƒ d}|  |d¡ |  |jjd¡ |  |jjd¡ |j d¡ dS )zTest the copy event.rY   r‰   ri   rt   Nr(   )	r‡   r   r   Zclipboard_clearZclipboard_appendÚcopyr   ra   rb   )r   rF   r   rz   r   r   r   Ú	test_copy¤  s    zExpandingButtonTest.test_copyc                 C   s€   |   ¡ }tddd|ƒ}tƒ |_tdtdB}|jtƒ d |  |jd¡ |  |j	d d	 d¡ W d
  ƒ n1 sr0    Y  d
S )zTest the view event.rY   r‰   ri   zidlelib.squeezer.view_textrˆ   rt   r(   r   r&   N)
r‡   r   r   Zselection_ownr   r   Úviewr   ra   r   )r   rF   r   Zmock_view_textr   r   r   Ú	test_view¸  s    zExpandingButtonTest.test_viewc                 C   s¢   |   ¡ }tddd|ƒ}tdƒp}tƒ }||_tƒ }d|_d|_|j|d |  |j	j
t|jƒ¡ |jD ]^}}|j	j|td qfW d  ƒ n1 s”0    Y  dS )	zTest the context menu.rY   r‰   ri   ztkinter.Menur0   rt   )ÚlabelZcommandN)r‡   r   r   r   r^   ÚxÚyZcontext_menu_eventr   Zadd_commandra   rc   Zrmenu_specsZassert_any_callr	   )r   rF   r   Z	mock_MenuZ	mock_menuZ
mock_eventr   Údatar   r   r   Ú
test_rmenuÉ  s    

ÿzExpandingButtonTest.test_rmenuN)r;   r<   r=   r>   r‡   r   r   rW   r–   r˜   rš   rœ   r¡   r   r   r   r   r†   1  s   


 (r†   Ú__main__r&   )Ú	verbosity)%r>   Útextwrapr   Útkinterr   r   ZunittestZunittest.mockr   r   r   r   r	   Ztest.supportr
   Zidlelib.configr   Zidlelib.squeezerr   r   r   Zidlelibr   Zidlelib.textviewr   Zidlelib.tooltipr   Zidlelib.pyshellr   r_   r   ZTestCaser   r?   r†   r;   r   r   r   r   r   Ú<module>   s(   / ` *