OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ''' |
| 3 PEXPECT LICENSE |
| 4 |
| 5 This license is approved by the OSI and FSF as GPL-compatible. |
| 6 http://opensource.org/licenses/isc-license.txt |
| 7 |
| 8 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 |
| 20 ''' |
| 21 from __future__ import print_function |
| 22 |
| 23 import pexpect |
| 24 import unittest |
| 25 from . import PexpectTestCase |
| 26 import time |
| 27 import sys |
| 28 |
| 29 from ptyprocess import ptyprocess |
| 30 ptyprocess._make_eof_intr() |
| 31 |
| 32 if sys.version_info[0] >= 3: |
| 33 def byte(i): |
| 34 return bytes([i]) |
| 35 else: |
| 36 byte = chr |
| 37 |
| 38 class TestCtrlChars(PexpectTestCase.PexpectTestCase): |
| 39 |
| 40 def test_control_chars(self): |
| 41 '''This tests that we can send all 256 8-bit characters to a child |
| 42 process.''' |
| 43 child = pexpect.spawn('python getch.py', echo=False, timeout=5) |
| 44 child.expect('READY') |
| 45 for i in range(1, 256): |
| 46 child.send(byte(i)) |
| 47 child.expect ('%d<STOP>' % (i,)) |
| 48 |
| 49 # This needs to be last, as getch.py exits on \x00 |
| 50 child.send(byte(0)) |
| 51 child.expect('0<STOP>') |
| 52 child.expect(pexpect.EOF) |
| 53 assert not child.isalive() |
| 54 assert child.exitstatus == 0 |
| 55 |
| 56 def test_sendintr (self): |
| 57 child = pexpect.spawn('python getch.py', echo=False, timeout=5) |
| 58 child.expect('READY') |
| 59 child.sendintr() |
| 60 child.expect(str(ord(ptyprocess._INTR)) + '<STOP>') |
| 61 |
| 62 child.send(byte(0)) |
| 63 child.expect('0<STOP>') |
| 64 child.expect(pexpect.EOF) |
| 65 assert not child.isalive() |
| 66 assert child.exitstatus == 0 |
| 67 |
| 68 def test_sendeof(self): |
| 69 child = pexpect.spawn('python getch.py', echo=False, timeout=5) |
| 70 child.expect('READY') |
| 71 child.sendeof() |
| 72 child.expect(str(ord(ptyprocess._EOF)) + '<STOP>') |
| 73 |
| 74 child.send(byte(0)) |
| 75 child.expect('0<STOP>') |
| 76 child.expect(pexpect.EOF) |
| 77 assert not child.isalive() |
| 78 assert child.exitstatus == 0 |
| 79 |
| 80 def test_bad_sendcontrol_chars (self): |
| 81 '''This tests that sendcontrol will return 0 for an unknown char. ''' |
| 82 |
| 83 child = pexpect.spawn('python getch.py', echo=False, timeout=5) |
| 84 child.expect('READY') |
| 85 assert 0 == child.sendcontrol('1') |
| 86 |
| 87 def test_sendcontrol(self): |
| 88 '''This tests that we can send all special control codes by name. |
| 89 ''' |
| 90 child = pexpect.spawn('python getch.py', echo=False, timeout=5) |
| 91 child.expect('READY') |
| 92 for ctrl in 'abcdefghijklmnopqrstuvwxyz': |
| 93 assert child.sendcontrol(ctrl) == 1 |
| 94 val = ord(ctrl) - ord('a') + 1 |
| 95 child.expect_exact(str(val)+'<STOP>') |
| 96 |
| 97 # escape character |
| 98 assert child.sendcontrol('[') == 1 |
| 99 child.expect('27<STOP>') |
| 100 assert child.sendcontrol('\\') == 1 |
| 101 child.expect('28<STOP>') |
| 102 # telnet escape character |
| 103 assert child.sendcontrol(']') == 1 |
| 104 child.expect('29<STOP>') |
| 105 assert child.sendcontrol('^') == 1 |
| 106 child.expect('30<STOP>') |
| 107 # irc protocol uses this to underline ... |
| 108 assert child.sendcontrol('_') == 1 |
| 109 child.expect('31<STOP>') |
| 110 # the real "backspace is delete" |
| 111 assert child.sendcontrol('?') == 1 |
| 112 child.expect('127<STOP>') |
| 113 |
| 114 # NUL, same as ctrl + ' ' |
| 115 assert child.sendcontrol('@') == 1 |
| 116 child.expect('0<STOP>') |
| 117 child.expect(pexpect.EOF) |
| 118 assert not child.isalive() |
| 119 assert child.exitstatus == 0 |
| 120 |
| 121 if __name__ == '__main__': |
| 122 unittest.main() |
| 123 |
| 124 suite = unittest.makeSuite(TestCtrlChars,'test') |
| 125 |
OLD | NEW |