How to use the icontract.DBC function in icontract

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_icontract.py View on Github external
def test_deleter(self):
        class SomeBase(icontract.DBC):
            def __init__(self):
                self.toggled = True

            @property
            def some_prop(self) -> int:
                return 0

            @some_prop.deleter
            @icontract.pre(lambda self: not self.toggled)
            def some_prop(self) -> None:
                pass

        class SomeClass(SomeBase):
            def __repr__(self):
                return self.__class__.__name__
github Parquery / icontract / tests / test_inheritance_precondition.py View on Github external
def test_inherited_with_implementation(self) -> None:
        class A(icontract.DBC):
            @icontract.require(lambda x: x < 100)
            def func(self, x: int) -> None:
                pass

        class B(A):
            def func(self, x: int) -> None:
                pass

        b = B()
        violation_error = None  # type: Optional[icontract.ViolationError]
        try:
            b.func(x=1000)
        except icontract.ViolationError as err:
            violation_error = err

        self.assertIsNotNone(violation_error)
github Parquery / icontract / tests / test_icontract.py View on Github external
def test_require_else_fails(self):
        class A(icontract.DBC):
            @icontract.pre(lambda x: x % 2 == 0)
            def func(self, x: int) -> None:
                pass

        class B(A):
            @icontract.pre(lambda x: x % 3 == 0)
            def func(self, x: int) -> None:
                pass

        b = B()

        violation_err = None  # type: Optional[icontract.ViolationError]
        try:
            b.func(x=5)
        except icontract.ViolationError as err:
            violation_err = err
github Parquery / icontract / tests / test_icontract.py View on Github external
def test_postcondition_in_abstract_class_method(self):
        class Abstract(icontract.DBC):
            @classmethod
            @abc.abstractmethod
            @icontract.post(lambda result: result != 0)
            def some_func(cls: Type, x: int) -> int:
                pass

        class SomeClass(Abstract):
            @classmethod
            def some_func(cls: Type, x: int) -> int:
                return x

        result = SomeClass.some_func(x=1)
        self.assertEqual(1, result)

        icontract_violation_error = None  # type: Optional[icontract.ViolationError]
        try:
github Parquery / icontract / tests / test_postcondition.py View on Github external
def test_postcondition_in_abstract_class_method(self) -> None:
        class Abstract(icontract.DBC):
            @classmethod
            @abc.abstractmethod
            @icontract.ensure(lambda result: result != 0)
            def some_func(cls: Type['Abstract'], x: int) -> int:
                pass

        class SomeClass(Abstract):
            @classmethod
            def some_func(cls: Type['SomeClass'], x: int) -> int:
                return x

        result = SomeClass.some_func(x=1)
        self.assertEqual(1, result)

        violation_error = None  # type: Optional[icontract.ViolationError]
        try:
github Parquery / icontract / tests / test_inheritance_invariant.py View on Github external
def test_inherited_getter(self) -> None:
        @icontract.invariant(lambda self: not self.toggled)
        class SomeBase(icontract.DBC):
            def __init__(self) -> None:
                self.toggled = False

            @property
            def some_prop(self) -> int:
                self.toggled = True
                return 0

        class SomeClass(SomeBase):
            def __repr__(self) -> str:
                return self.__class__.__name__

        some_inst = SomeClass()

        violation_error = None  # type: Optional[icontract.ViolationError]
        try:
github Parquery / icontract / tests / test_inheritance_postcondition.py View on Github external
def test_getter_setter_deleter_ok(self) -> None:
        class SomeBase(icontract.DBC):
            def __init__(self) -> None:
                self.deleted = False
                self._some_prop = 1

            @property  # type: ignore
            @icontract.ensure(lambda self, result: self._some_prop == result)
            def some_prop(self) -> int:
                return self._some_prop

            @some_prop.setter  # type: ignore
            @icontract.ensure(lambda self, value: self.some_prop == value)
            def some_prop(self, value: int) -> None:
                self._some_prop = value

            @some_prop.deleter  # type: ignore
            @icontract.ensure(lambda self: self.deleted)
github Parquery / icontract / tests / test_inheritance_snapshot.py View on Github external
def test_deleter_with_conflicting_snapshot_names(self) -> None:
        class SomeBase(icontract.DBC):
            def __init__(self) -> None:
                self.dels = 0

            @property
            def some_prop(self) -> int:
                return 0

            @some_prop.deleter  # type: ignore
            @icontract.snapshot(lambda self: self.dels, name="dels")
            @icontract.ensure(lambda OLD, self: self.dels == OLD.dels + 1)
            def some_prop(self) -> None:
                self.dels += 1

        value_error = None  # type: Optional[ValueError]
        try:
            # pylint: disable=unused-variable
github Parquery / icontract / tests / test_inheritance_precondition.py View on Github external
def test_deleter(self) -> None:
        class SomeBase(icontract.DBC):
            def __init__(self) -> None:
                self.toggled = True

            @property
            def some_prop(self) -> int:
                return 0

            @some_prop.deleter  # type: ignore
            @icontract.require(lambda self: not self.toggled)
            def some_prop(self) -> None:
                pass

        class SomeClass(SomeBase):
            def __repr__(self) -> str:
                return self.__class__.__name__