Ejemplo n.º 1
0
def test_can_nest_build_context():
    data = []
    with BuildContext():
        cleanup(lambda: data.append(1))
        with BuildContext():
            cleanup(lambda: data.append(2))
            assert not data
        assert data == [2]
    assert data == [2, 1]
Ejemplo n.º 2
0
 def test_templates_generated_from_same_random_are_equal_after_reify(
         self, i):
     try:
         t1 = strat.draw_and_produce(Random(i))
         t2 = strat.draw_and_produce(Random(i))
     except BadTemplateDraw:
         assume(False)
     if t1 is not t2:
         with BuildContext():
             strat.reify(t1)
         with BuildContext():
             strat.reify(t2)
         assert t1 == t2
         assert hash(t1) == hash(t2)
Ejemplo n.º 3
0
def test_cleanup_executes_on_leaving_build_context():
    data = []
    with BuildContext():
        cleanup(lambda: data.append(1))
        assert not data
    assert data == [1]
    assert _current_build_context.value is None
Ejemplo n.º 4
0
def find(specifier, condition, settings=None, random=None, storage=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
        max_shrinks=2000,
    )

    search = strategy(specifier, settings)

    if storage is None and settings.database is not None:
        storage = settings.database.storage(
            u'find(%s)' %
            (binascii.hexlify(function_digest(condition)).decode(u'ascii'), ))

    random = random or Random()
    successful_examples = [0]

    def template_condition(template):
        with BuildContext():
            result = search.reify(template)
            success = condition(result)

        if success:
            successful_examples[0] += 1

        if not successful_examples[0]:
            verbose_report(lambda: u'Trying example %s' % (repr(result), ))
        elif success:
            if successful_examples[0] == 1:
                verbose_report(lambda: u'Found satisfying example %s' %
                               (repr(result), ))
            else:
                verbose_report(lambda: u'Shrunk example to %s' %
                               (repr(result), ))
        return success

    template_condition.__name__ = condition.__name__
    tracker = Tracker()

    try:
        template = best_satisfying_template(
            search,
            random,
            template_condition,
            settings,
            tracker=tracker,
            max_parameter_tries=2,
            storage=storage,
        )
        with BuildContext():
            return search.reify(template)
    except Timeout:
        raise
    except NoSuchExample:
        if search.template_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.template_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
Ejemplo n.º 5
0
        def run(data):
            with self.settings:
                with BuildContext(data, is_final=is_final):
                    import random as rnd_module
                    rnd_module.seed(0)
                    args, kwargs = data.draw(self.search_strategy)
                    if expected_failure is not None:
                        text_repr[0] = arg_string(test, args, kwargs)

                    if print_example:
                        report(
                            lambda: 'Falsifying example: %s(%s)' % (
                                test.__name__, arg_string(test, args, kwargs)))
                    elif current_verbosity() >= Verbosity.verbose:
                        report(
                            lambda: 'Trying example: %s(%s)' % (
                                test.__name__, arg_string(test, args, kwargs)))

                    if self.collector is None or not collect:
                        return test(*args, **kwargs)
                    else:  # pragma: no cover
                        try:
                            self.collector.start()
                            return test(*args, **kwargs)
                        finally:
                            self.collector.stop()
Ejemplo n.º 6
0
def test_can_simplify_text_through_a_morpher(rnd):
    m = find(morphers,
             lambda x: bool(x.become(s.text())),
             random=rnd,
             settings=Settings(database=None))
    with BuildContext():
        assert m.become(s.text()) == u'0'
Ejemplo n.º 7
0
        def run(data):
            if not hasattr(data, 'can_reproduce_example_from_repr'):
                data.can_reproduce_example_from_repr = True
            with self.settings:
                with BuildContext(data, is_final=is_final):
                    with deterministic_PRNG():
                        args, kwargs = data.draw(self.search_strategy)
                    if expected_failure is not None:
                        text_repr[0] = arg_string(test, args, kwargs)

                    if print_example:
                        example = '%s(%s)' % (test.__name__,
                                              arg_string(test, args, kwargs))
                        try:
                            ast.parse(example)
                        except SyntaxError:
                            data.can_reproduce_example_from_repr = False
                        report('Falsifying example: %s' % (example, ))
                    elif current_verbosity() >= Verbosity.verbose:
                        report(lambda: 'Trying example: %s(%s)' %
                               (test.__name__, arg_string(test, args, kwargs)))

                    if self.collector is None or not collect:
                        with deterministic_PRNG():
                            return test(*args, **kwargs)
                    else:  # pragma: no cover
                        try:
                            self.collector.start()
                            with deterministic_PRNG():
                                return test(*args, **kwargs)
                        finally:
                            self.collector.stop()
Ejemplo n.º 8
0
def execute_explicit_examples(test_runner, test, wrapped_test, settings,
                              arguments, kwargs):
    original_argspec = getargspec(test)

    for example in reversed(
            getattr(wrapped_test, 'hypothesis_explicit_examples', ())):
        if example.args:
            if len(example.args) > len(original_argspec.args):
                raise InvalidArgument(
                    'example has too many arguments for test. '
                    'Expected at most %d but got %d' %
                    (len(original_argspec.args), len(example.args)))
            example_kwargs = dict(
                zip(original_argspec.args[-len(example.args):], example.args))
        else:
            example_kwargs = example.kwargs
        if Phase.explicit not in settings.phases:
            continue
        example_kwargs.update(kwargs)
        # Note: Test may mutate arguments and we can't rerun explicit
        # examples, so we have to calculate the failure message at this
        # point rather than than later.
        message_on_failure = 'Falsifying example: %s(%s)' % (
            test.__name__, arg_string(test, arguments, example_kwargs))
        try:
            with BuildContext(None) as b:
                test_runner(None,
                            lambda data: test(*arguments, **example_kwargs))
        except BaseException:
            traceback.print_exc()
            report(message_on_failure)
            for n in b.notes:
                report(n)
            raise
Ejemplo n.º 9
0
    def template_condition(data):
        with BuildContext(data):
            try:
                data.is_find = True
                result = data.draw(search)
                data.note(result)
                success = condition(result)
            except UnsatisfiedAssumption:
                data.mark_invalid()

        if success:
            successful_examples[0] += 1

        if settings.verbosity == Verbosity.verbose:
            if not successful_examples[0]:
                report(lambda: u'Trying example %s' % (
                    nicerepr(result),
                ))
            elif success:
                if successful_examples[0] == 1:
                    report(lambda: u'Found satisfying example %s' % (
                        nicerepr(result),
                    ))
                    last_data[0] = data
                elif (
                    sort_key(hbytes(data.buffer)) <
                    sort_key(last_data[0].buffer)
                ):
                    report(lambda: u'Shrunk example to %s' % (
                        nicerepr(result),
                    ))
                    last_data[0] = data
        if success and not data.frozen:
            data.mark_interesting()
Ejemplo n.º 10
0
        def run(data):
            if not hasattr(data, "can_reproduce_example_from_repr"):
                data.can_reproduce_example_from_repr = True
            with local_settings(self.settings):
                with deterministic_PRNG():
                    with BuildContext(data, is_final=is_final):
                        args, kwargs = data.draw(self.search_strategy)
                        if expected_failure is not None:
                            text_repr[0] = arg_string(test, args, kwargs)

                        if print_example:
                            example = "%s(%s)" % (
                                test.__name__,
                                arg_string(test, args, kwargs),
                            )
                            try:
                                ast.parse(example)
                            except SyntaxError:
                                data.can_reproduce_example_from_repr = False
                            report("Falsifying example: %s" % (example,))
                        elif current_verbosity() >= Verbosity.verbose:
                            report(
                                lambda: "Trying example: %s(%s)"
                                % (test.__name__, arg_string(test, args, kwargs))
                            )
                        return test(*args, **kwargs)
Ejemplo n.º 11
0
    def template_condition(data):
        with BuildContext(data):
            try:
                data.is_find = True
                with deterministic_PRNG():
                    result = data.draw(search)
                    data.note(result)
                    success = condition(result)
            except UnsatisfiedAssumption:
                data.mark_invalid()

        if success:
            successful_examples[0] += 1

        if settings.verbosity >= Verbosity.verbose:
            if not successful_examples[0]:
                report(u"Tried non-satisfying example %s" %
                       (nicerepr(result), ))
            elif success:
                if successful_examples[0] == 1:
                    last_repr[0] = nicerepr(result)
                    report(u"Found satisfying example %s" % (last_repr[0], ))
                    last_data[0] = data
                elif (sort_key(hbytes(data.buffer)) < sort_key(
                        last_data[0].buffer)
                      ) and nicerepr(result) != last_repr[0]:
                    last_repr[0] = nicerepr(result)
                    report(u"Shrunk example to %s" % (last_repr[0], ))
                    last_data[0] = data
        if success and not data.frozen:
            data.mark_interesting()
Ejemplo n.º 12
0
def test_all_minimal_elements_reify(spec):
    random = Random(hashlib.md5((
        show(spec) + ':test_all_minimal_elements_round_trip_via_the_database'
    ).encode('utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 13
0
def test_can_simplify_lists_of_morphers_of_single_type():
    ms = find(s.lists(morphers),
              lambda x: sum(t.become(s.integers()) for t in x) >= 100,
              settings=Settings(database=None))

    with BuildContext():
        ls = [t.become(s.integers()) for t in ms]
    assert sum(ls) == 100
Ejemplo n.º 14
0
def test_raises_error_if_cleanup_fails_but_block_does_not():
    with pytest.raises(CleanupFailed):
        with BuildContext():

            def foo():
                raise ValueError()

            cleanup(foo)
Ejemplo n.º 15
0
def test_all_minimal_elements_reify(spec):
    random = Random(hashlib.md5((
        show(spec) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = spec
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 16
0
def test_raises_error_if_cleanup_fails_but_block_does_not():
    with pytest.raises(CleanupFailed):
        with BuildContext():

            def foo():
                raise ValueError()

            cleanup(foo)
    assert _current_build_context.value is None
Ejemplo n.º 17
0
def test_check_serialization_preserves_changed_marker():
    strat = streaming(floats(min_value=0.0, max_value=2.2250738585072014e-308))
    template = strat.draw_template(Random(0), strat.draw_parameter(Random(0)))
    with BuildContext():
        strat.reify(template)[0]
    simpler = next(strat.full_simplify(Random(0), template))

    as_basic = strat.to_basic(simpler)
    assert as_basic == strat.to_basic(strat.from_basic(as_basic))
Ejemplo n.º 18
0
def test_suppresses_exceptions_in_teardown():
    with capture_out() as o:
        with pytest.raises(AssertionError):
            with BuildContext():
                def foo():
                    raise ValueError()
                cleanup(foo)
                assert False

    assert u'ValueError' in o.getvalue()
Ejemplo n.º 19
0
def test_round_tripping_lists_via_the_database(spec):
    random = Random(
        hashlib.md5((show(spec) + u':test_round_tripping_via_the_database'
                     ).encode(u'utf-8')).digest())
    strat = lists(spec)
    template = some_template(strat, random)
    template_via_db = via_database(spec, strat, template)
    with BuildContext():
        assert show(strat.reify(template)) == show(
            strat.reify(template_via_db))
Ejemplo n.º 20
0
    def run(data):
        with BuildContext(is_final=is_final):
            args, kwargs = data.draw(search_strategy)

            if print_example:
                report(lambda: 'Falsifying example: %s(%s)' %
                       (test.__name__, arg_string(test, args, kwargs)))
            elif current_verbosity() >= Verbosity.verbose:
                report(lambda: 'Trying example: %s(%s)' %
                       (test.__name__, arg_string(test, args, kwargs)))
            return test(*args, **kwargs)
Ejemplo n.º 21
0
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(hashlib.md5((
        repr(spec) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = spec
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        with BuildContext():
            assert repr(strat.reify(elt)) == repr(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
Ejemplo n.º 22
0
def test_thorough_cloning():
    def check(x):
        ids = list(map(id, x))
        assert len(set(ids)) == len(ids)
        return len(x) >= 5 and any(m.become(s.integers()) > 0 for m in x)

    r = find(s.lists(morphers), check)
    with BuildContext():
        results = [m.become(s.integers()) for m in r]
    results.sort(key=abs)
    assert results == [0] * 4 + [1]
Ejemplo n.º 23
0
def test_round_tripping_via_the_database(spec):
    random = Random(hashlib.md5((
        repr(spec) + u':test_round_tripping_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = spec
    template = some_template(strat, random)
    strat.from_basic(strat.to_basic(template))
    template_via_db = via_database(spec, strat, template)
    with BuildContext():
        assert repr(strat.reify(template)) == repr(
            strat.reify(template_via_db))
Ejemplo n.º 24
0
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(
        hashlib.md5((show(spec) +
                     u':test_all_minimal_elements_round_trip_via_the_database'
                     ).encode(u'utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        with BuildContext():
            assert show(strat.reify(elt)) == show(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
Ejemplo n.º 25
0
def some_template(spec, random=None):
    if random is None:
        random = Random()
    strat = strategy(spec)
    for _ in hrange(100):
        element = strat.draw_and_produce(random)
        try:
            with BuildContext():
                strat.reify(element)
            return element
        except UnsatisfiedAssumption:
            pass
    else:
        raise NoExamples(u'some_template called on strategy with no examples')
Ejemplo n.º 26
0
 def run():
     try:
         with transaction.atomic():
             for f in active_fixtures():
                 assert f is not self
                 if f.template == template:
                     return False
                 f()
             with BuildContext():
                 result = self.constraint(self.strategy.reify(template))
             transaction.set_rollback(True)
         return result
     except UnsatisfiedAssumption:
         return False
Ejemplo n.º 27
0
 def run():
     with BuildContext(is_final=is_final):
         args, kwargs = search_strategy.reify(template)
         text_version = arg_string(test, args, kwargs)
         if print_example:
             report(lambda: 'Falsifying example: %s(%s)' % (
                 test.__name__,
                 text_version,
             ))
         elif current_verbosity() >= Verbosity.verbose:
             report(lambda: 'Trying example: %s(%s)' %
                    (test.__name__, text_version))
         if record_repr is not None:
             record_repr[0] = text_version
         return test(*args, **kwargs)
Ejemplo n.º 28
0
def run_state_machine_as_test(state_machine_factory, settings=None):
    """Run a state machine definition as a test, either silently doing nothing
    or printing a minimal breaking program and raising an exception.

    state_machine_factory is anything which returns an instance of
    GenericStateMachine when called with no arguments - it can be a class or a
    function. settings will be used to control the execution of the test.

    """
    try:
        breaker = find_breaking_runner(state_machine_factory, settings)
    except NoSuchExample:
        return
    with BuildContext(is_final=True):
        breaker.run(state_machine_factory(), print_steps=True)
    raise Flaky(u'Run failed initially by succeeded on a second try')
Ejemplo n.º 29
0
def test_runs_multiple_cleanup_with_teardown():
    with capture_out() as o:
        with pytest.raises(AssertionError):
            with BuildContext():
                def foo():
                    raise ValueError()
                cleanup(foo)

                def bar():
                    raise TypeError()
                cleanup(foo)
                cleanup(bar)
                assert False

    assert u'ValueError' in o.getvalue()
    assert u'TypeError' in o.getvalue()
Ejemplo n.º 30
0
    def run(data):
        from hypothesis.control import note

        with BuildContext(is_final=is_final):
            seed = data.draw(random_module()).seed
            if seed != 0:
                note('random.seed(%d)' % (seed, ))
            args, kwargs = data.draw(search_strategy)

            if print_example:
                report(lambda: 'Falsifying example: %s(%s)' %
                       (test.__name__, arg_string(test, args, kwargs)))
            elif current_verbosity() >= Verbosity.verbose:
                report(lambda: 'Trying example: %s(%s)' %
                       (test.__name__, arg_string(test, args, kwargs)))
            return test(*args, **kwargs)