mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
add tests and small fixes
This commit is contained in:
parent
a466fda6d3
commit
fc2bf6d58b
2 changed files with 37 additions and 4 deletions
|
|
@ -57,7 +57,7 @@ class TextDecoration(ABC):
|
|||
"""
|
||||
result = "".join(
|
||||
self._unparse_entities(
|
||||
text.encode('utf-16-le'), sorted(entities, key=lambda item: item.offset) if entities else []
|
||||
self._add_surrogates(text), sorted(entities, key=lambda item: item.offset) if entities else []
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
|
@ -76,8 +76,8 @@ class TextDecoration(ABC):
|
|||
for index, entity in enumerate(entities):
|
||||
if entity.offset * 2 < offset:
|
||||
continue
|
||||
if entity.offset * 2> offset:
|
||||
yield self.quote(text[offset : entity.offset * 2].decode('utf-16-le'))
|
||||
if entity.offset * 2 > offset:
|
||||
yield self.quote(self._remove_surrogates(text[offset : entity.offset * 2]))
|
||||
start = entity.offset * 2
|
||||
offset = entity.offset * 2 + entity.length * 2
|
||||
|
||||
|
|
@ -94,7 +94,15 @@ class TextDecoration(ABC):
|
|||
)
|
||||
|
||||
if offset < length:
|
||||
yield self.quote(text[offset:length].decode('utf-16-le'))
|
||||
yield self.quote(self._remove_surrogates(text[offset:length]))
|
||||
|
||||
@staticmethod
|
||||
def _add_surrogates(text: str):
|
||||
return text.encode('utf-16-le')
|
||||
|
||||
@staticmethod
|
||||
def _remove_surrogates(text: bytes):
|
||||
return text.decode('utf-16-le')
|
||||
|
||||
@abstractmethod
|
||||
def link(self, value: str, link: str) -> str: # pragma: no cover
|
||||
|
|
|
|||
25
tests/test_utils/test_text_decorations.py
Normal file
25
tests/test_utils/test_text_decorations.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from aiogram.types import MessageEntity, MessageEntityType
|
||||
from aiogram.utils import text_decorations
|
||||
|
||||
|
||||
class TestTextDecorations:
|
||||
def test_unparse_entities_normal_text(self):
|
||||
assert text_decorations.markdown_decoration.unparse(
|
||||
"hi i'm bold and italic and still bold",
|
||||
entities=[
|
||||
MessageEntity(offset=3, length=34, type=MessageEntityType.BOLD),
|
||||
MessageEntity(offset=12, length=10, type=MessageEntityType.ITALIC),
|
||||
]
|
||||
) == "hi *i'm bold _and italic_\r and still bold*"
|
||||
|
||||
def test_unparse_entities_emoji_text(self):
|
||||
"""
|
||||
emoji is encoded as two chars in json
|
||||
"""
|
||||
assert text_decorations.markdown_decoration.unparse(
|
||||
"🚀 i'm bold and italic and still bold",
|
||||
entities=[
|
||||
MessageEntity(offset=3, length=34, type=MessageEntityType.BOLD),
|
||||
MessageEntity(offset=12, length=10, type=MessageEntityType.ITALIC),
|
||||
]
|
||||
) == "🚀 *i'm bold _and italic_\r and still bold*"
|
||||
Loading…
Add table
Add a link
Reference in a new issue