import sys import unittest import pexpect class TestInteractiveInterpreter(unittest.TestCase): def assertGotExpected(self, expected, msg=None): try: self.child.expect(expected) except pexpect.TIMEOUT: self.fail(msg) def assertGotExpectedExact(self, expected, msg=None): try: self.child.expect_exact(expected) except pexpect.TIMEOUT: self.fail(msg) def setUp(self): # XXX: should be sys.executable in the real thing shell_cmd = './python.exe' self.child = pexpect.spawn('bash', timeout=30, env={'PS1':'$ '}, ) self.child.expect_exact('$') self.child.sendline(shell_cmd) def tearDown(self): self.child.close() def test_ctrl_c(self): self.child.expect_exact('>>> ') self.child.sendcontrol('c') self.assertGotExpectedExact('KeyboardInterrupt') def test_ctrl_z(self): self.child.expect_exact('>>> ') self.child.sendline('expect_var = "EXPECT_THIS"') self.child.expect_exact('>>> ') self.child.sendcontrol('z') self.child.expect_exact('Stopped') self.child.sendline('fg') self.child.sendline('print(expect_var)') self.assertGotExpectedExact('EXPECT_THIS') self.child.expect_exact('>>> ') def test_eof(self): self.child.sendeof() self.assertGotExpectedExact('$') if __name__ == '__main__': unittest.main()