Machinery for tests of the main interact loop. Used by the mock_input decorator.
| 59 | # Decorator for interaction loop tests ----------------------------------------- |
| 60 | |
| 61 | class mock_input_helper(object): |
| 62 | """Machinery for tests of the main interact loop. |
| 63 | |
| 64 | Used by the mock_input decorator. |
| 65 | """ |
| 66 | def __init__(self, testgen): |
| 67 | self.testgen = testgen |
| 68 | self.exception = None |
| 69 | self.ip = get_ipython() |
| 70 | |
| 71 | def __enter__(self): |
| 72 | self.orig_prompt_for_code = self.ip.prompt_for_code |
| 73 | self.ip.prompt_for_code = self.fake_input |
| 74 | return self |
| 75 | |
| 76 | def __exit__(self, etype, value, tb): |
| 77 | self.ip.prompt_for_code = self.orig_prompt_for_code |
| 78 | |
| 79 | def fake_input(self): |
| 80 | try: |
| 81 | return next(self.testgen) |
| 82 | except StopIteration: |
| 83 | self.ip.keep_running = False |
| 84 | return u'' |
| 85 | except: |
| 86 | self.exception = sys.exc_info() |
| 87 | self.ip.keep_running = False |
| 88 | return u'' |
| 89 | |
| 90 | def mock_input(testfunc): |
| 91 | """Decorator for tests of the main interact loop. |