summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRodrigo Bernardo Pimentel <rbp@isnomore.net>2012-04-04 11:43:58 +0200
committerRodrigo Bernardo Pimentel <rbp@isnomore.net>2012-04-04 11:43:58 +0200
commit02a2472def23f1760b30af56d4b67fa58f1b620a (patch)
tree24041b02e026fa90cd1bcd64c4604ab706359c70
parentce47e5e417e6dd8f6cfb162af7591423edc910a7 (diff)
downloadpystache-02a2472def23f1760b30af56d4b67fa58f1b620a.tar.gz
Moved some more detailed tests into test_context.py
-rw-r--r--tests/test_context.py38
-rw-r--r--tests/test_renderengine.py24
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)