3  feU@sddlmZddlmZmZddlZddlmZmZm Z m Z m Z ddl m Z ddlmZddlmZmZmZddlmZdd lmZdd lmZdd lmZe jZd d ZGdddejZGdddejZ GdddejZ!e"dkrej#dddS)) namedtuple)TextTkN)MockNonCallableMagicMockpatchsentinelANY)requires)idleConf)count_lines_with_wrappingExpandingButtonSqueezer)macosx) view_text)Hovertip)PyShellcs0tdtjfdd}|j|S)z*Helper for tests: Create a root Tk object.ZguicsjjdS)N)Zupdate_idletasksZdestroy)rootrD/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)rrget_test_tk_roots   rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)CountLinesTestz1Tests for the count_lines_with_wrapping function.cCs|j|t|||S)N) assertEqualr )selfexpectedtext linewidthtabwidthrrrcheck#szCountLinesTest.checkcCs|jtdddS)zTest with an empty string.rN)rr )rrrrtest_count_empty)szCountLinesTest.test_count_emptycCs|jtdddS)z/Test with a string which begins with a newline.z textN)rr )rrrr!test_count_begins_with_empty_line-sz0CountLinesTest.test_count_begins_with_empty_linecCs|jtdddS)z-Test with a string which ends with a newline.ztext N)rr )rrrrtest_count_ends_with_empty_line1sz.CountLinesTest.test_count_ends_with_empty_linecCs|jtdddS)z Test with several lines of text.z1 2 3 N)rr )rrrrtest_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|jddddddS)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 )rrrrtest_tab_width9szCountLinesTest.test_tab_widthN) __name__ __module__ __qualname____doc__r r"r$r&r(r3rrrrr!src@seZdZdZddZd%ddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZeddZeejejejejfdd Zed!d"Zd#d$ZdS)& SqueezerTestzTests for the Squeezer class.cCst}t|_|S)z$Create a mock EditorWindow instance.)rr __class__)reditwinrrrmake_mock_editor_windowSsz$SqueezerTest.make_mock_editor_windowNcCs|dkr|j}t|S)zr*r=r'r?)r@rArBrC)_test_count_lines_helper)rrrrrrtest_count_linesaszSqueezerTest.test_count_linesc sdd}d}||j_|d|||jj_||ddfdd|j_t}fdd }||j_td |}|j|j |j dS) z|szDSqueezerTest._prepare_mock_editwin_for_count_lines..cs|dkr StddS)N0z%measure should only be called on '0'!) ValueError)char) CHAR_WIDTHrrmeasureszCSqueezerTest._prepare_mock_editwin_for_count_lines..measurezidlelib.squeezer.Font) Zget_tk_tabwidth return_valuerZ winfo_widthcgetrrLrstartrstop) 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.)rMTEXTz*idlelib.squeezer.count_lines_with_wrappingN) r;rQr<rSENTINEL_VALUErassertIs count_linesassert_called_with)rrrr:squeezerZmock_count_linesrrrrrDs  z%SqueezerTest._test_count_lines_helpercCs2|j}|j|}|j|j||j|jgdS)z(Test the creation of Squeezer instances.N)r;r<rTr:rexpandingbuttons)rr:rWrrr test_inits zSqueezerTest.test_initcCs|j}xxddddddgD]`}ttd|_}|j|}|j|jj|ft|j|jd|j|f|jt |j d qWd S) z@Test Squeezer's overriding of the EditorWindow's write() method.r!rRz LONG TEXTiz MANY_LINES d)rMr%rN) r;rrSwriter<rr: call_countrVlenrX)rr:r orig_writerWrrrtest_write_no_tagss  zSqueezerTest.test_write_no_tagscCsx~ddddddgD]f}|j}t|j_|j}|j|}|j|jj|dt|j|jd|j|d|jt |j d qWd S) z@Test Squeezer's overriding of the EditorWindow's write() method.r!rRz LONG TEXTiz MANY_LINES rZstderrr%rN) r;rSr[rMr<rr:r\rVr]rX)rrr:r^rWrrrtest_write_not_stdouts  z"SqueezerTest.test_write_not_stdoutcCs|j}|j|dddxndD]f}ttd|_}|j|}d|_|j|jj|dt|j|j d |j |d|jt |j d qWxnd d d dgD]Z}ttd|_}|j|}d|_|j|jj|dd|j|j d |jt |j d qWdS)z@Test Squeezer's overriding of the EditorWindow's write() method.r=r*)rrr!rR)rM2stdoutr%rz LONG TEXTiz MANY_LINES rZN)r!rR) r;rQrrSr[r<auto_squeeze_min_linesrr:r\rVr]rX)rr:rr^rWrrrtest_write_stdouts&     zSqueezerTest.test_write_stdoutcCst|}t|}|jdd|j}||_|j|}d|_tdd|_|j ddd|j |j ddd |j t |j d d S) zATest that the auto-squeezing creates an ExpandingButton properly.iomarkz1.0r,r1)rMzTEXT rcend r%N)rrmark_setr;rr<rdrrUr[rgetr]rX)rr text_widgetr:rWrrrtest_auto_squeezes   zSqueezerTest.test_auto_squeezecCs,t|}xdD]}t|}|jdd|j}||_|j_|j|}tdd|_ |j dd||jdd|j |j dd d |j t |jd |jtd }|j |d |j |j dd d|j t |jd|j |jd jd|jd jtd |j |j dd d |j t |jd qWdS)z$Test the squeeze_current_text event.rcr`rfz1.0r1)rMz SOME TEXT insertrgz SOME TEXT r)eventbreakz r%z SOME TEXTN)rcr`)rrrir;rperbottomr<rrUrmrrjr]rXsqueeze_current_text_eventsexpand)rrZtag_namerkr:rWretvalrrrtest_squeeze_current_text_events(     z,SqueezerTest.test_squeeze_current_text_eventcCst|}t|}|jdd|j}||_|j_|j|}tdd|_ |j ddd|jdd|j |j ddd |j t |jd |jtd }|j |d |j |j ddd |j t |jd 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_objargskwargsrrrget_GetOption_signatureFsz$SqueezerTest.get_GetOption_signaturecCs8|jtjttjddddtj|jtjddS)zTest the reload() class-method.mainrzauto-squeeze-min-linesZ42*N)ZassertIsInstancerrdintr Z SetOptionreloadr)rrrr test_reloadKszSqueezerTest.test_reload)N)r4r5r6r7r;r<rErQrDrYr_rarerlrvrwrzrr{ classmethodrZ NOT_GIVENrrrrrrrr8Qs.    " r8c@sTeZdZdZddZededddZdd Zd d Z d d Z ddZ ddZ 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)rrrWrrrmake_mock_squeezerWs  z&ExpandingButtonTest.make_mock_squeezerzidlelib.squeezer.Hovertip)autospeccCs|j}|jj}tddd|}|j|jd|j|j||jd|jdk|j|j ddd|j d |j d t j rd nd }|j ||j |j|jd |j|ttd|jdd }|j d|jdS)z1Test the simplest creation of an ExpandingButton.rRTAGSrbz50 linesrz1.0rgrhzz 23r%)Z hover_delayrz right-clickN)rr:rr rrsZmasterryrNrjZassertInZbindrZisAquaTkr\rVr call_argslower)rZ MockHovertiprWrkexpandingbuttonZright_button_codeZ tooltip_textrrrrYaszExpandingButtonTest.test_initcCs|j}tddd|}|j}|jd|d|j|_|jtd}|j|d|j|jddd |j d }|j|jd|d|j|j ddd|f|j|j j j d |j j j|dS) zTest the expand event.rRrrbz1.0)window)rnNrgzTEXT zend-1cr%)rr r window_create base_textrtrrrjindexZ tag_nextrangerXremover\rV)rrWrrkruZtext_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 dtd$}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,rrbz1.0)rzidlelib.squeezer.tkMessageBoxF)rnNrozend-1cr!Ti)rr Zset_is_dangerousryZ is_dangerousrrrrZ askokcancelrMZaskyesnortrrrj)rrWrrrkZ mock_msgboxrurrrtest_expand_dangerous_oupputs(     z0ExpandingButtonTest.test_expand_dangerous_oupputcCsp|j}tddd|}t|_t|_|jtd}|j|d|j|jjd|j|jjd|jjddS)zTest the copy event.rRrrb)rnNr%) rr rZclipboard_clearZclipboard_appendcopyrr\rV)rrWrrurrr test_copys zExpandingButtonTest.test_copyc Csl|j}tddd|}t|_tdtd8}|jtd|j|jd|j|j dd dWd QRXd S) zTest the view event.rRrrbzidlelib.squeezer.view_text)r)rnr%rr#N) rr rZ selection_ownrrZviewrr\r)rrWrZmock_view_textrrr test_views zExpandingButtonTest.test_viewc Cs|j}tddd|}tdj}t}||_t}d|_d|_|j|d|j|j j t |j x"|j D]^}}|j j |tdqhWWdQRXdS) zTest the context menu.rRrrbz tkinter.Menur-)rn)labelZcommandN)rr rrrMxyZcontext_menu_eventrZ add_commandr\r]Z rmenu_specsZassert_any_callr )rrWrZ mock_MenuZ mock_menuZ mock_eventrdatarrr test_rmenus    zExpandingButtonTest.test_rmenuN) r4r5r6r7rrrrYrrrrrrrrrrSs )r__main__r#) verbosity)$ collectionsrZtkinterrrZunittestZ unittest.mockrrrrr Z test.supportr Zidlelib.configr Zidlelib.squeezerr r rZidlelibrZidlelib.textviewrZidlelib.tooltiprZidlelib.pyshellrrSrZTestCaserr8rr4rrrrrs(       0*