| 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 |
|
| 8 |
class Func: |
| 9 |
'''Record call, capture args, return/raise result set by test. |
| 10 |
|
| 11 |
When mock function is called, set or use attributes: |
| 12 |
self.called - increment call number even if no args, kwds passed. |
| 13 |
self.args - capture positional arguments. |
| 14 |
self.kwds - capture keyword arguments. |
| 15 |
self.result - return or raise value set in __init__. |
| 16 |
self.return_self - return self instead, to mock query class return. |
| 17 |
|
| 18 |
Most common use will probably be to mock instance methods. |
| 19 |
Given class instance, can set and delete as instance attribute. |
| 20 |
Mock_tk.Var and Mbox_func are special variants of this. |
| 21 |
''' |
| 22 |
def __init__(self, result=None, return_self=False): |
| 23 |
self.called = 0 |
| 24 |
self.result = result |
| 25 |
self.return_self = return_self |
| 26 |
self.args = None |
| 27 |
self.kwds = None |
| 28 |
def __call__(self, *args, **kwds): |
| 29 |
self.called += 1 |
| 30 |
self.args = args |
| 31 |
self.kwds = kwds |
| 32 |
if isinstance(self.result, BaseException): |
| 33 |
raise self.result |
| 34 |
elif self.return_self: |
| 35 |
return self |
| 36 |
else: |
| 37 |
return self.result |
| 38 |
|
| 39 |
|
| 40 |
class Editor: |
| 41 |
'''Minimally imitate editor.EditorWindow class. |
| 42 |
''' |
| 43 |
def __init__(self, flist=None, filename=None, key=None, root=None, |
| 44 |
text=None): # Allow real Text with mock Editor. |
| 45 |
self.text = text or Text() |
| 46 |
self.undo = UndoDelegator() |
| 47 |
|
| 48 |
def get_selection_indices(self): |
| 49 |
first = self.text.index('1.0') |
| 50 |
last = self.text.index('end') |
| 51 |
return first, last |
| 52 |
|
| 53 |
|
| 54 |
class UndoDelegator: |
| 55 |
'''Minimally imitate undo.UndoDelegator class. |
| 56 |
''' |
| 57 |
# A real undo block is only needed for user interaction. |
| 58 |
def undo_block_start(*args): |
| 59 |
pass |
| 60 |
def undo_block_stop(*args): |
| 61 |
pass |