Пример #1
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()
Пример #2
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)
Пример #3
0
def test_can_handle_unicode_repr():
    def foo(x):
        pass
    from hypothesis import settings
    with settings(strict=False):
        assert arg_string(foo, [Snowman()], {}) == 'x=☃'
        assert arg_string(foo, [], {'x': Snowman()}) == 'x=☃'
Пример #4
0
def test_arg_strings_are_bad_repr_safe(recwarn):
    assert arg_string(varargs, (Frosty,), {}) == u'☃'
    if PY2:
        recwarn.pop(HypothesisDeprecationWarning)
    assert arg_string(varargs, (), {u'x': Frosty}) == u'x=☃'
    if PY2:
        recwarn.pop(HypothesisDeprecationWarning)
Пример #5
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 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)))

                    with deterministic_PRNG():
                        return test(*args, **kwargs)
Пример #6
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()
Пример #7
0
def test_can_handle_unicode_repr():
    def foo(x):
        pass
    from hypothesis import settings
    with settings(strict=False):
        assert arg_string(foo, [Snowman()], {}) == 'x=☃'
        assert arg_string(foo, [], {'x': Snowman()}) == 'x=☃'
Пример #8
0
def test_arg_string_is_in_order():
    def foo(c, a, b, f, a1):
        pass

    assert arg_string(foo, (1, 2, 3, 4, 5), {}) == "c=1, a=2, b=3, f=4, a1=5"
    assert (
        arg_string(foo, (1, 2), {"b": 3, "f": 4, "a1": 5}) == "c=1, a=2, b=3, f=4, a1=5"
    )
Пример #9
0
def test_arg_string_is_in_order():
    def foo(c, a, b, f, a1):
        pass

    assert arg_string(foo, (1, 2, 3, 4, 5), {}) == 'c=1, a=2, b=3, f=4, a1=5'
    assert arg_string(
        foo, (1, 2),
        {'b': 3, 'f': 4, 'a1': 5}) == 'c=1, a=2, b=3, f=4, a1=5'
Пример #10
0
def test_arg_string_is_in_order():
    def foo(c, a, b, f, a1):
        pass

    assert arg_string(foo, (1, 2, 3, 4, 5), {}) == 'c=1, a=2, b=3, f=4, a1=5'
    assert arg_string(
        foo, (1, 2),
        {'b': 3, 'f': 4, 'a1': 5}) == 'c=1, a=2, b=3, f=4, a1=5'
Пример #11
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         report(lambda: 'Falsifying example: %s(%s)' %
                (test.__name__, arg_string(test, args, kwargs)))
     else:
         verbose_report(lambda: 'Trying example: %s(%s)' %
                        (test.__name__, arg_string(test, args, kwargs)))
     return test(*args, **kwargs)
Пример #12
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)
Пример #13
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)
Пример #14
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)
Пример #15
0
 def __repr__(self):
     if self.__representation is None:
         _args = self.__args
         _kwargs = self.__kwargs
         argspec = getargspec(self.__function)
         defaults = {}
         if argspec.defaults is not None:
             for k in hrange(1, len(argspec.defaults) + 1):
                 defaults[argspec.args[-k]] = argspec.defaults[-k]
         if len(argspec.args) > 1 or argspec.defaults:
             _args, _kwargs = convert_positional_arguments(
                 self.__function, _args, _kwargs)
         else:
             _args, _kwargs = convert_keyword_arguments(
                 self.__function, _args, _kwargs)
         kwargs_for_repr = dict(_kwargs)
         for k, v in defaults.items():
             if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
                 del kwargs_for_repr[k]
         self.__representation = '%s(%s)' % (
             self.__function.__name__,
             arg_string(
                 self.__function, _args, kwargs_for_repr, reorder=False),
         )
     return self.__representation
Пример #16
0
 def __repr__(self):
     if self.__representation is None:
         _args = self.__args
         _kwargs = self.__kwargs
         argspec = getfullargspec(self.__function)
         defaults = dict(argspec.kwonlydefaults or {})
         if argspec.defaults is not None:
             for name, value in zip(reversed(argspec.args),
                                    reversed(argspec.defaults)):
                 defaults[name] = value
         if len(argspec.args) > 1 or argspec.defaults:
             _args, _kwargs = convert_positional_arguments(
                 self.__function, _args, _kwargs)
         else:
             _args, _kwargs = convert_keyword_arguments(
                 self.__function, _args, _kwargs)
         kwargs_for_repr = dict(_kwargs)
         for k, v in defaults.items():
             if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
                 del kwargs_for_repr[k]
         self.__representation = '%s(%s)' % (
             self.__function.__name__,
             arg_string(
                 self.__function, _args, kwargs_for_repr, reorder=False),
         )
     return self.__representation
Пример #17
0
def test_varkwargs_are_sorted_and_after_real_kwargs():
    def foo(d, e, f, **kwargs):
        pass

    assert arg_string(
        foo, (), {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
    ) == 'd=4, e=5, f=6, a=1, b=2, c=3'
Пример #18
0
def test_can_mix_varargs_and_varkwargs():
    def foo(*args, **kwargs):
        pass

    assert arg_string(
        foo, (1, 2, 3), {'c': 7}
    ) == '1, 2, 3, c=7'
Пример #19
0
def test_can_mix_varargs_and_varkwargs():
    def foo(*args, **kwargs):
        pass

    assert arg_string(
        foo, (1, 2, 3), {'c': 7}
    ) == '1, 2, 3, c=7'
Пример #20
0
def test_varkwargs_are_sorted_and_after_real_kwargs():
    def foo(d, e, f, **kwargs):
        pass

    assert (
        arg_string(foo, (), {u"a": 1, u"b": 2, u"c": 3, u"d": 4, u"e": 5, u"f": 6}) == u"d=4, e=5, f=6, a=1, b=2, c=3"
    )
Пример #21
0
def test_varkwargs_are_sorted_and_after_real_kwargs():
    def foo(d, e, f, **kwargs):
        pass

    assert arg_string(
        foo, (), {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
    ) == 'd=4, e=5, f=6, a=1, b=2, c=3'
Пример #22
0
 def test_or_flaky(*args, **kwargs):
     text_repr = arg_string(test, args, kwargs)
     raise Flaky(('Hypothesis %s(%s) produces unreliable results: Falsified'
                  ' on the first call but did not on a subsequent one') % (
                      test.__name__,
                      text_repr,
                  ))
Пример #23
0
 def __repr__(self):
     if self.__representation is None:
         _args = self.__args
         _kwargs = self.__kwargs
         argspec = getargspec(self.__function)
         defaults = {}
         if argspec.defaults is not None:
             for k in hrange(1, len(argspec.defaults) + 1):
                 defaults[argspec.args[-k]] = argspec.defaults[-k]
         if len(argspec.args) > 1 or argspec.defaults:
             _args, _kwargs = convert_positional_arguments(
                 self.__function, _args, _kwargs)
         else:
             _args, _kwargs = convert_keyword_arguments(
                 self.__function, _args, _kwargs)
         kwargs_for_repr = dict(_kwargs)
         for k, v in defaults.items():
             if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
                 del kwargs_for_repr[k]
         self.__representation = '%s(%s)' % (
             self.__function.__name__,
             arg_string(
                 self.__function, _args, kwargs_for_repr, reorder=False),
         )
     return self.__representation
Пример #24
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
Пример #25
0
 def test_or_flaky(*args, **kwargs):
     text_repr = arg_string(test, args, kwargs)
     raise Flaky(
         (
             'Hypothesis %s(%s) produces unreliable results: Falsified'
             ' on the first call but did not on a subsequent one'
         ) % (test.__name__, text_repr,))
Пример #26
0
 def __repr__(self):
     if self.__representation is None:
         _args = self.__args
         _kwargs = self.__kwargs
         argspec = getfullargspec(self.__function)
         defaults = dict(argspec.kwonlydefaults or {})
         if argspec.defaults is not None:
             for name, value in zip(reversed(argspec.args),
                                    reversed(argspec.defaults)):
                 defaults[name] = value
         if len(argspec.args) > 1 or argspec.defaults:
             _args, _kwargs = convert_positional_arguments(
                 self.__function, _args, _kwargs)
         else:
             _args, _kwargs = convert_keyword_arguments(
                 self.__function, _args, _kwargs)
         kwargs_for_repr = dict(_kwargs)
         for k, v in defaults.items():
             if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
                 del kwargs_for_repr[k]
         self.__representation = '%s(%s)' % (
             self.__function.__name__,
             arg_string(
                 self.__function, _args, kwargs_for_repr, reorder=False),
         )
     return self.__representation
Пример #27
0
 def __repr__(self):
     if self.__representation is None:
         sig = signature(self.function)
         pos = [
             p for p in sig.parameters.values()
             if "POSITIONAL" in p.kind.name
         ]
         if len(pos) > 1 or any(p.default is not sig.empty for p in pos):
             _args, _kwargs = convert_positional_arguments(
                 self.function, self.__args, self.__kwargs)
         else:
             _args, _kwargs = convert_keyword_arguments(
                 self.function, self.__args, self.__kwargs)
         kwargs_for_repr = {
             k: v
             for k, v in _kwargs.items() if k not in sig.parameters
             or v is not sig.parameters[k].default
         }
         self.__representation = "{}({}){}".format(
             self.function.__name__,
             arg_string(self.function,
                        _args,
                        kwargs_for_repr,
                        reorder=False),
             "".join(map(_repr_filter, self.__filters)),
         )
     return self.__representation
Пример #28
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)
Пример #29
0
def test_varkwargs_are_sorted_and_after_real_kwargs():
    def foo(d, e, f, **kwargs):
        pass

    assert (
        arg_string(foo, (), {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6})
        == "d=4, e=5, f=6, a=1, b=2, c=3"
    )
Пример #30
0
    def run(data):
        with BuildContext(data, is_final=is_final):
            orig = sys.gettrace()
            try:  # pragma: no cover
                sys.settrace(None)
                import random as rnd_module
                rnd_module.seed(0)
            finally:  # pragma: no cover
                sys.settrace(orig)
            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)
Пример #31
0
 def inner(*args, **kwargs):
     if data.frozen:
         raise InvalidState(
             "This generated %s function can only be called within the "
             "scope of the @given that created it." %
             (nicerepr(self.like), ))
     val = data.draw(self.returns)
     note("Called function: %s(%s) -> %r" % (nicerepr(
         self.like), arg_string(self.like, args, kwargs), val))
     return val
Пример #32
0
        def run(data):
            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),
                            )
                            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)
Пример #33
0
 def run():
     with BuildContext():
         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 or always_print:
             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)
Пример #34
0
    def run(data):
        with BuildContext(data, is_final=is_final):
            orig = sys.gettrace()
            try:  # pragma: no cover
                sys.settrace(None)
                import random as rnd_module
                rnd_module.seed(0)
            finally:  # pragma: no cover
                sys.settrace(orig)
            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)
Пример #35
0
 def accept(*args, **kwargs):
     result = strategy_definition(*args, **kwargs)
     args, kwargs = convert_positional_arguments(strategy_definition, args,
                                                 kwargs)
     kwargs_for_repr = dict(kwargs)
     for k, v in defaults.items():
         if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
             del kwargs_for_repr[k]
     representation = u'%s(%s)' % (strategy_definition.__name__,
                                   arg_string(strategy_definition, args,
                                              kwargs_for_repr))
     return ReprWrapperStrategy(result, representation)
Пример #36
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         report(
             lambda: 'Falsifying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     elif current_verbosity() >= Verbosity.verbose or always_print:
         report(
             lambda: 'Trying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     return test(*args, **kwargs)
Пример #37
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         report(
             lambda: 'Falsifying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     else:
         verbose_report(
             lambda: 'Trying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     return test(*args, **kwargs)
Пример #38
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         current_reporter()(
             'Falsifying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     return test(*args, **kwargs)
Пример #39
0
 def calc_repr():
     _args = args
     _kwargs = kwargs
     _args, _kwargs = convert_positional_arguments(
         strategy_definition, _args, _kwargs)
     kwargs_for_repr = dict(_kwargs)
     for k, v in defaults.items():
         if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
             del kwargs_for_repr[k]
     return u'%s(%s)' % (strategy_definition.__name__,
                         arg_string(strategy_definition, _args,
                                    kwargs_for_repr))
Пример #40
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         report(
             lambda: 'Falsifying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     elif current_verbosity() >= Verbosity.verbose or always_print:
         report(
             lambda: 'Trying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     return test(*args, **kwargs)
Пример #41
0
 def accept(*args, **kwargs):
     result = strategy_definition(*args, **kwargs)
     args, kwargs = convert_positional_arguments(
         strategy_definition, args, kwargs)
     kwargs_for_repr = dict(kwargs)
     for k, v in defaults.items():
         if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
             del kwargs_for_repr[k]
     representation = u'%s(%s)' % (
         strategy_definition.__name__,
         arg_string(strategy_definition, args, kwargs_for_repr)
     )
     return ReprWrapperStrategy(result, representation)
Пример #42
0
    def run(data):
        with BuildContext(data, is_final=is_final):
            import random as rnd_module
            rnd_module.seed(0)
            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)))
            if collector is None:
                return test(*args, **kwargs)
            else:  # pragma: no cover
                try:
                    collector.start()
                    return test(*args, **kwargs)
                finally:
                    collector.stop()
Пример #43
0
 def inner(*args, **kwargs):
     if data.frozen:
         raise InvalidState(
             "This generated %s function can only be called within the "
             "scope of the @given that created it." % (nicerepr(self.like),)
         )
     data.can_reproduce_example_from_repr = False
     val = data.draw(self.returns)
     note(
         "Called function: %s(%s) -> %r"
         % (nicerepr(self.like), arg_string(self.like, args, kwargs), val)
     )
     return val
Пример #44
0
 def calc_repr():
     _args = args
     _kwargs = kwargs
     _args, _kwargs = convert_positional_arguments(
         strategy_definition, _args, _kwargs)
     kwargs_for_repr = dict(_kwargs)
     for k, v in defaults.items():
         if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]:
             del kwargs_for_repr[k]
     return u'%s(%s)' % (
         strategy_definition.__name__,
         arg_string(strategy_definition, _args, kwargs_for_repr)
     )
Пример #45
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 BuildContext(data, is_final=is_final):
                    update_error_store('test_name', test.__name__)
                    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
                        one_error = {}
                        variable_list = []
                        for variableset in text_repr[0].split(', '):
                            [name, value] = variableset.split('=')
                            variable_pair = {'v_name': name, 'v_value': value}
                            variable_list.append(variable_pair)
                        one_error['variables'] = variable_list
                        one_error['error_type'] = ((
                            expected_failure[0]).__class__.__name__)
                        one_error['error_message'] = str(expected_failure[0])
                        one_error['traceback'] = expected_failure[1]
                        add_one_error(one_error)
                        report('Falsifying example: %s' % (example, ))
                    elif current_verbosity() >= Verbosity.verbose:
                        report(lambda: 'Trying example: %s(%s)' %
                               (test.__name__, arg_string(test, args, kwargs)))

                    with deterministic_PRNG():
                        return test(*args, **kwargs)
Пример #46
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)
Пример #47
0
 def inner(*args, **kwargs):
     if data.frozen:
         raise InvalidState(
             "This generated %s function can only be called within the "
             "scope of the @given that created it." %
             (nicerepr(self.like), ))
     if self.pure:
         args, kwargs = convert_positional_arguments(
             self.like, args, kwargs)
         key = (inner, args, frozenset(kwargs.items()))
         val = data.draw(SharedStrategy(base=self.returns, key=key))
     else:
         val = data.draw(self.returns)
     note("Called function: %s(%s) -> %r" % (nicerepr(
         self.like), arg_string(self.like, args, kwargs), val))
     return val
Пример #48
0
 def test_or_flaky(*args, **kwargs):
     text_repr = arg_string(test, args, kwargs)
     if text_repr == expected_repr:
         raise Flaky(
             (
                 u'Hypothesis %s(%s) produces unreliable results: Falsified'
                 u' on the first call but did not on a subsequent one'
             ) % (test.__name__, text_repr,))
     else:
         raise Flaky(
             (
                 u'Hypothesis %s produces unreliable results: Falsified'
                 u' on the first call but did not on a subsequent one.'
                 u' This is possibly due to unreliable values, which may '
                 u'be a bug in the strategy.\nCall 1: %s\nCall 2: %s\n'
             ) % (test.__name__, expected_repr, text_repr,))
Пример #49
0
 def test_or_flaky(*args, **kwargs):
     text_repr = arg_string(test, args, kwargs)
     if text_repr == expected_repr:
         raise Flaky(
             (
                 'Hypothesis %s(%s) produces unreliable results: Falsified'
                 ' on the first call but did not on a subsequent one'
             ) % (test.__name__, text_repr,))
     else:
         raise Flaky(
             (
                 'Hypothesis %s produces unreliable results: Falsified'
                 ' on the first call but did not on a subsequent one. This '
                 ' is possibly due to unreliable values, which may be a bug'
                 ' in the strategy.\nCall 1: %s\nCall 2: %s\n'
             ) % (test.__name__, expected_repr, text_repr,))
Пример #50
0
def execute_explicit_examples(
    test_runner, test, wrapped_test, settings, arguments, kwargs
):
    original_argspec = getfullargspec(test)

    for example in reversed(getattr(
        wrapped_test, 'hypothesis_explicit_examples', ()
    )):
        example_kwargs = dict(original_argspec.kwonlydefaults or {})
        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.update(dict(zip(
                original_argspec.args[-len(example.args):],
                example.args
            )))
        else:
            example_kwargs.update(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.
        example_string = '%s(%s)' % (
            test.__name__, arg_string(test, arguments, example_kwargs)
        )
        try:
            with BuildContext(None) as b:
                if settings.verbosity >= Verbosity.verbose:
                    report('Trying example: ' + example_string)
                test_runner(
                    None,
                    lambda data: test(*arguments, **example_kwargs)
                )
        except BaseException:
            report('Falsifying example: ' + example_string)
            for n in b.notes:
                report(n)
            raise
Пример #51
0
def execute_explicit_examples(test_runner, test, wrapped_test, settings,
                              arguments, kwargs):
    original_argspec = getfullargspec(test)

    for example in reversed(
            getattr(wrapped_test, "hypothesis_explicit_examples", ())):
        example_kwargs = dict(original_argspec.kwonlydefaults or {})
        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.update(
                dict(
                    zip(original_argspec.args[-len(example.args):],
                        example.args)))
        else:
            example_kwargs.update(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.
        example_string = "%s(%s)" % (
            test.__name__,
            arg_string(test, arguments, example_kwargs),
        )
        with local_settings(settings):
            try:
                with BuildContext(None) as b:
                    verbose_report("Trying example: " + example_string)
                    test_runner(
                        None, lambda data: test(*arguments, **example_kwargs))
            except BaseException:
                report("Falsifying example: " + example_string)
                for n in b.notes:
                    report(n)
                raise
Пример #52
0
def test_arg_string_kwargs_are_bad_repr_safe():
    assert arg_string(varargs, (), {"x": Frosty}) == "x=☃"
Пример #53
0
def test_arg_strings_are_bad_repr_safe():
    assert arg_string(varargs, (Frosty,), {}) == "☃"
Пример #54
0
        def wrapped_test(*arguments, **kwargs):
            import hypothesis.strategies as sd
            from hypothesis.internal.strategymethod import strategy

            selfy = None
            arguments, kwargs = convert_positional_arguments(
                wrapped_test, arguments, kwargs)
            # Anything in unused_kwargs hasn't been injected through
            # argspec.defaults, so we need to add them.
            for k in unused_kwargs:
                if k not in kwargs:
                    kwargs[k] = unused_kwargs[k]
            # If the test function is a method of some kind, the bound object
            # will be the first named argument if there are any, otherwise the
            # first vararg (if any).
            if argspec.args:
                selfy = kwargs.get(argspec.args[0])
            elif arguments:
                selfy = arguments[0]
            if isinstance(selfy, HypothesisProvided):
                selfy = None
            test_runner = executor(selfy)

            for example in getattr(
                wrapped_test, u'hypothesis_explicit_examples', ()
            ):
                if example.args:
                    example_kwargs = dict(zip(
                        argspec.args[-len(example.args):], example.args
                    ))
                else:
                    example_kwargs = dict(example.kwargs)

                for k, v in kwargs.items():
                    if not isinstance(v, HypothesisProvided):
                        example_kwargs[k] = v
                # 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 = u'Falsifying example: %s(%s)' % (
                    test.__name__, arg_string(test, arguments, example_kwargs)
                )
                try:
                    test_runner(
                        lambda: test(*arguments, **example_kwargs)
                    )
                except BaseException:
                    report(message_on_failure)
                    raise

            if not any(
                isinstance(x, HypothesisProvided)
                for xs in (arguments, kwargs.values())
                for x in xs
            ):
                # All arguments have been satisfied without needing to invoke
                # hypothesis
                test_runner(lambda: test(*arguments, **kwargs))
                return

            def convert_to_specifier(v):
                if isinstance(v, HypothesisProvided):
                    return strategy(v.value, settings)
                else:
                    return sd.just(v)

            given_specifier = sd.tuples(
                sd.tuples(*map(convert_to_specifier, arguments)),
                sd.fixed_dictionaries(dict(
                    (k, convert_to_specifier(v)) for (k, v) in kwargs.items()))
            )

            search_strategy = strategy(given_specifier, settings)

            if settings.database:
                storage = settings.database.storage(
                    fully_qualified_name(test))
            else:
                storage = None

            last_exception = [None]
            repr_for_last_exception = [None]

            def is_template_example(xs):
                record_repr = [None]
                try:
                    test_runner(reify_and_execute(
                        search_strategy, xs, test,
                        always_print=settings.max_shrinks <= 0,
                        record_repr=record_repr,
                    ))
                    return False
                except UnsatisfiedAssumption as e:
                    raise e
                except Exception as e:
                    if settings.max_shrinks <= 0:
                        raise e
                    last_exception[0] = traceback.format_exc()
                    repr_for_last_exception[0] = record_repr[0]
                    verbose_report(last_exception[0])
                    return True

            is_template_example.__name__ = test.__name__
            is_template_example.__qualname__ = qualname(test)

            falsifying_template = None
            try:
                falsifying_template = best_satisfying_template(
                    search_strategy, random, is_template_example,
                    settings, storage
                )
            except NoSuchExample:
                return

            assert last_exception[0] is not None

            with settings:
                test_runner(reify_and_execute(
                    search_strategy, falsifying_template, test,
                    print_example=True, is_final=True
                ))

                report(
                    u'Failed to reproduce exception. Expected: \n' +
                    last_exception[0],
                )

                test_runner(reify_and_execute(
                    search_strategy, falsifying_template,
                    test_is_flaky(test, repr_for_last_exception[0]),
                    print_example=True, is_final=True
                ))
Пример #55
0
        def run(data):
            # Set up dynamic context needed by a single test run.
            with local_settings(self.settings):
                with deterministic_PRNG():
                    with BuildContext(data, is_final=is_final):

                        # Generate all arguments to the test function.
                        args, kwargs = data.draw(self.search_strategy)
                        if expected_failure is not None:
                            text_repr[0] = arg_string(test, args, kwargs)

                        if print_example or current_verbosity(
                        ) >= Verbosity.verbose:
                            output = CUnicodeIO()

                            printer = RepresentationPrinter(output)
                            if print_example:
                                printer.text("Falsifying example:")
                            else:
                                printer.text("Trying example:")

                            if self.print_given_args:
                                printer.text(" ")
                                printer.text(test.__name__)
                                with printer.group(indent=4,
                                                   open="(",
                                                   close=""):
                                    printer.break_()
                                    for v in args:
                                        printer.pretty(v)
                                        # We add a comma unconditionally because
                                        # generated arguments will always be
                                        # kwargs, so there will always be more
                                        # to come.
                                        printer.text(",")
                                        printer.breakable()

                                    # We need to make sure to print these in the argument order for
                                    # Python 2 and older versionf of Python 3.5. In modern versions
                                    # this isn't an issue because kwargs is ordered.
                                    arg_order = {
                                        v: i
                                        for i, v in enumerate(
                                            getfullargspec(self.test).args)
                                    }
                                    for i, (k, v) in enumerate(
                                            sorted(
                                                kwargs.items(),
                                                key=lambda t: (
                                                    arg_order.get(
                                                        t[0], float("inf")),
                                                    t[0],
                                                ),
                                            )):
                                        printer.text(k)
                                        printer.text("=")
                                        printer.pretty(v)
                                        printer.text(",")
                                        if i + 1 < len(kwargs):
                                            printer.breakable()
                                printer.break_()
                                printer.text(")")
                            printer.flush()
                            report(output.getvalue())
                        return test(*args, **kwargs)
Пример #56
0
def test_can_handle_non_unicode_repr_containing_non_ascii():
    def foo(x):
        pass

    assert arg_string(foo, [BittySnowman()], {}) == 'x=☃'
    assert arg_string(foo, [], {'x': BittySnowman()}) == 'x=☃'
Пример #57
0
def test_can_handle_repr_of_none():
    def foo(x):
        pass

    assert arg_string(foo, [None], {}) == 'x=None'
    assert arg_string(foo, [], {'x': None}) == 'x=None'
Пример #58
0
def test_can_handle_repr_on_type():
    def foo(x):
        pass
    assert arg_string(foo, [Snowman], {}) == 'x=%r' % (Snowman,)
    assert arg_string(foo, [NoRepr], {}) == 'x=%r' % (NoRepr,)
Пример #59
0
def test_can_handle_unicode_repr():
    def foo(x):
        pass

    assert arg_string(foo, [Snowman()], {}) == 'x=☃'
    assert arg_string(foo, [], {'x': Snowman()}) == 'x=☃'
Пример #60
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         current_reporter()('Falsifying example: %s(%s)' %
                            (test.__name__, arg_string(test, args, kwargs)))
     return test(*args, **kwargs)