Moved dot notation tests to test_renderengine, and expanded them. Added helper class

parent 59771530
...@@ -51,3 +51,20 @@ class AssertIsMixin: ...@@ -51,3 +51,20 @@ class AssertIsMixin:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone # http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second): def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second))) self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
class Attachable(object):
"""A trivial object that attaches all constructor named parameters as attributes.
For instance,
>>> o = Attachable(foo=42, size="of the universe")
>>> o.foo
42
>>> o.size
of the universe
"""
def __init__(self, **kwargs):
for arg, value in kwargs.iteritems():
setattr(self, arg, value)
...@@ -11,7 +11,7 @@ import unittest ...@@ -11,7 +11,7 @@ import unittest
from pystache.context import Context from pystache.context import Context
from pystache.parser import ParsingError from pystache.parser import ParsingError
from pystache.renderengine import RenderEngine from pystache.renderengine import RenderEngine
from tests.common import AssertStringMixin from tests.common import AssertStringMixin, Attachable
class RenderEngineTestCase(unittest.TestCase): class RenderEngineTestCase(unittest.TestCase):
...@@ -453,3 +453,67 @@ class RenderTests(unittest.TestCase, AssertStringMixin): ...@@ -453,3 +453,67 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
expected = u' {{foo}} ' expected = u' {{foo}} '
self._assert_render(expected, '{{=$ $=}} {{foo}} ') self._assert_render(expected, '{{=$ $=}} {{foo}} ')
self._assert_render(expected, '{{=$ $=}} {{foo}} $={{ }}=$') # was yielding u' '. self._assert_render(expected, '{{=$ $=}} {{foo}} $={{ }}=$') # was yielding u' '.
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)
"""
# 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)
def test_dot_notation__missing_attributes_or_keys(self):
"""
Check that, when using dot notation, if the key or attribute does not
exist then its value is rendered as empty
"""
template = """I cannot see {{person.name}}'s age: {{person.age}}.
Nor {{other_person.name}}'s: ."""
expected = u"""I cannot see Biggles's age: .
Nor Mr. Bradshaw's: ."""
context = {'person': {'name': 'Biggles'},
'other_person': Attachable(name='Mr. Bradshaw')}
self._assert_render(expected, template, context)
def test_dot_notation__multiple_levels(self):
"""
Check that using multiple levels of dot attributes works as expected
"""
template = """Hello, Mr. {{person.name.lastname}}.
I see you're back from {{person.travels.last.country.city}}.
I'm missing some of your details: {{person.details.private.editor}}."""
expected = u"""Hello, Mr. Pither.
I see you're back from Cornwall.
I'm missing some of your details: ."""
context = {'person': {'name': {'firstname': 'unknown', 'lastname': 'Pither'},
'travels': {'last': {'country': {'city': 'Cornwall'}}},
'details': {'public': 'likes cycling'}}}
self._assert_render(expected, template, context)
# It should also work with user-defined objects
context = {'person': Attachable(name={'firstname': 'unknown', 'lastname': 'Pither'},
travels=Attachable(last=Attachable(country=Attachable(city='Cornwall'))),
details=Attachable())}
self._assert_render(expected, template, context)
...@@ -60,26 +60,6 @@ class TestSimple(unittest.TestCase, AssertStringMixin): ...@@ -60,26 +60,6 @@ class TestSimple(unittest.TestCase, AssertStringMixin):
template = '{{not_set}} {{blank}}' template = '{{not_set}} {{blank}}'
self.assertEquals(pystache.Renderer().render(template), ' ') self.assertEquals(pystache.Renderer().render(template), ' ')
def test_dot_notation_with_dict(self):
template = 'Name: {{person.name}}. Age: {{person.details.age}}. Intimate details (should be empty): {{person.details.intimate}}.'
renderer = Renderer()
context = {'person': {'name': 'Biggles', 'details': {'age': 42}}}
actual = renderer.render(template, context)
self.assertEquals(actual, 'Name: Biggles. Age: 42. Intimate details (should be empty): .')
def test_dot_notation_with_user_objects(self):
template = 'Name: {{person.name}}. Age: {{person.details.age}}. Intimate details (should be empty): {{person.details.intimate}}.'
renderer = Renderer()
class Person(object):
def __init__(self, name, details):
self.name = name
self.details = details
context = {'person': Person('Biggles', {'age': 42})}
actual = renderer.render(template, context)
self.assertEquals(actual, 'Name: Biggles. Age: 42. Intimate details (should be empty): .')
def test_template_partial_extension(self): def test_template_partial_extension(self):
""" """
Side note: Side note:
......
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