emoji: Add translate_emoticons flag in bugdown testcases.

Also switches the default behaviour of the code to not translate the
emoticons. Earlier, the code was testing-aware, and used to translate
when there was no user profile data available(assuming that as a testing
environment).
This commit is contained in:
Rohitt Vashishtha
2018-03-26 02:53:22 +05:30
committed by Tim Abbott
parent b976447102
commit 3c96b04763
4 changed files with 34 additions and 10 deletions

View File

@@ -181,6 +181,7 @@ var bugdown_data = JSON.parse(fs.readFileSync(path.join(__dirname, '../../zerver
tests.forEach(function (test) {
var message = {raw_content: test.input};
page_params.translate_emoticons = test.translate_emoticons || false;
markdown.apply_markdown(message);
var output = message.content;

View File

@@ -346,24 +346,40 @@
"expected_output": "<p>test <span class=\"emoji emoji-1f604\" title=\"smile\">:smile:</span> again <span class=\"emoji emoji-1f4a9\" title=\"poop\">:poop:</span><br>\n foobar x::y::z :wasted waste: :fakeemojithisshouldnotrender:</p>",
"text_content": "test \ud83d\ude04 again \ud83d\udca9\n foobar x::y::z :wasted waste: :fakeemojithisshouldnotrender:"
},
{
"name": "translate_emoticons_not_enabled",
"input": ":)",
"expected_output": "<p>:)</p>",
"text_content": ":)",
"translate_emoticons": false
},
{
"name": "translate_emoticons_enabled",
"input": ":)",
"expected_output": "<p><span class=\"emoji emoji-1f603\" title=\"smiley\">:smiley:</span></p>",
"text_content": "\ud83d\ude03",
"translate_emoticons": true
},
{
"name": "translate_emoticons",
"input": ":) foo :( bar <3 with space : ) real emoji :smiley:",
"expected_output": "<p><span class=\"emoji emoji-1f603\" title=\"smiley\">:smiley:</span> foo <span class=\"emoji emoji-1f641\" title=\"slightly frowning face\">:slightly_frowning_face:</span> bar <span class=\"emoji emoji-2764\" title=\"heart\">:heart:</span> with space : ) real emoji <span class=\"emoji emoji-1f603\" title=\"smiley\">:smiley:</span></p>",
"marked_expected_output": "<p>:) foo :( bar &lt;3 with space : ) real emoji <span class=\"emoji emoji-1f603\" title=\"smiley\">:smiley:</span></p>",
"text_content": "\ud83d\ude03 foo \ud83d\ude41 bar \u2764 with space : ) real emoji \ud83d\ude03"
"text_content": "\ud83d\ude03 foo \ud83d\ude41 bar \u2764 with space : ) real emoji \ud83d\ude03",
"translate_emoticons": true
},
{
"name": "translate_emoticons_whitepsace",
"input": "a:) ;)b",
"expected_output": "<p>a:) ;)b</p>",
"text_content": "a:) ;)b"
"text_content": "a:) ;)b",
"translate_emoticons": true
},
{
"name": "translate_emoticons_in_code",
"input": "`:)`",
"expected_output": "<p><code>:)</code></p>",
"text_content": ":)"
"text_content": ":)",
"translate_emoticons": true
},
{
"name": "random_emoji_1",

View File

@@ -1013,9 +1013,8 @@ def unicode_emoji_to_codepoint(unicode_emoji: Text) -> Text:
class EmoticonTranslation(markdown.inlinepatterns.Pattern):
""" Translates emoticons like `:)` into emoji like `:smile:`. """
def handleMatch(self, match: Match[Text]) -> Optional[Element]:
# If there is `db_data` and it is false, then don't do translating.
# If there is no `db_data`, such as during tests, translate.
if db_data is not None and not db_data['translate_emoticons']:
# If there is `db_data` and it is True, proceed with translating.
if db_data is None or not db_data['translate_emoticons']:
return None
emoticon = match.group('emoticon')

View File

@@ -245,15 +245,23 @@ class BugdownTest(ZulipTestCase):
@slow("Aggregate of runs dozens of individual markdown tests")
def test_bugdown_fixtures(self) -> None:
format_tests, linkify_tests = self.load_bugdown_tests()
valid_keys = set(['name', "input", "expected_output",
valid_keys = set(["name", "input", "expected_output",
"backend_only_rendering",
"marked_expected_output", "text_content"])
"marked_expected_output", "text_content",
"translate_emoticons"])
for name, test in format_tests.items():
# Check that there aren't any unexpected keys as those are often typos
self.assertEqual(len(set(test.keys()) - valid_keys), 0)
converted = bugdown_convert(test['input'])
if test.get('translate_emoticons', False):
# Create a userprofile and send message with it.
user_profile = self.example_user('othello')
do_set_user_display_setting(user_profile, 'translate_emoticons', True)
msg = Message(sender=user_profile, sending_client=get_client("test"))
converted = render_markdown(msg, test['input'])
else:
converted = bugdown_convert(test['input'])
print("Running Bugdown test %s" % (name,))
self.assertEqual(converted, test['expected_output'])