Skip to content

Commit 42daf96

Browse files
Eldinnietsnoam
authored andcommitted
mention_markdown/html py2 fixes + unitests (python-telegram-bot#1112)
Fixes python-telegram-bot#1108
1 parent 87afd98 commit 42daf96

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

telegram/utils/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def mention_html(user_id, name):
9494
:obj:`str`: The inline mention for the user as html.
9595
"""
9696
if isinstance(user_id, int):
97-
return '<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name))
97+
return u'<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name))
9898

9999

100100
def mention_markdown(user_id, name):
@@ -107,7 +107,7 @@ def mention_markdown(user_id, name):
107107
:obj:`str`: The inline mention for the user as markdown.
108108
"""
109109
if isinstance(user_id, int):
110-
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
110+
return u'[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
111111

112112

113113
def effective_message_type(entity):

tests/test_helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19-
from telegram import Update
2019

2120
from telegram import Sticker
21+
from telegram import Update
2222
from telegram import User
2323
from telegram.message import Message
2424
from telegram.utils import helpers
@@ -55,3 +55,13 @@ def test_effective_message_type(self):
5555

5656
empty_update = Update(2)
5757
assert helpers.effective_message_type(empty_update) is None
58+
59+
def test_mention_html(self):
60+
expected = '<a href="tg://user?id=1">the name</a>'
61+
62+
assert expected == helpers.mention_html(1, 'the name')
63+
64+
def test_mention_markdown(self):
65+
expected = '[the name](tg://user?id=1)'
66+
67+
assert expected == helpers.mention_markdown(1, 'the name')

tests/test_user.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ def json_dict():
3535

3636
@pytest.fixture(scope='function')
3737
def user(bot):
38-
return User(TestUser.id, TestUser.first_name, TestUser.is_bot, last_name=TestUser.last_name,
39-
username=TestUser.username, language_code=TestUser.language_code, bot=bot)
38+
return User(id=TestUser.id, first_name=TestUser.first_name, is_bot=TestUser.is_bot,
39+
last_name=TestUser.last_name, username=TestUser.username,
40+
language_code=TestUser.language_code, bot=bot)
4041

4142

4243
class TestUser(object):
@@ -164,6 +165,22 @@ def test(*args, **kwargs):
164165
monkeypatch.setattr('telegram.Bot.send_voice', test)
165166
assert user.send_voice('test_voice')
166167

168+
def test_mention_html(self, user):
169+
expected = u'<a href="tg://user?id={}">{}</a>'
170+
171+
assert user.mention_html() == expected.format(user.id, user.full_name)
172+
assert user.mention_html('the<b>name\u2022') == expected.format(user.id,
173+
'the&lt;b&gt;name\u2022')
174+
assert user.mention_html(user.username) == expected.format(user.id, user.username)
175+
176+
def test_mention_markdown(self, user):
177+
expected = u'[{}](tg://user?id={})'
178+
179+
assert user.mention_markdown() == expected.format(user.full_name, user.id)
180+
assert user.mention_markdown('the_name*\u2022') == expected.format('the\_name\*\u2022',
181+
user.id)
182+
assert user.mention_markdown(user.username) == expected.format(user.username, user.id)
183+
167184
def test_equality(self):
168185
a = User(self.id, self.first_name, self.is_bot, self.last_name)
169186
b = User(self.id, self.first_name, self.is_bot, self.last_name)

0 commit comments

Comments
 (0)