1#! /usr/bin/env python
2#
3# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
5#
6# SPDX-License-Identifier:    BSD-3-Clause
7"""\
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
14This modules contains test for the interaction between Serial and the io
15library. This only works on Python 2.6+ that introduced the io library.
16
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
27import io
28import sys
29import unittest
30import serial
31
32# on which port should the tests be performed:
33PORT = 'loop://'
34
35
36class Test_SerialAndIO(unittest.TestCase):
37
38    def setUp(self):
39        self.s = serial.serial_for_url(PORT, timeout=1)
40        #~ self.io = io.TextIOWrapper(self.s)
41        self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
42
43    def tearDown(self):
44        self.s.close()
45
46    def test_hello_raw(self):
47        self.io.write(b"hello\n".decode('utf-8'))
48        self.io.flush()  # it is buffering. required to get the data out
49        hello = self.io.readline()
50        self.assertEqual(hello, b"hello\n".decode('utf-8'))
51
52# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53if __name__ == '__main__':
54    import sys
55    sys.stdout.write(__doc__)
56    if len(sys.argv) > 1:
57        PORT = sys.argv[1]
58    sys.stdout.write("Testing port: {!r}\n".format(PORT))
59    sys.argv[1:] = ['-v']
60    # When this module is executed from the command-line, it runs all its tests
61    unittest.main()
62