Moved some more detailed tests into test_context.py

parent ce47e5e4
......@@ -11,7 +11,7 @@ import unittest
from pystache.context import _NOT_FOUND
from pystache.context import _get_value
from pystache.context import Context
from tests.common import AssertIsMixin
from tests.common import AssertIsMixin, Attachable
class SimpleObject(object):
......@@ -398,3 +398,39 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
# Confirm the original is unchanged.
self.assertEquals(original.get(key), "buzz")
def test_dot_notation__dict(self):
key = "foo.bar"
original = Context({"foo": {"bar": "baz"}})
self.assertEquals(original.get(key), "baz")
# Works all the way down
key = "a.b.c.d.e.f.g"
original = Context({"a": {"b": {"c": {"d": {"e": {"f": {"g": "w00t!"}}}}}}})
self.assertEquals(original.get(key), "w00t!")
def test_dot_notation__user_object(self):
key = "foo.bar"
original = Context({"foo": Attachable(bar="baz")})
self.assertEquals(original.get(key), "baz")
# Works on multiple levels, too
key = "a.b.c.d.e.f.g"
Obj = Attachable
original = Context({"a": Obj(b=Obj(c=Obj(d=Obj(e=Obj(f=Obj(g="w00t!"))))))})
self.assertEquals(original.get(key), "w00t!")
def test_dot_notation__mixed_dict_and_obj(self):
key = "foo.bar.baz.bak"
original = Context({"foo": Attachable(bar={"baz": Attachable(bak=42)})})
self.assertEquals(original.get(key), 42)
def test_dot_notation__missing_attr_or_key(self):
key = "foo.bar.baz.bak"
original = Context({"foo": {"bar": {}}})
self.assertEquals(original.get(key), None)
original = Context({"foo": Attachable(bar=Attachable())})
self.assertEquals(original.get(key), None)
......@@ -456,31 +456,11 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
def test_dot_notation(self):
"""
Check that we can use dot notation when the variable is a dict
or a used-defined object (or a combination of both)
Check that we can use dot notation when the variable is a dict,
a used-defined object, or a combination of both
"""
# With a dict:
template = 'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context = {'person': {'name': 'Biggles', 'details': {'age': 42}}}
self._assert_render(u'Hello, Biggles. I see you are 42.', template, context)
# With a user-defined object:
details = Attachable(age=42)
person = Attachable(name='Biggles', details=details)
template = 'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context = {'person': person}
self._assert_render(u'Hello, Biggles. I see you are 42.', template, context)
# All together now!
person = Attachable(name='Biggles', details={'age': 42})
template = 'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context = {'person': person}
self._assert_render(u'Hello, Biggles. I see you are 42.', template, context)
# And the other way around:
details = Attachable(age=42)
person = {'name': 'Biggles', 'details': details}
template = 'Hello, {{person.name}}. I see you are {{person.details.age}}.'
context = {'person': person}
self._assert_render(u'Hello, Biggles. I see you are 42.', template, context)
......
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