2
# Python Serial Port Extension for Win32, Linux, BSD, Jython
5
# (C) 2010 Chris Liechti <cliechti@gmx.net>
6
# this is distributed under a free software license, see license.txt
9
Some tests for the serial module.
10
Part of pyserial (http://pyserial.sf.net) (C)2010 cliechti@gmx.net
12
Intended to be run on different platforms, to ensure portability of
15
For all these tests a simple hardware is required.
17
Shortcut these pin pairs:
22
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
31
#~ print serial.VERSION
33
# on which port should the tests be performed:
36
if sys.version_info >= (3, 0):
38
return bytes(string, 'latin1')
40
def data(string): return string
44
class Test_Readline(unittest.TestCase):
45
"""Test readline function"""
48
self.s = serial.serial_for_url(PORT, timeout=1)
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(""))
62
def test_readlines(self):
63
"""Test readlines method"""
64
self.s.write(serial.to_bytes("1\n2\n3\n"))
67
[serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
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"))
75
list(self.s.xreadlines()),
76
[serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
79
def test_for_in(self):
80
"""Test for line in s"""
81
self.s.write(serial.to_bytes("1\n2\n3\n"))
87
[serial.to_bytes("1\n"), serial.to_bytes("2\n"), serial.to_bytes("3\n")]
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"))
95
self.s.readline(eol=serial.to_bytes("\r\n")),
96
serial.to_bytes("no\rno\nyes\r\n"))
99
if __name__ == '__main__':
101
sys.stdout.write(__doc__)
102
if len(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