Coverage for idle_test/mock_idle.py: 68%
30 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
1'''Mock classes that imitate idlelib modules or classes.
3Attributes and methods will be added as needed for tests.
4'''
6from idlelib.idle_test.mock_tk import Text
8class Func:
9 '''Record call, capture args, return/raise result set by test.
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.
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 1abcd
24 self.result = result 1abcd
25 self.return_self = return_self 1abcd
26 self.args = None 1abcd
27 self.kwds = None 1abcd
28 def __call__(self, *args, **kwds):
29 self.called += 1 1bcefd
30 self.args = args 1bcefd
31 self.kwds = kwds 1bcefd
32 if isinstance(self.result, BaseException): 32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true1bcefd
33 raise self.result
34 elif self.return_self: 34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never true1bcefd
35 return self
36 else:
37 return self.result 1bcefd
40class 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()
48 def get_selection_indices(self):
49 first = self.text.index('1.0')
50 last = self.text.index('end')
51 return first, last
54class 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