blob: 1eba9627e36810e70a3692390e76edc57451e649 [file] [log] [blame]
Chris Liechti122a9322016-09-12 23:42:51 +02001#!/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
14import time
15
16
17class Test_util(unittest.TestCase):
18 """Test serial utility functions"""
19
20 def test_to_bytes(self):
21 self.assertEqual(serial.to_bytes([1, 2, 3]), b'\x01\x02\x03')
22 self.assertEqual(serial.to_bytes(b'\x01\x02\x03'), b'\x01\x02\x03')
23 self.assertEqual(serial.to_bytes(bytearray([1,2,3])), b'\x01\x02\x03')
Chris Liechtid361d682016-10-05 03:37:47 +020024 # unicode is not supported test. use decode() instead of u'' syntax to be
25 # compatible to Python 3.x < 3.4
26 self.assertRaises(TypeError, serial.to_bytes, b'hello'.decode('utf-8'))
Chris Liechti122a9322016-09-12 23:42:51 +020027
28 def test_iterbytes(self):
29 self.assertEqual(list(serial.iterbytes(b'\x01\x02\x03')), [b'\x01', b'\x02', b'\x03'])
30
31
Chris Liechti122a9322016-09-12 23:42:51 +020032if __name__ == '__main__':
33 import sys
34 sys.stdout.write(__doc__)
35 if len(sys.argv) > 1:
36 PORT = sys.argv[1]
37 sys.argv[1:] = ['-v']
38 # When this module is executed from the command-line, it runs all its tests
39 unittest.main()