from typing import List, Optional import pytest from aiogram.types import MessageEntity, User from aiogram.utils.text_decorations import ( TextDecoration, html_decoration, markdown_decoration, ) class TestTextDecoration: @pytest.mark.parametrize( "decorator,entity,result", [ [html_decoration, MessageEntity(type="url", offset=0, length=5), "test"], [ html_decoration, MessageEntity(type="text_link", offset=0, length=5, url="https://aiogram.dev"), 'test', ], [html_decoration, MessageEntity(type="bold", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="italic", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="code", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="pre", offset=0, length=5), "
test
"], [ html_decoration, MessageEntity(type="pre", offset=0, length=5, language="python"), '
test
', ], [html_decoration, MessageEntity(type="underline", offset=0, length=5), "test"], [ html_decoration, MessageEntity(type="strikethrough", offset=0, length=5), "test", ], [html_decoration, MessageEntity(type="hashtag", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="cashtag", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="bot_command", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="email", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="phone_number", offset=0, length=5), "test"], [ html_decoration, MessageEntity( type="text_mention", offset=0, length=5, user=User(id=42, first_name="Test", is_bot=False), ), 'test', ], [html_decoration, MessageEntity(type="url", offset=0, length=5), "test"], [ html_decoration, MessageEntity(type="spoiler", offset=0, length=5), "test", ], [ html_decoration, MessageEntity(type="custom_emoji", offset=0, length=5, custom_emoji_id="42"), 'test', ], [ html_decoration, MessageEntity(type="text_link", offset=0, length=5, url="https://aiogram.dev"), 'test', ], [ html_decoration, MessageEntity(type="blockquote", offset=0, length=5), "
test
", ], [ html_decoration, MessageEntity(type="expandable_blockquote", offset=0, length=5), "
test
", ], [markdown_decoration, MessageEntity(type="bold", offset=0, length=5), "*test*"], [markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_\rtest_\r"], [markdown_decoration, MessageEntity(type="code", offset=0, length=5), "`test`"], [markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```\ntest\n```"], [ markdown_decoration, MessageEntity(type="pre", offset=0, length=5, language="python"), "```python\ntest\n```", ], [ markdown_decoration, MessageEntity(type="underline", offset=0, length=5), "__\rtest__\r", ], [ markdown_decoration, MessageEntity(type="strikethrough", offset=0, length=5), "~test~", ], [markdown_decoration, MessageEntity(type="hashtag", offset=0, length=5), "test"], [markdown_decoration, MessageEntity(type="cashtag", offset=0, length=5), "test"], [markdown_decoration, MessageEntity(type="bot_command", offset=0, length=5), "test"], [markdown_decoration, MessageEntity(type="email", offset=0, length=5), "test"], [markdown_decoration, MessageEntity(type="phone_number", offset=0, length=5), "test"], [markdown_decoration, MessageEntity(type="spoiler", offset=0, length=5), "||test||"], [ markdown_decoration, MessageEntity(type="custom_emoji", offset=0, length=5, custom_emoji_id="42"), "![test](tg://emoji?id=42)", ], [ markdown_decoration, MessageEntity( type="text_mention", offset=0, length=5, user=User(id=42, first_name="Test", is_bot=False), ), "[test](tg://user?id=42)", ], [ markdown_decoration, MessageEntity(type="blockquote", offset=0, length=5), ">test", ], [ markdown_decoration, MessageEntity(type="expandable_blockquote", offset=0, length=5), ">test||", ], ], ) def test_apply_single_entity( self, decorator: TextDecoration, entity: MessageEntity, result: str ): assert decorator.apply_entity(entity, "test") == result def test_unknown_apply_entity(self): assert ( html_decoration.apply_entity( MessageEntity(type="unknown", offset=0, length=5), "" ) == "<test>" ) @pytest.mark.parametrize( "decorator,before,after", [ [html_decoration, "test", "test"], [html_decoration, "test < test", "test < test"], [html_decoration, "test > test", "test > test"], [html_decoration, "test & test", "test & test"], [html_decoration, "test @ test", "test @ test"], [markdown_decoration, "test", "test"], [markdown_decoration, "[test]", "\\[test\\]"], [markdown_decoration, "test ` test", "test \\` test"], [markdown_decoration, "test * test", "test \\* test"], [markdown_decoration, "test _ test", "test \\_ test"], ], ) def test_quote(self, decorator: TextDecoration, before: str, after: str): assert decorator.quote(before) == after @pytest.mark.parametrize( "decorator,text,entities,result", [ [html_decoration, "test", None, "test"], [html_decoration, "test", [], "test"], [ html_decoration, "test1 test2 test3 test4 test5 test6 test7", [ MessageEntity(type="bold", offset=6, length=29), MessageEntity(type="underline", offset=12, length=5), MessageEntity(type="italic", offset=24, length=5), ], "test1 test2 test3 test4 test5 test6 test7", ], [ html_decoration, "test1 test2 test3 test4 test5", [ MessageEntity(type="bold", offset=6, length=17), MessageEntity(type="underline", offset=12, length=5), ], "test1 test2 test3 test4 test5", ], [ html_decoration, "test1 test2 test3 test4", [ MessageEntity(type="bold", offset=6, length=11), MessageEntity(type="underline", offset=12, length=5), ], "test1 test2 test3 test4", ], [ html_decoration, "test1 test2 test3", [MessageEntity(type="bold", offset=6, length=5)], "test1 test2 test3", ], [ html_decoration, "test1 test2", [MessageEntity(type="bold", offset=0, length=5)], "test1 test2", ], [ html_decoration, "strike bold", [ MessageEntity(type="strikethrough", offset=0, length=6), MessageEntity(type="bold", offset=7, length=4), ], "strike bold", ], [ html_decoration, "test", [ MessageEntity(type="strikethrough", offset=0, length=5), MessageEntity(type="bold", offset=0, length=5), ], "test", ], [ html_decoration, "strikeboldunder", [ MessageEntity(type="strikethrough", offset=0, length=15), MessageEntity(type="bold", offset=6, length=9), MessageEntity(type="underline", offset=10, length=5), ], "strikeboldunder", ], [ html_decoration, "@username", [ MessageEntity(type="mention", offset=0, length=9), MessageEntity(type="bold", offset=0, length=9), ], "@username", ], [ html_decoration, "/command", [ MessageEntity(type="bot_command", offset=0, length=8), MessageEntity(type="bold", offset=0, length=8), ], "/command", ], [ html_decoration, "+1-212-555-0123", [ MessageEntity(type="phone_number", offset=0, length=15), MessageEntity(type="bold", offset=0, length=15), ], "+1-212-555-0123", ], [ html_decoration, "test te๐Ÿ‘๐Ÿฟst test", [MessageEntity(type="bold", offset=5, length=8, url=None, user=None)], "test te๐Ÿ‘๐Ÿฟst test", ], [ html_decoration, "๐Ÿ‘‹๐Ÿพ Hi!", [MessageEntity(type="bold", offset=0, length=8, url=None, user=None)], "๐Ÿ‘‹๐Ÿพ Hi!", ], [ html_decoration, "#test", [ MessageEntity(type="hashtag", offset=0, length=5), MessageEntity(type="bold", offset=0, length=5), ], "#test", ], [ html_decoration, "$TEST", [ MessageEntity(type="cashtag", offset=0, length=5), MessageEntity(type="bold", offset=0, length=5), ], "$TEST", ], [ html_decoration, "test@example.com", [ MessageEntity(type="email", offset=0, length=16), MessageEntity(type="bold", offset=0, length=16), ], "test@example.com", ], ], ) def test_unparse( self, decorator: TextDecoration, text: str, entities: Optional[List[MessageEntity]], result: str, ): assert decorator.unparse(text, entities) == result