Chris Liechti | 122a932 | 2016-09-12 23:42:51 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This file is part of pySerial - Cross platform serial port support for Python |
| 4 | # (C) 2016 Chris Liechti <cliechti@gmx.net> |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
| 7 | """\ |
| 8 | Tests for utility functions of serualutil. |
| 9 | """ |
| 10 | |
| 11 | import os |
| 12 | import unittest |
| 13 | import serial |
Chris Liechti | 122a932 | 2016-09-12 23:42:51 +0200 | [diff] [blame] | 14 | |
| 15 | |
| 16 | class Test_util(unittest.TestCase): |
| 17 | """Test serial utility functions""" |
| 18 | |
| 19 | def test_to_bytes(self): |
| 20 | self.assertEqual(serial.to_bytes([1, 2, 3]), b'\x01\x02\x03') |
| 21 | self.assertEqual(serial.to_bytes(b'\x01\x02\x03'), b'\x01\x02\x03') |
| 22 | self.assertEqual(serial.to_bytes(bytearray([1,2,3])), b'\x01\x02\x03') |
Chris Liechti | d361d68 | 2016-10-05 03:37:47 +0200 | [diff] [blame] | 23 | # unicode is not supported test. use decode() instead of u'' syntax to be |
| 24 | # compatible to Python 3.x < 3.4 |
| 25 | self.assertRaises(TypeError, serial.to_bytes, b'hello'.decode('utf-8')) |
Chris Liechti | 122a932 | 2016-09-12 23:42:51 +0200 | [diff] [blame] | 26 | |
| 27 | def test_iterbytes(self): |
| 28 | self.assertEqual(list(serial.iterbytes(b'\x01\x02\x03')), [b'\x01', b'\x02', b'\x03']) |
| 29 | |
| 30 | |
Chris Liechti | 122a932 | 2016-09-12 23:42:51 +0200 | [diff] [blame] | 31 | if __name__ == '__main__': |
| 32 | import sys |
| 33 | sys.stdout.write(__doc__) |
Chris Liechti | 122a932 | 2016-09-12 23:42:51 +0200 | [diff] [blame] | 34 | sys.argv[1:] = ['-v'] |
| 35 | # When this module is executed from the command-line, it runs all its tests |
| 36 | unittest.main() |