amqtt/tests/test_codecs.py

38 wiersze
985 B
Python
Czysty Zwykły widok Historia

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
2021-03-27 12:16:42 +00:00
from amqtt.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")
2015-05-29 20:50:07 +00:00
def test_bytes_to_int(self):
ret = bytes_to_int(b"\x7f")
2015-05-29 20:50:07 +00:00
self.assertEqual(ret, 127)
ret = bytes_to_int(b"\xff\xff")
2015-05-29 20:50:07 +00:00
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))
self.assertEqual(ret, "AA")
2015-05-31 12:30:32 +00:00
def test_encode_string(self):
encoded = encode_string("AA")
self.assertEqual(b"\x00\x02AA", encoded)