From 3c082c3fb0047e5ce17a6c6b1a208cd857bf6a35 Mon Sep 17 00:00:00 2001 From: noah Date: Sun, 16 Dec 2007 20:49:10 +0000 Subject: Added Jorgen's pexpect-487-expect-order.patch git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@493 656d521f-e311-0410-88e0-e7920216d269 --- pexpect/pexpect.py | 10 ++-- pexpect/tests/test_expect.py | 121 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 5 deletions(-) (limited to 'pexpect') diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py index 1670e3f..5abce7d 100644 --- a/pexpect/pexpect.py +++ b/pexpect/pexpect.py @@ -35,7 +35,7 @@ Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin, Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick -Craig-Wood, Andrew Stone (Let me know if I forgot anyone.) +Craig-Wood, Andrew Stone, Jorgen Grahn (Let me know if I forgot anyone.) Free, open source, and all that good stuff. @@ -1267,7 +1267,7 @@ class spawn (object): This method is also useful when you don't want to have to worry about escaping regular expression characters that you want to match.""" - if type(pattern_list) in types.StringTypes + (TIMEOUT, EOF): + if type(pattern_list) in types.StringTypes or pattern_list in (TIMEOUT, EOF): pattern_list = [pattern_list] return self.expect_loop(searcher_string(pattern_list), timeout, searchwindowsize) @@ -1585,16 +1585,16 @@ class searcher_string (object): # or at the very end of the old data offset = -(freshlen+len(s)) else: - # better obey_searchwindowsize + # better obey searchwindowsize offset = -searchwindowsize n = buffer.find(s, offset) if n >= 0 and n <= first_match: # note that the last, not the longest, match rules first_match = n - best_index = index + best_index, best_match = index, s if first_match == absurd_match: return -1 - self.match = self._strings[best_index][1] + self.match = best_match self.start = first_match self.end = self.start + len(self.match) return best_index 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() -- cgit v1.2.1