cliechti | f16c770 | 2009-08-02 00:00:55 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 3 | # Python Serial Port Extension for Win32, Linux, BSD, Jython |
| 4 | # see __init__.py |
| 5 | # |
| 6 | # (C) 2001-2008 Chris Liechti <cliechti@gmx.net> |
| 7 | # this is distributed under a free software license, see license.txt |
| 8 | |
| 9 | """\ |
| 10 | Some tests for the serial module. |
| 11 | Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net |
| 12 | |
| 13 | Intended to be run on different platforms, to ensure portability of |
| 14 | the code. |
| 15 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 16 | This modules contains test for the interaction between Serial and the io |
| 17 | library. This only works on Python 2.6+ that introduced the io library. |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 18 | |
| 19 | For all these tests a simple hardware is required. |
| 20 | Loopback HW adapter: |
| 21 | Shortcut these pin pairs: |
| 22 | TX <-> RX |
| 23 | RTS <-> CTS |
| 24 | DTR <-> DSR |
| 25 | |
| 26 | On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8) |
| 27 | """ |
| 28 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 29 | import unittest |
| 30 | import sys |
cliechti | cb5b0aa | 2009-07-29 21:59:52 +0000 | [diff] [blame] | 31 | |
| 32 | if sys.version_info < (2, 6): |
| 33 | sys.stderr.write("""\ |
| 34 | ============================================================================== |
| 35 | WARNING: this test is intended for Python 2.6 and newer where the io library |
| 36 | is available. This seems to be an older version of Python running. |
| 37 | Continuing anyway... |
| 38 | ============================================================================== |
| 39 | """) |
| 40 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 41 | import io |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 42 | import serial |
| 43 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 44 | # trick to make that this test run under 2.6 and 3.x without modification. |
| 45 | # problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't |
| 46 | # like u'nicode' strings with the prefix and it is not providing an unicode |
| 47 | # function ('str' is now what 'unicode' used to be) |
| 48 | if sys.version_info >= (3, 0): |
| 49 | def unicode(x): return x |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 50 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 51 | |
| 52 | # on which port should the tests be performed: |
| 53 | PORT = 0 |
| 54 | |
| 55 | class Test_SerialAndIO(unittest.TestCase): |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 56 | |
| 57 | def setUp(self): |
cliechti | f16c770 | 2009-08-02 00:00:55 +0000 | [diff] [blame^] | 58 | self.s = serial.serial_class_for_url(PORT, timeout=1) |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 59 | self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s)) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 60 | |
| 61 | def tearDown(self): |
| 62 | self.s.close() |
| 63 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 64 | def test_hello_raw(self): |
| 65 | self.io.write(unicode("hello\n")) |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 66 | self.io.flush() # it is buffering. required to get the data out |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 67 | hello = self.io.readline() |
| 68 | self.failUnlessEqual(hello, unicode("hello\n")) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | import sys |
| 73 | sys.stdout.write(__doc__) |
| 74 | if len(sys.argv) > 1: |
| 75 | PORT = sys.argv[1] |
| 76 | sys.stdout.write("Testing port: %r\n" % PORT) |
| 77 | sys.argv[1:] = ['-v'] |
| 78 | # When this module is executed from the command-line, it runs all its tests |
| 79 | unittest.main() |