# Pyrogram - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan # # This file is part of Pyrogram. # # Pyrogram is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Pyrogram is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . import pyrogram from pyrogram.parser.html import HTML # expected: the expected unparsed HTML # text: original text without entities # entities: message entities coming from the server def test_html_unparse_bold(): expected = "bold" text = "bold" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=4)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_italic(): expected = "italic" text = "italic" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=0, length=6)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_underline(): expected = "underline" text = "underline" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=0, length=9)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_strike(): expected = "strike" text = "strike" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=0, length=6)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_spoiler(): expected = "spoiler" text = "spoiler" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=0, length=7)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_url(): expected = 'URL' text = "URL" entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.TEXT_LINK, offset=0, length=3, url='https://pyrogram.org/')]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_code(): expected = 'code' text = "code" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=0, length=4)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_pre(): expected = """
for i in range(10):
    print(i)
""" text = """for i in range(10): print(i)""" entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.PRE, offset=0, length=32, language='python')]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_blockquote(): expected = """
Quote text
from pyrogram""" text = """Quote text from pyrogram""" entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BLOCKQUOTE, offset=0, length=10)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_mixed(): expected = "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd" \ "eeeeeeeeeeffffffffffgggggggggghhhhhhhhhh" text = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhh" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=14), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=7, length=7), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=10, length=4), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=14, length=9), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=14, length=9), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=23, length=10), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=30, length=3), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=33, length=10), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=38, length=5), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=43, length=10), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=57, length=10)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_escaped(): expected = "<b>bold</b>" text = "bold" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=11)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_escaped_nested(): expected = "<b>bold <u>underline</u> bold</b>" text = "bold underline bold" entities = pyrogram.types.List( [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=33), pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=8, length=16)]) assert HTML.unparse(text=text, entities=entities) == expected def test_html_unparse_no_entities(): expected = "text" text = "text" entities = [] assert HTML.unparse(text=text, entities=entities) == expected