def test_strategy_repr_handles_instances_without_dicts(): strats = ss.StrategyTable() strats.define_specification_for_instances(int, lambda s, d: TrivialStrategy(d)) assert repr(strats.strategy(42)) == 'TrivialStrategy(42)' assert repr(strats.strategy(23)) == 'TrivialStrategy(23)'
def test_can_use_simplify_from_all_children(): always = lambda x: True table = ss.StrategyTable() bad_strategy = IntStrategyWithBrokenSimplify() assert list(bad_strategy.simplify_such_that(42, always)) == [42] hybrid_strategy = bad_strategy | table.strategy(int) assert list(hybrid_strategy.simplify_such_that(42, always))[-1] == 0
def test_can_distinguish_amongst_tuples_of_mixed_length(): st = ss.StrategyTable() mixed_strategy = st.strategy((int, int, int)) | st.strategy((int, int)) assert mixed_strategy.could_have_produced((1, 1)) assert mixed_strategy.could_have_produced((1, 1, 1)) assert not mixed_strategy.could_have_produced((1, 1, 1, 1)) assert not mixed_strategy.could_have_produced((1, 'foo')) assert not mixed_strategy.could_have_produced((1, 1, 'foo')) assert not mixed_strategy.could_have_produced([1, 1])
def test_strategy_for_integer_range_produces_only_integers_in_that_range(): table = ss.StrategyTable() just_one_integer = table.strategy(descriptors.IntegerRange(1, 1)) for _ in hrange(100): pv = just_one_integer.parameter.draw(random) x = just_one_integer.produce(random, pv) assert x == 1 some_integers = table.strategy(descriptors.IntegerRange(1, 10)) for _ in hrange(100): pv = some_integers.parameter.draw(random) x = some_integers.produce(random, pv) assert 1 <= x <= 10
def test_strategy_for_integer_range_can_produce_end_points(): table = ss.StrategyTable() some_integers = table.strategy(descriptors.IntegerRange(1, 10)) found = set() for _ in hrange(1000): # pragma: no branch pv = some_integers.parameter.draw(random) x = some_integers.produce(random, pv) found.add(x) if 1 in found and 10 in found: break else: assert False # pragma: no cover assert 1 in found assert 10 in found
def test_frozen_set_of_mutable_types_is_mutable(): class Foo(object): pass class FooStrategy(strat.SearchStrategy): descriptor = Foo parameter = params.CompositeParameter() has_immutable_data = False def produce(self, random, pv): return Foo() table = ss.StrategyTable() table.define_specification_for(Foo, lambda s, d: FooStrategy()) assert not table.strategy(frozenset([Foo])).has_immutable_data
def test_distinguishes_named_and_unnamed_tuples(): table = ss.StrategyTable() named = table.strategy(SomeNamedTuple(int, int)) unnamed = table.strategy((int, int)) assert unnamed.could_have_produced((1, 1)) assert not named.could_have_produced((1, 1)) assert not unnamed.could_have_produced(SomeNamedTuple(1, 1)) assert named.could_have_produced(SomeNamedTuple(1, 1)) for x in named.simplify(SomeNamedTuple(1, 1)): assert type(x) == SomeNamedTuple for x in unnamed.simplify((1, 1)): assert type(x) == tuple
def test_simplify_integer_range_can_push_to_near_boundaries(): table = ss.StrategyTable() some_integers = table.strategy(descriptors.IntegerRange(1, 10)) predicates = [ (lambda x: True, 1), (lambda x: x > 1, 2), (lambda x: x > 5, 10), (lambda x: x > 5 and x < 10, 9), ] for p, v in predicates: some = False for i in hrange(1, 10): if p(i): some = True assert last(some_integers.simplify_such_that(i, p)) == v assert some
def test_can_simplify_real_component(): cs = ss.StrategyTable().strategy(complex) t = complex(10.0, 1.0) for s in cs.simplify_such_that(t, lambda x: x.real >= 1 and x.imag >= 0): t = s assert t.real == 1.0
def test_can_simplify_dicts_of_ints(): ints = ss.StrategyTable().strategy({'a': int, 'b': int}) assert minimize(ints, {'a': 100000000000000, 'b': 2}) == {'a': 0, 'b': 0}
def test_can_simplify_large_ints(): ints = ss.StrategyTable().strategy(int) x = 2**63 assert ints.could_have_produced(x) assert minimize(ints, x) == 0 assert minimize(ints, 100000) == 0
def test_strategy_repr_handles_custom_types(): assert 'X(x=int)' in repr(ss.StrategyTable().strategy(X(int)))
def test_or_errors_when_given_non_strategy(): bools = ss.StrategyTable().strategy((bool, )) with pytest.raises(ValueError): bools | 'foo'
def test_or_does_not_change_descriptor_given_single_descriptor(): bools = ss.StrategyTable().strategy((bool, )) b = bools | bools assert b.descriptor == bools.descriptor
def test_does_not_shrink_tuple_length(): bools = ss.StrategyTable().strategy((bool, )) t = minimize(bools, (False, )) assert len(t) == 1
def test_float_strategy_does_not_overflow(): strategy = ss.StrategyTable().strategy(float) for _ in hrange(100): strategy.produce(random, strategy.parameter.draw(random))
def test_decompose_does_not_confuse_sets_and_frozen_sets_in_a_list(): s = ss.StrategyTable().strategy([frozenset([int]), {int}]) l = list(s.decompose([{0}])) assert len(l) == 1 d, v = l[0] assert strategy(d).could_have_produced(v)
def strategy(*args, **kwargs): return ss.StrategyTable().strategy(*args, **kwargs)