Skip to content
Open
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
3 changes: 2 additions & 1 deletion telebot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def extract_arguments(text: str) -> Union[str, None]:
:rtype: :obj:`str` or :obj:`None`
"""
regexp = re.compile(r"/\w*(@\w*)*\s*([\s\S]*)", re.IGNORECASE)
if text is None: return None
result = regexp.match(text)
return result.group(2) if is_command(text) else None

Expand Down Expand Up @@ -357,7 +358,7 @@ def _text_before_last(substr: str) -> str:

parts = []
while True:
if len(text) < chars_per_string:
if len(text) <= chars_per_string:
parts.append(text)
return parts

Expand Down
38 changes: 38 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from telebot import util


def test_smart_split_exact_multiple_has_no_trailing_empty_part():
# A chunk boundary landing exactly on the limit used to append an empty part.
for size in (1, 2, 3, 4, 10):
for chunks in (1, 2, 3):
text = "a" * (size * chunks)
assert util.smart_split(text, size) == ["a" * size] * chunks


def test_smart_split_short_text():
assert util.smart_split("abc", 3) == ["abc"]
assert util.smart_split("abcde", 3) == ["abc", "de"]
assert util.smart_split("", 3) == [""]


def test_smart_split_prefers_newline_then_space():
assert util.smart_split("one two\nthree four", 10) == ["one two\n", "three four"]
assert util.smart_split("one two three", 8) == ["one two ", "three"]


def test_smart_split_caps_at_max_message_length():
parts = util.smart_split("a" * (util.MAX_MESSAGE_LENGTH * 2), util.MAX_MESSAGE_LENGTH * 5)
assert [len(p) for p in parts] == [util.MAX_MESSAGE_LENGTH] * 2


def test_extract_arguments_accepts_none():
# message.text is None for non-text messages
assert util.extract_arguments(None) is None # type: ignore[arg-type]
assert util.extract_command(None) is None # type: ignore[arg-type]


def test_extract_arguments_values():
assert util.extract_arguments("/get name") == "name"
assert util.extract_arguments("/get") == ""
assert util.extract_arguments("/get@bot name") == "name"
assert util.extract_arguments("hello") is None
Loading