Execute code via 'cpaste' and ensure it was executed, unless should_fail is set.
(code, should_fail=False)
| 20 | #----------------------------------------------------------------------------- |
| 21 | |
| 22 | def check_cpaste(code, should_fail=False): |
| 23 | """Execute code via 'cpaste' and ensure it was executed, unless |
| 24 | should_fail is set. |
| 25 | """ |
| 26 | ip.user_ns['code_ran'] = False |
| 27 | |
| 28 | src = StringIO() |
| 29 | if not hasattr(src, 'encoding'): |
| 30 | # IPython expects stdin to have an encoding attribute |
| 31 | src.encoding = None |
| 32 | src.write(code) |
| 33 | src.write('\n--\n') |
| 34 | src.seek(0) |
| 35 | |
| 36 | stdin_save = sys.stdin |
| 37 | sys.stdin = src |
| 38 | |
| 39 | try: |
| 40 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints |
| 41 | with context("Traceback (most recent call last)"): |
| 42 | ip.magic('cpaste') |
| 43 | |
| 44 | if not should_fail: |
| 45 | assert ip.user_ns['code_ran'], "%r failed" % code |
| 46 | finally: |
| 47 | sys.stdin = stdin_save |
| 48 | |
| 49 | def test_cpaste(): |
| 50 | """Test cpaste magic""" |
no test coverage detected