a  ze-@spdZddlZddlmZGdddZGdddZGdd d ZGd d d ZGd d d ZGdddZ dS)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. N)TclErrorc@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)selfkwdsrM/opt/bitninja-python-dojo/embedded/lib/python3.9/idlelib/idle_test/mock_tk.py__init__szEvent.__init__N)__name__ __module__ __qualname____doc__r rrrr r s rc@s*eZdZdZd ddZddZddZdS) Varz)Use for String/Int/BooleanVar: incompleteNcCs||_||_||_dSN)mastervaluename)rrrrrrr r sz Var.__init__cCs ||_dSrr)rrrrr set#szVar.setcCs|jSrrrrrr get%szVar.get)NNN)r r r rr rrrrrr rs rc@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)rrrrr r 0szMbox_func.__init__cOs||_||_||_||_|jSr)titlemessageargsrr)rrrrrrrr __call__2s zMbox_func.__call__)N)r r r rr rrrrr r)s rc@s@eZdZdZeZeZeZeZeZ eZ eZ eZ dS)MboxaMock for tkinter.messagebox with an Mbox_func for each function. 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.messagebox showerror = Mbox.showerror # example, for attribute access in test methods class Test(unittest.TestCase): @classmethod def setUpClass(cls): module.messagebox = Mbox @classmethod def tearDownClass(cls): module.messagebox = orig_mbox --- For 'ask' functions, set func.result return value before calling the method that uses the message function. When messagebox functions are the only GUI calls in a method, this replacement makes the method GUI-free, N) r r r rrZ askokcancelZ askquestionZaskretrycancelZaskyesnoZaskyesnocancelZ showerrorZshowinfo showwarningrrrr r;src@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)rrZcnfkwrrr r ksz Text.__init__cCsd|j|ddS)zAReturn string version of index decoded according to current text.z%s.%s)endflag)_decoderindexrrr r)ssz Text.indexrcCs<t|ttfrt|}z |}Wn tyBtd|dYn0t|jd}|dkrp|t|j|dfS|dkr| |S| d\}}t |}|dkrdS||kr| |St|j|d}| ds|dkr||fSt d |t j}r||t |dfSt |}|d kr&d }n||kr4|}||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 lineendz end-(\d*)cr) isinstancefloatbytesstrlowerAttributeErrorrlenr#_endexsplitintendswithre fullmatchAgroup)rr)r&ZlastlinelinecharZ linelengthmrrr r'ws8      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&nrrr r4s  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)charsr<r=beforeZafterrrr r*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#rangerBjoin) rindex1index2 startline startcharendlineendcharlinesirrr rszText.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#rE)rrGrHrIrJrKrLrNrrr deletes&   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)rrGoprHZline1Zchar1Zline2Zchar2rrr compares"z Text.comparecCsdS)z.Set mark *name* before the character at index.Nr)rrr)rrr mark_setsz 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)rZtagNamerGrHrrr 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.NrrWrrr scan_mark!szText.scan_markcCsdS)z8Scroll screen to make the character at INDEX is visible.Nrr(rrr see$szText.seecCsdS)z>Bind to this widget at event sequence a call to function func.Nr)Zsequencefuncaddrrr bind+sz Text.bind)r)N)N)N)NNN)r r r rr r)r'r4r*rrOrSrTrUrVrZr[r\r_rrrr r ^s   5   r c@seZdZdZddZdS)EntryzMock for tkinter.Entry.cCsdSrrrrrr focus_set2szEntry.focus_setN)r r r rrarrrr r`0sr`) rr8Z_tkinterrrrrrr r`rrrr s  #S