예제 #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'')
예제 #2
0
    def test_dot_notation__missing_attr_or_key(self):
        name = "foo.bar.baz.bak"
        stack = ContextStack({"foo": {"bar": {}}})
        self.assertException(KeyNotFoundError,
                             "Key 'foo.bar.baz.bak' not found: missing 'baz'",
                             stack.get, name)

        stack = ContextStack({"foo": Attachable(bar=Attachable())})
        self.assertException(KeyNotFoundError,
                             "Key 'foo.bar.baz.bak' not found: missing 'baz'",
                             stack.get, name)
예제 #3
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")
예제 #4
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}}."""
        expected = """Hello, Mr. Pither.
        I see you're back from Cornwall."""
        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)
예제 #5
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!")
예제 #6
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('Hello, Biggles. I see you are 42.', template, context)
예제 #7
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)
예제 #8
0
 def bar(self):
     return Attachable(baz='Baz')
예제 #9
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)