blob: f5635f32c8a2e60d9bc011ba75fe87a196f9957d [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#
cliechtif7bc3bf2010-07-21 15:47:21 +00006# (C) 2001-2009 Chris Liechti <cliechti@gmx.net>
cliechtif81362e2009-07-25 03:44:33 +00007# 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
cliechti5eb4b892009-08-07 18:21:58 +000032if __name__ == '__main__' and sys.version_info < (2, 6):
cliechticb5b0aa2009-07-29 21:59:52 +000033 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):
cliechtie3ab3532009-08-05 12:40:38 +000058 self.s = serial.serial_for_url(PORT, timeout=1)
cliechti38077122013-10-16 02:57:27 +000059 #~ self.io = io.TextIOWrapper(self.s)
cliechtiddd78132009-07-28 01:13:28 +000060 self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
cliechtif81362e2009-07-25 03:44:33 +000061
62 def tearDown(self):
63 self.s.close()
64
cliechtiddd78132009-07-28 01:13:28 +000065 def test_hello_raw(self):
Chris Liechti16843852015-09-22 23:24:35 +020066 self.io.write(b"hello\n".decode('utf-8'))
cliechtidaf47ba2009-07-28 01:28:16 +000067 self.io.flush() # it is buffering. required to get the data out
cliechtiddd78132009-07-28 01:13:28 +000068 hello = self.io.readline()
Chris Liechti16843852015-09-22 23:24:35 +020069 self.failUnlessEqual(hello, b"hello\n".decode('utf-8'))
cliechtif81362e2009-07-25 03:44:33 +000070
cliechti38077122013-10-16 02:57:27 +000071# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cliechtif81362e2009-07-25 03:44:33 +000072if __name__ == '__main__':
73 import sys
74 sys.stdout.write(__doc__)
75 if len(sys.argv) > 1:
76 PORT = sys.argv[1]
77 sys.stdout.write("Testing port: %r\n" % PORT)
78 sys.argv[1:] = ['-v']
79 # When this module is executed from the command-line, it runs all its tests
80 unittest.main()