Ejemplo n.º 1
0
    def test_dot_notation__missing_attr_or_key(self):
        name = "foo.bar.baz.bak"
        stack = ContextStack({"foo": {"bar": {}}})
        self.assertString(stack.get(name), u'')

        stack = ContextStack({"foo": Attachable(bar=Attachable())})
        self.assertString(stack.get(name), u'')
Ejemplo n.º 2
0
    def test_dot_notation__autocall(self):
        name = "foo.bar.baz"

        # When any element in the path is callable, it should be automatically invoked
        stack = ContextStack({"foo": Attachable(bar=Attachable(baz=lambda: "Called!"))})
        self.assertEqual(stack.get(name), "Called!")

        class Foo(object):
            def bar(self):
                return Attachable(baz='Baz')

        stack = ContextStack({"foo": Foo()})
        self.assertEqual(stack.get(name), "Baz")
Ejemplo n.º 3
0
    def test_dot_notation__multiple_levels(self):
        """
        Test dot notation with multiple levels.

        """
        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)
Ejemplo n.º 4
0
    def test_dot_notation__user_object(self):
        name = "foo.bar"
        stack = ContextStack({"foo": Attachable(bar="baz")})
        self.assertEqual(stack.get(name), "baz")

        # Works on multiple levels, too
        name = "a.b.c.d.e.f.g"
        A = Attachable
        stack = ContextStack({"a": A(b=A(c=A(d=A(e=A(f=A(g="w00t!"))))))})
        self.assertEqual(stack.get(name), "w00t!")
Ejemplo n.º 5
0
    def test_dot_notation(self):
        """
        Test simple dot notation cases.

        Check that we can use dot notation when the variable is a dict,
        user-defined object, or combination of both.

        """
        template = 'Hello, {{person.name}}. I see you are {{person.details.age}}.'
        person = Attachable(name='Biggles', details={'age': 42})
        context = {'person': person}
        self._assert_render(u'Hello, Biggles. I see you are 42.', template,
                            context)
Ejemplo n.º 6
0
    def test_dot_notation__missing_attributes_or_keys(self):
        """
        Test dot notation with missing keys or attributes.

        Check that if a key or attribute in a dotted name does not exist, then
        the tag renders as the empty string.

        """
        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)
Ejemplo n.º 7
0
 def bar(self):
     return Attachable(baz='Baz')
Ejemplo n.º 8
0
 def test_dot_notation__mixed_dict_and_obj(self):
     name = "foo.bar.baz.bak"
     stack = ContextStack({"foo": Attachable(bar={"baz": Attachable(bak=42)})})
     self.assertEqual(stack.get(name), 42)