cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
cliechti | e542b36 | 2011-03-18 00:49:16 +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) 2001-2015 Chris Liechti <cliechti@gmx.net> |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 7 | """\ |
| 8 | Some tests for the serial module. |
| 9 | Part of pySerial (http://pyserial.sf.net) (C)2001-2011 cliechti@gmx.net |
| 10 | |
| 11 | Intended to be run on different platforms, to ensure portability of |
| 12 | the code. |
| 13 | |
| 14 | Cover some of the aspects of serial_for_url and the extension mechanism. |
| 15 | """ |
| 16 | |
| 17 | import unittest |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 18 | import serial |
| 19 | |
| 20 | |
| 21 | class Test_URL(unittest.TestCase): |
| 22 | """Test serial_for_url""" |
| 23 | |
| 24 | def test_loop(self): |
| 25 | """loop interface""" |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 26 | serial.serial_for_url('loop://', do_not_open=True) |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 27 | |
| 28 | def test_bad_url(self): |
| 29 | """invalid protocol specified""" |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 30 | self.assertRaises(ValueError, serial.serial_for_url, "imnotknown://") |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 31 | |
| 32 | def test_custom_url(self): |
| 33 | """custom protocol handlers""" |
| 34 | # it's unknown |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 35 | self.assertRaises(ValueError, serial.serial_for_url, "test://") |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 36 | # add search path |
| 37 | serial.protocol_handler_packages.append('handlers') |
| 38 | # now it should work |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 39 | serial.serial_for_url("test://") |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 40 | # remove our handler again |
| 41 | serial.protocol_handler_packages.remove('handlers') |
| 42 | # so it should not work anymore |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 43 | self.assertRaises(ValueError, serial.serial_for_url, "test://") |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | if __name__ == '__main__': |
| 47 | import sys |
| 48 | sys.stdout.write(__doc__) |
| 49 | sys.argv[1:] = ['-v'] |
| 50 | # When this module is executed from the command-line, it runs all its tests |
| 51 | unittest.main() |