summaryrefslogtreecommitdiffstats
path: root/contrib/python/MarkupSafe/py2/tests
diff options
context:
space:
mode:
authorDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/python/MarkupSafe/py2/tests
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/python/MarkupSafe/py2/tests')
-rw-r--r--contrib/python/MarkupSafe/py2/tests/conftest.py37
-rw-r--r--contrib/python/MarkupSafe/py2/tests/test_escape.py30
-rw-r--r--contrib/python/MarkupSafe/py2/tests/test_exception_custom_html.py21
-rw-r--r--contrib/python/MarkupSafe/py2/tests/test_leak.py29
-rw-r--r--contrib/python/MarkupSafe/py2/tests/test_markupsafe.py196
-rw-r--r--contrib/python/MarkupSafe/py2/tests/ya.make19
6 files changed, 332 insertions, 0 deletions
diff --git a/contrib/python/MarkupSafe/py2/tests/conftest.py b/contrib/python/MarkupSafe/py2/tests/conftest.py
new file mode 100644
index 00000000000..296cd58f5f6
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/conftest.py
@@ -0,0 +1,37 @@
+import pytest
+
+from markupsafe import _native
+
+try:
+ from markupsafe import _speedups
+except ImportError:
+ _speedups = None
+
+
+ scope="session",
+ params=(
+ _native,
+ pytest.param(
+ _speedups,
+ marks=pytest.mark.skipif(_speedups is None, reason="speedups unavailable"),
+ ),
+ ),
+)
+def _mod(request):
+ return request.param
+
+
[email protected](scope="session")
+def escape(_mod):
+ return _mod.escape
+
+
[email protected](scope="session")
+def escape_silent(_mod):
+ return _mod.escape_silent
+
+
[email protected](scope="session")
+def soft_str(_mod):
+ return _mod.soft_unicode
diff --git a/contrib/python/MarkupSafe/py2/tests/test_escape.py b/contrib/python/MarkupSafe/py2/tests/test_escape.py
new file mode 100644
index 00000000000..788134aeaa4
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/test_escape.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+from markupsafe import Markup
+
+
+ ("value", "expect"),
+ (
+ # empty
+ (u"", u""),
+ # ascii
+ (u"abcd&><'\"efgh", u"abcd&amp;&gt;&lt;&#39;&#34;efgh"),
+ (u"&><'\"efgh", u"&amp;&gt;&lt;&#39;&#34;efgh"),
+ (u"abcd&><'\"", u"abcd&amp;&gt;&lt;&#39;&#34;"),
+ # 2 byte
+ (u"こんにちは&><'\"こんばんは", u"こんにちは&amp;&gt;&lt;&#39;&#34;こんばんは"),
+ (u"&><'\"こんばんは", u"&amp;&gt;&lt;&#39;&#34;こんばんは"),
+ (u"こんにちは&><'\"", u"こんにちは&amp;&gt;&lt;&#39;&#34;"),
+ # 4 byte
+ (
+ u"\U0001F363\U0001F362&><'\"\U0001F37A xyz",
+ u"\U0001F363\U0001F362&amp;&gt;&lt;&#39;&#34;\U0001F37A xyz",
+ ),
+ (u"&><'\"\U0001F37A xyz", u"&amp;&gt;&lt;&#39;&#34;\U0001F37A xyz"),
+ (u"\U0001F363\U0001F362&><'\"", u"\U0001F363\U0001F362&amp;&gt;&lt;&#39;&#34;"),
+ ),
+)
+def test_escape(escape, value, expect):
+ assert escape(value) == Markup(expect)
diff --git a/contrib/python/MarkupSafe/py2/tests/test_exception_custom_html.py b/contrib/python/MarkupSafe/py2/tests/test_exception_custom_html.py
new file mode 100644
index 00000000000..5f9ffde438d
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/test_exception_custom_html.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+from markupsafe import escape
+
+
+class CustomHtmlThatRaises(object):
+ def __html__(self):
+ raise ValueError(123)
+
+
+def test_exception_custom_html():
+ """Checks whether exceptions in custom __html__ implementations are
+ propagated correctly.
+
+ There was a bug in the native implementation at some point:
+ https://github.com/pallets/markupsafe/issues/108
+ """
+ obj = CustomHtmlThatRaises()
+ with pytest.raises(ValueError):
+ escape(obj)
diff --git a/contrib/python/MarkupSafe/py2/tests/test_leak.py b/contrib/python/MarkupSafe/py2/tests/test_leak.py
new file mode 100644
index 00000000000..b36a4ce4bc1
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/test_leak.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+import gc
+import sys
+
+import pytest
+
+from markupsafe import escape
+
+
+ escape.__module__ == "markupsafe._native",
+ reason="only test memory leak with speedups",
+)
+def test_markup_leaks():
+ counts = set()
+
+ for _i in range(20):
+ for _j in range(1000):
+ escape("foo")
+ escape("<foo>")
+ escape(u"foo")
+ escape(u"<foo>")
+
+ if hasattr(sys, "pypy_version_info"):
+ gc.collect()
+
+ counts.add(len(gc.get_objects()))
+
+ assert len(counts) == 1
diff --git a/contrib/python/MarkupSafe/py2/tests/test_markupsafe.py b/contrib/python/MarkupSafe/py2/tests/test_markupsafe.py
new file mode 100644
index 00000000000..5b080062629
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/test_markupsafe.py
@@ -0,0 +1,196 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+from markupsafe import escape
+from markupsafe import escape_silent
+from markupsafe import Markup
+from markupsafe._compat import PY2
+from markupsafe._compat import text_type
+
+
+def test_adding():
+ unsafe = '<script type="application/x-some-script">alert("foo");</script>'
+ safe = Markup("<em>username</em>")
+ assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
+
+
+ ("template", "data", "expect"),
+ (
+ ("<em>%s</em>", "<bad user>", "<em>&lt;bad user&gt;</em>"),
+ (
+ "<em>%(username)s</em>",
+ {"username": "<bad user>"},
+ "<em>&lt;bad user&gt;</em>",
+ ),
+ ("%i", 3.14, "3"),
+ ("%.2f", 3.14, "3.14"),
+ ),
+)
+def test_string_interpolation(template, data, expect):
+ assert Markup(template) % data == expect
+
+
+def test_type_behavior():
+ assert type(Markup("foo") + "bar") is Markup
+ x = Markup("foo")
+ assert x.__html__() is x
+
+
+def test_html_interop():
+ class Foo(object):
+ def __html__(self):
+ return "<em>awesome</em>"
+
+ def __unicode__(self):
+ return "awesome"
+
+ __str__ = __unicode__
+
+ assert Markup(Foo()) == "<em>awesome</em>"
+ result = Markup("<strong>%s</strong>") % Foo()
+ assert result == "<strong><em>awesome</em></strong>"
+
+
+def test_tuple_interpol():
+ result = Markup("<em>%s:%s</em>") % ("<foo>", "<bar>")
+ expect = Markup(u"<em>&lt;foo&gt;:&lt;bar&gt;</em>")
+ assert result == expect
+
+
+def test_dict_interpol():
+ result = Markup("<em>%(foo)s</em>") % {"foo": "<foo>"}
+ expect = Markup(u"<em>&lt;foo&gt;</em>")
+ assert result == expect
+
+ result = Markup("<em>%(foo)s:%(bar)s</em>") % {"foo": "<foo>", "bar": "<bar>"}
+ expect = Markup(u"<em>&lt;foo&gt;:&lt;bar&gt;</em>")
+ assert result == expect
+
+
+def test_escaping():
+ assert escape("\"<>&'") == "&#34;&lt;&gt;&amp;&#39;"
+ assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
+
+
+def test_unescape():
+ assert Markup("&lt;test&gt;").unescape() == "<test>"
+
+ result = Markup("jack & tavi are cooler than mike &amp; russ").unescape()
+ expect = "jack & tavi are cooler than mike & russ"
+ assert result == expect
+
+ original = "&foo&#x3b;"
+ once = Markup(original).unescape()
+ twice = Markup(once).unescape()
+ expect = "&foo;"
+ assert once == expect
+ assert twice == expect
+
+
+def test_format():
+ result = Markup("<em>{awesome}</em>").format(awesome="<awesome>")
+ assert result == "<em>&lt;awesome&gt;</em>"
+
+ result = Markup("{0[1][bar]}").format([0, {"bar": "<bar/>"}])
+ assert result == "&lt;bar/&gt;"
+
+ result = Markup("{0[1][bar]}").format([0, {"bar": Markup("<bar/>")}])
+ assert result == "<bar/>"
+
+
+def test_formatting_empty():
+ formatted = Markup("{}").format(0)
+ assert formatted == Markup("0")
+
+
+def test_custom_formatting():
+ class HasHTMLOnly(object):
+ def __html__(self):
+ return Markup("<foo>")
+
+ class HasHTMLAndFormat(object):
+ def __html__(self):
+ return Markup("<foo>")
+
+ def __html_format__(self, spec):
+ return Markup("<FORMAT>")
+
+ assert Markup("{0}").format(HasHTMLOnly()) == Markup("<foo>")
+ assert Markup("{0}").format(HasHTMLAndFormat()) == Markup("<FORMAT>")
+
+
+def test_complex_custom_formatting():
+ class User(object):
+ def __init__(self, id, username):
+ self.id = id
+ self.username = username
+
+ def __html_format__(self, format_spec):
+ if format_spec == "link":
+ return Markup('<a href="/user/{0}">{1}</a>').format(
+ self.id, self.__html__()
+ )
+ elif format_spec:
+ raise ValueError("Invalid format spec")
+
+ return self.__html__()
+
+ def __html__(self):
+ return Markup("<span class=user>{0}</span>").format(self.username)
+
+ user = User(1, "foo")
+ result = Markup("<p>User: {0:link}").format(user)
+ expect = Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
+ assert result == expect
+
+
+def test_formatting_with_objects():
+ class Stringable(object):
+ def __unicode__(self):
+ return u"строка"
+
+ if PY2:
+
+ def __str__(self):
+ return "some other value"
+
+ else:
+ __str__ = __unicode__
+
+ assert Markup("{s}").format(s=Stringable()) == Markup(u"строка")
+
+
+def test_all_set():
+ import markupsafe as markup
+
+ for item in markup.__all__:
+ getattr(markup, item)
+
+
+def test_escape_silent():
+ assert escape_silent(None) == Markup()
+ assert escape(None) == Markup(None)
+ assert escape_silent("<foo>") == Markup(u"&lt;foo&gt;")
+
+
+def test_splitting():
+ expect = [Markup("a"), Markup("b")]
+ assert Markup("a b").split() == expect
+ assert Markup("a b").rsplit() == expect
+ assert Markup("a\nb").splitlines() == expect
+
+
+def test_mul():
+ assert Markup("a") * 3 == Markup("aaa")
+
+
+def test_escape_return_type():
+ assert isinstance(escape("a"), Markup)
+ assert isinstance(escape(Markup("a")), Markup)
+
+ class Foo:
+ def __html__(self):
+ return "<strong>Foo</strong>"
+
+ assert isinstance(escape(Foo()), Markup)
diff --git a/contrib/python/MarkupSafe/py2/tests/ya.make b/contrib/python/MarkupSafe/py2/tests/ya.make
new file mode 100644
index 00000000000..db00a36a1f1
--- /dev/null
+++ b/contrib/python/MarkupSafe/py2/tests/ya.make
@@ -0,0 +1,19 @@
+PY2TEST()
+
+OWNER(g:python-contrib)
+
+PEERDIR(
+ contrib/python/MarkupSafe
+)
+
+TEST_SRCS(
+ conftest.py
+ test_escape.py
+ test_exception_custom_html.py
+ test_leak.py
+ test_markupsafe.py
+)
+
+NO_LINT()
+
+END()