From ea75ec929f07b320ebbed5ddd0605cd9b5122097 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 22 Jun 2010 19:45:11 +0200 Subject: Added testsuite --- .gitignore | 1 + markupsafe/__init__.py | 2 +- markupsafe/tests.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 markupsafe/tests.py diff --git a/.gitignore b/.gitignore index 6cc7ed2..b57d2be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.pyc *.pyo *.o +*.so env dist build diff --git a/markupsafe/__init__.py b/markupsafe/__init__.py index 8053ce0..3e07f49 100644 --- a/markupsafe/__init__.py +++ b/markupsafe/__init__.py @@ -12,7 +12,7 @@ import re from itertools import imap -__all__ = ['Module', 'soft_unicode'] +__all__ = ['Module', 'soft_unicode', 'escape'] _striptags_re = re.compile(r'(|<[^>]*>)') diff --git a/markupsafe/tests.py b/markupsafe/tests.py new file mode 100644 index 0000000..51b7bb4 --- /dev/null +++ b/markupsafe/tests.py @@ -0,0 +1,70 @@ +import gc +import unittest +from markupsafe import Markup, escape + + +class MarkupTestCase(unittest.TestCase): + + def test_markup_operations(self): + # adding two strings should escape the unsafe one + unsafe = '' + safe = Markup('username') + assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe) + + # string interpolations are safe to use too + assert Markup('%s') % '' == \ + '<bad user>' + assert Markup('%(username)s') % { + 'username': '' + } == '<bad user>' + + # an escaped object is markup too + assert type(Markup('foo') + 'bar') is Markup + + # and it implements __html__ by returning itself + x = Markup("foo") + assert x.__html__() is x + + # it also knows how to treat __html__ objects + class Foo(object): + def __html__(self): + return 'awesome' + def __unicode__(self): + return 'awesome' + assert Markup(Foo()) == 'awesome' + assert Markup('%s') % Foo() == \ + 'awesome' + + # escaping and unescaping + assert escape('"<>&\'') == '"<>&'' + assert Markup("Foo & Bar").striptags() == "Foo & Bar" + assert Markup("<test>").unescape() == "" + + +class MarkupLeakTestCase(unittest.TestCase): + + def test_markup_leaks(self): + counts = set() + for count in xrange(20): + for item in xrange(1000): + escape("foo") + escape("") + escape(u"foo") + escape(u"") + counts.add(len(gc.get_objects())) + assert len(counts) == 1, 'ouch, c extension seems to leak objects' + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(MarkupTestCase)) + + # this test only tests the c extension + if not hasattr(escape, 'func_code'): + suite.addTest(unittest.makeSuite(MarkupLeakTestCase)) + + return suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/setup.py b/setup.py index 29aa203..8bd8164 100644 --- a/setup.py +++ b/setup.py @@ -75,6 +75,7 @@ def run_setup(with_binary): 'Topic :: Text Processing :: Markup :: HTML' ], packages=['markupsafe'], + test_suite='markupsafe.tests.suite', include_package_data=True, cmdclass={'build_ext': ve_build_ext}, features=features -- cgit v1.2.1