1#! /usr/bin/env python
2#
3# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2001-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)2001-2011 [email protected]
10
11Intended to be run on different platforms, to ensure portability of
12the code.
13
14Cover some of the aspects of serial_for_url and the extension mechanism.
15"""
16
17import unittest
18import serial
19
20
21class Test_URL(unittest.TestCase):
22    """Test serial_for_url"""
23
24    def test_loop(self):
25        """loop interface"""
26        serial.serial_for_url('loop://', do_not_open=True)
27
28    def test_bad_url(self):
29        """invalid protocol specified"""
30        self.assertRaises(ValueError, serial.serial_for_url, "imnotknown://")
31
32    def test_custom_url(self):
33        """custom protocol handlers"""
34        # it's unknown
35        self.assertRaises(ValueError, serial.serial_for_url, "test://")
36        # add search path
37        serial.protocol_handler_packages.append('handlers')
38        # now it should work
39        serial.serial_for_url("test://")
40        # remove our handler again
41        serial.protocol_handler_packages.remove('handlers')
42        # so it should not work anymore
43        self.assertRaises(ValueError, serial.serial_for_url, "test://")
44
45
46if __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()
52