Beispiel #1
0
def test_basics_13():
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE = 'tuple'

    template = '''
    {% for x in xs %}
        {{ x[2] }}
        {{ x[3] }}
    {% endfor %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'xs':
        List(Tuple((
            Unknown(label=None, linenos=[]),
            Unknown(label=None, linenos=[]),
            Scalar(label=None, linenos=[3]),
            Scalar(label=None, linenos=[4]),
        ),
                   label='x',
                   linenos=[3, 4]),
             label='xs',
             linenos=[2])
    })
    assert struct == expected_struct
Beispiel #2
0
def test_basics_15():
    # 1. Accept no unknown filter (default behavior)
    config = Config()

    template = '''
    {% for item in items %}
        {{ item.attr1|lower }}
        {{ item.attr2|bar }}
    {% endfor %}
    '''

    with pytest.raises(InvalidExpression) as e:
        infer(template, config)
    assert 'line 4: unknown filter "bar"' == str(e.value)

    # 2. Accept all unknown filters
    config = Config()
    config.IGNORE_UNKNOWN_FILTERS = True

    template = '''
    {% for item in items %}
        {{ item.attr1|lower }}
        {{ item.attr2|bar }}
    {% endfor %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'items': List(Dictionary({
            'attr1': String(label='attr1', linenos=[3]),
            'attr2': Unknown(label='attr2', linenos=[4])
        }, label='item', linenos=[3, 4]), label='items', linenos=[2]),
    })
    assert struct == expected_struct
Beispiel #3
0
def test_boolean_conditions_setting_1():
    template = '''
    {% if x %}
        Hello!
    {% endif %}
    {{ 'Hello!' if y else '' }}
    '''
    config_1 = Config()
    struct = infer(template, config_1)
    expected_struct = Dictionary({
        'x': Unknown(label='x', linenos=[2]),
        'y': Unknown(label='y', linenos=[5]),
    })
    assert struct == expected_struct

    infer('{% if [] %}{% endif %}', config_1)  # make sure it doesn't raise

    config_2 = Config(BOOLEAN_CONDITIONS=True)
    struct = infer(template, config_2)
    expected_struct = Dictionary({
        'x': Boolean(label='x', linenos=[2]),
        'y': Boolean(label='y', linenos=[5]),
    })
    assert struct == expected_struct

    with pytest.raises(UnexpectedExpression) as e:
        infer('{% if [] %}{% endif %}', config_2)  # make sure this does raise
    assert str(e.value) == (
        'conflict on the line 1\n'
        'got: AST node jinja2.nodes.List of structure [<unknown>]\n'
        'expected structure: <boolean>')
Beispiel #4
0
def test_getitem_1():
    template = '''{{ a['b']['c'][1]['d'][x] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a':
        Dictionary(
            {
                'b':
                Dictionary(
                    {
                        'c':
                        List(Dictionary(
                            {
                                'd':
                                List(Scalar(linenos=[1]),
                                     label='d',
                                     linenos=[1])
                            },
                            linenos=[1]),
                             label='c',
                             linenos=[1]),
                    },
                    label='b',
                    linenos=[1]),
            },
            label='a',
            linenos=[1]),
        'x':
        Scalar(label='x', linenos=[1]),
    })
    assert struct == expected_struct
Beispiel #5
0
def test_getitem_2():
    template = '''{{ a[z] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'dictionary'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': Dictionary(label='a', linenos=[1]),
        'z': Scalar(label='z', linenos=[1]),
    })
    assert struct == expected_struct
def test_getitem_2():
    template = '''{{ a[z] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'dictionary'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': Dictionary(label='a', linenos=[1]),
        'z': Scalar(label='z', linenos=[1]),
    })
    assert struct == expected_struct
Beispiel #7
0
def test_getitem_3():
    template = '''{{ a[3] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE = 'tuple'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': Tuple([
            Unknown(),
            Unknown(),
            Unknown(),
            Scalar(linenos=[1]),
        ], label='a', linenos=[1]),
    })
    assert struct == expected_struct
def test_getitem_3():
    template = '''{{ a[3] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE = 'tuple'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': Tuple([
            Unknown(),
            Unknown(),
            Unknown(),
            Scalar(linenos=[1]),
        ], label='a', linenos=[1]),
    })
    assert struct == expected_struct
def test_getitem_1():
    template = '''{{ a['b']['c'][1]['d'][x] }}'''
    ast = parse(template).find(nodes.Getitem)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list'
    rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': Dictionary({
            'b': Dictionary({
                'c': List(Dictionary({
                    'd': List(Scalar(linenos=[1]), label='d', linenos=[1])
                }, linenos=[1]), label='c', linenos=[1]),
            }, label='b', linenos=[1]),
        }, label='a', linenos=[1]),
        'x': Scalar(label='x', linenos=[1]),
    })
    assert struct == expected_struct
Beispiel #10
0
def test_extend_with_block_override_1():
    env = Environment(loader=PackageLoader('tests', 'templates'))
    struct = infer(
        env.loader.get_source(env, 'inner_override_1.html')[0],
        Config(PACKAGE_NAME='tests'))
    expected_struct = Dictionary({
        'name': Scalar(label='name', linenos=[3]),
    })
    assert struct == expected_struct
Beispiel #11
0
def test_basics_13():
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE = 'tuple'

    template = '''
    {% for x in xs %}
        {{ x[2] }}
        {{ x[3] }}
    {% endfor %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'xs': List(Tuple((
            Unknown(label=None, linenos=[]),
            Unknown(label=None, linenos=[]),
            Scalar(label=None, linenos=[3]),
            Scalar(label=None, linenos=[4]),
        ), label='x', linenos=[3, 4]), label='xs', linenos=[2])
    })
    assert struct == expected_struct
Beispiel #12
0
def test_order_number_setting_1_5():
    config = Config(ORDER_NUMBER=True)

    template = '''
    {% if yy %}
    {{ intooo }}
    {{ zz }}
    {% endif %}
    '''
    struct = infer(template, config)
    assert struct['yy'].order_nr < struct['zz'].order_nr
Beispiel #13
0
def test_getattr_3():
    template = '''{{ a[z][1:\nn][1].x }}'''
    ast = parse(template).find(nodes.Getattr)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list'
    rtype, struct = visit_getattr(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a':
        List(List(List(Dictionary({'x': Scalar(label='x', linenos=[2])},
                                  linenos=[2]),
                       linenos=[2]),
                  linenos=[1]),
             label='a',
             linenos=[1]),
        'z':
        Scalar(label='z', linenos=[1]),
        'n':
        Number(label='n', linenos=[2])
    })
    assert struct == expected_struct
Beispiel #14
0
def test_boolean_conditions_setting_2():
    config = Config(BOOLEAN_CONDITIONS=True)

    template = '''
    {% if x == 'test' %}
        Hello!
    {% endif %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x': Unknown(label='x', linenos=[2]),
    })
    assert struct == expected_struct
Beispiel #15
0
def test_order_number_setting_1():
    config = Config(ORDER_NUMBER=True)

    template = '''
    {{ x }}
    {{ y }}
    {{ z }}
    {{ x }}
    {{ x }}
    '''
    struct = infer(template, config)
    assert struct['x'].order_nr < struct['y'].order_nr
    assert struct['y'].order_nr < struct['z'].order_nr
Beispiel #16
0
def test_block_1():
    config = Config()

    template = '''
        {% block test %}
            {{ x }}
            {{ y }}
        {% endblock %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x': Scalar(label='x', linenos=[3]),
        'y': Scalar(label='y', linenos=[4]),
    })
    assert struct == expected_struct
def test_getattr_3():
    template = '''{{ a[z][1:\nn][1].x }}'''
    ast = parse(template).find(nodes.Getattr)
    config = Config()
    config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list'
    rtype, struct = visit_getattr(ast, get_scalar_context(ast), {}, config)

    expected_struct = Dictionary({
        'a': List(
            List(
                List(
                    Dictionary({
                        'x': Scalar(label='x', linenos=[2])
                    }, linenos=[2]),
                    linenos=[2]),
                linenos=[1]
            ),
            label='a',
            linenos=[1]
        ),
        'z': Scalar(label='z', linenos=[1]),
        'n': Number(label='n', linenos=[2])
    })
    assert struct == expected_struct
Beispiel #18
0
def test_order_number_setting_4():
    config = Config(ORDER_NUMBER=True, ORDER_NUMBER_SUB_COUNTER=False)

    template = '''
    {% for a in aa %}
    {{ ax }}
    {% for b in bb %}
    {{ bx }}
    {% for c in cc %}
    {{ cx }}
    {% endfor %}
    {% endfor %}
    {% endfor %}
    '''
    struct = infer(template, config)
    assert struct['ax'].order_nr != struct['bx'].order_nr != struct['cx'].order_nr
Beispiel #19
0
def test_extend_1():
    env = Environment(loader=PackageLoader('tests', 'templates'))
    struct = infer(
        env.loader.get_source(env, 'inner_extend.html')[0],
        Config(PACKAGE_NAME='tests'))
    expected_struct = Dictionary({
        'var':
        Dictionary({'a': Scalar(label='a', linenos=[1])},
                   label='var',
                   linenos=[1]),
        'some':
        Scalar(label='some', linenos=[2]),
        'extended':
        Scalar(label='extended', linenos=[2]),
    })
    assert struct == expected_struct
Beispiel #20
0
def test_order_number_setting_2():
    config = Config(ORDER_NUMBER=True)

    template = '''
    {% for n in nx %}
    {{ y }}
    {{ z }}
    {% endfor %}
    {% if yy %}
    {{ zz }}
    {{ xx }}
    {% endif %}
    '''
    struct = infer(template, config)
    assert struct['y'].order_nr < struct['z'].order_nr
    assert struct['nx'].order_nr < struct['yy'].order_nr
    assert struct['zz'].order_nr < struct['xx'].order_nr
Beispiel #21
0
def test_include_1():
    env = Environment(loader=PackageLoader('tests', 'templates'))
    struct = infer(
        env.loader.get_source(env, 'inner_include.html')[0],
        Config(PACKAGE_NAME='tests'))
    expected_struct = Dictionary({
        'var':
        Dictionary(
            {
                'x': Scalar(label='x', linenos=[1]),
                'y': Scalar(label='y', linenos=[1])
            },
            label='var',
            linenos=[1]),
        'more':
        Scalar(label='more', linenos=[2]),
    })
    assert struct == expected_struct
Beispiel #22
0
def test_3():
    config = Config(BOOLEAN_CONDITIONS=True)
    template = '''
    {%- if new_configuration is undefined %}
      {%- if production is defined and production %}
        {% set new_configuration = 'prefix-' ~ timestamp %}
      {%- else %}
        {% set new_configuration = 'prefix-' ~ timestamp %}
      {%- endif %}
    {%- endif %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'new_configuration':
        String(label='new_configuration',
               may_be_defined=True,
               checked_as_undefined=True,
               linenos=[2, 4, 6]),
        'production':
        Boolean(label='production', checked_as_defined=True, linenos=[3]),
        'timestamp':
        String(label='timestamp', linenos=[4, 6]),
    })
    assert struct == expected_struct
def config():
    return Config(PACKAGE_NAME='tests')
Beispiel #24
0
def test_4():
    template = '''{{ 'x and y' if x and y is defined else ':(' }}'''
    config = Config(BOOLEAN_CONDITIONS=True)
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x':
        Boolean(label='x', linenos=[1]),
        'y':
        Unknown(label='y', checked_as_defined=True, linenos=[1]),
    })
    assert struct == expected_struct

    template = '''
    {% if x is defined and x.a == 'a' %}
        {{ x.b }}
    {% endif %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x':
        Dictionary(
            {
                'a': Unknown(label='a', linenos=[2]),
                'b': Scalar(label='b', linenos=[3]),
            },
            label='x',
            checked_as_defined=True,
            linenos=[2, 3])
    })
    assert struct == expected_struct

    template = '''
    {% if x is undefined and x.a == 'a' %}
        {{ x.b }}
    {% endif %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x':
        Dictionary(
            {
                'a': Unknown(label='a', linenos=[2]),
                'b': Scalar(label='b', linenos=[3]),
            },
            label='x',
            linenos=[2, 3])
    })
    assert struct == expected_struct

    template = '''
    {% if x is undefined %}
        none
    {% endif %}
    {{ x }}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x': Scalar(label='x', linenos=[2, 5]),
    })
    assert struct == expected_struct

    template = '''
    {% if x is defined %}
        none
    {% endif %}
    {{ x }}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x': Scalar(label='x', linenos=[2, 5]),
    })
    assert struct == expected_struct

    template = '''
    {% if x is defined %}
    {% else %}
        {{ x }}
    {% endif %}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'x': Scalar(label='x', linenos=[2, 4]),
    })
    assert struct == expected_struct

    template = '''
    queue: {{ queue if queue is defined else 'wizard' }}
    queue: {{ queue if queue is defined else 'wizard' }}
    '''
    struct = infer(template, config)
    expected_struct = Dictionary({
        'queue':
        Scalar(label='queue', linenos=[2, 3], checked_as_defined=True)
    })
    assert struct == expected_struct
Beispiel #25
0
def config():
    ret = Config(PACKAGE_NAME='tests')
    ret.add_hint(url_for)
    ret.add_hint(get_flashed_messages)
    return ret