diff options
| author | Thomas Kluyver <takowl@gmail.com> | 2014-07-02 19:22:28 -0700 |
|---|---|---|
| committer | Thomas Kluyver <takowl@gmail.com> | 2014-09-21 12:06:50 -0700 |
| commit | d9fc2cfdcfaf13f2e8491ace60680f3c94ad5c83 (patch) | |
| tree | 976444caeaf5e74316a97f1e6a7fe2dff6baa5f4 /tests | |
| parent | 1215c993193d8d9b535213efe15bc4885670aab3 (diff) | |
| download | pexpect-d9fc2cfdcfaf13f2e8491ace60680f3c94ad5c83.tar.gz | |
Expand tests for async expect
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_async.py | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/tests/test_async.py b/tests/test_async.py index f5ce3d2..bb945a4 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -1,23 +1,42 @@ try: import asyncio - loop = asyncio.get_event_loop() except ImportError: asyncio = None import pexpect import unittest +def run(coro): + return asyncio.get_event_loop().run_until_complete(coro) + @unittest.skipIf(asyncio is None, "Requires asyncio") class AsyncTests(unittest.TestCase): def test_simple_expect(self): p = pexpect.spawn('cat') p.sendline('Hello asyncio') - coro = p.expect('Hello', async=True) - task = asyncio.Task(coro) - results = [] - def complete(task): - results.append(task.result()) - task.add_done_callback(complete) - loop.run_until_complete(task) + coro = p.expect(['Hello', pexpect.EOF] , async=True) + assert run(coro) == 0 + print('Done') + + def test_timeout(self): + p = pexpect.spawn('cat') + coro = p.expect('foo', timeout=1, async=True) + with self.assertRaises(pexpect.TIMEOUT): + run(coro) - assert results == [0]
\ No newline at end of file + p = pexpect.spawn('cat') + coro = p.expect(['foo', pexpect.TIMEOUT], timeout=1, async=True) + assert run(coro) == 1 + + def test_eof(self): + p = pexpect.spawn('cat') + p.sendline('Hi') + coro = p.expect(pexpect.EOF, async=True) + p.sendeof() + assert run(coro) == 0 + + p = pexpect.spawn('cat') + p.sendeof() + coro = p.expect('Blah', async=True) + with self.assertRaises(pexpect.EOF): + run(coro)
\ No newline at end of file |
