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
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 }, }
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 } }
def loc(start, end): return Loc(start, end, source)
def create_loc_fn(body): source = Source(body) return lambda start, end: Loc(start, end, source)
def create_loc_fn(body): # type: (str) -> Callable source = Source(body) return lambda start, end: Loc(start, end, source)
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, ), ], ), ) ], ), ) ], )
def test_repr_loc(): # type: () -> None loc = Loc(start=10, end=25, source="foo") assert repr(loc) == "<Loc start=10 end=25 source=foo>"
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)]))]))])
def test_repr_loc(): loc = Loc(start=10, end=25, source='foo') assert repr(loc) == '<Loc start=10 end=25 source=foo>'