cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 2 | # |
Chris Liechti | 7c032f1 | 2015-10-27 23:02:00 +0100 | [diff] [blame] | 3 | # This file is part of pySerial - Cross platform serial port support for Python |
| 4 | # (C) 2010-2015 Chris Liechti <cliechti@gmx.net> |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 7 | """\ |
| 8 | Some tests for the serial module. |
| 9 | Part of pyserial (http://pyserial.sf.net) (C)2010 cliechti@gmx.net |
| 10 | |
| 11 | Intended to be run on different platforms, to ensure portability of |
| 12 | the code. |
| 13 | |
| 14 | For all these tests a simple hardware is required. |
| 15 | Loopback HW adapter: |
| 16 | Shortcut these pin pairs: |
| 17 | TX <-> RX |
| 18 | RTS <-> CTS |
| 19 | DTR <-> DSR |
| 20 | |
| 21 | On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8) |
| 22 | """ |
| 23 | |
| 24 | import unittest |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 25 | import sys |
| 26 | import serial |
| 27 | |
| 28 | #~ print serial.VERSION |
| 29 | |
| 30 | # on which port should the tests be performed: |
Chris Liechti | 73ff15c | 2016-10-08 21:06:58 +0200 | [diff] [blame] | 31 | PORT = 'loop://' |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 32 | |
| 33 | if sys.version_info >= (3, 0): |
| 34 | def data(string): |
| 35 | return bytes(string, 'latin1') |
| 36 | else: |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 37 | def data(string): |
| 38 | return string |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class Test_Readline(unittest.TestCase): |
| 42 | """Test readline function""" |
| 43 | |
| 44 | def setUp(self): |
| 45 | self.s = serial.serial_for_url(PORT, timeout=1) |
| 46 | |
| 47 | def tearDown(self): |
| 48 | self.s.close() |
| 49 | |
| 50 | def test_readline(self): |
| 51 | """Test readline method""" |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 52 | self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a])) |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 53 | self.assertEqual(self.s.readline(), serial.to_bytes([0x31, 0x0a])) |
| 54 | self.assertEqual(self.s.readline(), serial.to_bytes([0x32, 0x0a])) |
| 55 | self.assertEqual(self.s.readline(), serial.to_bytes([0x33, 0x0a])) |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 56 | # this time we will get a timeout |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 57 | self.assertEqual(self.s.readline(), serial.to_bytes([])) |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 58 | |
| 59 | def test_readlines(self): |
| 60 | """Test readlines method""" |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 61 | self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a])) |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 62 | self.assertEqual( |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 63 | self.s.readlines(), |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 64 | [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])] |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 65 | ) |
| 66 | |
| 67 | def test_xreadlines(self): |
| 68 | """Test xreadlines method (skipped for io based systems)""" |
| 69 | if hasattr(self.s, 'xreadlines'): |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 70 | self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a])) |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 71 | self.assertEqual( |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 72 | list(self.s.xreadlines()), |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 73 | [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])] |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 74 | ) |
| 75 | |
| 76 | def test_for_in(self): |
| 77 | """Test for line in s""" |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 78 | self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a])) |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 79 | lines = [] |
| 80 | for line in self.s: |
| 81 | lines.append(line) |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 82 | self.assertEqual( |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 83 | lines, |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 84 | [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])] |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 85 | ) |
| 86 | |
| 87 | def test_alternate_eol(self): |
| 88 | """Test readline with alternative eol settings (skipped for io based systems)""" |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 89 | if hasattr(self.s, 'xreadlines'): # test if it is our FileLike base class |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 90 | self.s.write(serial.to_bytes("no\rno\nyes\r\n")) |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 91 | self.assertEqual( |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 92 | self.s.readline(eol=serial.to_bytes("\r\n")), |
| 93 | serial.to_bytes("no\rno\nyes\r\n")) |
| 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | import sys |
| 98 | sys.stdout.write(__doc__) |
| 99 | if len(sys.argv) > 1: |
| 100 | PORT = sys.argv[1] |
Chris Liechti | 3debab2 | 2016-06-20 22:52:22 +0200 | [diff] [blame] | 101 | sys.stdout.write("Testing port: {!r}\n".format(PORT)) |
cliechti | f7bc3bf | 2010-07-21 15:47:21 +0000 | [diff] [blame] | 102 | sys.argv[1:] = ['-v'] |
| 103 | # When this module is executed from the command-line, it runs all its tests |
| 104 | unittest.main() |