summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 14:37:13 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 14:37:13 -0700
commit37f6d8a96855f65574f1b03f967501a0f85cc2a3 (patch)
tree49b143c139b973766cb15da094b7ebc5ec2b7473
parent34ca106c1aac8dad7cfc504ddfa5f9c24d1967b7 (diff)
parent0e8552c5ad85294a35e0be38eb322877797bc62a (diff)
downloadpystache-37f6d8a96855f65574f1b03f967501a0f85cc2a3.tar.gz
Merge branch 'issue-135' into development
This closes issue #135. Thanks to @bennoleslie for the report and fix.
-rw-r--r--HISTORY.md5
-rw-r--r--pystache/parser.py4
-rw-r--r--pystache/tests/test_defaults.py68
3 files changed, 74 insertions, 3 deletions
diff --git a/HISTORY.md b/HISTORY.md
index 30d3c34..01c9049 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -17,7 +17,10 @@ History
parse tree.
- Added support for rendering pre-compiled templates.
- Added support for [PyPy](http://pypy.org/) (issue \#125).
-- Added support for [Travis CI](http://travis-ci.org) (issue \#124). [msabramo]
+- Added support for [Travis CI](http://travis-ci.org) (issue \#124).
+ [msabramo]
+- Bugfix: `defaults.DELIMITERS` can now be changed at runtime (issue \#135).
+ [bennoleslie]
- Bugfix: exceptions raised from a property are no longer swallowed
when getting a key from a context stack (issue \#110).
- Bugfix: lambda section values can now return non-ascii, non-unicode
diff --git a/pystache/parser.py b/pystache/parser.py
index 8dc74b5..c6a171f 100644
--- a/pystache/parser.py
+++ b/pystache/parser.py
@@ -7,7 +7,7 @@ Exposes a parse() function to parse template strings.
import re
-from pystache.defaults import DELIMITERS
+from pystache import defaults
from pystache.parsed import ParsedTemplate
@@ -228,7 +228,7 @@ class _Parser(object):
def __init__(self, delimiters=None):
if delimiters is None:
- delimiters = DELIMITERS
+ delimiters = defaults.DELIMITERS
self._delimiters = delimiters
diff --git a/pystache/tests/test_defaults.py b/pystache/tests/test_defaults.py
new file mode 100644
index 0000000..c78ea7c
--- /dev/null
+++ b/pystache/tests/test_defaults.py
@@ -0,0 +1,68 @@
+# coding: utf-8
+
+"""
+Unit tests for defaults.py.
+
+"""
+
+import unittest
+
+import pystache
+
+from pystache.tests.common import AssertStringMixin
+
+
+# TODO: make sure each default has at least one test.
+class DefaultsConfigurableTestCase(unittest.TestCase, AssertStringMixin):
+
+ """Tests that the user can change the defaults at runtime."""
+
+ # TODO: switch to using a context manager after 2.4 is deprecated.
+ def setUp(self):
+ """Save the defaults."""
+ defaults = [
+ 'DECODE_ERRORS', 'DELIMITERS',
+ 'FILE_ENCODING', 'MISSING_TAGS',
+ 'SEARCH_DIRS', 'STRING_ENCODING',
+ 'TAG_ESCAPE', 'TEMPLATE_EXTENSION'
+ ]
+ self.saved = {}
+ for e in defaults:
+ self.saved[e] = getattr(pystache.defaults, e)
+
+ def tearDown(self):
+ for key, value in self.saved.items():
+ setattr(pystache.defaults, key, value)
+
+ def test_tag_escape(self):
+ """Test that changes to defaults.TAG_ESCAPE take effect."""
+ template = u"{{foo}}"
+ context = {'foo': '<'}
+ actual = pystache.render(template, context)
+ self.assertString(actual, u"&lt;")
+
+ pystache.defaults.TAG_ESCAPE = lambda u: u
+ actual = pystache.render(template, context)
+ self.assertString(actual, u"<")
+
+ def test_delimiters(self):
+ """Test that changes to defaults.DELIMITERS take effect."""
+ template = u"[[foo]]{{foo}}"
+ context = {'foo': 'FOO'}
+ actual = pystache.render(template, context)
+ self.assertString(actual, u"[[foo]]FOO")
+
+ pystache.defaults.DELIMITERS = ('[[', ']]')
+ actual = pystache.render(template, context)
+ self.assertString(actual, u"FOO{{foo}}")
+
+ def test_missing_tags(self):
+ """Test that changes to defaults.MISSING_TAGS take effect."""
+ template = u"{{foo}}"
+ context = {}
+ actual = pystache.render(template, context)
+ self.assertString(actual, u"")
+
+ pystache.defaults.MISSING_TAGS = 'strict'
+ self.assertRaises(pystache.context.KeyNotFoundError,
+ pystache.render, template, context)