test_markup.py 2.5 KB
Newer Older
Diana Huang committed
1 2
# -*- coding: utf-8 -*-
"""
3
Tests for openedx.core.djangolib.markup
Diana Huang committed
4 5
"""

6
from nose.plugins.attrib import attr
Diana Huang committed
7 8 9
import unittest

import ddt
10
from django.utils.translation import ugettext as _, ungettext
11
from mako.template import Template
Diana Huang committed
12

13
from openedx.core.djangolib.markup import HTML, Text
Diana Huang committed
14 15


16
@attr(shard=2)
Diana Huang committed
17 18 19 20 21 22 23 24 25 26 27 28 29
@ddt.ddt
class FormatHtmlTest(unittest.TestCase):
    """Test that we can format plain strings and HTML into them properly."""

    @ddt.data(
        (u"hello", u"hello"),
        (u"<hello>", u"&lt;hello&gt;"),
        (u"It's cool", u"It&#39;s cool"),
        (u'"cool," she said.', u'&#34;cool,&#34; she said.'),
        (u"Stop & Shop", u"Stop &amp; Shop"),
        (u"<a>нтмℓ-єѕ¢αρє∂</a>", u"&lt;a&gt;нтмℓ-єѕ¢αρє∂&lt;/a&gt;"),
    )
    def test_simple(self, (before, after)):
30 31
        self.assertEqual(unicode(Text(_(before))), after)  # pylint: disable=translation-of-non-string
        self.assertEqual(unicode(Text(before)), after)
Diana Huang committed
32 33 34

    def test_formatting(self):
        # The whole point of this function is to make sure this works:
35
        out = Text(_(u"Point & click {start}here{end}!")).format(
Diana Huang committed
36 37 38 39 40 41 42 43 44 45 46
            start=HTML("<a href='http://edx.org'>"),
            end=HTML("</a>"),
        )
        self.assertEqual(
            unicode(out),
            u"Point &amp; click <a href='http://edx.org'>here</a>!",
        )

    def test_nested_formatting(self):
        # Sometimes, you have plain text, with html inserted, and the html has
        # plain text inserted.  It gets twisty...
47
        out = Text(_(u"Send {start}email{end}")).format(
Diana Huang committed
48 49 50 51 52 53 54 55 56 57 58 59
            start=HTML("<a href='mailto:{email}'>").format(email="A&B"),
            end=HTML("</a>"),
        )
        self.assertEqual(
            unicode(out),
            u"Send <a href='mailto:A&amp;B'>email</a>",
        )

    def test_mako(self):
        # The default_filters used here have to match the ones in edxmako.
        template = Template(
            """
60 61 62
                <%!
                from django.utils.translation import ugettext as _

63
                from openedx.core.djangolib.markup import HTML, Text
64 65
                %>
                ${Text(_(u"A & {BC}")).format(BC=HTML("B & C"))}
Diana Huang committed
66 67 68
            """,
            default_filters=['decode.utf8', 'h'],
        )
69
        out = template.render()
Diana Huang committed
70 71 72 73
        self.assertEqual(out.strip(), u"A &amp; B & C")

    def test_ungettext(self):
        for i in [1, 2]:
74
            out = Text(ungettext("1 & {}", "2 & {}", i)).format(HTML("<>"))
Diana Huang committed
75
            self.assertEqual(out, "{} &amp; <>".format(i))