~ampelbein/ubuntu/natty/pyserial/lp-715766

« back to all changes in this revision

Viewing changes to test/test_readline.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-25 07:59:17 UTC
  • mfrom: (3.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110125075917-m132a8pxfff5a7sf
Tags: 2.5-1
* New upstream version. Closes: #520618.
* Build a python3-serial package.
* Don't use string exception in miniterm.py. Closes: #585328.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# Python Serial Port Extension for Win32, Linux, BSD, Jython
 
3
# see __init__.py
 
4
#
 
5
# (C) 2010 Chris Liechti <cliechti@gmx.net>
 
6
# this is distributed under a free software license, see license.txt
 
7
 
 
8
"""\
 
9
Some tests for the serial module.
 
10
Part of pyserial (http://pyserial.sf.net)  (C)2010 cliechti@gmx.net
 
11
 
 
12
Intended to be run on different platforms, to ensure portability of
 
13
the code.
 
14
 
 
15
For all these tests a simple hardware is required.
 
16
Loopback HW adapter:
 
17
Shortcut these pin pairs:
 
18
 TX  <-> RX
 
19
 RTS <-> CTS
 
20
 DTR <-> DSR
 
21
 
 
22
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
 
23
"""
 
24
 
 
25
import unittest
 
26
import threading
 
27
import time
 
28
import sys
 
29
import serial
 
30
 
 
31
#~ print serial.VERSION
 
32
 
 
33
# on which port should the tests be performed:
 
34
PORT = 0
 
35
 
 
36
if sys.version_info >= (3, 0):
 
37
    def data(string):
 
38
        return bytes(string, 'latin1')
 
39
else:
 
40
    def data(string): return string
 
41
 
 
42
 
 
43
 
 
44
class Test_Readline(unittest.TestCase):
 
45
    """Test readline function"""
 
46
 
 
47
    def setUp(self):
 
48
        self.s = serial.serial_for_url(PORT, timeout=1)
 
49
 
 
50
    def tearDown(self):
 
51
        self.s.close()
 
52
 
 
53
    def test_readline(self):
 
54
        """Test readline method"""
 
55
        self.s.write(serial.to_bytes("1\n2\n3\n"))
 
56
        self.failUnlessEqual(self.s.readline(), serial.to_bytes("1\n"))
 
57
        self.failUnlessEqual(self.s.readline(), serial.to_bytes("2\n"))
 
58
        self.failUnlessEqual(self.s.readline(), serial.to_bytes("3\n"))
 
59
        # this time we will get a timeout
 
60
        self.failUnlessEqual(self.s.readline(), serial.to_bytes(""))
 
61
 
 
62
    def test_readlines(self):
 
63
        """Test readlines method"""
 
64
        self.s.write(serial.to_bytes("1\n2\n3\n"))
 
65
        self.failUnlessEqual(
 
66
                self.s.readlines(),
 
67
                [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
 
68
                )
 
69
 
 
70
    def test_xreadlines(self):
 
71
        """Test xreadlines method (skipped for io based systems)"""
 
72
        if hasattr(self.s, 'xreadlines'):
 
73
            self.s.write(serial.to_bytes("1\n2\n3\n"))
 
74
            self.failUnlessEqual(
 
75
                    list(self.s.xreadlines()),
 
76
                    [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
 
77
                    )
 
78
 
 
79
    def test_for_in(self):
 
80
        """Test for line in s"""
 
81
        self.s.write(serial.to_bytes("1\n2\n3\n"))
 
82
        lines = []
 
83
        for line in self.s:
 
84
            lines.append(line)
 
85
        self.failUnlessEqual(
 
86
                lines,
 
87
                [serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
 
88
                )
 
89
 
 
90
    def test_alternate_eol(self):
 
91
        """Test readline with alternative eol settings (skipped for io based systems)"""
 
92
        if hasattr(self.s, 'xreadlines'): # test if it is our FileLike base class
 
93
            self.s.write(serial.to_bytes("no\rno\nyes\r\n"))
 
94
            self.failUnlessEqual(
 
95
                    self.s.readline(eol=serial.to_bytes("\r\n")),
 
96
                    serial.to_bytes("no\rno\nyes\r\n"))
 
97
 
 
98
 
 
99
if __name__ == '__main__':
 
100
    import sys
 
101
    sys.stdout.write(__doc__)
 
102
    if len(sys.argv) > 1:
 
103
        PORT = sys.argv[1]
 
104
    sys.stdout.write("Testing port: %r\n" % PORT)
 
105
    sys.argv[1:] = ['-v']
 
106
    # When this module is executed from the command-line, it runs all its tests
 
107
    unittest.main()