Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: third_party/pexpect/tests/test_unicode.py

Issue 1398903002: Add third_party/pexpect (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@end-to-end-test
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import platform
5 import tempfile
6 import sys
7 import time
8
9 import pexpect
10 import unittest
11 from . import PexpectTestCase
12
13 # the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent
14 _CAT_EOF = '^D\x08\x08'
15
16 class UnicodeTests(PexpectTestCase.PexpectTestCase):
17 def test_expect_basic (self):
18 p = pexpect.spawnu('cat')
19 p.sendline('Hello')
20 p.sendline('there')
21 p.sendline('Mr. þython') # þ is more like th than p, but never mind
22 p.expect('Hello')
23 p.expect('there')
24 p.expect('Mr. þython')
25 p.sendeof ()
26 p.expect (pexpect.EOF)
27
28 def test_expect_exact_basic (self):
29 p = pexpect.spawnu('cat')
30 p.sendline('Hello')
31 p.sendline('there')
32 p.sendline('Mr. þython')
33 p.expect_exact('Hello')
34 p.expect_exact('there')
35 p.expect_exact('Mr. þython')
36 p.sendeof()
37 p.expect_exact (pexpect.EOF)
38
39 def test_expect_setecho_toggle(self):
40 '''This tests that echo may be toggled off.
41 '''
42 p = pexpect.spawnu('cat', timeout=5)
43 try:
44 self._expect_echo_toggle_off(p)
45 except IOError:
46 if sys.platform.lower().startswith('sunos'):
47 if hasattr(unittest, 'SkipTest'):
48 raise unittest.SkipTest("Not supported on this platform.")
49 return 'skip'
50 raise
51 self._expect_echo_toggle_on(p)
52
53 def test_expect_echo_exact (self):
54 '''Like test_expect_echo(), but using expect_exact().
55 '''
56 p = pexpect.spawnu('cat', timeout=5)
57 p.expect = p.expect_exact
58 self._expect_echo(p)
59
60 def test_expect_setecho_toggle_exact(self):
61 p = pexpect.spawnu('cat', timeout=5)
62 p.expect = p.expect_exact
63 try:
64 self._expect_echo_toggle_off(p)
65 except IOError:
66 if sys.platform.lower().startswith('sunos'):
67 if hasattr(unittest, 'SkipTest'):
68 raise unittest.SkipTest("Not supported on this platform.")
69 return 'skip'
70 raise
71 self._expect_echo_toggle_on(p)
72
73 def _expect_echo (self, p):
74 p.sendline('1234') # Should see this twice (once from tty echo and again from cat).
75 index = p.expect (['1234', 'abcdé', 'wxyz', pexpect.EOF, pexpect.TIMEOUT ])
76 assert index == 0, (index, p.before)
77 index = p.expect (['1234', 'abcdé', 'wxyz', pexpect.EOF])
78 assert index == 0, index
79
80 def _expect_echo_toggle_off(self, p):
81 p.setecho(0) # Turn off tty echo
82 p.waitnoecho()
83 p.sendline('abcdé') # Now, should only see this once.
84 p.sendline('wxyz') # Should also be only once.
85 index = p.expect ([pexpect.EOF,pexpect.TIMEOUT, 'abcdé', 'wxyz', '1234'] )
86 assert index == 2, index
87 index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
88 assert index == 2, index
89
90 def _expect_echo_toggle_on(self, p):
91 p.setecho(1) # Turn on tty echo
92 time.sleep(0.2) # there is no waitecho() !
93 p.sendline('7890') # Should see this twice.
94 index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
95 assert index == 3, index
96 index = p.expect ([pexpect.EOF, 'abcdé', 'wxyz', '7890'])
97 assert index == 3, index
98 p.sendeof()
99
100 def test_log_unicode(self):
101 msg = "abcΩ÷"
102 filename_send = tempfile.mktemp()
103 filename_read = tempfile.mktemp()
104 p = pexpect.spawnu('cat')
105 if platform.python_version_tuple() < ('3', '0', '0'):
106 import codecs
107 def open(fname, mode, **kwargs):
108 if 'newline' in kwargs:
109 del kwargs['newline']
110 return codecs.open(fname, mode, **kwargs)
111 else:
112 import io
113 open = io.open
114
115 p.logfile_send = open(filename_send, 'w', encoding='utf-8')
116 p.logfile_read = open(filename_read, 'w', encoding='utf-8')
117 p.sendline(msg)
118 p.sendeof()
119 p.expect(pexpect.EOF)
120 p.close()
121 p.logfile_send.close()
122 p.logfile_read.close()
123
124 # ensure the 'send' log is correct,
125 with open(filename_send, 'r', encoding='utf-8') as f:
126 self.assertEqual(f.read(), msg + '\n\x04')
127
128 # ensure the 'read' log is correct,
129 with open(filename_read, 'r', encoding='utf-8', newline='') as f:
130 output = f.read().replace(_CAT_EOF, '')
131 self.assertEqual(output, (msg + '\r\n')*2 )
132
133
134 def test_spawn_expect_ascii_unicode(self):
135 # A bytes-based spawn should be able to handle ASCII-only unicode, for
136 # backwards compatibility.
137 p = pexpect.spawn('cat')
138 p.sendline('Camelot')
139 p.expect('Camelot')
140
141 p.sendline('Aargh')
142 p.sendline('Aårgh')
143 p.expect_exact('Aargh')
144
145 p.sendeof()
146 p.expect(pexpect.EOF)
147
148 def test_spawn_send_unicode(self):
149 # A bytes-based spawn should be able to send arbitrary unicode
150 p = pexpect.spawn('cat')
151 p.sendline('3½')
152 p.sendeof()
153 p.expect(pexpect.EOF)
154
155 def test_spawn_utf8_incomplete(self):
156 # This test case ensures correct incremental decoding, which
157 # otherwise fails when the stream inspected by os.read()
158 # does not align exactly at a utf-8 multibyte boundry:
159 # UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in
160 # position 0: unexpected end of data
161 p = pexpect.spawnu('cat', maxread=1)
162 p.sendline('▁▂▃▄▅▆▇█')
163 p.sendeof()
164 p.expect('▁▂▃▄▅▆▇█')
165
166 def test_readline_bin_echo(self):
167 # Test using readline() with spawnu objects. pexpect 3.2 had threw
168 # a TypeError when concatenating a bytestring to a unicode type.
169
170 # given,
171 child = pexpect.spawnu('echo', ['input', ])
172
173 # exercise,
174 assert child.readline() == 'input' + child.crlf
175
176 def test_unicode_argv(self):
177 """ Ensure a program can be executed with unicode arguments. """
178 p = pexpect.spawn(u'echo ǝpoɔıun', timeout=5, encoding='utf8')
179 p.expect(u'ǝpoɔıun')
180 p.expect(pexpect.EOF)
181 assert not p.isalive()
182 assert p.exitstatus == 0
183
184 if __name__ == '__main__':
185 unittest.main()
186
187 suite = unittest.makeSuite(UnicodeTests, 'test')
OLDNEW
« no previous file with comments | « third_party/pexpect/tests/test_timeout_pattern.py ('k') | third_party/pexpect/tests/test_which.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698