def test_simplifies_towards_2000(): d = strategy(datetime).reify( falsify(lambda x: x.year <= 2000, datetime)[0]) assert d.year == 2001 d = strategy(datetime).reify( falsify(lambda x: x.year >= 2000, datetime)[0]) assert d.year == 1999
def define_frozen_set_strategy(specifier, settings): if not specifier: return frozensets(max_size=0) else: with settings: return frozensets( one_of(*[strategy(s, settings) for s in specifier]))
def define_dictionary_strategy(specifier, settings): return strategy( [(specifier.keys, specifier.values)], settings ).map(specifier.dict_class)
def define_dict_strategy(specifier, settings): strategy_dict = {} for k, v in specifier.items(): strategy_dict[k] = strategy(v, settings) return fixed_dictionaries(strategy_dict)
def define_tuple_strategy(specifier, settings): from hypothesis.searchstrategy.collections import TupleStrategy return TupleStrategy( tuple(strategy(d, settings) for d in specifier), tuple_type=type(specifier) )
def test_simplifies_towards_midnight(): d = strategy(datetime).reify(falsify(lambda x: False, datetime)[0]) assert d.hour == 0 assert d.minute == 0 assert d.second == 0 assert d.microsecond == 0
def wrapped_test(*arguments, **kwargs): selfy = None # Because we converted all kwargs to given into real args and # error if we have neither args nor kwargs, this should always # be valid assert argspec.args selfy = kwargs.get(argspec.args[0]) if isinstance(selfy, HypothesisProvided): selfy = None if selfy is not None: setup_example = getattr(selfy, 'setup_example', None) teardown_example = getattr(selfy, 'teardown_example', None) else: setup_example = None teardown_example = None 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 if setup_example is not None: setup_example() try: test(*arguments, **kwargs) finally: if teardown_example is not None: teardown_example((arguments, kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return v.value else: return just(v) given_specifier = ( tuple(map(convert_to_specifier, arguments)), {k: convert_to_specifier(v) for k, v in kwargs.items()} ) def to_falsify(xs): testargs, testkwargs = xs try: test(*testargs, **testkwargs) return True except UnsatisfiedAssumption as e: raise e except Exception: # pylint: disable=broad-except return False to_falsify.__name__ = test.__name__ to_falsify.__qualname__ = getattr( test, '__qualname__', test.__name__) try: falsifying_example = verifier.falsify( to_falsify, given_specifier, setup_example=setup_example, teardown_example=teardown_example, )[0] except Unfalsifiable: return strat = strategy(given_specifier) if setup_example is not None: setup_example() try: reified = strat.reify(falsifying_example) false_args, false_kwargs = reified current_reporter()( 'Falsifying example: %s(%s)' % ( test.__name__, arg_string( test, false_args, false_kwargs, ) ) ) # We run this one final time so we get good errors # Otherwise we would have swallowed all the reports of it # actually having gone wrong. test(*false_args, **false_kwargs) finally: if teardown_example is not None: teardown_example(reified) # If we get here then something has gone wrong: We found a counter # example but it didn't fail when we invoked it again. raise Flaky(test, falsifying_example)
def stream_strategy(stream, settings): return streaming(strategy(stream.data, settings))
def wrapped_test(*arguments, **kwargs): selfy = None # Because we converted all kwargs to given into real args and # error if we have neither args nor kwargs, this should always # be valid assert argspec.args selfy = kwargs.get(argspec.args[0]) if isinstance(selfy, HypothesisProvided): selfy = None if selfy is not None: setup_example = getattr(selfy, 'setup_example', None) teardown_example = getattr(selfy, 'teardown_example', None) else: setup_example = None teardown_example = None 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 if setup_example is not None: setup_example() try: test(*arguments, **kwargs) finally: if teardown_example is not None: teardown_example((arguments, kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return v.value else: return just(v) given_specifier = ( tuple(map(convert_to_specifier, arguments)), {k: convert_to_specifier(v) for k, v in kwargs.items()} ) def to_falsify(xs): testargs, testkwargs = xs try: test(*testargs, **testkwargs) return True except UnsatisfiedAssumption as e: raise e except Exception: # pylint: disable=broad-except return False to_falsify.__name__ = test.__name__ to_falsify.__qualname__ = getattr( test, '__qualname__', test.__name__) try: falsifying_example = verifier.falsify( to_falsify, given_specifier, setup_example=setup_example, teardown_example=teardown_example, )[0] except Unfalsifiable: return strat = strategy(given_specifier) if setup_example is not None: setup_example() try: reified = strat.reify(falsifying_example) if _debugging_return_failing_example.value: return reified false_args, false_kwargs = reified current_reporter()( 'Falsifying example: %s(%s)' % ( test.__name__, arg_string( test, false_args, false_kwargs, ) ) ) # We run this one final time so we get good errors # Otherwise we would have swallowed all the reports of it # actually having gone wrong. test(*false_args, **false_kwargs) finally: if teardown_example is not None: teardown_example(reified) # If we get here then something has gone wrong: We found a counter # example but it didn't fail when we invoked it again. raise Flaky(test, falsifying_example)
def smallest_tree(predicate): d = NAryTree(int, int, int) strat = strategy(d) v = Verifier() return strat.reify(v.falsify(lambda t: not predicate(t), d)[0])
def strategy_for_one_of(oneof, settings): return one_of(*[strategy(d, settings) for d in oneof.elements])
def test_can_get_specification_for_fake_factory(): ff = fake_factory(u'email') strategy(ff)