diff options
Diffstat (limited to 'pexpect/tests/test_expect.py')
-rwxr-xr-x | pexpect/tests/test_expect.py | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/pexpect/tests/test_expect.py b/pexpect/tests/test_expect.py index c4547c4..7ded07b 100755 --- a/pexpect/tests/test_expect.py +++ b/pexpect/tests/test_expect.py @@ -34,6 +34,17 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): p.sendeof () p.expect (pexpect.EOF) + def test_expect_exact_basic (self): + p = pexpect.spawn('cat') + p.sendline ('Hello') + p.sendline ('there') + p.sendline ('Mr. Python') + p.expect_exact ('Hello') + p.expect_exact ('there') + p.expect_exact ('Mr. Python') + p.sendeof () + p.expect_exact (pexpect.EOF) + def test_expect_ignore_case(self): """This test that the ignorecase flag will match patterns even if case is different using the regex (?i) directive. @@ -61,8 +72,21 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_expect_order (self): """This tests that patterns are matched in the same order as given in the pattern_list. + + (Or does it? Doesn't it also pass if expect() always chooses + (one of the) the leftmost matches in the input? -- grahn) + """ + p = pexpect.spawn('cat') + self._expect_order(p) + + def test_expect_order_exact (self): + """Like test_expect_order(), but using expect_exact(). """ p = pexpect.spawn('cat') + p.expect = p.expect_exact + self._expect_order(p) + + def _expect_order (self, p): p.sendline ('1234') p.sendline ('abcd') p.sendline ('wxyz') @@ -89,6 +113,16 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): """This tests that echo can be turned on and off. """ p = pexpect.spawn('cat', timeout=10) + self._expect_echo(p) + + def test_expect_echo_exact (self): + """Like test_expect_echo(), but using expect_exact(). + """ + p = pexpect.spawn('cat', timeout=10) + p.expect = p.expect_exact + self._expect_echo(p) + + def _expect_echo (self, p): p.sendline ('1234') # Should see this twice (once from tty echo and again from cat). index = p.expect (['1234','abcd','wxyz',pexpect.EOF,pexpect.TIMEOUT]) assert index == 0, "index="+str(index)+"\n"+p.before @@ -115,6 +149,16 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): """ #pdb.set_trace() p = pexpect.spawn('cat') + self._expect_index(p) + + def test_expect_index_exact (self): + """Like test_expect_index(), but using expect_exact(). + """ + p = pexpect.spawn('cat') + p.expect = p.expect_exact + self._expect_index(p) + + def _expect_index (self, p): p.setecho(0) p.sendline ('1234') index = p.expect (['abcd','wxyz','1234',pexpect.EOF]) @@ -186,6 +230,83 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): else: self.fail ('Expected an EOF exception.') +class AdditionalExpectTestCase (PexpectTestCase.PexpectTestCase): + + def _before_after(self, p): + p.timeout = 5 + + p.expect('>>> ') + self.assertEqual(p.after, '>>> ') + self.assert_(p.before.startswith('Python ')) + + p.sendline('range(4*3)') + + p.expect('5') + self.assertEqual(p.after, '5') + self.assert_(p.before.startswith('range(4*3)')) + + p.expect('>>> ') + self.assertEqual(p.after, '>>> ') + self.assert_(p.before.startswith(', 6, 7, 8')) + + def test_before_after(self): + """This tests expect() for some simple before/after things. + """ + p = pexpect.spawn(self.PYTHONBIN) + self._before_after(p) + + def test_before_after_exact(self): + """This tests some simple before/after things, for + expect_exact(). (Grahn broke it at one point.) + """ + p = pexpect.spawn(self.PYTHONBIN) + # mangle the spawn so we test expect_exact() instead + p.expect = p.expect_exact + self._before_after(p) + + def _ordering(self, p): + p.timeout = 5 + p.expect('>>> ') + + p.sendline('range(4*3)') + self.assertEqual(p.expect(['5,', '5,']), 0) + p.expect('>>> ') + + p.sendline('range(4*3)') + self.assertEqual(p.expect(['7,', '5,']), 1) + p.expect('>>> ') + + p.sendline('range(4*3)') + self.assertEqual(p.expect(['5,', '7,']), 0) + p.expect('>>> ') + + p.sendline('range(4*5)') + self.assertEqual(p.expect(['2,', '12,']), 0) + p.expect('>>> ') + + p.sendline('range(4*5)') + self.assertEqual(p.expect(['12,', '2,']), 1) + + def test_ordering(self): + """This tests expect() for which pattern is returned + when many may eventually match. I (Grahn) am a bit + confused about what should happen, but this test passes + with pexpect 2.1. + """ + p = pexpect.spawn(self.PYTHONBIN) + self._ordering(p) + + def test_ordering_exact(self): + """This tests expect_exact() for which pattern is returned + when many may eventually match. I (Grahn) am a bit + confused about what should happen, but this test passes + for the expect() method with pexpect 2.1. + """ + p = pexpect.spawn(self.PYTHONBIN) + # mangle the spawn so we test expect_exact() instead + p.expect = p.expect_exact + self._ordering(p) + if __name__ == '__main__': unittest.main() |