summaryrefslogtreecommitdiff
path: root/tests/test_ansi.py
diff options
context:
space:
mode:
authorDavid O'Shea <doshea@doshea-centos-x86-64.adl.quantum.com>2014-07-08 14:22:54 +0930
committerDavid O'Shea <doshea@doshea-centos-x86-64.adl.quantum.com>2014-07-08 14:26:24 +0930
commitfbfcfe3a02715c9d54b19c3ea5934b50984071e9 (patch)
tree63fbc9b0c2ca0edddb90361be48deb956f0f1710 /tests/test_ansi.py
parentcfca6f309c1c8109bd3ad023c6382849fb5c6632 (diff)
downloadpexpect-git-fbfcfe3a02715c9d54b19c3ea5934b50984071e9.tar.gz
Issue #85: Don't leave unwanted numbers in ANSI FSM stack/memory.
The FSM transitions set up by ANSI accept a number of escape sequences that accept one or more numeric parameters where the escape sequence as a whole is ignored (no action is executed at the end of the sequence). This means that the numeric parameters are left on the FSM's stack, making it harder to improve or subclass ANSI to handle any of the sequences that take multiple numeric parameters, where it is necessary to look at the entire stack rather than just, say, pop one element off the top of the stack. This fix sets up handlers that discard all items on the stack other than the bottom-most, which is a reference to the ANSI instance itself. The handlers are created as members of ANSI, rather than methods in the ANSI package, so that they can be overridden in subclasses without needing to replace FSM transitions. A unit test is added to verify the new behavior, and the existing test_number_x() unit test is modified to no longer expect the parameters to be left on the stack after parsing, and instead use a subclass of ANSI to capture them.
Diffstat (limited to 'tests/test_ansi.py')
-rwxr-xr-xtests/test_ansi.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/tests/test_ansi.py b/tests/test_ansi.py
index 3b8d6a9..26163a6 100755
--- a/tests/test_ansi.py
+++ b/tests/test_ansi.py
@@ -142,10 +142,25 @@ class ansiTestCase (PexpectTestCase.PexpectTestCase):
def test_number_x(self):
"""Test the FSM state used to handle more than 2 numeric parameters."""
- s = ANSI.ANSI(1, 20)
+ class TestANSI(ANSI.ANSI):
+ captured_memory = None
+ def do_sgr(self, fsm):
+ assert self.captured_memory is None
+ self.captured_memory = fsm.memory
+
+ s = TestANSI(1, 20)
s.write('\x1b[0;1;32;45mtest')
assert str(s) == ('test ')
- assert(s.state.memory == [s, '0', '1', '32', '45'])
+ assert s.captured_memory is not None
+ assert(s.captured_memory == [s, '0', '1', '32', '45'])
+
+ def test_fsm_memory(self):
+ """Test the FSM stack/memory does not have numbers left on it
+ after some sequences with numbers are passed in."""
+ s = ANSI.ANSI(1, 20)
+ s.write('\x1b[0;1;2;3m\x1b[4;5;6;7q\x1b[?8h\x1b[?9ltest')
+ assert str(s) == ('test ')
+ assert(s.state.memory == [s])
if __name__ == '__main__':
unittest.main()