3 § feUã@søddlmZddlmZmZddlZddlmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZmZddlmZdd lmZdd lmZdd lmZe jZd d „ZGdd„dejƒZGdd„dejƒZ Gdd„dejƒZ!e"dkrôej#dd�dS)é)Ú namedtuple)ÚTextÚTkN)ÚMockÚNonCallableMagicMockÚpatchÚsentinelÚANY)Úrequires)ÚidleConf)Úcount_lines_with_wrappingÚExpandingButtonÚSqueezer)Úmacosx)Ú view_text)ÚHovertip)ÚPyShellcs0tdƒtƒ‰ˆjƒ‡fdd„}|j|ƒˆS)z*Helper for tests: Create a root Tk object.ZguicsˆjƒˆjƒdS)N)Zupdate_idletasksZdestroy©)ÚrootrúD/opt/alt/python36/lib64/python3.6/idlelib/idle_test/test_squeezer.pyÚ cleanup_rootsz&get_test_tk_root..cleanup_root)r rZwithdrawÚ addCleanup)Z test_instancerr)rrÚget_test_tk_roots   rc@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)ÚCountLinesTestz1Tests for the count_lines_with_wrapping function.cCs|j|t|||ƒƒS)N)Ú assertEqualr )ÚselfÚexpectedÚtextÚ linewidthÚtabwidthrrrÚcheck#szCountLinesTest.checkcCs|jtdƒdƒdS)zTest with an empty string.ÚrN)rr )rrrrÚtest_count_empty)szCountLinesTest.test_count_emptycCs|jtdƒdƒdS)z/Test with a string which begins with a newline.z textéN)rr )rrrrÚ!test_count_begins_with_empty_line-sz0CountLinesTest.test_count_begins_with_empty_linecCs|jtdƒdƒdS)z-Test with a string which ends with a newline.ztext éN)rr )rrrrÚtest_count_ends_with_empty_line1sz.CountLinesTest.test_count_ends_with_empty_linecCs|jtdƒdƒdS)z Test with several lines of text.z1 2 3 éN)rr )rrrrÚtest_count_several_lines5sz'CountLinesTest.test_count_several_linescCs|jddddd�|jddddd�|jddddd�|jddddd�|jddddd�|jddd dd�|jd dd dd�|jddd dd�|jddddd�|jddddd�|jddddd�|jddd dd�|jddd dd�|jddddd�dS)z-Test with various tab widths and line widths.r%ú éé)rrrrr#r'éé é é é éé Nr)z z z z z z z z z z z z z )r )rrrrÚtest_tab_width9szCountLinesTest.test_tab_widthN) Ú__name__Ú __module__Ú __qualname__Ú__doc__r r"r$r&r(r3rrrrr!src@s¶eZdZdZdd„Zd%dd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„ZeddƒZeejejejejfdd „ƒZed!d"„ƒZd#d$„ZdS)&Ú SqueezerTestzTests for the Squeezer class.cCstƒ}t|_|S)z$Create a mock EditorWindow instance.)rrÚ __class__)rÚeditwinrrrÚmake_mock_editor_windowSsz$SqueezerTest.make_mock_editor_windowNcCs|dkr|jƒ}t|ƒS)z©r*r=©r'r?)r@rArBrC)Ú_test_count_lines_helper)rrrrrrÚtest_count_linesaszSqueezerTest.test_count_linesc s†d‰d}d}||j_|ˆd|||jj_||ddœ‰‡fdd„|j_tƒ}‡fdd „}||j_td |ƒ}|jƒ|j |j ƒdS) z|szDSqueezerTest._prepare_mock_editwin_for_count_lines..cs|dkr ˆStdƒ‚dS)NÚ0z%measure should only be called on '0'!)Ú ValueError)Úchar)Ú CHAR_WIDTHrrÚmeasure�szCSqueezerTest._prepare_mock_editwin_for_count_lines..measurezidlelib.squeezer.Font) Zget_tk_tabwidthÚ return_valuerZ winfo_widthÚcgetrrLrÚstartrÚstop) rr:rrZ BORDER_WIDTHZ PADDING_WIDTHZ mock_fontrLZpatcherr)rKrFrÚ%_prepare_mock_editwin_for_count_linesks  z2SqueezerTest._prepare_mock_editwin_for_count_linesc Csh|jƒ}|j|||ƒ|j|ƒ}ttd�}d}td|ƒ�&|j|j|ƒtƒ|j|||ƒWdQRXdS)zHelper for test_count_lines.)rMÚTEXTz*idlelib.squeezer.count_lines_with_wrappingN) r;rQr<rÚSENTINEL_VALUErÚassertIsÚ count_linesÚassert_called_with)rrrr:ÚsqueezerZmock_count_linesrrrrrDŠs  z%SqueezerTest._test_count_lines_helpercCs2|jƒ}|j|ƒ}|j|j|ƒ|j|jgƒdS)z(Test the creation of Squeezer instances.N)r;r<rTr:rÚexpandingbuttons)rr:rWrrrÚ test_init—s zSqueezerTest.test_initcCs†|jƒ}xxddddddgD]`}ttd�|_}|j|ƒ}|j|jj|fƒtƒ|j|jdƒ|j|fƒ|jt |j ƒd ƒqWd S) z@Test Squeezer's overriding of the EditorWindow's write() method.r!rRz LONG TEXTièz MANY_LINES éd)rMr%rN) r;rrSÚwriter<rr:Ú call_countrVÚlenrX)rr:rÚ orig_writerWrrrÚtest_write_no_tagsžs  zSqueezerTest.test_write_no_tagscCs„x~ddddddgD]f}|jƒ}t|j_|j}|j|ƒ}|j|jj|dƒtƒ|j|jdƒ|j|dƒ|jt |j ƒd ƒqWd S) z@Test Squeezer's overriding of the EditorWindow's write() method.r!rRz LONG TEXTièz MANY_LINES rZÚstderrr%rN) r;rSr[rMr<rr:r\rVr]rX)rrr:r^rWrrrÚtest_write_not_stdoutªs  z"SqueezerTest.test_write_not_stdoutcCsü|jƒ}|j|ddd�xndD]f}ttd�|_}|j|ƒ}d|_|j|jj|dƒtƒ|j|j d ƒ|j |dƒ|jt |j ƒd ƒqWxnd d d dgD]Z}ttd�|_}|j|ƒ}d|_|j|jj|dƒdƒ|j|j d ƒ|jt |j ƒd ƒqšWdS)z@Test Squeezer's overriding of the EditorWindow's write() method.r=r*)rrr!rR)rMé2Ústdoutr%rz LONG TEXTièz MANY_LINES rZN)r!rR) r;rQrrSr[r<Úauto_squeeze_min_linesrr:r\rVr]rX)rr:rr^rWrrrÚtest_write_stdout¸s&     zSqueezerTest.test_write_stdoutcCs€t|ƒ}t|ƒ}|jddƒ|jƒ}||_|j|ƒ}d|_tdd�|_|j dddƒ|j |j ddƒd ƒ|j t |j ƒd ƒd S) zATest that the auto-squeezing creates an ExpandingButton properly.Úiomarkz1.0r,r1)rMzTEXT rcÚendÚ r%N)rrÚmark_setr;rr<rdrrUr[rÚgetr]rX)rrÚ text_widgetr:rWrrrÚtest_auto_squeezeÒs   zSqueezerTest.test_auto_squeezecCs,t|ƒ}�xdD�]}t|ƒ}|jddƒ|jƒ}||_|j_|j|ƒ}tdd�|_ |j dd|ƒ|jddƒ|j |j dd ƒd ƒ|j t |jƒd ƒ|jtƒd �}|j |d ƒ|j |j dd ƒdƒ|j t |jƒdƒ|j |jd jdƒ|jd jtƒd �|j |j dd ƒd ƒ|j t |jƒd ƒqWdS)z$Test the squeeze_current_text event.rcr`rfz1.0r1)rMz SOME TEXT Úinsertrgz SOME TEXT r)ÚeventÚbreakz r%z SOME TEXTN)rcr`)rrrir;rÚperÚbottomr<rrUrmrrjr]rXÚsqueeze_current_text_eventÚsÚexpand)rrZtag_namerkr:rWÚretvalrrrÚtest_squeeze_current_text_eventâs(     z,SqueezerTest.test_squeeze_current_text_eventcCsÌt|ƒ}t|ƒ}|jddƒ|jƒ}||_|j_|j|ƒ}tdd�|_ |j dddƒ|jddƒ|j |j ddƒd ƒ|j t |jƒd ƒ|jtƒd �}|j |d ƒ|j |j ddƒd ƒ|j t |jƒd ƒd S)z@Test that the event doesn't squeeze text without a relevant tag.rfz1.0r1)rMz SOME TEXT ZTAGrmrgz SOME TEXT r)rnroN)rrrir;rrprqr<rrUrmrrjr]rXrr)rrrkr:rWrurrrÚ/test_squeeze_current_text_event_no_allowed_tagss     zs zSqueezerTest._make_sigcCs|dd…\}}|j||ŽS)Nr#éþÿÿÿ)r€)r|Z mock_call_objÚargsÚkwargsrrrÚget_GetOption_signatureFsz$SqueezerTest.get_GetOption_signaturecCs8|jtjtƒtjddddƒtjƒ|jtjdƒdS)zTest the reload() class-method.Úmainrzauto-squeeze-min-linesZ42é*N)ZassertIsInstancerrdÚintr Z SetOptionÚreloadr)rrrrÚ test_reloadKszSqueezerTest.test_reload)N)r4r5r6r7r;r<rErQrDrYr_rarerlrvrwrzrr{Ú classmethodrZ NOT_GIVENr€r„r‰rrrrr8Qs.    " r8c@sTeZdZdZdd„Zeded�dd„ƒZdd „Zd d „Z d d „Z dd„Z dd„Z dS)ÚExpandingButtonTestz$Tests for the ExpandingButton class.cCs$t|ƒ}tƒ}t|ƒ|j_d|_|S)z0Helper for tests: Create a mock Squeezer object.rb)rrrr:rrd)rrrWrrrÚmake_mock_squeezerWs  z&ExpandingButtonTest.make_mock_squeezerzidlelib.squeezer.Hovertip)ÚautospeccCsÖ|jƒ}|jj}tddd|ƒ}|j|jdƒ|j|j|ƒ|jd|jdƒkƒ|j|j ddƒdƒ|j d |j ƒƒd t j ƒr€d nd }|j ||j ƒƒ|j|jd ƒ|j|ttd�|jdd }|j d|jƒƒdS)z1Test the simplest creation of an ExpandingButton.rRÚTAGSrbz50 linesrz1.0rgrhzz Ú2Ú3r%)Z hover_delayrz right-clickN)rŒr:rr rrsZmasterryrNrjZassertInZbindrZisAquaTkr\rVr Ú call_argsÚlower)rZ MockHovertiprWrkÚexpandingbuttonZright_button_codeZ tooltip_textrrrrYaszExpandingButtonTest.test_initcCsº|jƒ}tddd|ƒ}|j}|jd|d�|j|_|jtƒd�}|j|dƒ|j|jddƒd ƒ|j d ƒ}|j|jd|ƒdƒ|j|j ddƒd|fƒ|j|j j j d ƒ|j j j|ƒdS) zTest the expand event.rRrŽrbz1.0)Úwindow)rnNrgzTEXT zend-1cr%)rŒr rÚ window_createÚ base_textrtrrrjÚindexZ tag_nextrangerXÚremover\rV)rrWr“rkruZtext_end_indexrrrÚ test_expand~s   zExpandingButtonTest.test_expandcCsú|jƒ}dd}t|dd|ƒ}|jƒ|j|jƒ|j}|jd|d�|j|_tdƒ�$}d |j _ d |j _ |j t ƒd �}Wd QRX|j|d ƒ|j|jjdd ƒdƒtdƒ�$}d|j _ d|j _ |j t ƒd �}Wd QRX|j|d ƒ|j|jjdd ƒ|ƒd S)z@Test that expanding very long output asks user for confirmation.Úar-r,rŽrbz1.0)r”zidlelib.squeezer.tkMessageBoxF)rnNrozend-1cr!Ti †)rŒr Zset_is_dangerousryZ is_dangerousrr•r–rZ askokcancelrMZaskyesnortrrrj)rrWrr“rkZ mock_msgboxrurrrÚtest_expand_dangerous_oupput�s(     z0ExpandingButtonTest.test_expand_dangerous_oupputcCsp|jƒ}tddd|ƒ}tƒ|_tƒ|_|jtƒd�}|j|dƒ|j|jjdƒ|j|jjdƒ|jjdƒdS)zTest the copy event.rRrŽrb)rnNr%) rŒr rZclipboard_clearZclipboard_appendÚcopyrr\rV)rrWr“rurrrÚ test_copyÆs zExpandingButtonTest.test_copyc Csl|jƒ}tddd|ƒ}tƒ|_tdtd��8}|jtƒd�|j|jdƒ|j|j dd dƒWd QRXd S) zTest the view event.rRrŽrbzidlelib.squeezer.view_text)r�)rnr%rr#N) rŒr rZ selection_ownrrZviewrr\r‘)rrWr“Zmock_view_textrrrÚ test_viewÚs zExpandingButtonTest.test_viewc Cs’|jƒ}tddd|ƒ}tdƒ�j}tƒ}||_tƒ}d|_d|_|j|d�|j|j j t |j ƒƒx"|j D]^}}|j j |td�qhWWdQRXdS) zTest the context menu.rRrŽrbz tkinter.Menur-)rn)ÚlabelZcommandN)rŒr rrrMÚxÚyZcontext_menu_eventrZ add_commandr\r]Z rmenu_specsZassert_any_callr )rrWr“Z mock_MenuZ mock_menuZ mock_eventrŸÚdatarrrÚ test_rmenuës    zExpandingButtonTest.test_rmenuN) r4r5r6r7rŒrrrYr™r›r�ržr£rrrrr‹Ss )r‹Ú__main__r#)Ú verbosity)$Ú collectionsrZtkinterrrZunittestZ unittest.mockrrrrr Z test.supportr Zidlelib.configr Zidlelib.squeezerr r rZidlelibrZidlelib.textviewrZidlelib.tooltiprZidlelib.pyshellrrSrZTestCaserr8r‹r4r…rrrrÚs(       0*