• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""\
8Tests for utility functions of serualutil.
9"""
10
11import os
12import unittest
13import serial
14
15
16class 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')
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'))
26
27    def test_iterbytes(self):
28        self.assertEqual(list(serial.iterbytes(b'\x01\x02\x03')), [b'\x01', b'\x02', b'\x03'])
29
30
31if __name__ == '__main__':
32    import sys
33    sys.stdout.write(__doc__)
34    sys.argv[1:] = ['-v']
35    # When this module is executed from the command-line, it runs all its tests
36    unittest.main()
37