for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
from py14.tracer import value_type, value_expr, decltype, is_list, is_recursive
from py14.context import add_variable_context, add_list_calls
from py14.scope import add_scope_context
import ast
def parse(*args):
source = ast.parse("\n".join(args))
add_variable_context(source)
add_scope_context(source)
return source
class TestValueType:
def test_direct_assignment(self):
source = parse("x = 3")
x = source.body[0]
t = value_type(x)
assert t == "3"
def test_assign_name(self):
source = parse(
"x = 3",
"y = x",
)
y = source.body[1]
t = value_type(y)
assert t == "x"
def test_assign_function(self):
"y = foo(x)",
assert t == "foo(x)"
def test_list_assignment_with_default_values(self):
"results = [x]",
results = source.body[1]
t = value_type(results)
def test_list_assignment_with_function_call_as_value(self):
"results = [foo(x)]",
def test_list_assignment_based_on_later_append(self):
"results = []",
"results.append(x)",
add_list_calls(source)
def test_list_assignment_with_append_unknown_value(self):
results = source.body[0]
def test_global_list_assignment_with_later_append(self):
"def add_x():",
" results.append(2)",
assert t == "2"
class TestValueExpr:
def test_catch_long_expression_chain(self):
"x = 3 * 1",
"y = x - 5",
"z = y + 2",
z = source.body[2]
t = value_expr(z)
assert t == "3 * 1 - 5 + 2"
def test_catch_expression_chain_with_functions(self):
assert t == "foo(3 * 1) + 2"
def test_decltype_normal_var():
t = decltype(y)
assert t == "decltype(foo(x))"
def test_decltype_list_var():
t = decltype(results)
assert t == "std::vector<decltype(3)>"
def test_is_list():
"list1 = []",
"list2 = list1",
list2 = source.body[1]
assert is_list(list2)
def test_is_recursive():
"def rec(n):",
" return rec(n-1) + rec(n)",
fun = source.body[0]
assert is_recursive(fun)