示例#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
示例#2
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
示例#3
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
示例#4
0
def test_for_3():
    template = '''
    {% for a, b in list %}
        {{ a.field }}
        {{ b }}
    {% endfor %}
    '''
    ast = parse(template).find(nodes.For)
    struct = visit_for(ast)

    expected_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])
    })
    assert struct == expected_struct
示例#5
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',
        },
    }