Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_check_pure_no_returns():
checker = CheckPure()
text = """
@deal.pure
def test(a):
a + 3
"""
text = dedent(text).strip()
funcs1 = Func.from_ast(ast.parse(text))
funcs2 = Func.from_astroid(astroid.parse(text))
for func in (funcs1[0], funcs2[0]):
actual = [tuple(err) for err in checker(func)]
assert len(actual) == 1
expected = 'DEAL023 pure contract error (no return)'
assert actual[0][2] == expected
def test_check_returns_ok_unresolved():
checker = CheckReturns()
text = """
@deal.post(unknown)
def test(a):
return 1
"""
text = dedent(text).strip()
funcs1 = Func.from_ast(ast.parse(text))
funcs2 = Func.from_astroid(astroid.parse(text))
for func in (funcs1[0], funcs2[0]):
actual = tuple(checker(func))
assert not actual
def test_check_returns():
checker = CheckReturns()
text = """
@deal.post(lambda x: x > 0)
def test(a):
if a:
return 1
else:
return -1
"""
text = dedent(text).strip()
funcs1 = Func.from_ast(ast.parse(text))
funcs2 = Func.from_astroid(astroid.parse(text))
for func in (funcs1[0], funcs2[0]):
actual = [tuple(err) for err in checker(func)]
expected = [(6, 15, 'DEAL012 post contract error (-1)')]
assert actual == expected
def test_resolve_func():
text = """
import deal
def contract(x):
return x > 0
@deal.post(contract)
def f(x):
...
"""
text = dedent(text).strip()
funcs = Func.from_astroid(astroid.parse(text))
assert len(funcs) == 2
func = funcs[-1]
assert len(func.contracts) == 1
c = func.contracts[0]
assert c.run(1) is True
assert c.run(-1) is False
def test_return_message():
text = """
import deal
@deal.post(lambda x: x > 0 or 'oh no!')
def f(x):
return x
"""
text = dedent(text).strip()
funcs1 = Func.from_ast(ast.parse(text))
assert len(funcs1) == 1
funcs2 = Func.from_astroid(astroid.parse(text))
assert len(funcs2) == 1
for func in (funcs1[0], funcs2[0]):
assert len(func.contracts) == 1
c = func.contracts[0]
assert c.run(1) is True
assert c.run(-1) == 'oh no!'
def test_from_astroid():
funcs = Func.from_astroid(astroid.parse(TEXT))
assert len(funcs) == 3
assert len(funcs[0].contracts) == 2
checker = CheckPre()
text = """
@deal.pre(lambda x: x > 0)
def example(x):
return -x
@deal.raises()
def caller():
return example(-3)
# ignore funcs without contracts
def caller():
return example(-3)
"""
text = dedent(text).strip()
funcs = Func.from_astroid(astroid.parse(text))
assert len(funcs) == 3
actual = []
for func in funcs:
actual.extend(tuple(err) for err in checker(func))
expected = [(7, 11, 'DEAL011 pre contract error (-3)')]
assert actual == expected
def test_skip_asserts_in_tests():
checker = CheckAsserts()
text = """
def test_example(a):
assert False, "oh no!"
"""
text = dedent(text).strip()
funcs1 = Func.from_ast(ast.parse(text))
funcs2 = Func.from_astroid(astroid.parse(text))
for func in (funcs1[0], funcs2[0]):
actual = list(checker(func))
assert actual == []
def test_exceptions():
funcs1 = Func.from_ast(ast.parse(TEXT))
assert len(funcs1) == 1
funcs2 = Func.from_astroid(astroid.parse(TEXT))
assert len(funcs2) == 1
for func in (funcs1[0], funcs2[0]):
assert len(func.contracts) == 1
contract = func.contracts[0]
assert contract.exceptions == [ValueError, 'UnknownError']