blob: 8c03a860e205c564a460036df263ecf1fde1b672 [file] [log] [blame]
cliechtif16c7702009-08-02 00:00:55 +00001#! /usr/bin/env python
2#
cliechtif81362e2009-07-25 03:44:33 +00003# Python Serial Port Extension for Win32, Linux, BSD, Jython
4# see __init__.py
5#
6# (C) 2001-2008 Chris Liechti <cliechti@gmx.net>
7# this is distributed under a free software license, see license.txt
8
9"""\
10Some tests for the serial module.
11Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net
12
13Intended to be run on different platforms, to ensure portability of
14the code.
15
cliechtiddd78132009-07-28 01:13:28 +000016This modules contains test for the interaction between Serial and the io
17library. This only works on Python 2.6+ that introduced the io library.
cliechtif81362e2009-07-25 03:44:33 +000018
19For all these tests a simple hardware is required.
20Loopback HW adapter:
21Shortcut these pin pairs:
22 TX <-> RX
23 RTS <-> CTS
24 DTR <-> DSR
25
26On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
27"""
28
cliechtiddd78132009-07-28 01:13:28 +000029import unittest
30import sys
cliechticb5b0aa2009-07-29 21:59:52 +000031
32if sys.version_info < (2, 6):
33 sys.stderr.write("""\
34==============================================================================
35WARNING: this test is intended for Python 2.6 and newer where the io library
36is available. This seems to be an older version of Python running.
37Continuing anyway...
38==============================================================================
39""")
40
cliechtiddd78132009-07-28 01:13:28 +000041import io
cliechtif81362e2009-07-25 03:44:33 +000042import serial
43
cliechtiddd78132009-07-28 01:13:28 +000044# trick to make that this test run under 2.6 and 3.x without modification.
45# problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't
46# like u'nicode' strings with the prefix and it is not providing an unicode
47# function ('str' is now what 'unicode' used to be)
48if sys.version_info >= (3, 0):
49 def unicode(x): return x
cliechtif81362e2009-07-25 03:44:33 +000050
cliechtiddd78132009-07-28 01:13:28 +000051
52# on which port should the tests be performed:
53PORT = 0
54
55class Test_SerialAndIO(unittest.TestCase):
cliechtif81362e2009-07-25 03:44:33 +000056
57 def setUp(self):
cliechtif16c7702009-08-02 00:00:55 +000058 self.s = serial.serial_class_for_url(PORT, timeout=1)
cliechtiddd78132009-07-28 01:13:28 +000059 self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
cliechtif81362e2009-07-25 03:44:33 +000060
61 def tearDown(self):
62 self.s.close()
63
cliechtiddd78132009-07-28 01:13:28 +000064 def test_hello_raw(self):
65 self.io.write(unicode("hello\n"))
cliechtidaf47ba2009-07-28 01:28:16 +000066 self.io.flush() # it is buffering. required to get the data out
cliechtiddd78132009-07-28 01:13:28 +000067 hello = self.io.readline()
68 self.failUnlessEqual(hello, unicode("hello\n"))
cliechtif81362e2009-07-25 03:44:33 +000069
70
71if __name__ == '__main__':
72 import sys
73 sys.stdout.write(__doc__)
74 if len(sys.argv) > 1:
75 PORT = sys.argv[1]
76 sys.stdout.write("Testing port: %r\n" % PORT)
77 sys.argv[1:] = ['-v']
78 # When this module is executed from the command-line, it runs all its tests
79 unittest.main()