|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import pyrogram |
| 20 | +from pyrogram.parser.html import HTML |
| 21 | + |
| 22 | + |
| 23 | +# expected: the expected unparsed HTML |
| 24 | +# text: original text without entities |
| 25 | +# entities: message entities coming from the server |
| 26 | + |
| 27 | +def test_html_unparse_bold(): |
| 28 | + expected = "<b>bold</b>" |
| 29 | + text = "bold" |
| 30 | + entities = pyrogram.types.List( |
| 31 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=4)]) |
| 32 | + |
| 33 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 34 | + |
| 35 | + |
| 36 | +def test_html_unparse_italic(): |
| 37 | + expected = "<i>italic</i>" |
| 38 | + text = "italic" |
| 39 | + entities = pyrogram.types.List( |
| 40 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=0, length=6)]) |
| 41 | + |
| 42 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 43 | + |
| 44 | + |
| 45 | +def test_html_unparse_underline(): |
| 46 | + expected = "<u>underline</u>" |
| 47 | + text = "underline" |
| 48 | + entities = pyrogram.types.List( |
| 49 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=0, length=9)]) |
| 50 | + |
| 51 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 52 | + |
| 53 | + |
| 54 | +def test_html_unparse_strike(): |
| 55 | + expected = "<s>strike</s>" |
| 56 | + text = "strike" |
| 57 | + entities = pyrogram.types.List( |
| 58 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=0, length=6)]) |
| 59 | + |
| 60 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 61 | + |
| 62 | + |
| 63 | +def test_html_unparse_spoiler(): |
| 64 | + expected = "<spoiler>spoiler</spoiler>" |
| 65 | + text = "spoiler" |
| 66 | + entities = pyrogram.types.List( |
| 67 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=0, length=7)]) |
| 68 | + |
| 69 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 70 | + |
| 71 | + |
| 72 | +def test_html_unparse_url(): |
| 73 | + expected = '<a href="https://pyrogram.org/">URL</a>' |
| 74 | + text = "URL" |
| 75 | + entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.TEXT_LINK, |
| 76 | + offset=0, length=3, url='https://pyrogram.org/')]) |
| 77 | + |
| 78 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 79 | + |
| 80 | + |
| 81 | +def test_html_unparse_code(): |
| 82 | + expected = '<code>code</code>' |
| 83 | + text = "code" |
| 84 | + entities = pyrogram.types.List( |
| 85 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=0, length=4)]) |
| 86 | + |
| 87 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 88 | + |
| 89 | + |
| 90 | +def test_html_unparse_pre(): |
| 91 | + expected = """<pre language="python">for i in range(10): |
| 92 | + print(i)</pre>""" |
| 93 | + |
| 94 | + text = """for i in range(10): |
| 95 | + print(i)""" |
| 96 | + |
| 97 | + entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.PRE, offset=0, |
| 98 | + length=32, language='python')]) |
| 99 | + |
| 100 | + assert HTML.unparse(text=text, entities=entities) == expected |
| 101 | + |
| 102 | + |
| 103 | +def test_html_unparse_mixed(): |
| 104 | + expected = "<b>aaaaaaa<i>aaa<u>bbbb</u></i></b><u><i>bbbbbbccc</i></u><u>ccccccc<s>ddd</s></u><s>ddddd<spoiler>dd" \ |
| 105 | + "eee</spoiler></s><spoiler>eeeeeeefff</spoiler>ffff<code>fffggggggg</code>ggghhhhhhhhhh" |
| 106 | + text = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhh" |
| 107 | + entities = pyrogram.types.List( |
| 108 | + [pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=14), |
| 109 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=7, length=7), |
| 110 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=10, length=4), |
| 111 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=14, length=9), |
| 112 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=14, length=9), |
| 113 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.UNDERLINE, offset=23, length=10), |
| 114 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=30, length=3), |
| 115 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=33, length=10), |
| 116 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=38, length=5), |
| 117 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=43, length=10), |
| 118 | + pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=57, length=10)]) |
| 119 | + |
| 120 | + assert HTML.unparse(text=text, entities=entities) == expected |
0 commit comments