forked from KurimuzonAkuma/kurigram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_markdown.py
More file actions
147 lines (105 loc) · 5.63 KB
/
Copy pathtest_markdown.py
File metadata and controls
147 lines (105 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram.parser.markdown import Markdown
# expected: the expected unparsed Markdown
# text: original text without entities
# entities: message entities coming from the server
def test_markdown_unparse_bold():
expected = "**bold**"
text = "bold"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=4)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_italic():
expected = "__italic__"
text = "italic"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=0, length=6)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_strike():
expected = "~~strike~~"
text = "strike"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=0, length=6)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_spoiler():
expected = "||spoiler||"
text = "spoiler"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=0, length=7)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_url():
expected = '[URL](https://pyrogram.org/)'
text = "URL"
entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.TEXT_LINK,
offset=0, length=3, url='https://pyrogram.org/')])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_emoji():
expected = ' im crying'
text = "🥲 im crying"
entities = pyrogram.types.List([pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CUSTOM_EMOJI,
offset=0, length=2, custom_emoji_id=5195264424893488796)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_code():
expected = '`code`'
text = "code"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=0, length=4)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_pre():
expected = """```python
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 Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_blockquote():
expected = """> Hello
> from
> pyrogram!"""
text = """Hello\nfrom\n\npyrogram!"""
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BLOCKQUOTE, offset=0, length=10),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BLOCKQUOTE, offset=12, length=9)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_mixed():
expected = "**aaaaaaa__aaabbb**__~~dddddddd||ddeee~~||||eeeeeeefff||ffff`fffggggggg`ggghhhhhhhhhh"
text = "aaaaaaaaaabbbddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhh"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.BOLD, offset=0, length=13),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=7, length=6),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.STRIKETHROUGH, offset=13, length=13),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=21, length=5),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.SPOILER, offset=26, length=10),
pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.CODE, offset=40, length=10)])
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_no_entities():
expected = "text"
text = "text"
entities = []
assert Markdown.unparse(text=text, entities=entities) == expected
def test_markdown_unparse_html():
expected = "__This works, it's ok__ <b>This shouldn't</b>"
text = "This works, it's ok <b>This shouldn't</b>"
entities = pyrogram.types.List(
[pyrogram.types.MessageEntity(type=pyrogram.enums.MessageEntityType.ITALIC, offset=0, length=19)])
assert Markdown.unparse(text=text, entities=entities) == expected