Exemplo n.º 1
0
    def test_with_two_preconditions(self) -> None:
        class A(icontract.DBC):
            @icontract.require(lambda x: x % 3 == 0)
            def some_func(self, x: int) -> None:
                pass

        recorded_inputs = []  # type: List[int]

        class B(A):
            @icontract.require(lambda x: x > 0)
            @icontract.require(lambda x: x % 7 == 0)
            def some_func(self, x: int) -> None:
                # The inputs from B.some_func need to satisfy either their own preconditions or
                # the preconditions of A.some_func ("require else").
                recorded_inputs.append(x)

        b = B()
        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            b.some_func)

        @hypothesis.given(x=hypothesis.strategies.sampled_from(
            [-14, 3, 7, 9, 10, 14]))
        def execute(x: int) -> None:
            assume_preconditions(x)
            b.some_func(x)

        execute()

        self.assertSetEqual({3, 7, 9, 14}, set(recorded_inputs))
Exemplo n.º 2
0
    def test_assumed_preconditions_pass(self) -> None:
        @icontract.require(lambda x: x > 0)
        def some_func(x: int) -> None:
            pass

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        assume_preconditions(x=100)
Exemplo n.º 3
0
    def test_input_value_generated_from_user_defined_strategy_is_constrained_by_icontract_preconditions(
        value, ) -> None:
        assume_version_callback_precondition = (
            icontract_hypothesis.make_assume_preconditions(version_callback))

        # Reject input values that that do not satisfy contract preconditions
        # under the assumption that preconditions will be satisfied at run-time,
        # i.e. either implicitly or explicitly via contract enforcement by
        # `icontract` or other input validation scheme.
        assume_version_callback_precondition(value)
        assert value is not False
        _test_version_callback(value)
Exemplo n.º 4
0
    def test_assumed_preconditions_fail(self) -> None:
        @icontract.require(lambda x: x > 0)
        def some_func(x: int) -> None:
            pass

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        unsatisfied_assumption = (
            None)  # type: Optional[hypothesis.errors.UnsatisfiedAssumption]
        try:
            assume_preconditions(x=-100)
        except hypothesis.errors.UnsatisfiedAssumption as err:
            unsatisfied_assumption = err

        self.assertIsNotNone(unsatisfied_assumption)
Exemplo n.º 5
0
    def test_without_contracts(self) -> None:
        recorded_inputs = []  # type: List[Any]

        def some_func(x: int) -> None:
            recorded_inputs.append(x)

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        @hypothesis.given(x=hypothesis.strategies.integers())
        def execute(x: int) -> None:
            assume_preconditions(x)
            some_func(x)

        execute()

        # 10 is an arbitrary, but plausible value.
        self.assertGreater(len(recorded_inputs), 10)
Exemplo n.º 6
0
    def test_with_only_postconditions(self) -> None:
        recorded_inputs = []  # type: List[Any]

        @icontract.ensure(lambda result: result > 0)
        def some_func(x: int) -> int:
            recorded_inputs.append(x)
            return 1

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        @hypothesis.given(x=hypothesis.strategies.integers())
        def execute(x: int) -> None:
            assume_preconditions(x)
            some_func(x)

        execute()

        self.assertGreater(len(recorded_inputs), 10)
Exemplo n.º 7
0
    def test_with_a_single_precondition(self) -> None:
        recorded_inputs = []  # type: List[int]

        @icontract.require(lambda x: x > 0)
        def some_func(x: int) -> None:
            recorded_inputs.append(x)

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        samples = [-1, 1]

        @hypothesis.given(x=hypothesis.strategies.sampled_from(samples))
        def execute(x: int) -> None:
            samples.append(x)
            assume_preconditions(x)
            some_func(x)

        execute()

        self.assertSetEqual({1}, set(recorded_inputs))
def benchmark_icontract_assume_preconditions(arg_count: int = 1) -> None:
    """Benchmark the Hypothesis testing with icontract and rejection sampling."""
    count = 0

    if arg_count == 1:

        @icontract.require(lambda a: a > 0)
        def some_func(a: int) -> None:
            nonlocal count
            count += 1
            pass

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        @hypothesis.settings(
            suppress_health_check=(hypothesis.HealthCheck.filter_too_much, ))
        @hypothesis.given(a=hypothesis.strategies.integers())
        def execute(a: int) -> None:
            assume_preconditions(a)
            some_func(a)

    elif arg_count == 2:

        @icontract.require(lambda a: a > 0)
        @icontract.require(lambda b: b > 0)
        def some_func(a: int, b: int) -> None:
            nonlocal count
            count += 1
            pass

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        @hypothesis.settings(
            suppress_health_check=(hypothesis.HealthCheck.filter_too_much, ))
        @hypothesis.given(a=hypothesis.strategies.integers(),
                          b=hypothesis.strategies.integers())
        def execute(a: int, b: int) -> None:
            assume_preconditions(a=a, b=b)
            some_func(a, b)

    elif arg_count == 3:

        @icontract.require(lambda a: a > 0)
        @icontract.require(lambda b: b > 0)
        @icontract.require(lambda c: c > 0)
        def some_func(a: int, b: int, c: int) -> None:
            nonlocal count
            count += 1
            pass

        assume_preconditions = icontract_hypothesis.make_assume_preconditions(
            some_func)

        @hypothesis.settings(
            suppress_health_check=(hypothesis.HealthCheck.filter_too_much, ))
        @hypothesis.given(
            a=hypothesis.strategies.integers(),
            b=hypothesis.strategies.integers(),
            c=hypothesis.strategies.integers(),
        )
        def execute(a: int, b: int, c: int) -> None:
            assume_preconditions(a=a, b=b, c=c)
            some_func(a, b, c)

    else:
        raise NotImplementedError("arg_count {}".format(arg_count))

    execute()

    # Assert the count of function executions for fair tests
    assert count == 100