2015-05-29 20:50:07 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import unittest
|
|
|
|
import asyncio
|
2015-05-30 11:58:29 +00:00
|
|
|
|
2015-06-11 21:12:29 +00:00
|
|
|
from hbmqtt.codecs import (
|
2015-05-29 20:50:07 +00:00
|
|
|
bytes_to_hex_str,
|
|
|
|
bytes_to_int,
|
2015-05-31 12:23:45 +00:00
|
|
|
decode_string,
|
2015-05-31 12:30:32 +00:00
|
|
|
encode_string,
|
2015-05-29 20:50:07 +00:00
|
|
|
)
|
2015-05-30 11:58:29 +00:00
|
|
|
|
2015-05-29 20:50:07 +00:00
|
|
|
|
2015-06-13 12:38:59 +00:00
|
|
|
class TestCodecs(unittest.TestCase):
|
2015-05-29 20:50:07 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
|
|
|
def test_bytes_to_hex_str(self):
|
|
|
|
ret = bytes_to_hex_str(b'\x7f')
|
|
|
|
self.assertEqual(ret, '0x7f')
|
|
|
|
|
|
|
|
def test_bytes_to_int(self):
|
|
|
|
ret = bytes_to_int(b'\x7f')
|
|
|
|
self.assertEqual(ret, 127)
|
|
|
|
ret = bytes_to_int(b'\xff\xff')
|
|
|
|
self.assertEqual(ret, 65535)
|
|
|
|
|
2015-05-31 12:30:32 +00:00
|
|
|
def test_decode_string(self):
|
2015-05-29 20:50:07 +00:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'\x00\x02AA')
|
2015-05-31 12:23:45 +00:00
|
|
|
ret = self.loop.run_until_complete(decode_string(stream))
|
2015-05-31 12:30:32 +00:00
|
|
|
self.assertEqual(ret, 'AA')
|
|
|
|
|
|
|
|
def test_encode_string(self):
|
|
|
|
encoded = encode_string('AA')
|
2017-08-06 22:09:07 +00:00
|
|
|
self.assertEqual(b'\x00\x02AA', encoded)
|