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_raises_inherited():
checker = CheckRaises()
text = """
@deal.raises(LookupError)
def test(a):
raise KeyError
raise ValueError
"""
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 = [(4, 10, 'DEAL021 raises contract error (ValueError)')]
assert actual == expected
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_with_message():
checker = CheckReturns()
text = """
@deal.post(lambda x: x > 0 or 'oh no!')
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 oh no! (-1)')]
assert actual == expected
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']
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_check_asserts():
checker = CheckAsserts()
text = """
def 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 = [tuple(err) for err in checker(func)]
expected = [(2, 11, 'DEAL031 assert error (False)')]
assert actual == expected
def test_repr():
funcs1 = Func.from_ast(ast.parse(TEXT))
funcs2 = Func.from_astroid(astroid.parse(TEXT))
for func in (funcs1[0], funcs2[0]):
assert repr(func) == 'Func(post, raises)'
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 == []