a Z^k-c@shdZGdddZGdddZGdddZGdddZd d lmZGd d d ZGd ddZdS)zClasses that replace tkinter gui objects used by an object being tested. A gui object is anything with a master or parent parameter, which is typically required in spite of what the doc strings say. c@eZdZdZddZdS)EventaMinimal mock with attributes for testing event handlers. This is not a gui object, but is used as an argument for callbacks that access attributes of the event passed. If a callback ignores the event, other than the fact that is happened, pass 'event'. Keyboard, mouse, window, and other sources generate Event instances. Event instances have the following attributes: serial (number of event), time (of event), type (of event as number), widget (in which event occurred), and x,y (position of mouse). There are other attributes for specific events, such as keycode for key events. tkinter.Event.__doc__ has more but is still not complete. cKs|j|dS)z,Create event with attributes needed for testN)Z__dict__Zupdate)selfkwdsr1/usr/lib64/python3.9/idlelib/idle_test/mock_tk.py__init__zEvent.__init__N)__name__ __module__ __qualname____doc__rrrrrrs rc@s*eZdZdZd ddZddZddZdS) Varz)Use for String/Int/BooleanVar: incompleteNcCs||_||_||_dSN)mastervaluename)rrrrrrrrsz Var.__init__cC ||_dSr r)rrrrrsetzVar.setcCs|jSr rrrrrget!rzVar.getNNN)rr r r rrrrrrrr s r c@s"eZdZdZdddZddZdS) Mbox_funcaGeneric mock for messagebox functions, which all have the same signature. Instead of displaying a message box, the mock's call method saves the arguments as instance attributes, which test functions can then examine. The test can set the result returned to ask function NcCrr )result)rrrrrr+rzMbox_func.__init__cOs||_||_||_||_|jSr )titlemessageargsrr)rrrrrrrr__call__-s zMbox_func.__call__r )rr r r rrrrrrr$s rc@s@eZdZdZeZeZeZeZeZ eZ eZ eZ dS)Mboxa5Mock for tkinter.messagebox with an Mbox_func for each function. This module was 'tkMessageBox' in 2.x; hence the 'import as' in 3.x. Example usage in test_module.py for testing functions in module.py: --- from idlelib.idle_test.mock_tk import Mbox import module orig_mbox = module.tkMessageBox showerror = Mbox.showerror # example, for attribute access in test methods class Test(unittest.TestCase): @classmethod def setUpClass(cls): module.tkMessageBox = Mbox @classmethod def tearDownClass(cls): module.tkMessageBox = orig_mbox --- For 'ask' functions, set func.result return value before calling the method that uses the message function. When tkMessageBox functions are the only gui alls in a method, this replacement makes the method gui-free, N) rr r r rZ askokcancelZ askquestionZaskretrycancelZaskyesnoZaskyesnocancelZ showerrorZshowinfoZ showwarningrrrrr5sr)TclErrorc@seZdZdZdifddZddZd"dd Zd d Zd d Zd#ddZ d$ddZ ddZ ddZ ddZ d%ddZddZddZddZd&d d!ZdS)'TextaA semi-functional non-gui replacement for tkinter.Text text editors. The mock's data model is that a text is a list of -terminated lines. The mock adds an empty string at the beginning of the list so that the index of actual lines start at 1, as with Tk. The methods never see this. Tk initializes files with a terminal that cannot be deleted. It is invisible in the sense that one cannot move the cursor beyond it. This class is only tested (and valid) with strings of ascii chars. For testing, we are not concerned with Tk Text's treatment of, for instance, 0-width characters or character + accent. NcKsddg|_dS)zInitialize mock, non-gui, text-only Text widget. At present, all args are ignored. Almost all affect visual behavior. There are just a few Text-only options that affect text behavior.  N)data)rrZcnfZkwrrrrgsz Text.__init__cCsd|j|ddS)zAReturn string version of index decoded according to current text.z%s.%s)endflag)_decoderindexrrrr)orz Text.indexrcCst|ttfrt|}z |}Wn tyBtd|dYn0t|jd}|dkrp|t|j|dfS|dkr| |S| d\}}t |}|dkrdS||kr| |St|j|d}| ds|dkr||fSt |}|d krd }n||kr|}||fS) a8Return a (line, char) tuple of int indexes into self.data. This implements .index without converting the result back to a string. The result is constrained by the number of lines and linelengths of self.data. For many indexes, the result is initially (1, 0). The input index may have any of several possible forms: * line.char float: converted to 'line.char' string; * 'line.char' string, where line and char are decimal integers; * 'line.char lineend', where lineend='lineend' (and char is ignored); * 'line.end', where end='end' (same as above); * 'insert', the positions before terminal ; * 'end', whose meaning depends on the endflag passed to ._endex. * 'sel.first' or 'sel.last', where sel is a tag -- not implemented. zbad text index "%s"Nr%insertZendZ.)r%rz lineendr) Z isinstanceZfloatZbytesZstrZlowerZAttributeErrorr lenr$_endexZsplitZintZendswith)rr)r&ZlastlinelinecharZ linelengthrrrr'ss4     z Text._decodecCs<t|j}|dkr|dfS|d8}|t|j||fSdS)aReturn position for 'end' or line overflow corresponding to endflag. -1: position before terminal ; for .insert(), .delete 0: position after terminal ; for .get, .delete index 1 1: same viewed as beginning of non-existent next line (for .index) r%rN)r+r$)rr&Znrrrr,s  z Text._endexcCs|sdS|d}|dddkr,|d||d\}}|j|d|}|j||d}||d|j|<|dd|j|d|d<|j|t|d|7<dS)z+Insert chars before the character at index.NTr#r"rr%)Z splitlinesappendr'r$r+)rr)Zcharsr-r.ZbeforeZafterrrrr*s  z Text.insertc Cs||\}}|dur&||d}}n||\}}||krN|j|||S|j||dg}t|d|D]}||j|qp||j|d|d|SdS)z;Return slice from index1 to index2 (default is 'index1+1').Nr%r")r'r$ranger0Zjoin) rindex1index2 startline startcharendlineendcharZlinesirrrrszText.getcCs||d\}}|durf|t|j|dkr>||d}}qv|t|jdkr`|dd}}qvdSn||d\}}||kr||kr|j|d||j||d|j|<nZ||kr |j|d||j||d|j|<|d7}t||dD]}|j|=qdS)zDelete slice from index1 to index2 (default is 'index1+1'). Adjust default index2 ('index+1) for line ends. Do not delete the terminal at the very end of self.data ([-1][-1]). r/Nr%r)r'r+r$r1)rr2r3r4r5r6r7r8rrrdeletes&   z Text.deletecCs||\}}||\}}|dkr<||kp:||ko:||kS|dkr\||kpZ||koZ||kS|dkr|||kpz||koz||kS|dkr||kp||ko||kS|dkr||ko||kS|dkr||kp||kStd|dS)Nzz>=z==z!=z=bad comparison operator "%s": must be <, <=, ==, >=, >, or !=)r'r )rr2Zopr3Zline1Zchar1Zline2Zchar2rrrcompares"z Text.comparecCdS)z.Set mark *name* before the character at index.Nr)rrr)rrrmark_set rz Text.mark_setcGr;)zDelete all marks in markNames.Nr)rZ markNamesrrr mark_unsetrzText.mark_unsetcCr;)zARemove tag tagName from all characters between index1 and index2.Nr)rZtagNamer2r3rrr tag_removerzText.tag_removecCr;)z2Adjust the view of the text according to scan_markNrrZxZyrrr scan_dragtorzText.scan_dragtocCr;)z&Remember the current X, Y coordinates.Nrr?rrr scan_markrzText.scan_markcCr;)z8Scroll screen to make the character at INDEX is visible.Nrr(rrrsee!rzText.seecCr;)z>Bind to this widget at event sequence a call to function func.Nr)ZsequenceZfuncZaddrrrbind(rz Text.bind)rr r r r)rr r r rr)r'r,r*rr9r:r<r=r>r@rArBrCrrrrr!Zs   3   r!c@r)EntryzMock for tkinter.Entry.cCsdSr rrrrr focus_set.rzEntry.focus_setN)rr r r rErrrrrD,srDN) r rr rrZ_tkinterr r!rDrrrrZs # S