Commit 3f30290b by Chris Jerdonek

Getting "." from an empty context stack now raises a KeyNotFoundError.

parent 049a76c1
...@@ -14,6 +14,9 @@ spec, we define these categories mutually exclusively as follows: ...@@ -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. # This equals '__builtin__' in Python 2 and 'builtins' in Python 3.
_BUILTIN_MODULE = type(0).__module__ _BUILTIN_MODULE = type(0).__module__
...@@ -73,6 +76,21 @@ def _get_value(context, key): ...@@ -73,6 +76,21 @@ def _get_value(context, key):
return _NOT_FOUND 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): class ContextStack(object):
""" """
...@@ -252,8 +270,10 @@ class ContextStack(object): ...@@ -252,8 +270,10 @@ class ContextStack(object):
""" """
if name == '.': if name == '.':
# TODO: should we add a test case for an empty context stack? try:
return self.top() return self.top()
except IndexError:
raise KeyNotFoundError(".", "empty context stack")
parts = name.split('.') parts = name.split('.')
......
...@@ -8,10 +8,8 @@ Unit tests of context.py. ...@@ -8,10 +8,8 @@ Unit tests of context.py.
from datetime import datetime from datetime import datetime
import unittest import unittest
from pystache.context import _NOT_FOUND from pystache.context import _NOT_FOUND, _get_value, KeyNotFoundError, ContextStack
from pystache.context import _get_value from pystache.tests.common import AssertIsMixin, AssertStringMixin, AssertExceptionMixin, Attachable
from pystache.context import ContextStack
from pystache.tests.common import AssertIsMixin, AssertStringMixin, Attachable
class SimpleObject(object): class SimpleObject(object):
...@@ -39,7 +37,7 @@ class DictLike(object): ...@@ -39,7 +37,7 @@ class DictLike(object):
return self._dict[key] return self._dict[key]
class GetValueTests(unittest.TestCase, AssertIsMixin): class GetValueTestCase(unittest.TestCase, AssertIsMixin):
"""Test context._get_value().""" """Test context._get_value()."""
...@@ -224,7 +222,8 @@ class GetValueTests(unittest.TestCase, AssertIsMixin): ...@@ -224,7 +222,8 @@ class GetValueTests(unittest.TestCase, AssertIsMixin):
self.assertNotFound(item2, 'pop') self.assertNotFound(item2, 'pop')
class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
AssertExceptionMixin):
""" """
Test the ContextStack class. Test the ContextStack class.
...@@ -326,6 +325,24 @@ class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -326,6 +325,24 @@ class ContextStackTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
context = ContextStack.create({'foo': 'bar'}, foo='buzz') context = ContextStack.create({'foo': 'bar'}, foo='buzz')
self.assertEqual(context.get('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): def test_get__key_present(self):
""" """
Test getting a key. Test getting a key.
......
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