How to use icontract - 10 common examples

To help you get started, we’ve selected a few icontract examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Parquery / icontract / tests / test_inheritance_invariant.py View on Github external
def test_additional_invariant_violated_in_childs_init(self) -> None:
        @icontract.invariant(lambda self: self.x > 0)
        class A(icontract.DBC):
            def __init__(self) -> None:
                self.x = 10

            def __repr__(self) -> str:
                return "instance of A"

        @icontract.invariant(lambda self: self.x > 100)
        class B(A):
            def __repr__(self) -> str:
                return "instance of B"

        violation_error = None  # type: Optional[icontract.ViolationError]
        try:
            _ = B()
        except icontract.ViolationError as err:
            violation_error = err
github Parquery / icontract / tests / test_inheritance_precondition.py View on Github external
            @icontract.require(lambda x: x % 2 == 0)
            def func(self, x: int) -> None:
                pass
github Parquery / icontract / tests / test_represent.py View on Github external
        @icontract.ensure(lambda result: all(single_res[1].is_absolute() for single_res in result))
        def some_func() -> List[Tuple[pathlib.Path, pathlib.Path]]:
            return [(pathlib.Path("/home/file1"), pathlib.Path("home/file2"))]
github Parquery / icontract / tests / test_inheritance_postcondition.py View on Github external
            @icontract.ensure(lambda result: result < 100)
            def func(self) -> int:
                return 1000
github Parquery / icontract / tests / test_postcondition.py View on Github external
            @icontract.ensure(lambda self: self.some_prop > 0)
            def some_prop(self) -> None:
                pass
github Parquery / icontract / tests / test_invariant.py View on Github external
            @icontract.ensure(lambda result: result > 0)
            def some_method(self) -> int:
                self.x = -1
                return 10
github Parquery / icontract / tests / test_snapshot.py View on Github external
        @icontract.ensure(lambda OLD, val, lst: OLD.len_lst + 1 == len(lst))
        def some_func(lst: List[int], val: int) -> None:
            lst.append(val)
            lst.append(1984)
github Parquery / icontract / tests / test_inheritance_snapshot.py View on Github external
            @icontract.ensure(lambda OLD, self, val: OLD.lst + [val] == self.lst)
            def some_func(self, val: int) -> None:
                self.lst.append(val)
                self.lst.append(1984)
github Parquery / icontract / tests / test_postcondition.py View on Github external
        @icontract.ensure(lambda result, x: result > x, "expected summation")
        def some_func(x: int, y: int = 5) -> int:
            return x - y
github Parquery / icontract / tests / test_inheritance_postcondition.py View on Github external
            @icontract.ensure(lambda result: result % 3 == 0)
            def func(self) -> int:
                return 2