Example #1
0
def test_ast_equal():
    code = """
@public
def test() -> int128:
    a: uint256 = 100
    return 123
    """

    ast1 = parse_to_ast(code)
    ast2 = parse_to_ast("\n   \n" + code + "\n\n")

    assert ast1 == ast2
Example #2
0
def test_ast_unequal():
    code1 = """
@public
def test() -> int128:
    a: uint256 = 100
    return 123
    """
    code2 = """
@public
def test() -> int128:
    a: uint256 = 100
    return 121
    """

    ast1 = parse_to_ast(code1)
    ast2 = parse_to_ast(code2)

    assert ast1 != ast2
Example #3
0
def test_ast_to_string():
    code = """
@public
def testme(a: int128) -> int128:
    return a
    """
    vyper_ast = parse_to_ast(code)
    assert ast_to_string(vyper_ast) == (
        "Module(body=[FunctionDef(name='testme', args=arguments(args=[arg(arg='a',"
        " annotation=Name(id='int128'))], defaults=[]), body=[Return(value=Name(id='a'))],"
        " decorator_list=[Name(id='public')], returns=Name(id='int128'))])")
Example #4
0
def test_dict_to_ast():
    code = """
@public
def test() -> int128:
    a: uint256 = 100
    b: int128 = -22
    c: decimal = 3.31337
    d: bytes[11] = b"oh hai mark"
    e: address = 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
    f: bool = False
    return 123
    """

    original_ast = parse_to_ast(code)
    out_dict = ast_to_dict(original_ast)
    new_ast = dict_to_ast(out_dict)

    assert new_ast == original_ast
Example #5
0
def test_dict_to_ast():
    code = """
@external
def test() -> int128:
    a: uint256 = 100
    b: int128 = -22
    c: decimal = -3.3133700
    d: Bytes[11] = b"oh hai mark"
    e: Bytes[1] = 0b01010101
    f: Bytes[88] = b"\x01\x70"
    g: String[100] = "  baka baka   "
    h: address = 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
    i: bool = False
    return 123
    """

    original_ast = parse_to_ast(code)
    out_dict = ast_to_dict(original_ast)
    out_json = json.dumps(out_dict)
    new_dict = json.loads(out_json)
    new_ast = dict_to_ast(new_dict)

    assert new_ast == original_ast
Example #6
0
def vyper_funcs_from_source(source_text):
    """ Generate an AST and pull all function defs from it

    :param source_text: (:code:`str`) The full source code
    :returns: (:code:`list`) The source code definition of the functions
    """

    source_ast: List = parse_to_ast(source_text)

    funcs: List = []

    for i in range(0, len(source_ast)):
        node = source_ast[i]

        if type(node) == FunctionDef:
            start = node.lineno
            end = -1

            if i < len(source_ast) - 1:
                end = source_ast[i + 1].lineno - 1

            funcs.append(source_extract(source_text, start, end))
    return funcs