summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 13:53:12 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 13:53:12 -0700
commitb8859ff5c9bfb139621723c351273a6aa32c1ba1 (patch)
treefe1e4e6af47e6614e88257437c1b165c63188915
parent34ca106c1aac8dad7cfc504ddfa5f9c24d1967b7 (diff)
parentd0c65dd9aefa432bc056a78f204474ddc7b5fb04 (diff)
downloadpystache-b8859ff5c9bfb139621723c351273a6aa32c1ba1.tar.gz
Merge remote-tracking branch 'BreakawayConsulting/issue-135-default-delimiters' into issue-135
This is the initial patch for issue #135 from @bennoleslie.
-rw-r--r--pystache/parser.py4
-rw-r--r--pystache/tests/test_defaults.py60
2 files changed, 62 insertions, 2 deletions
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..903867a
--- /dev/null
+++ b/pystache/tests/test_defaults.py
@@ -0,0 +1,60 @@
+import unittest
+
+import pystache
+import pystache.defaults
+
+from pystache.tests.common import AssertStringMixin
+
+
+class TestDefaults(unittest.TestCase, AssertStringMixin):
+ """Set of tests to ensure that the user can override defaults."""
+
+ def setUp(self):
+ """save all 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 TAG_ESCAPE default takes 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 DELIMITERS default takes 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 MISSING_TAGS default 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)