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_init(self):
with self.subTest(text='init has not raised any exceptions'):
func = deal.pre(lambda x: x > 0)
with self.subTest(text='validator'):
func = deal.pre(lambda x: x > 0)(lambda x: x)
with pytest.raises(deal.PreContractError):
func(-2)
with self.subTest(text='message'):
func = deal.pre(lambda x: x > 0, message='TEST')(lambda x: x)
try:
func(-2)
except AssertionError as e:
self.assertEqual(e.args[0], 'TEST')
with self.subTest(text='exception'):
func = deal.pre(lambda x: x > 0, exception=NameError)(lambda x: x)
with pytest.raises(NameError):
func(-2)
with self.subTest(text='exception with name'):
func = deal.pre(lambda x: x > 0, exception=NameError('TEST'))(lambda x: x)
with self.subTest(text='exception/exception'):
with pytest.raises(NameError):
func(-2)
with self.subTest(text='exception/message'):
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_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
@deal.pre(scheme, message='old message')
def func(name):
return name * 2
@deal.pre(lambda name: name != 'Oleg')
def func(name):
return name * 2
@deal.pre(lambda x: x > 0 or 'new message', message='old message')
def double(x):
return x * 2