blob: 5bf8e606c0c036ff929233dc2b1a53142b8511d7 [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
Chris Liechti122a9322016-09-12 23:42:51 +020014
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')
Chris Liechtid361d682016-10-05 03:37:47 +020023 # 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 Liechti122a9322016-09-12 23:42:51 +020026
27 def test_iterbytes(self):
28 self.assertEqual(list(serial.iterbytes(b'\x01\x02\x03')), [b'\x01', b'\x02', b'\x03'])
29
30
Chris Liechti122a9322016-09-12 23:42:51 +020031if __name__ == '__main__':
32 import sys
33 sys.stdout.write(__doc__)
Chris Liechti122a9322016-09-12 23:42:51 +020034 sys.argv[1:] = ['-v']
35 # When this module is executed from the command-line, it runs all its tests
36 unittest.main()