Commit 20e89cf6 by Chris Jerdonek

Merge branch 'issue_54' into development: closing issue #54

Markupsafe can now be turned on and off (for testing purposes).
The unit tests now run test_pystache.py twice if markupsafe is
enabled: once with and once without.  Moreover, all unit tests
now pass.  Previously, one test case in test_pystache.py failed
with markupsafe enabled.
parents bf450600 cdd4484f
...@@ -9,6 +9,7 @@ Features: ...@@ -9,6 +9,7 @@ Features:
* Support for disabling HTML escape in Template and View classes. [cjerdonek] * Support for disabling HTML escape in Template and View classes. [cjerdonek]
* A custom template loader can now be passed to a View. [cjerdonek] * A custom template loader can now be passed to a View. [cjerdonek]
* Added a command-line interface. [vrde, cjerdonek] * Added a command-line interface. [vrde, cjerdonek]
* Markupsafe can now be disabled after import. [cjerdonek]
API changes: API changes:
......
...@@ -13,13 +13,11 @@ from .context import Context ...@@ -13,13 +13,11 @@ from .context import Context
from .loader import Loader from .loader import Loader
markupsafe = None
try: try:
import markupsafe import markupsafe
escape = markupsafe.escape
literal = markupsafe.Markup
except ImportError: except ImportError:
escape = lambda x: cgi.escape(unicode(x)) pass
literal = unicode
try: try:
...@@ -86,19 +84,22 @@ class Template(object): ...@@ -86,19 +84,22 @@ class Template(object):
loader = Loader() loader = Loader()
load_template = loader.load_template load_template = loader.load_template
if markupsafe:
escape = markupsafe.escape
literal = markupsafe.Markup
else:
escape = lambda x: cgi.escape(unicode(x))
literal = unicode
self.disable_escape = disable_escape self.disable_escape = disable_escape
self.escape = escape
self.literal = literal
self.load_template = load_template self.load_template = load_template
self.output_encoding = output_encoding self.output_encoding = output_encoding
self.template = template self.template = template
self._compile_regexps() self._compile_regexps()
def escape(self, text):
return escape(text)
def literal(self, text):
return literal(text)
def _initialize_context(self, context, **kwargs): def _initialize_context(self, context, **kwargs):
""" """
Initialize the context attribute. Initialize the context attribute.
...@@ -274,7 +275,7 @@ class Template(object): ...@@ -274,7 +275,7 @@ class Template(object):
Render a tag without escaping it. Render a tag without escaping it.
""" """
return literal(self.context.get(tag_name, '')) return self.literal(self.context.get(tag_name, ''))
def render(self, context=None, **kwargs): def render(self, context=None, **kwargs):
""" """
......
...@@ -2,8 +2,19 @@ ...@@ -2,8 +2,19 @@
import unittest import unittest
import pystache import pystache
from pystache import template
class PystacheTests(object):
"""
Contains tests to run with markupsafe both enabled and disabled.
To run the tests in this class, this class should be subclassed by
a class that implements unittest.TestCase.
"""
class TestPystache(unittest.TestCase):
def test_basic(self): def test_basic(self):
ret = pystache.render("Hi {{thing}}!", { 'thing': 'world' }) ret = pystache.render("Hi {{thing}}!", { 'thing': 'world' })
self.assertEquals(ret, "Hi world!") self.assertEquals(ret, "Hi world!")
...@@ -47,6 +58,8 @@ class TestPystache(unittest.TestCase): ...@@ -47,6 +58,8 @@ class TestPystache(unittest.TestCase):
ret = pystache.render(template, { 'set': True }) ret = pystache.render(template, { 'set': True })
self.assertEquals(ret, "Ready set go!") self.assertEquals(ret, "Ready set go!")
non_strings_expected = """(123 & ['something'])(chris & 0.9)"""
def test_non_strings(self): def test_non_strings(self):
template = "{{#stats}}({{key}} & {{value}}){{/stats}}" template = "{{#stats}}({{key}} & {{value}}){{/stats}}"
stats = [] stats = []
...@@ -54,7 +67,7 @@ class TestPystache(unittest.TestCase): ...@@ -54,7 +67,7 @@ class TestPystache(unittest.TestCase):
stats.append({'key': u"chris", 'value': 0.900}) stats.append({'key': u"chris", 'value': 0.900})
ret = pystache.render(template, { 'stats': stats }) ret = pystache.render(template, { 'stats': stats })
self.assertEquals(ret, """(123 & ['something'])(chris & 0.9)""") self.assertEquals(ret, self.non_strings_expected)
def test_unicode(self): def test_unicode(self):
template = 'Name: {{name}}; Age: {{age}}' template = 'Name: {{name}}; Age: {{age}}'
...@@ -80,3 +93,30 @@ class TestPystache(unittest.TestCase): ...@@ -80,3 +93,30 @@ class TestPystache(unittest.TestCase):
template = "first{{#spacing}} second {{/spacing}}third" template = "first{{#spacing}} second {{/spacing}}third"
ret = pystache.render(template, {"spacing": True}) ret = pystache.render(template, {"spacing": True})
self.assertEquals(ret, "first second third") self.assertEquals(ret, "first second third")
class PystacheWithoutMarkupsafeTests(PystacheTests, unittest.TestCase):
"""Test pystache without markupsafe enabled."""
def setUp(self):
self.original_markupsafe = template.markupsafe
template.markupsafe = None
def tearDown(self):
template.markupsafe = self.original_markupsafe
# If markupsafe is available, then run the same tests again but without
# disabling markupsafe.
_BaseClass = unittest.TestCase if template.markupsafe else object
class PystacheWithMarkupsafeTests(PystacheTests, _BaseClass):
"""Test pystache with markupsafe enabled."""
# markupsafe.escape() escapes single quotes: "'" becomes "'".
non_strings_expected = """(123 & ['something'])(chris & 0.9)"""
def test_markupsafe_available(self):
self.assertTrue(template.markupsafe, "markupsafe isn't available. "
"The with-markupsafe tests shouldn't be running.")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment