
    ;eL                     ^   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 dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ ej        Zd Z G d dej                  Z  G d dej                  Z! G d dej                  Z"e#dk    r ej$        d           dS dS )zTest squeezer, coverage 95%    )dedent)TextTkN)MockNonCallableMagicMockpatchsentinelANY)requires)idleConf)
Percolator)count_lines_with_wrappingExpandingButtonSqueezer)macosx)	view_text)Hovertipc                     t          d           t                                                       fd}|                     |           S )z*Helper for tests: Create a root Tk object.guic                  X                                                                         d S N)update_idletasksdestroyroots   6/usr/lib/python3.11/idlelib/idle_test/test_squeezer.pycleanup_rootz&get_test_tk_root.<locals>.cleanup_root   s&        )r   r   withdraw
addCleanup)test_instancer   r   s     @r   get_test_tk_rootr"      sX    UOOO44DMMOOO     \***Kr   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )CountLinesTestz1Tests for the count_lines_with_wrapping function.c                 J    |                      |t          ||                    S r   assertEqualr   )selfexpectedtext	linewidths       r   checkzCountLinesTest.check$   s*    %dI66
 
 	
r   c                 L    |                      t          d          d           dS )zTest with an empty string. r   Nr&   r(   s    r   test_count_emptyzCountLinesTest.test_count_empty*   s'    2266:::::r   c                 L    |                      t          d          d           dS )z/Test with a string which begins with a newline.z
text   Nr&   r/   s    r   !test_count_begins_with_empty_linez0CountLinesTest.test_count_begins_with_empty_line.   '    28<<a@@@@@r   c                 L    |                      t          d          d           dS )z-Test with a string which ends with a newline.ztext
   Nr&   r/   s    r   test_count_ends_with_empty_linez.CountLinesTest.test_count_ends_with_empty_line2   r4   r   c                 L    |                      t          d          d           dS )z Test with several lines of text.z1
2
3
   Nr&   r/   s    r   test_count_several_linesz'CountLinesTest.test_count_several_lines6   s'    2;??CCCCCr   c                     |                      ddd           |                      ddd           |                      ddd           d S )	Nr6   
P   r)   r*   r+   r2   


   z










r,   r/   s    r   test_empty_lineszCountLinesTest.test_empty_lines:   sP    

ADB
777

AFb
999

BY"
=====r   c                 f    |                      ddd           |                      ddd           d S )Nr9   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar=   r>   zaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
rA   r/   s    r   test_long_linezCountLinesTest.test_long_line?   s9    

AI
<<<

A$4
CCCCCr   c                    t          d          }|                     d|d           |                     d|dz   d           |                     d|d           |                     d|d	           |                     d
|d           d S )Nz            13 characters
            43 is the number of characters on this line

            7 chars
            13 characters   r=   r>   r<      (            r@   )r   r,   )r(   r*   s     r   $test_several_lines_different_lengthsz3CountLinesTest.test_several_lines_different_lengthsC   s        	

ADB
777

AD4K2
>>>

ADB
777

ADB
777

BTR
88888r   N)__name__
__module____qualname____doc__r,   r0   r3   r7   r:   rB   rE   rM    r   r   r$   r$   "   s        ;;
 
 
; ; ;A A AA A AD D D> > >
D D D9 9 9 9 9r   r$   c                   r    e Zd ZdZddZddZddZd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zd Zd Zd ZdS )SqueezerTestzTests for the Squeezer class.Fc                     t                      }d|_        |rBt          |           |_        |                     |j                  }|x|_        |j        _        |S )z$Create a mock EditorWindow instance.r=   r   )r   widthr"   r   make_text_widgetr*   perbottom)r(   with_text_widgeteditwintext_widgets       r   make_mock_editor_windowz$SqueezerTest.make_mock_editor_windowS   sY    &(( 	<+D11GL//W\/BBK0;;GL7;-r   Nc                 P    ||                                  }t          |          }|S )z<Create an actual Squeezer instance with a mock EditorWindow.)r]   r   )r(   editor_windowsqueezers      r   make_squeezer_instancez#SqueezerTest.make_squeezer_instance_   s+      88::MM**r   c                 |    |t          |           }t          |          }d|d<   |                    dd           |S )N)Courierr@   fontiomark1.0)r"   r   mark_set)r(   r   r\   s      r   rW   zSqueezerTest.make_text_widgetf   sE    <#D))D4jj-FXu---r   c                     t          j        |||          }t          j        ||||           |                     t           j        ||||           d S r   )r   	GetOption	SetOptionr    )r(   
configTypesectionoptionvalueprev_vals         r    set_idleconf_option_with_cleanupz-SqueezerTest.set_idleconf_option_with_cleanupn   s[    %j'6BB:w>>>*"GVX	? 	? 	? 	? 	?r   c                    |                                  }|                     |          }dD ]\  }}}|                     |||          5  t          |          }t	          j        |d|          5  |                     |                    |          |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   dS )z0Test Squeezer.count_lines() with various inputs.))z'\n'r=   r6   )z'\n' * 3r=   r9   )z'a' * 40 + '\n'r=   r6   )z'a' * 80 + '\n'r=   r6   )z'a' * 200 + '\n'r=   r9   )'aa\t' * 20r=   r2   )z'aa\t' * 21r=   r9   )rr   rI      )	text_code
line_widthr)   rV   N)r]   ra   subTestevalr   objectr'   count_lines)r(   r[   r`   rt   ru   r)   r*   s          r   test_count_lineszSqueezerTest.test_count_linest   s   ..00..w77	0
 	K 	K+Iz8 	)3'/  1 1 K K I\'7J?? K K$$X%9%9$%?%?JJJK K K K K K K K K K K K K K K	K K K K K K K K K K K K K K K	K 	Ks6   
&B=0*B&B=&B**B=-B*.B==C	C	c                     |                                  }|                     |          }|                     |j        |           |                     |j        g            dS )z(Test the creation of Squeezer instances.N)r]   ra   assertIsr[   r'   expandingbuttons)r(   r[   r`   s      r   	test_initzSqueezerTest.test_init   s\    ..00..w77h&0002B77777r   c                    |                                  }ddddz  dfD ]}t          t                    x|_        }|                     |          }|                     |j                            |d          t                     |                     |j        d           |                    |d           |                     t          |j
                  d	           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_valuerR   r6   r   N)r]   r   SENTINEL_VALUEwritera   r'   r[   
call_countassert_called_withlenr}   r(   r[   r*   
orig_writer`   s        r   test_write_no_tagszSqueezerTest.test_write_no_tags   s    ..00t!35IJ 	@ 	@D)->)J)J)JJGMJ227;;HX-33D"==~NNNZ2A666))$333S!:;;Q????	@ 	@r   c                    ddddz  dfD ]}|                                  }t          |j        _        |j        }|                     |          }|                     |j                            |d          t                     |                     |j        d           |                    |d           |                     t          |j
                  d           d	S )
r   r.   r   r   r   r   stderrr6   r   N)r]   r   r   r   ra   r'   r[   r   r   r   r}   )r(   r*   r[   r   r`   s        r   test_write_not_stdoutz"SqueezerTest.test_write_not_stdout   s    t!35IJ 
	@ 
	@D2244G)7GM& J227;;HX-33D(CC,. . .Z2A666))$999S!:;;Q????
	@ 
	@r   c                 *   |                                  }dD ]}t          t                    x|_        }|                     |          }d|_        |                     |j                            |d          t                     |                     |j        d           |	                    |d           |                     t          |j                  d           ddz  d	fD ]}t          t                    x|_        }|                     |          }d|_        |                     |j                            |d          d
           |                     |j        d           |                     t          |j                  d           d
S )r   )r.   r   r   2   stdoutr6   r   r   r   r   N)r]   r   r   r   ra   auto_squeeze_min_linesr'   r[   r   r   r   r}   r   s        r   test_write_stdoutzSqueezerTest.test_write_stdout   s   ..00  		@ 		@D)->)J)J)JJGMJ227;;H.0H+X-33D(CC+- - -Z2A666))$999S!:;;Q???? 4')=> 	@ 	@D)->)J)J)JJGMJ227;;H.0H+X-33D(CCTJJJZ2A666S!:;;Q????	@ 	@r   c                 r   |                      d          }|j        }|                     |          }d|_        t	          d          |_        |                    dd           |                     |                    dd	          d
           |                     t          |j
                  d           dS )zATest that the auto-squeezing creates an ExpandingButton properly.TrZ   rG   rH   r   zTEXT
TEXT
TEXT
TEXT
TEXT
TEXT
r   rf   endr<   r6   N)r]   r*   ra   r   r   ry   r   r'   getr   r}   )r(   r[   r\   r`   s       r   test_auto_squeezezSqueezerTest.test_auto_squeeze   s    ...EEl..w77*+'#333j(+++66===X677;;;;;r   c                    dD ]}|                      d          }|j        }|                     |          }t          d          |_        |                    dd|           |                    dd           |                     |                    dd	          d
           |                     t          |j
                  d           |                                }|                     |d           |                     |                    dd	          d           |                     t          |j
                  d           |                     |j
        d         j        d           |j
        d                                          |                     |                    dd	          d
           |                     t          |j
                  d           dS )z%Test the squeeze_current_text method.)r   r   Tr   rH   r   rf   
SOME
TEXT
insertr   SOME
TEXT

r   breakr?   r6   z	SOME
TEXTN)r]   r*   ra   r   ry   r   rg   r'   r   r   r}   squeeze_current_textsexpand)r(   tag_namer[   r\   r`   retvals         r   test_squeeze_current_textz&SqueezerTest.test_squeeze_current_text   s    - 	@ 	@H22D2IIG!,K227;;H#'Q#7#7#7H  unh???  5111[__UE::<LMMMS!:;;Q??? 2244FVW---[__UE::FCCCS!:;;Q???X6q9;\JJJ %a(//111[__UE::<LMMMS!:;;Q????1	@ 	@r   c                    |                      d          }|j        }|                     |          }t          d          |_        |                    ddd           |                    dd           |                     |                    dd	          d
           |                     t          |j
                  d           |                                }|                     |d           |                     |                    dd	          d
           |                     t          |j
                  d           dS )z@Test that the event doesn't squeeze text without a relevant tag.Tr   rH   r   rf   r   TAGr   r   r   r   r   N)r]   r*   ra   r   ry   r   rg   r'   r   r   r}   r   r(   r[   r\   r`   r   s        r   )test_squeeze_current_text_no_allowed_tagsz6SqueezerTest.test_squeeze_current_text_no_allowed_tags   s0   ...EEl..w77#333 	5.%888Xu---668HIIIX677;;; ..00)))668HIIIX677;;;;;r   c                 8   |                      d          }|j        }|                     |          }t          d          |_        |                    ddd           |                    dd           |                                 |                     t          |j
                  d	           |                    dd
d           |                    dd           |                                }|                     |d           |                     |                    dd          d           |                     t          |j
                  d           |                     |                    |j
        d         d|j
        d	                              dS )z2Test squeezing text before existing squeezed text.Tr   rH   r   rf   r   r   r   r6   zMORE
STUFF
r   r   z


r2   r   <N)r]   r*   ra   r   ry   r   rg   r   r'   r   r}   r   
assertTruecomparer   s        r   /test_squeeze_text_before_existing_squeezed_textz<SqueezerTest.test_squeeze_text_before_existing_squeezed_text   s   ...EEl..w77#333 	5.(;;;Xu---%%'''X677;;; 	5/8<<<Xu---..00)))66AAAX677;;;++%a(%a(
 
 	 	 	 	 	r   c                    |                      d          }|                     |          }|j        }|dz   }|                     dddt	          |                     t          j                     |                     |j        |           dS )zTest the reload() class-method.Tr   r@   mainPyShellzauto-squeeze-min-linesN)r]   ra   r   rp   strr   reloadr'   )r(   r[   r`   orig_auto_squeeze_min_linesnew_auto_squeeze_min_liness        r   test_reloadzSqueezerTest.test_reload  s    ...EE..w77&.&E# &A2%E"--I7*++	- 	- 	- 	83	5 	5 	5 	5 	5r   c                 ,    t          j                     dS )z@Test that Squeezer.reload() runs without any instances existing.N)r   r   r/   s    r   !test_reload_no_squeezer_instancesz.SqueezerTest.test_reload_no_squeezer_instances+  s    r   )Fr   )rN   rO   rP   rQ   r]   ra   rW   rp   rz   r~   r   r   r   r   r   r   r   r   r   rR   r   r   rT   rT   Q   s       ''
 
 
 
      ? ? ?K K K,8 8 8
@ 
@ 
@@ @ @@ @ @0
< 
< 
<@ @ @:< < <(  45 5 5"    r   rT   c                   b    e Zd ZdZd Z ede          d             Zd Zd Z	d Z
d	 Zd
 ZdS )ExpandingButtonTestz$Tests for the ExpandingButton class.c                    t          |           }t                      }t          |          |j        _        t          |j        j                  |j        _        |                     |j        j        j                   d|_	        |S )z0Helper for tests: Create a mock Squeezer object.r   )
r"   r   r   r[   r*   r   rX   r    closer   )r(   r   r`   s      r   make_mock_squeezerz&ExpandingButtonTest.make_mock_squeezer4  sl    %%66 $T

)(*:*?@@(,2333 +-'r   zidlelib.squeezer.Hovertipautospecc                 B   |                                  }|j        j        }t          ddd|          }|                     |j        d           |                     |j        |           |                     d|                    d          v            |                     |	                    dd          d           | 
                    d	|                                           d
t          j                    rdndz  }| 
                    ||                                           |                     |j        d           |                    |t           t                      |j        d         d         }| 
                    d|                                           dS )z1Test the simplest creation of an ExpandingButton.r   TAGSr   z50 linesr*   rf   r   r<   z<Double-Button-1>z<Button-%s>23r6   )hover_delayr   zright-clickN)r   r[   r*   r   r'   r   masterr   cgetr   assertInbindr   isAquaTkr   r   r
   	call_argslower)r(   MockHovertipr`   r\   expandingbuttonright_button_codetooltip_texts          r   r~   zExpandingButtonTest.test_init@  s    **,,&+)&&"hGG*F333 	/===
o&:&:6&B&BBCCC 	66=== 	)?+?+?+A+ABBB)FO4E4E-NSS3O')=)=)?)?@@@ 	0!444''#'NNN $-a03m\%7%7%9%9:::::r   c                    |                                  }t          ddd|          }|j        j        }|                    d|           |                    t                                }|                     |d           |                     |                    dd          d	           |	                    d
          }|                     |                    d|          d           |                     |
                    dd          d|f           |                     |j        j        j        d           |j        j                            |           dS )zTest the expand event.r   r   r   rf   windoweventNr   zTEXT
end-1cr6   )r   r   r[   r*   window_creater   r   r'   r   indextag_nextranger}   remover   r   )r(   r`   r   r\   r   text_end_indexs         r   test_expandzExpandingButtonTest.test_expand]  sP   **,,)&&"hGG &+!!%!@@@ !''dff'55&&& 	66AAA %**844??HHH2265AA .1	3 	3 	3 	29DaHHH!(;;OLLLLLr   c                    |                                  }ddz  }t          |dd|          }|                                 |                     |j                   |j        }|                    d|           t          d          5 }d|j        _	        d|j
        _	        |                    t                      	          }d
d
d
           n# 1 swxY w Y   |                     |d           |                     |j                            dd          d           t          d          5 }d|j        _	        d|j
        _	        |                    t                      	          }d
d
d
           n# 1 swxY w Y   |                     |d
           |                     |j                            dd          |           d
S )z@Test that expanding very long output asks user for confirmation.ai r   r   rf   r   zidlelib.squeezer.messageboxFr   Nr   r   r.   T)r   r   set_is_dangerousr   is_dangerousr*   r   r   askokcancelr   askyesnor   r   r'   r   )r(   r`   r*   r   r\   mock_msgboxr   s          r   test_expand_dangerous_oupputz0ExpandingButtonTest.test_expand_dangerous_oupputx  s'   **,,U{)$HEE((***4555 &*!!%!@@@ 011 	:[38K#005K -$++$&&+99F		: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	)))-11%BBBGGG 011 	:[37K#004K -$++$&&+99F		: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	&&&-11%BBDIIIIIs$   ;CCC-;E44E8;E8c                    |                                  }t          ddd|          }t                      |_        t                      |_        |                    t                                }|                     |d           |                     |j        j        d           |                     |j        j        d           |j                            d           dS )zTest the copy event.r   r   r   r   Nr6   )	r   r   r   clipboard_clearclipboard_appendcopyr'   r   r   )r(   r`   r   r   s       r   	test_copyzExpandingButtonTest.test_copy  s    
 **,,)&&"hGG*.&&'+/66( !%%DFF%33&&& 	8CQGGG9DaHHH(;;FCCCCCr   c                    |                                  }t          ddd|          }t                      |_        t	          dt
                    5 }|                    t                                 |                     |j        d           |                     |j	        d         d	         d           d
d
d
           d
S # 1 swxY w Y   d
S )zTest the view event.r   r   r   zidlelib.squeezer.view_textr   r   r6   r   r2   N)
r   r   r   selection_ownr   r   viewr'   r   r   )r(   r`   r   mock_view_texts       r   	test_viewzExpandingButtonTest.test_view  s   **,,)&&"hGG(,%/)DDD 		E!  tvv ... ^6::: ^5a8;VDDD		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		E 		Es   A%CC	Cc                    |                                  }t          ddd|          }t          d          5 }t                      }||_        t                      }d|_        d|_        |                    |           |                     |j	        j
        t          |j                             |j        D ]%^}}|j	                            |t                     &	 ddd           dS # 1 swxY w Y   dS )	zTest the context menu.r   r   r   ztkinter.Menur@   r   )labelcommandN)r   r   r   r   r   xycontext_menu_eventr'   add_commandr   r   rmenu_specsassert_any_callr
   )r(   r`   r   	mock_Menu	mock_menu
mock_eventr   datas           r   
test_rmenuzExpandingButtonTest.test_rmenu  sR   **,,)&&"hGG>"" 
	PiI%.I"JJLJL..Z.@@@Y2= !<==? ? ? / ; P P%55E35OOOOP
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	P 
	Ps   B'C++C/2C/N)rN   rO   rP   rQ   r   r   r   r~   r   r   r   r   r   rR   r   r   r   r   0  s        ..
 
 
 U&:::; ; ;:;8M M M6!J !J !JFD D D(E E E"P P P P Pr   r   __main__r2   )	verbosity)%rQ   textwrapr   tkinterr   r   unittestunittest.mockr   r   r   r	   r
   test.supportr   idlelib.configr   idlelib.percolatorr   idlelib.squeezerr   r   r   idlelibr   idlelib.textviewr   idlelib.tooltipr   r   r"   TestCaser$   rT   r   rN   r   rR   r   r   <module>r     s                    J J J J J J J J J J J J J J ! ! ! ! ! ! # # # # # # ) ) ) ) ) )                & & & & & & $ $ $ $ $ $(  ,9 ,9 ,9 ,9 ,9X& ,9 ,9 ,9^\ \ \ \ \8$ \ \ \~^P ^P ^P ^P ^P(+ ^P ^P ^PB zHMA r   