blob: 7f30a495dad739f422b84d9c3c8fc622cd6ad81e [file] [log] [blame]
cliechtif16c7702009-08-02 00:00:55 +00001#! /usr/bin/env python
2#
Chris Liechti7c032f12015-10-27 23:02:00 +01003# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
cliechtif81362e2009-07-25 03:44:33 +00005#
Chris Liechti7c032f12015-10-27 23:02:00 +01006# SPDX-License-Identifier: BSD-3-Clause
cliechtif81362e2009-07-25 03:44:33 +00007"""\
8Some tests for the serial module.
9Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net
10
11Intended to be run on different platforms, to ensure portability of
12the code.
13
cliechtiddd78132009-07-28 01:13:28 +000014This modules contains test for the interaction between Serial and the io
15library. This only works on Python 2.6+ that introduced the io library.
cliechtif81362e2009-07-25 03:44:33 +000016
17For all these tests a simple hardware is required.
18Loopback HW adapter:
19Shortcut these pin pairs:
20 TX <-> RX
21 RTS <-> CTS
22 DTR <-> DSR
23
24On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
25"""
26
cliechtiddd78132009-07-28 01:13:28 +000027import unittest
28import sys
cliechticb5b0aa2009-07-29 21:59:52 +000029
cliechti5eb4b892009-08-07 18:21:58 +000030if __name__ == '__main__' and sys.version_info < (2, 6):
cliechticb5b0aa2009-07-29 21:59:52 +000031 sys.stderr.write("""\
32==============================================================================
33WARNING: this test is intended for Python 2.6 and newer where the io library
34is available. This seems to be an older version of Python running.
35Continuing anyway...
36==============================================================================
37""")
38
cliechtiddd78132009-07-28 01:13:28 +000039import io
cliechtif81362e2009-07-25 03:44:33 +000040import serial
41
cliechtiddd78132009-07-28 01:13:28 +000042# trick to make that this test run under 2.6 and 3.x without modification.
43# problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't
44# like u'nicode' strings with the prefix and it is not providing an unicode
45# function ('str' is now what 'unicode' used to be)
46if sys.version_info >= (3, 0):
47 def unicode(x): return x
cliechtif81362e2009-07-25 03:44:33 +000048
cliechtiddd78132009-07-28 01:13:28 +000049
50# on which port should the tests be performed:
51PORT = 0
52
53class Test_SerialAndIO(unittest.TestCase):
cliechtif81362e2009-07-25 03:44:33 +000054
55 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +000056 self.s = serial.serial_for_url(PORT, timeout=1)
cliechti38077122013-10-16 02:57:27 +000057 #~ self.io = io.TextIOWrapper(self.s)
cliechtiddd78132009-07-28 01:13:28 +000058 self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
cliechtif81362e2009-07-25 03:44:33 +000059
60 def tearDown(self):
61 self.s.close()
62
cliechtiddd78132009-07-28 01:13:28 +000063 def test_hello_raw(self):
Chris Liechti16843852015-09-22 23:24:35 +020064 self.io.write(b"hello\n".decode('utf-8'))
cliechtidaf47ba2009-07-28 01:28:16 +000065 self.io.flush() # it is buffering. required to get the data out
cliechtiddd78132009-07-28 01:13:28 +000066 hello = self.io.readline()
Chris Liechti16843852015-09-22 23:24:35 +020067 self.failUnlessEqual(hello, b"hello\n".decode('utf-8'))
cliechtif81362e2009-07-25 03:44:33 +000068
cliechti38077122013-10-16 02:57:27 +000069# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cliechtif81362e2009-07-25 03:44:33 +000070if __name__ == '__main__':
71 import sys
72 sys.stdout.write(__doc__)
73 if len(sys.argv) > 1:
74 PORT = sys.argv[1]
75 sys.stdout.write("Testing port: %r\n" % PORT)
76 sys.argv[1:] = ['-v']
77 # When this module is executed from the command-line, it runs all its tests
78 unittest.main()