From 02a2472def23f1760b30af56d4b67fa58f1b620a Mon Sep 17 00:00:00 2001 From: Rodrigo Bernardo Pimentel Date: Wed, 4 Apr 2012 11:43:58 +0200 Subject: Moved some more detailed tests into test_context.py --- tests/test_context.py | 38 +++++++++++++++++++++++++++++++++++++- tests/test_renderengine.py | 24 ++---------------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/tests/test_context.py b/tests/test_context.py index decf4fb..e216405 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -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) + + + diff --git a/tests/test_renderengine.py b/tests/test_renderengine.py index 617d4f0..7752161 100644 --- a/tests/test_renderengine.py +++ b/tests/test_renderengine.py @@ -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) -- cgit v1.2.1