| 1 | n/a | """Classes that replace tkinter gui objects used by an object being tested. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | A gui object is anything with a master or parent parameter, which is |
|---|
| 4 | n/a | typically required in spite of what the doc strings say. |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | class Event: |
|---|
| 8 | n/a | '''Minimal mock with attributes for testing event handlers. |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | This is not a gui object, but is used as an argument for callbacks |
|---|
| 11 | n/a | that access attributes of the event passed. If a callback ignores |
|---|
| 12 | n/a | the event, other than the fact that is happened, pass 'event'. |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | Keyboard, mouse, window, and other sources generate Event instances. |
|---|
| 15 | n/a | Event instances have the following attributes: serial (number of |
|---|
| 16 | n/a | event), time (of event), type (of event as number), widget (in which |
|---|
| 17 | n/a | event occurred), and x,y (position of mouse). There are other |
|---|
| 18 | n/a | attributes for specific events, such as keycode for key events. |
|---|
| 19 | n/a | tkinter.Event.__doc__ has more but is still not complete. |
|---|
| 20 | n/a | ''' |
|---|
| 21 | n/a | def __init__(self, **kwds): |
|---|
| 22 | n/a | "Create event with attributes needed for test" |
|---|
| 23 | n/a | self.__dict__.update(kwds) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | class Var: |
|---|
| 26 | n/a | "Use for String/Int/BooleanVar: incomplete" |
|---|
| 27 | n/a | def __init__(self, master=None, value=None, name=None): |
|---|
| 28 | n/a | self.master = master |
|---|
| 29 | n/a | self.value = value |
|---|
| 30 | n/a | self.name = name |
|---|
| 31 | n/a | def set(self, value): |
|---|
| 32 | n/a | self.value = value |
|---|
| 33 | n/a | def get(self): |
|---|
| 34 | n/a | return self.value |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | class Mbox_func: |
|---|
| 37 | n/a | """Generic mock for messagebox functions, which all have the same signature. |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | Instead of displaying a message box, the mock's call method saves the |
|---|
| 40 | n/a | arguments as instance attributes, which test functions can then examime. |
|---|
| 41 | n/a | The test can set the result returned to ask function |
|---|
| 42 | n/a | """ |
|---|
| 43 | n/a | def __init__(self, result=None): |
|---|
| 44 | n/a | self.result = result # Return None for all show funcs |
|---|
| 45 | n/a | def __call__(self, title, message, *args, **kwds): |
|---|
| 46 | n/a | # Save all args for possible examination by tester |
|---|
| 47 | n/a | self.title = title |
|---|
| 48 | n/a | self.message = message |
|---|
| 49 | n/a | self.args = args |
|---|
| 50 | n/a | self.kwds = kwds |
|---|
| 51 | n/a | return self.result # Set by tester for ask functions |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | class Mbox: |
|---|
| 54 | n/a | """Mock for tkinter.messagebox with an Mbox_func for each function. |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | This module was 'tkMessageBox' in 2.x; hence the 'import as' in 3.x. |
|---|
| 57 | n/a | Example usage in test_module.py for testing functions in module.py: |
|---|
| 58 | n/a | --- |
|---|
| 59 | n/a | from idlelib.idle_test.mock_tk import Mbox |
|---|
| 60 | n/a | import module |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | orig_mbox = module.tkMessageBox |
|---|
| 63 | n/a | showerror = Mbox.showerror # example, for attribute access in test methods |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | class Test(unittest.TestCase): |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | @classmethod |
|---|
| 68 | n/a | def setUpClass(cls): |
|---|
| 69 | n/a | module.tkMessageBox = Mbox |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | @classmethod |
|---|
| 72 | n/a | def tearDownClass(cls): |
|---|
| 73 | n/a | module.tkMessageBox = orig_mbox |
|---|
| 74 | n/a | --- |
|---|
| 75 | n/a | For 'ask' functions, set func.result return value before calling the method |
|---|
| 76 | n/a | that uses the message function. When tkMessageBox functions are the |
|---|
| 77 | n/a | only gui alls in a method, this replacement makes the method gui-free, |
|---|
| 78 | n/a | """ |
|---|
| 79 | n/a | askokcancel = Mbox_func() # True or False |
|---|
| 80 | n/a | askquestion = Mbox_func() # 'yes' or 'no' |
|---|
| 81 | n/a | askretrycancel = Mbox_func() # True or False |
|---|
| 82 | n/a | askyesno = Mbox_func() # True or False |
|---|
| 83 | n/a | askyesnocancel = Mbox_func() # True, False, or None |
|---|
| 84 | n/a | showerror = Mbox_func() # None |
|---|
| 85 | n/a | showinfo = Mbox_func() # None |
|---|
| 86 | n/a | showwarning = Mbox_func() # None |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | from _tkinter import TclError |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | class Text: |
|---|
| 91 | n/a | """A semi-functional non-gui replacement for tkinter.Text text editors. |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | The mock's data model is that a text is a list of \n-terminated lines. |
|---|
| 94 | n/a | The mock adds an empty string at the beginning of the list so that the |
|---|
| 95 | n/a | index of actual lines start at 1, as with Tk. The methods never see this. |
|---|
| 96 | n/a | Tk initializes files with a terminal \n that cannot be deleted. It is |
|---|
| 97 | n/a | invisible in the sense that one cannot move the cursor beyond it. |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | This class is only tested (and valid) with strings of ascii chars. |
|---|
| 100 | n/a | For testing, we are not concerned with Tk Text's treatment of, |
|---|
| 101 | n/a | for instance, 0-width characters or character + accent. |
|---|
| 102 | n/a | """ |
|---|
| 103 | n/a | def __init__(self, master=None, cnf={}, **kw): |
|---|
| 104 | n/a | '''Initialize mock, non-gui, text-only Text widget. |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | At present, all args are ignored. Almost all affect visual behavior. |
|---|
| 107 | n/a | There are just a few Text-only options that affect text behavior. |
|---|
| 108 | n/a | ''' |
|---|
| 109 | n/a | self.data = ['', '\n'] |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def index(self, index): |
|---|
| 112 | n/a | "Return string version of index decoded according to current text." |
|---|
| 113 | n/a | return "%s.%s" % self._decode(index, endflag=1) |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def _decode(self, index, endflag=0): |
|---|
| 116 | n/a | """Return a (line, char) tuple of int indexes into self.data. |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | This implements .index without converting the result back to a string. |
|---|
| 119 | n/a | The result is constrained by the number of lines and linelengths of |
|---|
| 120 | n/a | self.data. For many indexes, the result is initially (1, 0). |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | The input index may have any of several possible forms: |
|---|
| 123 | n/a | * line.char float: converted to 'line.char' string; |
|---|
| 124 | n/a | * 'line.char' string, where line and char are decimal integers; |
|---|
| 125 | n/a | * 'line.char lineend', where lineend='lineend' (and char is ignored); |
|---|
| 126 | n/a | * 'line.end', where end='end' (same as above); |
|---|
| 127 | n/a | * 'insert', the positions before terminal \n; |
|---|
| 128 | n/a | * 'end', whose meaning depends on the endflag passed to ._endex. |
|---|
| 129 | n/a | * 'sel.first' or 'sel.last', where sel is a tag -- not implemented. |
|---|
| 130 | n/a | """ |
|---|
| 131 | n/a | if isinstance(index, (float, bytes)): |
|---|
| 132 | n/a | index = str(index) |
|---|
| 133 | n/a | try: |
|---|
| 134 | n/a | index=index.lower() |
|---|
| 135 | n/a | except AttributeError: |
|---|
| 136 | n/a | raise TclError('bad text index "%s"' % index) from None |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | lastline = len(self.data) - 1 # same as number of text lines |
|---|
| 139 | n/a | if index == 'insert': |
|---|
| 140 | n/a | return lastline, len(self.data[lastline]) - 1 |
|---|
| 141 | n/a | elif index == 'end': |
|---|
| 142 | n/a | return self._endex(endflag) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | line, char = index.split('.') |
|---|
| 145 | n/a | line = int(line) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | # Out of bounds line becomes first or last ('end') index |
|---|
| 148 | n/a | if line < 1: |
|---|
| 149 | n/a | return 1, 0 |
|---|
| 150 | n/a | elif line > lastline: |
|---|
| 151 | n/a | return self._endex(endflag) |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | linelength = len(self.data[line]) -1 # position before/at \n |
|---|
| 154 | n/a | if char.endswith(' lineend') or char == 'end': |
|---|
| 155 | n/a | return line, linelength |
|---|
| 156 | n/a | # Tk requires that ignored chars before ' lineend' be valid int |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | # Out of bounds char becomes first or last index of line |
|---|
| 159 | n/a | char = int(char) |
|---|
| 160 | n/a | if char < 0: |
|---|
| 161 | n/a | char = 0 |
|---|
| 162 | n/a | elif char > linelength: |
|---|
| 163 | n/a | char = linelength |
|---|
| 164 | n/a | return line, char |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | def _endex(self, endflag): |
|---|
| 167 | n/a | '''Return position for 'end' or line overflow corresponding to endflag. |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | -1: position before terminal \n; for .insert(), .delete |
|---|
| 170 | n/a | 0: position after terminal \n; for .get, .delete index 1 |
|---|
| 171 | n/a | 1: same viewed as beginning of non-existent next line (for .index) |
|---|
| 172 | n/a | ''' |
|---|
| 173 | n/a | n = len(self.data) |
|---|
| 174 | n/a | if endflag == 1: |
|---|
| 175 | n/a | return n, 0 |
|---|
| 176 | n/a | else: |
|---|
| 177 | n/a | n -= 1 |
|---|
| 178 | n/a | return n, len(self.data[n]) + endflag |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def insert(self, index, chars): |
|---|
| 182 | n/a | "Insert chars before the character at index." |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | if not chars: # ''.splitlines() is [], not [''] |
|---|
| 185 | n/a | return |
|---|
| 186 | n/a | chars = chars.splitlines(True) |
|---|
| 187 | n/a | if chars[-1][-1] == '\n': |
|---|
| 188 | n/a | chars.append('') |
|---|
| 189 | n/a | line, char = self._decode(index, -1) |
|---|
| 190 | n/a | before = self.data[line][:char] |
|---|
| 191 | n/a | after = self.data[line][char:] |
|---|
| 192 | n/a | self.data[line] = before + chars[0] |
|---|
| 193 | n/a | self.data[line+1:line+1] = chars[1:] |
|---|
| 194 | n/a | self.data[line+len(chars)-1] += after |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | def get(self, index1, index2=None): |
|---|
| 198 | n/a | "Return slice from index1 to index2 (default is 'index1+1')." |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | startline, startchar = self._decode(index1) |
|---|
| 201 | n/a | if index2 is None: |
|---|
| 202 | n/a | endline, endchar = startline, startchar+1 |
|---|
| 203 | n/a | else: |
|---|
| 204 | n/a | endline, endchar = self._decode(index2) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | if startline == endline: |
|---|
| 207 | n/a | return self.data[startline][startchar:endchar] |
|---|
| 208 | n/a | else: |
|---|
| 209 | n/a | lines = [self.data[startline][startchar:]] |
|---|
| 210 | n/a | for i in range(startline+1, endline): |
|---|
| 211 | n/a | lines.append(self.data[i]) |
|---|
| 212 | n/a | lines.append(self.data[endline][:endchar]) |
|---|
| 213 | n/a | return ''.join(lines) |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | def delete(self, index1, index2=None): |
|---|
| 217 | n/a | '''Delete slice from index1 to index2 (default is 'index1+1'). |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | Adjust default index2 ('index+1) for line ends. |
|---|
| 220 | n/a | Do not delete the terminal \n at the very end of self.data ([-1][-1]). |
|---|
| 221 | n/a | ''' |
|---|
| 222 | n/a | startline, startchar = self._decode(index1, -1) |
|---|
| 223 | n/a | if index2 is None: |
|---|
| 224 | n/a | if startchar < len(self.data[startline])-1: |
|---|
| 225 | n/a | # not deleting \n |
|---|
| 226 | n/a | endline, endchar = startline, startchar+1 |
|---|
| 227 | n/a | elif startline < len(self.data) - 1: |
|---|
| 228 | n/a | # deleting non-terminal \n, convert 'index1+1 to start of next line |
|---|
| 229 | n/a | endline, endchar = startline+1, 0 |
|---|
| 230 | n/a | else: |
|---|
| 231 | n/a | # do not delete terminal \n if index1 == 'insert' |
|---|
| 232 | n/a | return |
|---|
| 233 | n/a | else: |
|---|
| 234 | n/a | endline, endchar = self._decode(index2, -1) |
|---|
| 235 | n/a | # restricting end position to insert position excludes terminal \n |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | if startline == endline and startchar < endchar: |
|---|
| 238 | n/a | self.data[startline] = self.data[startline][:startchar] + \ |
|---|
| 239 | n/a | self.data[startline][endchar:] |
|---|
| 240 | n/a | elif startline < endline: |
|---|
| 241 | n/a | self.data[startline] = self.data[startline][:startchar] + \ |
|---|
| 242 | n/a | self.data[endline][endchar:] |
|---|
| 243 | n/a | startline += 1 |
|---|
| 244 | n/a | for i in range(startline, endline+1): |
|---|
| 245 | n/a | del self.data[startline] |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | def compare(self, index1, op, index2): |
|---|
| 248 | n/a | line1, char1 = self._decode(index1) |
|---|
| 249 | n/a | line2, char2 = self._decode(index2) |
|---|
| 250 | n/a | if op == '<': |
|---|
| 251 | n/a | return line1 < line2 or line1 == line2 and char1 < char2 |
|---|
| 252 | n/a | elif op == '<=': |
|---|
| 253 | n/a | return line1 < line2 or line1 == line2 and char1 <= char2 |
|---|
| 254 | n/a | elif op == '>': |
|---|
| 255 | n/a | return line1 > line2 or line1 == line2 and char1 > char2 |
|---|
| 256 | n/a | elif op == '>=': |
|---|
| 257 | n/a | return line1 > line2 or line1 == line2 and char1 >= char2 |
|---|
| 258 | n/a | elif op == '==': |
|---|
| 259 | n/a | return line1 == line2 and char1 == char2 |
|---|
| 260 | n/a | elif op == '!=': |
|---|
| 261 | n/a | return line1 != line2 or char1 != char2 |
|---|
| 262 | n/a | else: |
|---|
| 263 | n/a | raise TclError('''bad comparison operator "%s":''' |
|---|
| 264 | n/a | '''must be <, <=, ==, >=, >, or !=''' % op) |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | # The following Text methods normally do something and return None. |
|---|
| 267 | n/a | # Whether doing nothing is sufficient for a test will depend on the test. |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def mark_set(self, name, index): |
|---|
| 270 | n/a | "Set mark *name* before the character at index." |
|---|
| 271 | n/a | pass |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def mark_unset(self, *markNames): |
|---|
| 274 | n/a | "Delete all marks in markNames." |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | def tag_remove(self, tagName, index1, index2=None): |
|---|
| 277 | n/a | "Remove tag tagName from all characters between index1 and index2." |
|---|
| 278 | n/a | pass |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | # The following Text methods affect the graphics screen and return None. |
|---|
| 281 | n/a | # Doing nothing should always be sufficient for tests. |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def scan_dragto(self, x, y): |
|---|
| 284 | n/a | "Adjust the view of the text according to scan_mark" |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | def scan_mark(self, x, y): |
|---|
| 287 | n/a | "Remember the current X, Y coordinates." |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | def see(self, index): |
|---|
| 290 | n/a | "Scroll screen to make the character at INDEX is visible." |
|---|
| 291 | n/a | pass |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | # The following is a Misc method inherited by Text. |
|---|
| 294 | n/a | # It should properly go in a Misc mock, but is included here for now. |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | def bind(sequence=None, func=None, add=None): |
|---|
| 297 | n/a | "Bind to this widget at event sequence a call to function func." |
|---|
| 298 | n/a | pass |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | class Entry: |
|---|
| 301 | n/a | "Mock for tkinter.Entry." |
|---|
| 302 | n/a | def focus_set(self): |
|---|
| 303 | n/a | pass |
|---|