1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
11
# the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent
12
_CAT_EOF = '^D\x08\x08'
14
class UnicodeTests(PexpectTestCase.PexpectTestCase):
15
def test_expect_basic (self):
16
p = pexpect.spawnu('cat')
19
p.sendline('Mr. þython') # þ is more like th than p, but never mind
22
p.expect('Mr. þython')
24
p.expect (pexpect.EOF)
26
def test_expect_exact_basic (self):
27
p = pexpect.spawnu('cat')
30
p.sendline('Mr. þython')
31
p.expect_exact('Hello')
32
p.expect_exact('there')
33
p.expect_exact('Mr. þython')
35
p.expect_exact (pexpect.EOF)
37
def test_expect_echo (self):
38
'''This tests that echo can be turned on and off.
40
p = pexpect.spawnu('cat', timeout=10)
43
def test_expect_echo_exact (self):
44
'''Like test_expect_echo(), but using expect_exact().
46
p = pexpect.spawnu('cat', timeout=10)
47
p.expect = p.expect_exact
50
def _expect_echo (self, p):
51
p.sendline('1234') # Should see this twice (once from tty echo and again from cat).
52
index = p.expect (['1234', 'abcdé', 'wxyz', pexpect.EOF, pexpect.TIMEOUT])
53
assert index == 0, (index, p.before)
54
index = p.expect (['1234', 'abcdé', 'wxyz', pexpect.EOF])
55
assert index == 0, index
56
p.setecho(0) # Turn off tty echo
57
p.sendline('abcdé') # Now, should only see this once.
58
p.sendline('wxyz') # Should also be only once.
59
index = p.expect ([pexpect.EOF,pexpect.TIMEOUT, 'abcdé', 'wxyz', '1234'])
60
assert index == 2, index
61
index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
62
assert index == 2, index
63
p.setecho(1) # Turn on tty echo
64
p.sendline('7890') # Should see this twice.
65
index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
66
assert index == 3, index
67
index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
68
assert index == 3, index
71
def test_log_unicode(self):
73
filename_send = tempfile.mktemp()
74
filename_read = tempfile.mktemp()
75
p = pexpect.spawnu('cat')
76
if platform.python_version_tuple() < ('3', '0', '0'):
78
def open(fname, mode, **kwargs):
79
if 'newline' in kwargs:
81
return codecs.open(fname, mode, **kwargs)
86
p.logfile_send = open(filename_send, 'w', encoding='utf-8')
87
p.logfile_read = open(filename_read, 'w', encoding='utf-8')
92
p.logfile_send.close()
93
p.logfile_read.close()
95
# ensure the 'send' log is correct,
96
with open(filename_send, 'r', encoding='utf-8') as f:
97
self.assertEqual(f.read(), msg + '\n\x04')
99
# ensure the 'read' log is correct,
100
with open(filename_read, 'r', encoding='utf-8', newline='') as f:
101
output = f.read().replace(_CAT_EOF, '')
102
self.assertEqual(output, (msg + '\r\n')*2 )
105
def test_spawn_expect_ascii_unicode(self):
106
# A bytes-based spawn should be able to handle ASCII-only unicode, for
107
# backwards compatibility.
108
p = pexpect.spawn('cat')
109
p.sendline('Camelot')
114
p.expect_exact('Aargh')
117
p.expect(pexpect.EOF)
119
def test_spawn_send_unicode(self):
120
# A bytes-based spawn should be able to send arbitrary unicode
121
p = pexpect.spawn('cat')
124
p.expect(pexpect.EOF)
126
def test_spawn_utf8_incomplete(self):
127
# This test case ensures correct incremental decoding, which
128
# otherwise fails when the stream inspected by os.read()
129
# does not align exactly at a utf-8 multibyte boundry:
130
# UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in
131
# position 0: unexpected end of data
132
p = pexpect.spawnu('cat', maxread=1)
133
p.sendline('▁▂▃▄▅▆▇█')
138
if __name__ == '__main__':
141
suite = unittest.makeSuite(UnicodeTests, 'test')