예제 #1
0
def test_ast_to_code_using_kitchen_sink():
    doc = parse(fixtures.KITCHEN_SINK)
    code_ast = ast_to_code(doc)
    source = Source(fixtures.KITCHEN_SINK)
    loc = lambda start, end: Loc(start, end, source)

    parsed_code_ast = eval(code_ast, {}, {'ast': ast, 'loc': loc})
    assert doc == parsed_code_ast
예제 #2
0
def test_converts_simple_ast_to_dict():
    node = ast.Name(value="test", loc=Loc(start=5, end=10))

    assert ast_to_dict(node) == {"kind": "Name", "value": "test"}
    assert ast_to_dict(node, include_loc=True) == {
        "kind": "Name",
        "value": "test",
        "loc": {
            "start": 5,
            "end": 10
        },
    }
예제 #3
0
def test_converts_simple_ast_to_dict():
    node = ast.Name(value='test', loc=Loc(start=5, end=10))

    assert ast_to_dict(node) == {'kind': 'Name', 'value': 'test'}
    assert ast_to_dict(node, include_loc=True) == {
        'kind': 'Name',
        'value': 'test',
        'loc': {
            'start': 5,
            'end': 10
        }
    }
예제 #4
0
 def loc(start, end):
     return Loc(start, end, source)
예제 #5
0
def create_loc_fn(body):
    source = Source(body)
    return lambda start, end: Loc(start, end, source)
예제 #6
0
def create_loc_fn(body):
    # type: (str) -> Callable
    source = Source(body)
    return lambda start, end: Loc(start, end, source)
예제 #7
0
def test_parse_creates_ast():
    # type: () -> None
    source = Source(
        """{
  node(id: 4) {
    id,
    name
  }
}
"""
    )
    result = parse(source)

    assert result == ast.Document(
        loc=Loc(start=0, end=41, source=source),
        definitions=[
            ast.OperationDefinition(
                loc=Loc(start=0, end=40, source=source),
                operation="query",
                name=None,
                variable_definitions=None,
                directives=[],
                selection_set=ast.SelectionSet(
                    loc=Loc(start=0, end=40, source=source),
                    selections=[
                        ast.Field(
                            loc=Loc(start=4, end=38, source=source),
                            alias=None,
                            name=ast.Name(
                                loc=Loc(start=4, end=8, source=source), value="node"
                            ),
                            arguments=[
                                ast.Argument(
                                    name=ast.Name(
                                        loc=Loc(start=9, end=11, source=source),
                                        value="id",
                                    ),
                                    value=ast.IntValue(
                                        loc=Loc(start=13, end=14, source=source),
                                        value="4",
                                    ),
                                    loc=Loc(start=9, end=14, source=source),
                                )
                            ],
                            directives=[],
                            selection_set=ast.SelectionSet(
                                loc=Loc(start=16, end=38, source=source),
                                selections=[
                                    ast.Field(
                                        loc=Loc(start=22, end=24, source=source),
                                        alias=None,
                                        name=ast.Name(
                                            loc=Loc(start=22, end=24, source=source),
                                            value="id",
                                        ),
                                        arguments=[],
                                        directives=[],
                                        selection_set=None,
                                    ),
                                    ast.Field(
                                        loc=Loc(start=30, end=34, source=source),
                                        alias=None,
                                        name=ast.Name(
                                            loc=Loc(start=30, end=34, source=source),
                                            value="name",
                                        ),
                                        arguments=[],
                                        directives=[],
                                        selection_set=None,
                                    ),
                                ],
                            ),
                        )
                    ],
                ),
            )
        ],
    )
예제 #8
0
def test_repr_loc():
    # type: () -> None
    loc = Loc(start=10, end=25, source="foo")
    assert repr(loc) == "<Loc start=10 end=25 source=foo>"
예제 #9
0
def test_parse_creates_ast():
    source = Source("""{
  node(id: 4) {
    id,
    name
  }
}
""")
    result = parse(source)

    assert result == \
        ast.Document(
            loc=Loc(start=0, end=41, source=source),
            definitions=[ast.OperationDefinition(
                loc=Loc(start=0, end=40, source=source),
                operation='query',
                name=None,
                variable_definitions=None,
                directives=[],
                selection_set=ast.SelectionSet(
                    loc=Loc(start=0, end=40, source=source),
                    selections=[ast.Field(
                        loc=Loc(start=4, end=38, source=source),
                        alias=None,
                        name=ast.Name(
                            loc=Loc(start=4, end=8, source=source),
                            value='node'),
                        arguments=[ast.Argument(
                            name=ast.Name(loc=Loc(start=9, end=11, source=source),
                                          value='id'),
                            value=ast.IntValue(
                                   loc=Loc(start=13, end=14, source=source),
                                   value='4'),
                            loc=Loc(start=9, end=14, source=source))],
                        directives=[],
                        selection_set=ast.SelectionSet(
                            loc=Loc(start=16, end=38, source=source),
                            selections=[ast.Field(
                                loc=Loc(start=22, end=24, source=source),
                                alias=None,
                                name=ast.Name(
                                    loc=Loc(start=22, end=24, source=source),
                                    value='id'),
                                arguments=[],
                                directives=[],
                                selection_set=None),
                                ast.Field(
                                loc=Loc(start=30, end=34, source=source),
                                alias=None,
                                name=ast.Name(
                                    loc=Loc(start=30, end=34, source=source),
                                    value='name'),
                                arguments=[],
                                directives=[],
                                selection_set=None)]))]))])
예제 #10
0
def test_repr_loc():
    loc = Loc(start=10, end=25, source='foo')
    assert repr(loc) == '<Loc start=10 end=25 source=foo>'