def test_dict_call():
    template = '''{{ dict(x=\ndict(\na=1, b=2), y=a) }}'''
    call_ast = parse(template).find(nodes.Call)
    rtype, struct = visit_call(
        call_ast, Context(predicted_struct=Unknown.from_ast(call_ast)))

    expected_rtype = Dictionary(
        {
            'x':
            Dictionary(
                {
                    'a': Number(linenos=[3], constant=True, value=1),
                    'b': Number(linenos=[3], constant=True, value=2)
                },
                linenos=[2],
                constant=True),
            'y':
            Unknown(label='a', linenos=[3]),
        },
        linenos=[1],
        constant=True)
    assert rtype == expected_rtype

    expected_struct = Dictionary({
        'a': Unknown(label='a', linenos=[3]),
    })
    assert struct == expected_struct
Example #2
0
def test_slice():
    template = '''{{ xs[a:2:b] }}'''
    ast = parse(template).find(nodes.Getitem)
    rtype, struct = visit_getitem(ast, get_scalar_context(ast))
    assert struct == Dictionary({
        'xs': List(Scalar(linenos=[1]), label='xs', linenos=[1]),
        'a': Number(label='a', linenos=[1]),
        'b': Number(label='b', linenos=[1]),
    })
def test_range_call():
    template = '{{ range(n) }}'
    ast = parse(template).find(nodes.Call)

    rtype, struct = visit_call(ast, Context(predicted_struct=Unknown()))

    expected_rtype = List(Number())
    assert rtype == expected_rtype

    expected_struct = Dictionary({
        'n': Number(label='n', linenos=[1]),
    })
    assert struct == expected_struct
def test_int_filter():
    ast = parse('{{ x|int }}').find(nodes.Filter)
    rtype, struct = visit_filter(ast, get_scalar_context(ast))
    assert rtype == Number(label='x', linenos=[1])
    assert struct == Dictionary({
        'x': Scalar(label='x', linenos=[1]),
    })
Example #5
0
def test_macro_visitor_1():
    template = '''
    {% macro input(name, value='', type='text', size=20) -%}
        <input type="{{ type }}" name="{{ name }}" value="{{value|e }}" size="{{ size }}">
        {{ x }}
    {%- endmacro %}
    '''
    ast = parse(template).find(nodes.Macro)

    macroses = {}
    struct = visit_macro(ast, macroses)

    expected_macro = Macro('input', [
        ('name', Scalar(label='argument #1', linenos=[2])),
    ], [
        ('value', String(label='argument "value"', linenos=[2])),
        ('type', String(label='argument "type"', linenos=[2])),
        ('size', Number(label='argument "size"', linenos=[2])),
    ])
    macro = macroses['input']
    assert macro.name == expected_macro.name
    assert macro.args == expected_macro.args
    assert macro.kwargs == expected_macro.kwargs

    expected_struct = Dictionary({'x': Scalar(label='x', linenos=[4])})
    assert struct == expected_struct
def test_length_filter():
    ast = parse('{{ xs|length }}').find(nodes.Filter)
    rtype, struct = visit_filter(ast, get_scalar_context(ast))
    assert rtype == Number(label='xs', linenos=[1])
    assert struct == Dictionary({
        'xs': List(Unknown(), label='xs', linenos=[1]),
    })
Example #7
0
def test_1():
    template = '''
    {%- if x is undefined %}
        {{ test }}
    {%- endif %}

    {%- if y is undefined %}
        {% set y = 123 %}
    {%- endif %}

    {%- if y is defined %}
        {{ y }}
    {%- endif %}

    {%- if z is undefined %}
        {{ z }}
    {%- endif %}
    '''
    struct = infer(template)
    expected_struct = Dictionary({
        'x':
        Unknown(label='x', checked_as_undefined=True, linenos=[2]),
        'test':
        Scalar(label='test', linenos=[3]),
        'y':
        Number(label='y', may_be_defined=True, linenos=[6, 7, 10, 11]),
        'z':
        Scalar(label='z', linenos=[14, 15]),
    })
    assert struct == expected_struct
Example #8
0
def test_test_1():
    template = '''{{ x is divisibleby (data.field) }}'''
    ast = parse(template).find(nodes.Test)
    rtype, struct = visit_test(ast, get_scalar_context(ast))

    expected_struct = Dictionary({
        'x':
        Scalar(label='x', linenos=[1]),
        'data':
        Dictionary({
            'field': Number(label='field', linenos=[1]),
        },
                   label='data',
                   linenos=[1])
    })

    assert struct == expected_struct

    template = '''{{ x is divisibleby 3 }}'''
    ast = parse(template).find(nodes.Test)
    rtype, struct = visit_test(ast, get_scalar_context(ast))

    expected_struct = Dictionary({
        'x': Scalar(label='x', linenos=[1]),
    })
    assert struct == expected_struct
def test_filesizeformat_filter():
    template = '{{ x|filesizeformat }}'

    ast = parse(template).find(nodes.Filter)
    rtype, struct = visit_filter(ast, get_scalar_context(ast))

    assert rtype == String(label='x', linenos=[1])
    expected_struct = Dictionary({
        'x': Number(label='x', linenos=[1]),
    })
    assert struct == expected_struct
Example #10
0
def test_length_filter():
    for filter in ('count', 'length'):
        template = '{{ xs|' + filter + ' }}'

        ast = parse(template).find(nodes.Filter)
        rtype, struct = visit_filter(ast, get_scalar_context(ast))
        assert rtype == Number(label='xs', linenos=[1])
        assert struct == Dictionary({
            'xs':
            List(Unknown(), label='xs', linenos=[1]),
        })
Example #11
0
def test_assign_4():
    template = '''{% set a, b = 1, {'gsom': 'gsom', z: z} %}'''
    ast = parse(template).find(nodes.Assign)

    struct = visit_assign(ast)
    expected_struct = Dictionary({
        'a': Number(label='a', linenos=[1], constant=True, value=1),
        'b': Dictionary(data={
            'gsom': String(linenos=[1], constant=True, value='gsom'),
        }, label='b', linenos=[1], constant=True),
        'z': Scalar(label='z', linenos=[1]),
    })
    assert struct == expected_struct
Example #12
0
def test_assign_5():
    template = '''
    {%- set weights = [
        ('A', {'data': 0.3}),
        ('B', {'data': 0.9}),
    ] %}
    '''
    ast = parse(template).find(nodes.Assign)
    struct = visit_assign(ast)
    expected_struct = Dictionary({
        'weights': List(Tuple([
            String(linenos=[3, 4], constant=True),
            Dictionary({
                'data': Number(linenos=[3, 4], constant=True)
            }, linenos=[3, 4], constant=True),
        ], linenos=[3, 4], constant=True), label='weights', linenos=[2], constant=True)
    })
    assert struct == expected_struct
Example #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
Example #14
0
def test_to_json_schema():
    struct = Dictionary({
        'list':
        List(Tuple((Dictionary({
            'field': Scalar(label='field', linenos=[3]),
        },
                               label='a',
                               linenos=[3]), Scalar(label='b', linenos=[4])),
                   linenos=[2]),
             label='list',
             linenos=[2]),
        'x':
        Unknown(may_be_defined=True),
        'number_var':
        Number(),
        'string_var':
        String(),
        'boolean_var':
        Boolean(),
    })
    scalar_anyof = [
        {
            'type': 'boolean'
        },
        {
            'type': 'null'
        },
        {
            'type': 'number'
        },
        {
            'type': 'string'
        },
    ]
    unknown_anyof = [
        {
            'type': 'object'
        },
        {
            'type': 'array'
        },
        {
            'type': 'string'
        },
        {
            'type': 'number'
        },
        {
            'type': 'boolean'
        },
        {
            'type': 'null'
        },
    ]

    json_schema = core.to_json_schema(struct)
    assert json_schema['type'] == 'object'
    assert set(json_schema['required']) == set(
        ['string_var', 'list', 'boolean_var', 'number_var'])
    assert json_schema['properties'] == {
        'list': {
            'title': 'list',
            'type': 'array',
            'items': {
                'type':
                'array',
                'items': [{
                    'title': 'a',
                    'type': 'object',
                    'required': ['field'],
                    'properties': {
                        'field': {
                            'anyOf': scalar_anyof,
                            'title': 'field'
                        }
                    },
                }, {
                    'title': 'b',
                    'anyOf': scalar_anyof,
                }],
            },
        },
        'x': {
            'anyOf': unknown_anyof,
        },
        'number_var': {
            'type': 'number',
        },
        'string_var': {
            'type': 'string',
        },
        'boolean_var': {
            'type': 'boolean',
        },
    }