| 1 |
"""Test cases for Highlight.""" |
| 2 |
from .. import util |
| 3 |
import pytest |
| 4 |
import pymdownx.emoji as emoji |
| 5 |
import markdown |
| 6 |
import textwrap |
| 7 |
from pymdownx.emoji import EMOJIONE_PNG_CDN, TWEMOJI_PNG_CDN |
| 8 |
|
| 9 |
|
| 10 |
def _old_style_index(): |
| 11 |
"""Custom index with no arguments (old style).""" |
| 12 |
|
| 13 |
return emoji.twemoji({}, None) |
| 14 |
|
| 15 |
|
| 16 |
def _new_style_index(options, md): |
| 17 |
"""Custom index with arguments (new style).""" |
| 18 |
|
| 19 |
index = emoji.twemoji({}, None) |
| 20 |
|
| 21 |
if 'append_alias' in options: |
| 22 |
for alias, original in options['append_alias']: |
| 23 |
index['aliases'][alias] = original |
| 24 |
|
| 25 |
return index |
| 26 |
|
| 27 |
|
| 28 |
class TestEmojiOldIndex(util.MdCase): |
| 29 |
"""Test old style index.""" |
| 30 |
|
| 31 |
extension = [ |
| 32 |
'pymdownx.emoji' |
| 33 |
] |
| 34 |
extension_configs = { |
| 35 |
'pymdownx.emoji': { |
| 36 |
'emoji_index': _old_style_index |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
@pytest.mark.filterwarnings("ignore") |
| 41 |
def test_old_index(self): |
| 42 |
"""Test that index works.""" |
| 43 |
|
| 44 |
with pytest.warns(DeprecationWarning): |
| 45 |
self.assertEqual( |
| 46 |
markdown.Markdown( |
| 47 |
extensions=self.extension, |
| 48 |
extension_configs=self.extension_configs |
| 49 |
).convert(':smile:'), |
| 50 |
f'<p><img alt="\U0001f604" class="twemoji" src="{TWEMOJI_PNG_CDN}1f604.png" title=":smile:" /></p>' |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
class TestEmojiNewIndex(util.MdCase): |
| 55 |
"""Test new style index.""" |
| 56 |
|
| 57 |
extension = [ |
| 58 |
'pymdownx.emoji' |
| 59 |
] |
| 60 |
extension_configs = { |
| 61 |
'pymdownx.emoji': { |
| 62 |
'emoji_index': _new_style_index, |
| 63 |
'options': {'append_alias': [(':grin:', ":smile:")]} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
def test_new_index(self): |
| 68 |
"""Test that we can pass options using the new style.""" |
| 69 |
|
| 70 |
self.check_markdown( |
| 71 |
':grin:', |
| 72 |
f'<p><img alt="\U0001f604" class="twemoji" src="{TWEMOJI_PNG_CDN}1f604.png" title=":grin:" /></p>' |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
class TestEmojiStrict(util.MdCase): |
| 77 |
"""Test strict mode.""" |
| 78 |
|
| 79 |
def test_strict(self): |
| 80 |
"""Test strict mode.""" |
| 81 |
|
| 82 |
MD1 = ":apple:" |
| 83 |
|
| 84 |
MD2 = ":apple:\n:bad:\n:whatever:\n\n:yep:\n" |
| 85 |
|
| 86 |
extension_configs = { |
| 87 |
'pymdownx.emoji': { |
| 88 |
'strict': True |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
extensions = ['pymdownx.emoji'] |
| 93 |
md = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) |
| 94 |
self.assertEqual( |
| 95 |
md.convert(MD1), |
| 96 |
f"""<p><img alt="🍎" class="emojione" src="{EMOJIONE_PNG_CDN}1f34e.png" title=":apple:" /></p>""" |
| 97 |
) |
| 98 |
md.reset() |
| 99 |
error = "" |
| 100 |
try: |
| 101 |
md.convert(MD2) |
| 102 |
except RuntimeError as e: |
| 103 |
error = str(e) |
| 104 |
self.assertEqual( |
| 105 |
error, |
| 106 |
textwrap.dedent( |
| 107 |
""" |
| 108 |
Emoji Extension (strict mode): The following emoji were detected and either had |
| 109 |
their name change, were removed, or have never existed. |
| 110 |
|
| 111 |
- :bad: |
| 112 |
- :whatever: |
| 113 |
- :yep: |
| 114 |
""" |
| 115 |
) |
| 116 |
) |
| 117 |
|
| 118 |
md.reset() |
| 119 |
self.assertEqual( |
| 120 |
md.convert(MD1), |
| 121 |
f"""<p><img alt="🍎" class="emojione" src="{EMOJIONE_PNG_CDN}1f34e.png" title=":apple:" /></p>""" |
| 122 |
) |