Execute code via 'cpaste' and ensure it was executed, unless should_fail is set.
(code, should_fail=False)
| 41 | |
| 42 | |
| 43 | def check_cpaste(code, should_fail=False): |
| 44 | """Execute code via 'cpaste' and ensure it was executed, unless |
| 45 | should_fail is set. |
| 46 | """ |
| 47 | ip.user_ns["code_ran"] = False |
| 48 | |
| 49 | src = StringIO() |
| 50 | src.write(code) |
| 51 | src.write("\n--\n") |
| 52 | src.seek(0) |
| 53 | |
| 54 | stdin_save = sys.stdin |
| 55 | sys.stdin = src |
| 56 | |
| 57 | try: |
| 58 | context = tt.AssertPrints if should_fail else tt.AssertNotPrints |
| 59 | with context("Traceback (most recent call last)"): |
| 60 | ip.run_line_magic("cpaste", "") |
| 61 | |
| 62 | if not should_fail: |
| 63 | assert ip.user_ns["code_ran"], "%r failed" % code |
| 64 | finally: |
| 65 | sys.stdin = stdin_save |
| 66 | |
| 67 | |
| 68 | def test_cpaste(): |
no test coverage detected
searching dependent graphs…