mention_markdown/html py2 fixes + unitests (#1112)

Fixes #1108
This commit is contained in:
Eldinnie 2018-05-28 22:51:39 +02:00 committed by Noam Meltzer
parent 87afd98e02
commit 42daf96d20
3 changed files with 32 additions and 5 deletions

View file

@ -94,7 +94,7 @@ def mention_html(user_id, name):
:obj:`str`: The inline mention for the user as html. :obj:`str`: The inline mention for the user as html.
""" """
if isinstance(user_id, int): if isinstance(user_id, int):
return '<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name)) return u'<a href="tg://user?id={}">{}</a>'.format(user_id, escape(name))
def mention_markdown(user_id, name): def mention_markdown(user_id, name):
@ -107,7 +107,7 @@ def mention_markdown(user_id, name):
:obj:`str`: The inline mention for the user as markdown. :obj:`str`: The inline mention for the user as markdown.
""" """
if isinstance(user_id, int): if isinstance(user_id, int):
return '[{}](tg://user?id={})'.format(escape_markdown(name), user_id) return u'[{}](tg://user?id={})'.format(escape_markdown(name), user_id)
def effective_message_type(entity): def effective_message_type(entity):

View file

@ -16,9 +16,9 @@
# #
# You should have received a copy of the GNU Lesser Public License # You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
from telegram import Update
from telegram import Sticker from telegram import Sticker
from telegram import Update
from telegram import User from telegram import User
from telegram.message import Message from telegram.message import Message
from telegram.utils import helpers from telegram.utils import helpers
@ -55,3 +55,13 @@ class TestHelpers(object):
empty_update = Update(2) empty_update = Update(2)
assert helpers.effective_message_type(empty_update) is None assert helpers.effective_message_type(empty_update) is None
def test_mention_html(self):
expected = '<a href="tg://user?id=1">the name</a>'
assert expected == helpers.mention_html(1, 'the name')
def test_mention_markdown(self):
expected = '[the name](tg://user?id=1)'
assert expected == helpers.mention_markdown(1, 'the name')

View file

@ -35,8 +35,9 @@ def json_dict():
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def user(bot): def user(bot):
return User(TestUser.id, TestUser.first_name, TestUser.is_bot, last_name=TestUser.last_name, return User(id=TestUser.id, first_name=TestUser.first_name, is_bot=TestUser.is_bot,
username=TestUser.username, language_code=TestUser.language_code, bot=bot) last_name=TestUser.last_name, username=TestUser.username,
language_code=TestUser.language_code, bot=bot)
class TestUser(object): class TestUser(object):
@ -164,6 +165,22 @@ class TestUser(object):
monkeypatch.setattr('telegram.Bot.send_voice', test) monkeypatch.setattr('telegram.Bot.send_voice', test)
assert user.send_voice('test_voice') assert user.send_voice('test_voice')
def test_mention_html(self, user):
expected = u'<a href="tg://user?id={}">{}</a>'
assert user.mention_html() == expected.format(user.id, user.full_name)
assert user.mention_html('the<b>name\u2022') == expected.format(user.id,
'the&lt;b&gt;name\u2022')
assert user.mention_html(user.username) == expected.format(user.id, user.username)
def test_mention_markdown(self, user):
expected = u'[{}](tg://user?id={})'
assert user.mention_markdown() == expected.format(user.full_name, user.id)
assert user.mention_markdown('the_name*\u2022') == expected.format('the\_name\*\u2022',
user.id)
assert user.mention_markdown(user.username) == expected.format(user.username, user.id)
def test_equality(self): def test_equality(self):
a = User(self.id, self.first_name, self.is_bot, self.last_name) a = User(self.id, self.first_name, self.is_bot, self.last_name)
b = User(self.id, self.first_name, self.is_bot, self.last_name) b = User(self.id, self.first_name, self.is_bot, self.last_name)