Terry Jan Reedy | 4f133e2 | 2013-07-13 02:34:43 -0400 | [diff] [blame] | 1 | '''Mock classes that imitate idlelib modules or classes. |
| 2 | |
| 3 | Attributes and methods will be added as needed for tests. |
| 4 | ''' |
| 5 | |
| 6 | from idlelib.idle_test.mock_tk import Text |
| 7 | |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 8 | class Func: |
| 9 | '''Mock function captures args and returns result set by test. |
| 10 | |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 11 | Attributes: |
| 12 | self.called - records call even if no args, kwds passed. |
| 13 | self.result - set by init, returned by call. |
| 14 | self.args - captures positional arguments. |
| 15 | self.kwds - captures keyword arguments. |
| 16 | |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 17 | Most common use will probably be to mock methods. |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 18 | Mock_tk.Var and Mbox_func are special variants of this. |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 19 | ''' |
| 20 | def __init__(self, result=None): |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 21 | self.called = False |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 22 | self.result = result |
| 23 | self.args = None |
| 24 | self.kwds = None |
| 25 | def __call__(self, *args, **kwds): |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 26 | self.called = True |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 27 | self.args = args |
| 28 | self.kwds = kwds |
Terry Jan Reedy | 223dd8d | 2014-07-11 00:16:00 -0400 | [diff] [blame] | 29 | if isinstance(self.result, BaseException): |
| 30 | raise self.result |
| 31 | else: |
| 32 | return self.result |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 33 | |
| 34 | |
Terry Jan Reedy | 4f133e2 | 2013-07-13 02:34:43 -0400 | [diff] [blame] | 35 | class Editor: |
| 36 | '''Minimally imitate EditorWindow.EditorWindow class. |
| 37 | ''' |
| 38 | def __init__(self, flist=None, filename=None, key=None, root=None): |
| 39 | self.text = Text() |
| 40 | self.undo = UndoDelegator() |
| 41 | |
| 42 | def get_selection_indices(self): |
| 43 | first = self.text.index('1.0') |
| 44 | last = self.text.index('end') |
| 45 | return first, last |
| 46 | |
Terry Jan Reedy | e3fcfc2 | 2014-06-03 20:54:21 -0400 | [diff] [blame] | 47 | |
Terry Jan Reedy | 4f133e2 | 2013-07-13 02:34:43 -0400 | [diff] [blame] | 48 | class UndoDelegator: |
| 49 | '''Minimally imitate UndoDelegator,UndoDelegator class. |
| 50 | ''' |
| 51 | # A real undo block is only needed for user interaction. |
| 52 | def undo_block_start(*args): |
| 53 | pass |
| 54 | def undo_block_stop(*args): |
| 55 | pass |