Commit eb736a1c by Chris Jerdonek

Added a Context class with support for dictionaries (for issue #49).

parent 117e3530
# coding: utf-8
"""
Defines a Context class to represent mustache(5)'s notion of context.
"""
class Context(object):
"""
Encapsulates a queryable stack of zero or more dictionary-like objects.
Instances of this class are intended to act as the context when
rendering mustache templates in accordance with mustache(5).
"""
# We reserve keyword arguments for future options (e.g. a "strict=True"
# option for enabling a strict mode).
def __init__(self, *obj):
"""
Construct an instance and initialize the stack.
The variable argument list *obj are the objects with which to
populate the initial stack. Objects in the argument list are added
to the stack in order so that, in particular, items at the end of
the argument list are queried first when querying the stack.
The objects should be dictionary-like in the following sense:
(1) They can be dictionaries or objects.
(2) If they implement __getitem__, a KeyError should be raised
if __getitem__ is called on a missing key.
"""
self.stack = list(obj)
def get(self, key, default=None):
"""
Query the stack for the given key, and return the resulting value.
Querying for a key queries objects in the stack in order from
last-added objects to first (last in, first out).
Querying an item in the stack is done as follows:
(1) The __getitem__ method is attempted first, if it exists.
This method returns None if no item in the stack contains the key.
"""
for obj in reversed(self.stack):
try:
return obj[key]
except KeyError:
pass
return default
# coding: utf-8
"""
Unit tests of context.py.
"""
import unittest
from pystache.context import Context
class ContextTestCase(unittest.TestCase):
"""
Test the Context class.
"""
def test_init__no_elements(self):
"""
Check that passing nothing to __init__() raises no exception.
"""
context = Context()
def test_init__no_elements(self):
"""
Check that passing more than two items to __init__() raises no exception.
"""
context = Context({}, {}, {})
def test_get__missing_key(self):
"""
Test getting a missing key.
"""
context = Context()
self.assertTrue(context.get("foo") is None)
def test_get__default(self):
"""
Test that get() respects the default value .
"""
context = Context()
self.assertEquals(context.get("foo", "bar"), "bar")
def test_get__key_present(self):
"""
Test get() with a key that is present.
"""
context = Context({"foo": "bar"})
self.assertEquals(context.get("foo"), "bar")
def test_get__precedence(self):
"""
Test that get() respects the order of precedence (later items first).
"""
context = Context({"foo": "bar"}, {"foo": "buzz"})
self.assertEquals(context.get("foo"), "buzz")
def test_get__fallback(self):
"""
Check that first-added stack items are queried on context misses.
"""
context = Context({"fuzz": "buzz"}, {"foo": "bar"})
self.assertEquals(context.get("fuzz"), "buzz")
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