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