mock_idle.py 1.9 KB

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