~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/test_unicode.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
from __future__ import unicode_literals
 
3
 
 
4
import platform
 
5
import tempfile
 
6
 
 
7
import pexpect
 
8
import unittest
 
9
import PexpectTestCase
 
10
 
 
11
# the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent
 
12
_CAT_EOF = '^D\x08\x08'
 
13
 
 
14
class UnicodeTests(PexpectTestCase.PexpectTestCase):
 
15
    def test_expect_basic (self):
 
16
        p = pexpect.spawnu('cat')
 
17
        p.sendline('Hello')
 
18
        p.sendline('there')
 
19
        p.sendline('Mr. þython') # þ is more like th than p, but never mind
 
20
        p.expect('Hello')
 
21
        p.expect('there')
 
22
        p.expect('Mr. þython')
 
23
        p.sendeof ()
 
24
        p.expect (pexpect.EOF)
 
25
 
 
26
    def test_expect_exact_basic (self):
 
27
        p = pexpect.spawnu('cat')
 
28
        p.sendline('Hello')
 
29
        p.sendline('there')
 
30
        p.sendline('Mr. þython')
 
31
        p.expect_exact('Hello')
 
32
        p.expect_exact('there')
 
33
        p.expect_exact('Mr. þython')
 
34
        p.sendeof()
 
35
        p.expect_exact (pexpect.EOF)
 
36
 
 
37
    def test_expect_echo (self):
 
38
        '''This tests that echo can be turned on and off.
 
39
        '''
 
40
        p = pexpect.spawnu('cat', timeout=10)
 
41
        self._expect_echo(p)
 
42
 
 
43
    def test_expect_echo_exact (self):
 
44
        '''Like test_expect_echo(), but using expect_exact().
 
45
        '''
 
46
        p = pexpect.spawnu('cat', timeout=10)
 
47
        p.expect = p.expect_exact
 
48
        self._expect_echo(p)
 
49
 
 
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
 
69
        p.sendeof()
 
70
 
 
71
    def test_log_unicode(self):
 
72
        msg = "abcΩ÷"
 
73
        filename_send = tempfile.mktemp()
 
74
        filename_read = tempfile.mktemp()
 
75
        p = pexpect.spawnu('cat')
 
76
        if platform.python_version_tuple() < ('3', '0', '0'):
 
77
            import codecs
 
78
            def open(fname, mode, **kwargs):
 
79
                if 'newline' in kwargs:
 
80
                    del kwargs['newline']
 
81
                return codecs.open(fname, mode, **kwargs)
 
82
        else:
 
83
            import io
 
84
            open = io.open
 
85
 
 
86
        p.logfile_send = open(filename_send, 'w', encoding='utf-8')
 
87
        p.logfile_read = open(filename_read, 'w', encoding='utf-8')
 
88
        p.sendline(msg)
 
89
        p.sendeof()
 
90
        p.expect(pexpect.EOF)
 
91
        p.close()
 
92
        p.logfile_send.close()
 
93
        p.logfile_read.close()
 
94
 
 
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')
 
98
 
 
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 )
 
103
 
 
104
 
 
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')
 
110
        p.expect('Camelot')
 
111
 
 
112
        p.sendline('Aargh')
 
113
        p.sendline('Aårgh')
 
114
        p.expect_exact('Aargh')
 
115
 
 
116
        p.sendeof()
 
117
        p.expect(pexpect.EOF)
 
118
 
 
119
    def test_spawn_send_unicode(self):
 
120
        # A bytes-based spawn should be able to send arbitrary unicode
 
121
        p = pexpect.spawn('cat')
 
122
        p.sendline('3½')
 
123
        p.sendeof()
 
124
        p.expect(pexpect.EOF)
 
125
 
 
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('▁▂▃▄▅▆▇█')
 
134
        p.sendeof()
 
135
        p.expect('▁▂▃▄▅▆▇█')
 
136
 
 
137
 
 
138
if __name__ == '__main__':
 
139
    unittest.main()
 
140
 
 
141
suite = unittest.makeSuite(UnicodeTests, 'test')