コード例 #1
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_get__key_missing(self):
        """
        Test getting a missing key.

        """
        context = ContextStack()
        self.assertString(context.get("foo"), u'')
コード例 #2
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_get__fallback(self):
        """
        Check that first-added stack items are queried on context misses.

        """
        context = ContextStack({"fuzz": "buzz"}, {"foo": "bar"})
        self.assertEqual(context.get("fuzz"), "buzz")
コード例 #3
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_get__default(self):
        """
        Test that get() respects the default value.

        """
        context = ContextStack()
        self.assertEqual(context.get("foo", "bar"), "bar")
コード例 #4
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_get__precedence(self):
        """
        Test that get() respects the order of precedence (later items first).

        """
        context = ContextStack({"foo": "bar"}, {"foo": "buzz"})
        self.assertEqual(context.get("foo"), "buzz")
コード例 #5
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_get__key_present(self):
        """
        Test getting a key.

        """
        context = ContextStack({"foo": "bar"})
        self.assertEqual(context.get("foo"), "bar")
コード例 #6
0
ファイル: test_context.py プロジェクト: dcolish/braces
    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'')
コード例 #7
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_dot_notation__dict(self):
        name = "foo.bar"
        stack = ContextStack({"foo": {"bar": "baz"}})
        self.assertEqual(stack.get(name), "baz")

        # Works all the way down
        name = "a.b.c.d.e.f.g"
        stack = ContextStack({"a": {"b": {"c": {"d": {"e": {"f": {"g": "w00t!"}}}}}}})
        self.assertEqual(stack.get(name), "w00t!")
コード例 #8
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_top(self):
        key = "foo"
        context = ContextStack({key: "bar"}, {key: "buzz"})
        self.assertEqual(context.get(key), "buzz")

        top = context.top()
        self.assertEqual(top, {"foo": "buzz"})
        # Make sure calling top() didn't remove the item from the stack.
        self.assertEqual(context.get(key), "buzz")
コード例 #9
0
ファイル: test_context.py プロジェクト: dcolish/braces
    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!")
コード例 #10
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_push(self):
        """
        Test push().

        """
        key = "foo"
        context = ContextStack({key: "bar"})
        self.assertEqual(context.get(key), "bar")

        context.push({key: "buzz"})
        self.assertEqual(context.get(key), "buzz")
コード例 #11
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_pop(self):
        """
        Test pop().

        """
        key = "foo"
        context = ContextStack({key: "bar"}, {key: "buzz"})
        self.assertEqual(context.get(key), "buzz")

        item = context.pop()
        self.assertEqual(item, {"foo": "buzz"})
        self.assertEqual(context.get(key), "bar")
コード例 #12
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_copy(self):
        key = "foo"
        original = ContextStack({key: "bar"}, {key: "buzz"})
        self.assertEqual(original.get(key), "buzz")

        new = original.copy()
        # Confirm that the copy behaves the same.
        self.assertEqual(new.get(key), "buzz")
        # Change the copy, and confirm it is changed.
        new.pop()
        self.assertEqual(new.get(key), "bar")
        # Confirm the original is unchanged.
        self.assertEqual(original.get(key), "buzz")
コード例 #13
0
ファイル: test_context.py プロジェクト: dcolish/braces
    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")
コード例 #14
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__none(self):
        """
        Test passing None.

        """
        context = ContextStack.create({'foo': 'bar'}, None)
        self.assertEqual(context.get('foo'), 'bar')
コード例 #15
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__precedence_keyword(self):
        """
        Test precedence of keyword arguments.

        """
        context = ContextStack.create({'foo': 'bar'}, foo='buzz')
        self.assertEqual(context.get('foo'), 'buzz')
コード例 #16
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__precedence_positional(self):
        """
        Test precedence of positional arguments.

        """
        context = ContextStack.create({'foo': 'bar'}, {'foo': 'buzz'})
        self.assertEqual(context.get('foo'), 'buzz')
コード例 #17
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__kwarg(self):
        """
        Test passing a keyword argument.

        """
        context = ContextStack.create(foo='bar')
        self.assertEqual(context.get('foo'), 'bar')
コード例 #18
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__dictionary(self):
        """
        Test passing a dictionary.

        """
        context = ContextStack.create({'foo': 'bar'})
        self.assertEqual(context.get('foo'), 'bar')
コード例 #19
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__context(self):
        """
        Test passing a ContextStack instance.

        """
        obj = ContextStack({'foo': 'bar'})
        context = ContextStack.create(obj)
        self.assertEqual(context.get('foo'), 'bar')
コード例 #20
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_create__object(self):
        """
        Test passing an object.

        """
        class Foo(object):
            foo = 'bar'
        context = ContextStack.create(Foo())
        self.assertEqual(context.get('foo'), 'bar')
コード例 #21
0
ファイル: test_context.py プロジェクト: dcolish/braces
    def test_dot_notation__missing_part_terminates_search(self):
        """
        Test that dotted name resolution terminates on a later part not found.

        Check that if a later dotted name part is not found in the result from
        the former resolution, then name resolution terminates rather than
        starting the search over with the next element of the context stack.
        From the spec (interpolation section)--

          5) If any name parts were retained in step 1, each should be resolved
          against a context stack containing only the result from the former
          resolution.  If any part fails resolution, the result should be considered
          falsey, and should interpolate as the empty string.

        This test case is equivalent to the test case in the following pull
        request:

          https://github.com/mustache/spec/pull/48

        """
        stack = ContextStack({'a': {'b': 'A.B'}}, {'a': 'A'})
        self.assertEqual(stack.get('a'), 'A')
        self.assertString(stack.get('a.b'), u'')
        stack.pop()
        self.assertEqual(stack.get('a.b'), 'A.B')
コード例 #22
0
ファイル: renderer.py プロジェクト: dcolish/braces
    def _render_string(self, template, *context, **kwargs):
        """
        Render the given template string using the given context.

        """
        # RenderEngine.render() requires that the template string be unicode.
        template = self._to_unicode_hard(template)

        context = ContextStack.create(*context, **kwargs)
        self._context = context

        # We keep this type-check as an added check because this method is
        # called with template strings coming from potentially externally-
        # supplied functions like self.literal, self.load_partial, etc.
        # Beyond this point, we have much better control over the type.
        if type(template) is not unicode:
            raise Exception("Argument 'template' not unicode: %s: %s" % (
                    type(template), repr(template)))

        parsed_template = self.parser.parse(template=template)
        rendered = parsed_template.render(context)

        return unicode(rendered)
コード例 #23
0
ファイル: test_context.py プロジェクト: dcolish/braces
 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)