a Z^k-@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@seZdZdZddZdS)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)__dict__update)selfkwdsr1/usr/lib64/python3.9/idlelib/idle_test/mock_tk.py__init__szEvent.__init__N)__name__ __module__ __qualname____doc__rrrrrrs rc@s*eZdZdZd ddZddZddZdS) Varz)Use for String/Int/BooleanVar: incompleteNcCs||_||_||_dSN)mastervaluename)rrrrrrrrsz Var.__init__cCs ||_dSrr)rrrrrsetszVar.setcCs|jSrrrrrrget!szVar.get)NNN)r r 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 NcCs ||_dSr)result)rrrrrr+szMbox_func.__init__cOs||_||_||_||_|jSr)titlemessageargsrr)rrrrrrrr__call__-s zMbox_func.__call__)N)r r 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) r r r r rZ askokcancelZ askquestionZaskretrycancelZaskyesnoZaskyesnocancelZ showerrorZshowinfo 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)rrZcnfkwrrrrgsz Text.__init__cCsd|j|ddS)zAReturn string version of index decoded according to current text.z%s.%s)endflag)_decoderindexrrrr)osz 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%insertend.)r%rz lineendr) isinstancefloatbytesstrlowerAttributeErrorrlenr#_endexsplitintendswith)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)r3r#)rr&nrrrr4s  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%) splitlinesappendr'r#r3)rr)charsr8r9beforeZafterrrrr*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#ranger=join) rindex1index2 startline startcharendlineendcharlinesirrrrszText.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'r3r#r@)rrBrCrDrErFrGrIrrrdeletes&   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)Nz>=z==z!=z=bad comparison operator "%s": must be <, <=, ==, >=, >, or !=)r'r)rrBoprCZline1Zchar1Zline2Zchar2rrrcompares"z Text.comparecCsdS)z.Set mark *name* before the character at index.Nr)rrr)rrrmark_set sz Text.mark_setcGsdS)zDelete all marks in markNames.Nr)rZ markNamesrrr mark_unsetszText.mark_unsetcCsdS)zARemove tag tagName from all characters between index1 and index2.Nr)rZtagNamerBrCrrr tag_removeszText.tag_removecCsdS)z2Adjust the view of the text according to scan_markNrrxyrrr scan_dragtoszText.scan_dragtocCsdS)z&Remember the current X, Y coordinates.NrrRrrr scan_markszText.scan_markcCsdS)z8Scroll screen to make the character at INDEX is visible.Nrr(rrrsee!szText.seecCsdS)z>Bind to this widget at event sequence a call to function func.Nr)Zsequencefuncaddrrrbind(sz Text.bind)r)N)N)N)NNN)r r r r rr)r'r4r*rrJrNrOrPrQrUrVrWrZrrrrr Zs   3   r c@seZdZdZddZdS)EntryzMock for tkinter.Entry.cCsdSrrrrrr focus_set.szEntry.focus_setN)r r r r r\rrrrr[,sr[N) r rr rrZ_tkinterrr r[rrrrs # S