blob: 677ce85e2887ba82768a32add79cc5251ed5d9b5 [file] [log] [blame]
cliechtif7bc3bf2010-07-21 15:47:21 +00001#! /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"""\
9Some tests for the serial module.
10Part of pyserial (http://pyserial.sf.net) (C)2010 cliechti@gmx.net
11
12Intended to be run on different platforms, to ensure portability of
13the code.
14
15For all these tests a simple hardware is required.
16Loopback HW adapter:
17Shortcut these pin pairs:
18 TX <-> RX
19 RTS <-> CTS
20 DTR <-> DSR
21
22On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
23"""
24
25import unittest
26import threading
27import time
28import sys
29import serial
30
31#~ print serial.VERSION
32
33# on which port should the tests be performed:
34PORT = 0
35
36if sys.version_info >= (3, 0):
37 def data(string):
38 return bytes(string, 'latin1')
39else:
40 def data(string): return string
41
42
43
44class 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"""
cliechtie30868d2013-10-16 15:35:11 +000055 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
56 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x31, 0x0a]))
57 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x32, 0x0a]))
58 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000059 # this time we will get a timeout
cliechtie30868d2013-10-16 15:35:11 +000060 self.failUnlessEqual(self.s.readline(), serial.to_bytes([]))
cliechtif7bc3bf2010-07-21 15:47:21 +000061
62 def test_readlines(self):
63 """Test readlines method"""
cliechtie30868d2013-10-16 15:35:11 +000064 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000065 self.failUnlessEqual(
66 self.s.readlines(),
cliechtie30868d2013-10-16 15:35:11 +000067 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000068 )
69
70 def test_xreadlines(self):
71 """Test xreadlines method (skipped for io based systems)"""
72 if hasattr(self.s, 'xreadlines'):
cliechtie30868d2013-10-16 15:35:11 +000073 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000074 self.failUnlessEqual(
75 list(self.s.xreadlines()),
cliechtie30868d2013-10-16 15:35:11 +000076 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000077 )
78
79 def test_for_in(self):
80 """Test for line in s"""
cliechtie30868d2013-10-16 15:35:11 +000081 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000082 lines = []
83 for line in self.s:
84 lines.append(line)
85 self.failUnlessEqual(
86 lines,
cliechtie30868d2013-10-16 15:35:11 +000087 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000088 )
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
99if __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()