def test_get_filters():
    assert(parse_str("{{ a }}").get_filters() == set())
    assert(parse_str("{{ a | foo }}").get_filters() == {"foo"})
    assert(parse_str(
        "a{{ a | baz }}b{{ a | bar }}c{{ c | bar }}").get_filters() == {"baz", "bar"})
    assert(parse_str("a{% switch foo %}{% case \"test\" %}{{ a | baz }}b{{ a | bar }}{% endswitch %}c{{ c | bar }}{{ a | foo }}").get_filters() == {"foo", "baz", "bar"})  # noqa
    assert parse_str("{% for foo in bar %}{{ foo | bang }}{% endfor %}").get_filters() == {'bang'}
def test_get_variables():
    assert(parse_str("a").get_scoped_arguments() ==
           {'variables': set(), 'sub_scopes': dict()})
    assert(parse_str("{{ a }}").get_scoped_arguments() ==
           {'variables': {"a"}, 'sub_scopes': dict()})
    assert(parse_str("{{ a | foo }}").get_scoped_arguments() ==
           {'variables': {"a"}, 'sub_scopes': dict()})
    assert(parse_str("a{{ a }}b{{ c }}").get_scoped_arguments() ==
           {'variables': {"a", "c"}, 'sub_scopes': dict()})
    assert(parse_str("a{{ a }}b{{ a }}c{{ c | baz }}").get_scoped_arguments() ==
           {'variables': {"a", "c"}, 'sub_scopes': dict()})
    assert(parse_str("a{{ a }}b{{ a | bar }}c{{ c }}").get_scoped_arguments() ==
           {'variables': {"a", "c"}, 'sub_scopes': dict()})
    assert(parse_str("{{ a }}{% switch b %}{% case \"c\" %}{{ d }}{% endswitch %}{{ e }}").get_scoped_arguments() == {
        'variables': {'a', 'e'},
        'sub_scopes': {
            'b': {
                'c': {
                    'variables': {'d'},
                    'sub_scopes': {}
                }

            }
        }
    })

    assert (parse_str("{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}").get_scoped_arguments() ==
            {'variables': {'bar', 'baz'}, 'sub_scopes': dict()})

    # TODO(cmaloney): Disallow reusing a for new variable as a general variable.
    assert (parse_str("{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}{{ foo }}").get_scoped_arguments() ==
            {'variables': {'foo', 'bar', 'baz'}, 'sub_scopes': dict()})
Example #3
0
def test_get_filters():
    assert (parse_str("{{ a }}").get_filters() == set())
    assert (parse_str("{{ a | foo }}").get_filters() == {"foo"})
    assert (parse_str(
        "a{{ a | baz }}b{{ a | bar }}c{{ c | bar }}").get_filters() == {
            "baz", "bar"
        })
    assert (parse_str(
        "a{% switch foo %}{% case \"test\" %}{{ a | baz }}b{{ a | bar }}{% endswitch %}c{{ c | bar }}{{ a | foo }}"
    ).get_filters() == {"foo", "baz", "bar"})  # noqa
    assert parse_str(
        "{% for foo in bar %}{{ foo | bang }}{% endfor %}").get_filters() == {
            'bang'
        }
Example #4
0
def test_get_variables():
    assert (parse_str("a").get_scoped_arguments() == {
        'variables': set(),
        'sub_scopes': dict()
    })
    assert (parse_str("{{ a }}").get_scoped_arguments() == {
        'variables': {"a"},
        'sub_scopes': dict()
    })
    assert (parse_str("{{ a | foo }}").get_scoped_arguments() == {
        'variables': {"a"},
        'sub_scopes': dict()
    })
    assert (parse_str("a{{ a }}b{{ c }}").get_scoped_arguments() == {
        'variables': {"a", "c"},
        'sub_scopes': dict()
    })
    assert (
        parse_str("a{{ a }}b{{ a }}c{{ c | baz }}").get_scoped_arguments() == {
            'variables': {"a", "c"},
            'sub_scopes': dict()
        })
    assert (
        parse_str("a{{ a }}b{{ a | bar }}c{{ c }}").get_scoped_arguments() == {
            'variables': {"a", "c"},
            'sub_scopes': dict()
        })
    assert (parse_str(
        "{{ a }}{% switch b %}{% case \"c\" %}{{ d }}{% endswitch %}{{ e }}").
            get_scoped_arguments() == {
                'variables': {'a', 'e'},
                'sub_scopes': {
                    'b': {
                        'c': {
                            'variables': {'d'},
                            'sub_scopes': {}
                        }
                    }
                }
            })

    assert (
        parse_str("{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}"
                  ).get_scoped_arguments() == {
                      'variables': {'bar', 'baz'},
                      'sub_scopes': dict()
                  })

    # TODO(cmaloney): Disallow reusing a for new variable as a general variable.
    assert (parse_str(
        "{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}{{ foo }}"
    ).get_scoped_arguments() == {
        'variables': {'foo', 'bar', 'baz'},
        'sub_scopes': dict()
    })
Example #5
0
def test_target_from_ast():
    assert parse_str("a").target_from_ast() == Target()

    assert parse_str("{{ a }}").target_from_ast() == Target({'a'})
    assert parse_str("{{ a | foo }}").target_from_ast() == Target({'a'})
    assert parse_str("a{{ a }}b{{ c }}").target_from_ast() == Target({'a', 'c'})
    assert parse_str("a{{ a }}b{{ a }}c{{ c | baz }}").target_from_ast() == Target({'a', 'c'})
    assert parse_str("a{{ a }}b{{ a | bar }}c{{ c }}").target_from_ast() == Target({'a', 'c'})
    assert(parse_str("{{ a }}{% switch b %}{% case \"c\" %}{{ d }}{% endswitch %}{{ e }}").target_from_ast() ==
           Target({'a', 'e'}, {'b': Scope('b', {'c': Target({'d'})})}))

    assert (parse_str("{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}").target_from_ast() ==
            Target({'bar', 'baz'}))

    # TODO(cmaloney): Disallow reusing a for new variable as a general variable.
    assert (parse_str("{% for foo in bar %}{{ foo }}{{ bar }}{{ baz }}{% endfor %}{{ foo }}").target_from_ast() ==
            Target({'foo', 'bar', 'baz'}))
Example #6
0
def test_parse():
    assert (parse_str("a").ast == ["a"])
    assert (parse_str("{{ a }}").ast == [Replacement(("a", None))])
    assert (parse_str("a {{ a | foo }}{{ b }} c {{ d | bar }}").ast == [
        "a ",
        Replacement(("a", 'foo')),
        Replacement(("b", None)), " c ",
        Replacement(("d", 'bar'))
    ])
    assert (parse_str(
        '{% switch foo %}{% case "as\\"df" %}foobar{% endswitch %}}}').ast == [
            Switch("foo", {'as"df': ["foobar"]}), '}}'
        ])
    assert (parse_str(
        '{{ a }}b{{ c }}{% switch foo %}  \n  {% case "as\\"df" %}foobar{% endswitch %}}}'
    ).ast == [
        Replacement(("a", None)), "b",
        Replacement(("c", None)),
        Switch("foo", {'as"df': ["foobar"]}), "}}"
    ])
    # TODO(cmaloney): Add parse syntax error tests

    assert parse_str("{% for foo in bar %}{{ foo }}{% endfor %}").ast == [
        For("foo", "bar", [Replacement('foo')])
    ]
def test_parse():
    assert(parse_str("a").ast == ["a"])
    assert(parse_str("{{ a }}").ast == [Replacement(("a", None))])
    assert(parse_str("a {{ a | foo }}{{ b }} c {{ d | bar }}").ast == [
        "a ",
        Replacement(("a", 'foo')),
        Replacement(("b", None)),
        " c ",
        Replacement(("d", 'bar'))
    ])
    assert(parse_str('{% switch foo %}{% case "as\\"df" %}foobar{% endswitch %}}}').ast ==
           [Switch("foo", {'as"df': ["foobar"]}), '}}'])
    assert(parse_str('{{ a }}b{{ c }}{% switch foo %}  \n  {% case "as\\"df" %}foobar{% endswitch %}}}').ast == [
        Replacement(("a", None)),
        "b",
        Replacement(("c", None)),
        Switch("foo", {'as"df': ["foobar"]}),
        "}}"
    ])
    # TODO(cmaloney): Add parse syntax error tests

    assert parse_str("{% for foo in bar %}{{ foo }}{% endfor %}").ast == [For("foo", "bar", [Replacement('foo')])]
Example #8
0
def test_render():
    assert (parse_str("a").render({}) == "a")
    assert (parse_str("{{ a }}a{{ b }}").render({"a": "1", "b": "2"}) == "1a2")
    assert (parse_str("{{ a | foo }}a{{ b }}").render(
        {
            "a": "1",
            "b": "2"
        }, {'foo': lambda x: x + 'foo'}) == "1fooa2")
    with pytest.raises(UnsetParameter):
        parse_str("{{ a }}a{{ b }}").render({"a": "1"})
    with pytest.raises(UnsetParameter):
        parse_str("{{ a }}").render({"c": "1"})
    with pytest.raises(UnsetParameter):
        parse_str("{{ a | foo }}").render({"a": "1"})
    assert parse_str("{% for a in b %}{{ a }}{% endfor %}").render(
        {"b": ['a', 'test']}) == "atest"

    assert (parse_str("{% for a in b %}{{ a }}{% endfor %}else{{ a }}").render(
        {
            "b": ['b', 't', 'c'],
            "a": "foo"
        }) == "btcelsefoo")
    with pytest.raises(UnsetParameter):
        parse_str("{% for a in b %}{{ a }}{% endfor %}else{{ a }}").render(
            {"b": ['b', 't', 'c']})
def test_render():
    assert(parse_str("a").render({}) == "a")
    assert(parse_str("{{ a }}a{{ b }}").render({"a": "1", "b": "2"}) == "1a2")
    assert(parse_str("{{ a | foo }}a{{ b }}").render(
        {"a": "1", "b": "2"},
        {'foo': lambda x: x + 'foo'}
    ) == "1fooa2")
    with pytest.raises(UnsetParameter):
        parse_str("{{ a }}a{{ b }}").render({"a": "1"})
    with pytest.raises(UnsetParameter):
        parse_str("{{ a }}").render({"c": "1"})
    with pytest.raises(UnsetParameter):
        parse_str("{{ a | foo }}").render({"a": "1"})
    assert parse_str("{% for a in b %}{{ a }}{% endfor %}").render({"b": ['a', 'test']}) == "atest"

    assert (parse_str("{% for a in b %}{{ a }}{% endfor %}else{{ a }}").render({"b": ['b', 't', 'c'], "a": "foo"}) ==
            "btcelsefoo")
    with pytest.raises(UnsetParameter):
        parse_str("{% for a in b %}{{ a }}{% endfor %}else{{ a }}").render({"b": ['b', 't', 'c']})