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