Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@icontract.ensure(lambda OLD, self: self.dels == OLD.dels + 1)
def some_prop(self) -> None:
# no self.dels increment
return
class SomeClass(SomeBase):
def __repr__(self) -> str:
return self.__class__.__name__
some_inst = SomeClass()
# getter fails
violation_error = None # type: Optional[icontract.ViolationError]
try:
_ = some_inst.some_prop
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual('self.gets == OLD.gets + 1:\n'
'OLD was a bunch of OLD values\n'
'OLD.gets was 0\n'
'self was SomeClass\n'
'self.gets was 0', tests.error.wo_mandatory_location(str(violation_error)))
# setter fails
violation_error = None
try:
some_inst.some_prop = 1 # type: ignore
except icontract.ViolationError as err:
violation_error = err
def test_dict_comprehension(self) -> None:
@icontract.require(lambda x: len({i: i**2 for i in range(x)}) == 0)
def func(x: int) -> int:
return x
violation_error = None # type: Optional[icontract.ViolationError]
try:
func(x=2)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual('len({i: i**2 for i in range(x)}) == 0:\n'
'len({i: i**2 for i in range(x)}) was 2\n'
'range(x) was range(0, 2)\n'
'x was 2\n'
'{i: i**2 for i in range(x)} was {0: 0, 1: 1}',
tests.error.wo_mandatory_location(str(violation_error)))
def test_condition_as_function(self) -> None:
def some_condition(result: int) -> bool:
return result > 3
@icontract.ensure(some_condition)
def some_func(x: int) -> int:
return x
# Valid call
some_func(x=4)
# Invalid call
violation_error = None # type: Optional[icontract.ViolationError]
try:
some_func(x=1)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual('some_condition: result was 1', tests.error.wo_mandatory_location(str(violation_error)))
def test_inherited_without_implementation(self) -> None:
class A(icontract.DBC):
@icontract.ensure(lambda result: result < 100)
def func(self) -> int:
return 1000
class B(A):
pass
b = B()
violation_error = None # type: Optional[icontract.ViolationError]
try:
b.func()
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("result < 100: result was 1000", tests.error.wo_mandatory_location(str(violation_error)))
return 0
@some_prop.setter
def some_prop(self, value: int) -> None:
self.toggled = True
class SomeClass(SomeBase):
def __repr__(self) -> str:
return self.__class__.__name__
some_inst = SomeClass()
violation_error = None # type: Optional[icontract.ViolationError]
try:
some_inst.some_prop = 0
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual('not self.toggled:\n'
'self was SomeClass\n'
'self.toggled was True', tests.error.wo_mandatory_location(str(violation_error)))
def test_multiple_invs_first_violated(self) -> None:
@icontract.invariant(lambda self: self.x > 0)
@icontract.invariant(lambda self: self.x < 10)
class SomeClass:
def __init__(self) -> None:
self.x = -1
def __repr__(self) -> str:
return "some instance"
violation_error = None # type: Optional[icontract.ViolationError]
try:
_ = SomeClass()
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("self.x > 0:\n"
"self was some instance\n"
"self.x was -1", tests.error.wo_mandatory_location(str(violation_error)))
def some_func(a: int, b: int = 21, c: int = 22) -> int:
return a + b
violation_error = None # type: Optional[icontract.ViolationError]
try:
some_func(a=2)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("c < 10: c was 22", tests.error.wo_mandatory_location(str(violation_error)))
violation_error = None
try:
some_func(a=2, c=8)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("b < 10: b was 21", tests.error.wo_mandatory_location(str(violation_error)))
self.toggled = False
@property
def some_prop(self) -> int:
self.toggled = True
return 0
def __repr__(self):
return self.__class__.__name__
some_inst = SomeClass()
icontract_violation_error = None # type: Optional[icontract.ViolationError]
try:
_ = some_inst.some_prop
except icontract.ViolationError as err:
icontract_violation_error = err
self.assertIsNotNone(icontract_violation_error)
self.assertEqual('not self.toggled:\n'
'self was SomeClass\n'
'self.toggled was True', str(icontract_violation_error))
class A(icontract.DBC):
@icontract.require(lambda x: x % 2 == 0)
def func(self, x: int) -> None:
pass
class B(A):
@icontract.require(lambda x: x % 3 == 0)
def func(self, x: int) -> None:
pass
b = B()
violation_error = None # type: Optional[icontract.ViolationError]
try:
b.func(x=5)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("x % 3 == 0: x was 5", tests.error.wo_mandatory_location(str(violation_error)))
def test_with_multiple_arguments(self) -> None:
@icontract.snapshot(lambda lst_a, lst_b: set(lst_a).union(lst_b), name="union")
@icontract.ensure(lambda OLD, lst_a, lst_b: set(lst_a).union(lst_b) == OLD.union)
def some_func(lst_a: List[int], lst_b: List[int]) -> None:
lst_a.append(1984) # bug
violation_error = None # type: Optional[icontract.ViolationError]
try:
some_func(lst_a=[1, 2], lst_b=[3, 4])
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual(
textwrap.dedent('''\
set(lst_a).union(lst_b) == OLD.union:
OLD was a bunch of OLD values
OLD.union was {1, 2, 3, 4}
lst_a was [1, 2, 1984]
lst_b was [3, 4]
set(lst_a) was {1, 2, 1984}
set(lst_a).union(lst_b) was {1, 2, 3, 4, 1984}'''), tests.error.wo_mandatory_location(str(violation_error)))