Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for string to bytes conversion and refactor #108

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/blinkstick/blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from blinkstick.constants import VENDOR_ID, PRODUCT_ID, BlinkStickVariant
from blinkstick.exceptions import BlinkStickException
from blinkstick.utilities import string_to_info_block_data

if sys.platform == "win32":
from blinkstick.backends.win32 import Win32Backend as USBBackend
Expand Down Expand Up @@ -454,25 +455,6 @@ def get_info_block2(self) -> str:
result += chr(i)
return result

def _data_to_message(self, data: str) -> bytes:
"""
Helper method to convert a string to byte array of 32 bytes.

@type data: str
@param data: The data to convert to byte array

@rtype: byte[32]
@return: It fills the rest of bytes with zeros.
"""
byte_array = bytearray([1])
for c in data:
byte_array.append(ord(c))

for i in range(32 - len(data)):
byte_array.append(0)

return bytes(byte_array)

def set_info_block1(self, data: str) -> None:
"""
Sets the infoblock1 with specified string.
Expand All @@ -482,7 +464,9 @@ def set_info_block1(self, data: str) -> None:
@type data: str
@param data: InfoBlock1 for the backend to set
"""
self.backend.control_transfer(0x20, 0x9, 0x0002, 0, self._data_to_message(data))
self.backend.control_transfer(
0x20, 0x9, 0x0002, 0, string_to_info_block_data(data)
)

def set_info_block2(self, data: str) -> None:
"""
Expand All @@ -493,7 +477,9 @@ def set_info_block2(self, data: str) -> None:
@type data: str
@param data: InfoBlock2 for the backend to set
"""
self.backend.control_transfer(0x20, 0x9, 0x0003, 0, self._data_to_message(data))
self.backend.control_transfer(
0x20, 0x9, 0x0003, 0, string_to_info_block_data(data)
)

def set_random_color(self) -> None:
"""
Expand Down
17 changes: 17 additions & 0 deletions src/blinkstick/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def string_to_info_block_data(data: str) -> bytes:
"""
Helper method to convert a string to byte array of 32 bytes.

@type data: str
@param data: The data to convert to byte array

@rtype: byte[32]
@return: It fills the rest of bytes with zeros.
"""
info_block_data = data[:31]
byte_array = bytearray([1] + [0] * 31)

for i, c in enumerate(info_block_data):
byte_array[i + 1] = ord(c)

return bytes(byte_array)
Empty file added tests/utilities/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions tests/utilities/test_string_to_info_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from blinkstick.utilities import string_to_info_block_data


def test_string_to_info_block_data_converts_string_to_byte_array():
block_string = "hello"
expected_padding_length = 31 - len(block_string)
result = string_to_info_block_data("hello")
expected = b"\x01hello" + b"\x00" * expected_padding_length
assert result == expected


def test_string_to_info_block_data_handles_empty_string():
result = string_to_info_block_data("")
expected = b"\x01" + b"\x00" * 31
assert result == expected


def test_string_to_info_block_data_truncates_long_string():
long_string = "a" * 40
result = string_to_info_block_data(long_string)
expected = b"\x01" + b"a" * 31
assert result == expected


def test_string_to_info_block_data_handles_exact_31_characters():
exact_string = "a" * 31
result = string_to_info_block_data(exact_string)
expected = b"\x01" + b"a" * 31
assert result == expected
Loading