summaryrefslogtreecommitdiff
path: root/pystache
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-05-04 03:26:03 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-05-04 05:38:19 -0700
commit3f30290b82ccc2299baaf28bc9f331586c1df6d7 (patch)
tree5a62a15b63654a0f86ef8ae1efa573a07e7a537a /pystache
parent049a76c11318b3e389863f2f4a2e39fe73f7597a (diff)
downloadpystache-3f30290b82ccc2299baaf28bc9f331586c1df6d7.tar.gz
Getting "." from an empty context stack now raises a KeyNotFoundError.
Diffstat (limited to 'pystache')
-rw-r--r--pystache/context.py24
-rw-r--r--pystache/tests/test_context.py29
2 files changed, 45 insertions, 8 deletions
diff --git a/pystache/context.py b/pystache/context.py
index cf309d6..c9d954e 100644
--- a/pystache/context.py
+++ b/pystache/context.py
@@ -14,6 +14,9 @@ spec, we define these categories mutually exclusively as follows:
"""
+from pystache.common import PystacheError
+
+
# This equals '__builtin__' in Python 2 and 'builtins' in Python 3.
_BUILTIN_MODULE = type(0).__module__
@@ -73,6 +76,21 @@ def _get_value(context, key):
return _NOT_FOUND
+class KeyNotFoundError(PystacheError):
+
+ """
+ An exception raised when a key is not found in a context stack.
+
+ """
+
+ def __init__(self, key, details):
+ self.key = key
+ self.details = details
+
+ def __str__(self):
+ return "Key %s not found: %s" % (repr(self.key), self.details)
+
+
class ContextStack(object):
"""
@@ -252,8 +270,10 @@ class ContextStack(object):
"""
if name == '.':
- # TODO: should we add a test case for an empty context stack?
- return self.top()
+ try:
+ return self.top()
+ except IndexError:
+ raise KeyNotFoundError(".", "empty context stack")
parts = name.split('.')
diff --git a/pystache/tests/test_context.py b/pystache/tests/test_context.py
index fb69981..1c0cd43 100644
--- a/pystache/tests/test_context.py
+++ b/pystache/tests/test_context.py
@@ -8,10 +8,8 @@ Unit tests of context.py.
from datetime import datetime
import unittest
-from pystache.context import _NOT_FOUND
-from pystache.context import _get_value
-from pystache.context import ContextStack
-from pystache.tests.common import AssertIsMixin, AssertStringMixin, Attachable
+from pystache.context import _NOT_FOUND, _get_value, KeyNotFoundError, ContextStack
+from pystache.tests.common import AssertIsMixin, AssertStringMixin, AssertExceptionMixin, Attachable
class SimpleObject(object):
@@ -39,7 +37,7 @@ class DictLike(object):
return self._dict[key]
-class GetValueTests(unittest.TestCase, AssertIsMixin):
+class GetValueTestCase(unittest.TestCase, AssertIsMixin):
"""Test context._get_value()."""
@@ -224,7 +222,8 @@ class GetValueTests(unittest.TestCase, AssertIsMixin):
self.assertNotFound(item2, 'pop')
-class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
+class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
+ AssertExceptionMixin):
"""
Test the ContextStack class.
@@ -326,6 +325,24 @@ class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
context = ContextStack.create({'foo': 'bar'}, foo='buzz')
self.assertEqual(context.get('foo'), 'buzz')
+ ## Test the get() method.
+
+ def test_get__single_dot(self):
+ """
+ Test getting a single dot (".").
+
+ """
+ context = ContextStack("a", "b")
+ self.assertEqual(context.get("."), "b")
+
+ def test_get__single_dot__missing(self):
+ """
+ Test getting a single dot (".") with an empty context stack.
+
+ """
+ context = ContextStack()
+ self.assertException(KeyNotFoundError, "Key '.' not found: empty context stack", context.get, ".", "b")
+
def test_get__key_present(self):
"""
Test getting a key.