-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from robberwick/_data_to_message
Add tests for string to bytes conversion and refactor
- Loading branch information
Showing
4 changed files
with
53 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |