Exemple #1
0
def define_decimal_strategy(specifier, settings):
    return (
        strategy(float, settings).map(specifier) |
        strategy(Fraction, settings).map(
            lambda f: specifier(f.numerator) / f.denominator
        )
    )
Exemple #2
0
def define_decimal_strategy(specifier, settings):
    return (
        strategy(float, settings).map(specifier) |
        strategy(Fraction, settings).map(
            lambda f: specifier(f.numerator) / f.denominator
        )
    )
def test_tuple_strategy_too_large_to_fit():
    x = frozenset({specifiers.integers_in_range(0, 30)})
    assert not math.isinf(strategy(x).size_lower_bound)
    for _ in hrange(8):
        x = (x, x)
    assert math.isinf(
        strategy((int, x)).size_lower_bound)
Exemple #4
0
    def __init__(self, specifier, settings):
        self.specifier = specifier
        self.leaf_strategy = strategy(specifier.leaf_values, settings)
        self.branch_key_strategy = strategy(specifier.branch_keys, settings)
        self.branch_label_strategy = strategy(specifier.branch_labels,
                                              settings)

        self.child_strategy = (lists(tuples(self.branch_key_strategy, self)))
Exemple #5
0
    def __init__(self, specifier, settings):
        self.specifier = specifier
        self.leaf_strategy = strategy(specifier.leaf_values, settings)
        self.branch_key_strategy = strategy(
            specifier.branch_keys, settings)
        self.branch_label_strategy = strategy(
            specifier.branch_labels, settings)

        self.child_strategy = (
            lists(tuples(self.branch_key_strategy, self)))
Exemple #6
0
def define_strategy_for_float_Range(specifier, settings):
    if math.isinf(specifier.end - specifier.start):
        assert specifier.start < 0 and specifier.end > 0
        return strategy(
            specifiers.FloatRange(0, specifier.end), settings
        ) | strategy(
            specifiers.FloatRange(specifier.start, 0), settings
        )

    return FixedBoundedFloatStrategy(specifier.start, specifier.end)
Exemple #7
0
def define_strategy_for_float_Range(specifier, settings):
    if math.isinf(specifier.end - specifier.start):
        assert specifier.start < 0 and specifier.end > 0
        return strategy(
            specifiers.FloatRange(0, specifier.end), settings
        ) | strategy(
            specifiers.FloatRange(specifier.start, 0), settings
        )

    return FixedBoundedFloatStrategy(specifier.start, specifier.end)
Exemple #8
0
def test_strategy_for_integer_range_produces_only_integers_in_that_range():
    just_one_integer = strategy(specifiers.IntegerRange(1, 1))
    for _ in hrange(100):
        pv = just_one_integer.draw_parameter(random)
        x = just_one_integer.produce_template(BuildContext(random), pv)
        assert x == 1
    some_integers = strategy(specifiers.IntegerRange(1, 10))
    for _ in hrange(100):
        pv = some_integers.produce_parameter(random)
        x = some_integers.produce_template(BuildContext(random), pv)
        assert 1 <= x <= 10
def define_list_strategy(specifier, settings):
    if len(specifier) == 1:
        elt = strategy(specifier[0], settings)
        from hypothesis.searchstrategy.numbers import IntegersFromStrategy
        if elt.size_upper_bound == 1:
            return SingleElementListStrategy(
                elt,
                IntegersFromStrategy(
                    0, average_size=settings.average_list_length))
    return ListStrategy(
        [strategy(d, settings) for d in specifier],
        average_length=settings.average_list_length
    )
Exemple #10
0
def test_errors_if_you_mark_bad_before_fetching():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    with pytest.raises(ValueError):
        source.mark_bad()
def test_errors_if_you_mark_bad_before_fetching():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    with pytest.raises(ValueError):
        source.mark_bad()
Exemple #12
0
def test_just_strategy_uses_repr():
    class WeirdRepr(object):
        def __repr__(self):
            return 'ABCDEFG'

    assert repr(strategy(specifiers.just(
        WeirdRepr()))) == 'JustStrategy(value=%r)' % (WeirdRepr(), )
Exemple #13
0
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
    )

    search = strategy(specifier, settings)
    random = random or Random()

    def template_condition(template):
        return assume(condition(search.reify(template)))

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

    try:
        return search.reify(
            best_satisfying_template(
                search,
                random,
                template_condition,
                settings,
                None,
                tracker=tracker,
            ))
    except NoSuchExample:
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
Exemple #14
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))
Exemple #15
0
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
    )

    search = strategy(specifier, settings)
    random = random or Random()

    def template_condition(template):
        return assume(condition(search.reify(template)))

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

    try:
        return search.reify(best_satisfying_template(
            search, random, template_condition, settings, None,
            tracker=tracker,
        ))
    except NoSuchExample:
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
def test_random_only_produces_special_random():
    st = strategy(random.Random)
    assert isinstance(
        st.reify(st.produce_template(
            BuildContext(random), st.draw_parameter(random))),
        RandomWithSeed
    )
Exemple #17
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(
            'find(%s)' % (
                binascii.hexlify(function_digest(condition)).decode('ascii'),
            )
        )

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

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

        if success:
            successful_examples[0] += 1

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

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

    try:
        return search.reify(best_satisfying_template(
            search, random, template_condition, settings,
            tracker=tracker, max_parameter_tries=2,
            storage=storage,
        ))
    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))
Exemple #18
0
 def __init__(self, settings):
     super(DescriptorStrategy, self).__init__(strategy=strategy(
         NAryTree(branch_labels=sampled_from((tuple, dict, set, frozenset,
                                              list)),
                  branch_keys=one_of((int, str)),
                  leaf_values=sampled_from((int, float, text_type,
                                            binary_type, bool, complex,
                                            type(None)))), settings))
def test_just_strategy_uses_repr():
    class WeirdRepr(object):

        def __repr__(self):
            return 'ABCDEFG'

    assert repr(
        strategy(specifiers.just(WeirdRepr()))
    ) == 'JustStrategy(value=%r)' % (WeirdRepr(),)
def test_errors_if_you_mark_bad_twice():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    next(iter(source))
    source.mark_bad()
    with pytest.raises(ValueError):
        source.mark_bad()
Exemple #21
0
 def storage_for(self, specifier, search_strategy=None):
     """Get a storage object corresponding to this specifier."""
     return Storage(
         specifier=specifier,
         database=self,
         backend=self.backend,
         format=self.format,
         strategy=search_strategy or strategy(specifier),
     )
Exemple #22
0
def test_errors_if_you_mark_bad_twice():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    next(iter(source))
    source.mark_bad()
    with pytest.raises(ValueError):
        source.mark_bad()
Exemple #23
0
 def storage_for(self, specifier, search_strategy=None):
     """Get a storage object corresponding to this specifier."""
     return Storage(
         specifier=specifier,
         database=self,
         backend=self.backend,
         format=self.format,
         strategy=search_strategy or strategy(specifier),
     )
Exemple #24
0
def test_named_tuples_always_produce_named_tuples():
    s = strategy(Litter(int, int))

    for i in hrange(100):
        assert isinstance(
            s.produce_template(BuildContext(random),
                               s.produce_parameter(random)), Litter)

    for x in s.simplify(Litter(100, 100)):
        assert isinstance(x, Litter)
def test_named_tuples_always_produce_named_tuples():
    s = strategy(Litter(int, int))

    for i in hrange(100):
        assert isinstance(
            s.produce_template(
                BuildContext(random), s.produce_parameter(random)), Litter)

    for x in s.simplify(Litter(100, 100)):
        assert isinstance(x, Litter)
Exemple #26
0
def test_can_perform_all_basic_operations(specifier, random):
    strat = strategy(specifier, settings)
    parameter = strat.draw_parameter(random)
    template = strat.produce_template(BuildContext(random), parameter)
    assert (strat.to_basic(template) == strat.to_basic(
        strat.from_basic(strat.to_basic(template))))
    minimal_template = last(strat.simplify_such_that(template, lambda x: True))
    strat.reify(minimal_template)
    assert (strat.to_basic(minimal_template) == strat.to_basic(
        strat.from_basic(strat.to_basic(minimal_template))))
Exemple #27
0
def define_float_strategy(specifier, settings):
    return WrapperFloatStrategy(
        GaussianFloatStrategy() |
        BoundedFloatStrategy() |
        ExponentialFloatStrategy() |
        JustIntFloats(strategy(int)) |
        NastyFloats() |
        NastyFloats() |
        FullRangeFloats() |
        SmallFloats()
    )
Exemple #28
0
def define_float_strategy(specifier, settings):
    return WrapperFloatStrategy(
        GaussianFloatStrategy() |
        BoundedFloatStrategy() |
        ExponentialFloatStrategy() |
        JustIntFloats(strategy(int)) |
        NastyFloats() |
        NastyFloats() |
        FullRangeFloats() |
        SmallFloats()
    )
def test_caps_number_of_parameters_tried():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(bool),
        max_tries=1,
    )

    for p in islice(source, 200):
        pass

    assert all(t <= 1 for t in source.counts)
Exemple #30
0
 def __init__(self, settings):
     super(DescriptorStrategy, self).__init__(
         strategy=strategy(NAryTree(
             branch_labels=sampled_from((
                 tuple, dict, set, frozenset, list
             )),
             branch_keys=one_of((int, str)),
             leaf_values=sampled_from((
                 int, float, text_type, binary_type,
                 bool, complex, type(None)))
         ), settings)
     )
Exemple #31
0
    def define_rule(cls, targets, function, arguments):
        converted_arguments = {}
        for k, v in arguments.items():
            if not isinstance(v, Bundle):
                v = strategy(v)
            converted_arguments[k] = v
        if cls in cls._rules_per_class:
            target = cls._rules_per_class[cls]
        else:
            target = cls._base_rules_per_class.setdefault(cls, [])

        return target.append(Rule(targets, function, converted_arguments))
Exemple #32
0
def test_tries_each_parameter_at_least_min_index_times():
    source = ParameterSource(context=BuildContext(random.Random()),
                             strategy=strategy(int),
                             min_tries=5)
    i = 0
    for x in source.examples():
        i += 1
        if i > 500:
            break
        if i % 2:
            source.mark_bad()
    # The last index may not have been fully populated
    assert all(c >= 5 for c in source.counts[:-1])
Exemple #33
0
def test_strategy_for_integer_range_can_produce_end_points():
    some_integers = strategy(specifiers.IntegerRange(1, 10))
    found = set()
    for _ in hrange(1000):  # pragma: no branch
        pv = some_integers.produce_parameter(random)
        x = some_integers.produce_template(BuildContext(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_negative_is_not_too_far_off_mean():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    positive = 0
    i = 0
    for example in source.examples():
        if example >= 0:
            positive += 1
        i += 1
        if i >= N_EXAMPLES:
            break
    assert 0.3 <= float(positive) / N_EXAMPLES <= 0.7
Exemple #35
0
    def storage_for(self, specifier):
        """Get a storage object corresponding to this specifier.

        Will cache the result so that x.storage_for(d) is
        x.storage_for(d). You can rely on that behaviour.

        """
        return Storage(
            specifier=specifier,
            database=self,
            backend=self.backend,
            format=self.format,
            strategy=strategy(specifier),
        )
Exemple #36
0
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
        max_shrinks=2000,
    )

    search = strategy(specifier, settings)
    random = random or Random()
    successful_examples = [0]

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

        if success:
            successful_examples[0] += 1

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

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

    try:
        return search.reify(best_satisfying_template(
            search, random, template_condition, settings, None,
            tracker=tracker, max_parameter_tries=2,
        ))
    except Timeout:
        raise
    except (NoSuchExample, Unsatisfiable):
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
Exemple #37
0
    def define_rule(cls, targets, function, arguments):
        converted_arguments = {}
        for k, v in arguments.items():
            if not isinstance(v, Bundle):
                v = strategy(v)
            converted_arguments[k] = v
        if cls in cls._rules_per_class:
            target = cls._rules_per_class[cls]
        else:
            target = cls._base_rules_per_class.setdefault(cls, [])

        return target.append(
            Rule(targets, function, converted_arguments)
        )
Exemple #38
0
def test_negative_is_not_too_far_off_mean():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    positive = 0
    i = 0
    for example in source.examples():
        if example >= 0:
            positive += 1
        i += 1
        if i >= N_EXAMPLES:
            break
    assert 0.3 <= float(positive) / N_EXAMPLES <= 0.7
Exemple #39
0
    def storage_for(self, specifier):
        """Get a storage object corresponding to this specifier.

        Will cache the result so that x.storage_for(d) is
        x.storage_for(d). You can rely on that behaviour.

        """
        return Storage(
            specifier=specifier,
            database=self,
            backend=self.backend,
            format=self.format,
            strategy=strategy(specifier),
        )
def test_tries_each_parameter_at_least_min_index_times():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
        min_tries=5
    )
    i = 0
    for x in source.examples():
        i += 1
        if i > 500:
            break
        if i % 2:
            source.mark_bad()
    # The last index may not have been fully populated
    assert all(c >= 5 for c in source.counts[:-1])
Exemple #41
0
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
    )

    search = strategy(specifier, settings)
    random = random or Random()
    successful_examples = [0]

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

        if success:
            successful_examples[0] += 1

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

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

    try:
        return search.reify(
            best_satisfying_template(
                search,
                random,
                template_condition,
                settings,
                None,
                tracker=tracker,
            ))
    except NoSuchExample:
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
Exemple #42
0
def test_marking_negative_avoids_similar_examples():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    positive = 0
    i = 0
    for example in source.examples():
        if example >= 0:
            positive += 1
        else:
            source.mark_bad()
        i += 1
        if i >= N_EXAMPLES:
            break
    assert float(positive) / N_EXAMPLES >= 0.8
def test_culls_valid_parameters_if_lots_are_bad():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
        min_tries=5
    )
    for _ in islice(source, 200):
        source.mark_bad()

    for i in hrange(len(source.parameters)):
        assert source.counts[i] >= source.bad_counts[i]

    for i in hrange(len(source.parameters)):
        assert source.counts[i] == source.bad_counts[i]

    assert len(source.valid_parameters) <= 1
def test_marking_negative_avoids_similar_examples():
    positive = 0
    k = 10

    for _ in hrange(k):
        source = ParameterSource(
            context=BuildContext(random.Random()),
            strategy=strategy(int),
        )
        n = N_EXAMPLES // k
        for example in islice(source.examples(), n):
            if example >= 0:
                positive += 1
            else:
                source.mark_bad()
    assert float(positive) / N_EXAMPLES >= 0.7
def test_marking_negative_avoids_similar_examples():
    source = ParameterSource(
        context=BuildContext(random.Random()),
        strategy=strategy(int),
    )
    positive = 0
    i = 0
    for example in source.examples():
        if example >= 0:
            positive += 1
        else:
            source.mark_bad()
        i += 1
        if i >= N_EXAMPLES:
            break
    assert float(positive) / N_EXAMPLES >= 0.8
Exemple #46
0
    def run_test():
        if condition is None:
            _condition = lambda x: True
            condition_string = ''
        else:
            _condition = condition
            condition_string = strip_lambda(
                reflection.get_pretty_function_description(condition))

        count = 0
        successful_runs = 0
        s = strategy(specifier)
        for _ in hrange(MAX_RUNS):
            pv = s.draw_parameter(random)
            try:
                x = s.reify(s.draw_template(random, pv))
            except UnsatisfiedAssumption:
                continue
            if not _condition(x):
                continue
            successful_runs += 1
            if predicate(x):
                count += 1
        if successful_runs < MIN_RUNS:
            raise ConditionTooHard((
                'Unable to find enough examples satisfying predicate %s '
                'only found %d but required at least %d for validity'
            ) % (
                condition_string, successful_runs, MIN_RUNS
            ))

        result = Result(
            count,
            successful_runs,
            q,
            predicate,
            condition_string,
        )

        p = cumulative_binomial_probability(successful_runs, q, count)
        run_test.test_result = result
        # The test passes if we fail to reject the null hypothesis that
        # the probability is at least q
        if p < REQUIRED_P:
            result.failed = True
            raise HypothesisFalsified(result.description() + ' rejected')
Exemple #47
0
def test_simplify_integer_range_can_push_to_near_boundaries():
    some_integers = strategy(specifiers.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_simplify_integer_range_can_push_to_near_boundaries():
    some_integers = strategy(specifiers.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 run_test():
        if condition is None:
            _condition = lambda x: True
            condition_string = u''
        else:
            _condition = condition
            condition_string = strip_lambda(
                reflection.get_pretty_function_description(condition))

        count = 0
        successful_runs = 0
        s = strategy(specifier)
        for _ in hrange(MAX_RUNS):
            pv = s.draw_parameter(random)
            try:
                x = s.reify(s.draw_template(random, pv))
            except UnsatisfiedAssumption:
                continue
            if not _condition(x):
                continue
            successful_runs += 1
            if predicate(x):
                count += 1
        if successful_runs < MIN_RUNS:
            raise ConditionTooHard((
                u'Unable to find enough examples satisfying predicate %s '
                u'only found %d but required at least %d for validity'
            ) % (
                condition_string, successful_runs, MIN_RUNS
            ))

        result = Result(
            count,
            successful_runs,
            q,
            predicate,
            condition_string,
        )

        p = cumulative_binomial_probability(successful_runs, q, count)
        run_test.test_result = result
        # The test passes if we fail to reject the null hypothesis that
        # the probability is at least q
        if p < REQUIRED_P:
            result.failed = True
            raise HypothesisFalsified(result.description() + u' rejected')
def test_float_simplicity():
    s = strategy(float).strictly_simpler

    def order(x, y):
        x = float(x)
        y = float(y)
        assert s(x, y)
        assert not s(y, x)

    order(sys.float_info.max, '-inf')
    order(1.0, 0.5)
    order(1.0, 2.0)
    order(2, -1)
    order('inf', 'nan')
    order('inf', '-inf')
    order('0.25', '0.5')
    order(-1, 0.5)
    order(1.5, '-inf')
def test_can_perform_all_basic_operations(specifier, random):
    strat = strategy(specifier, settings)
    parameter = strat.draw_parameter(random)
    template = strat.produce_template(BuildContext(random), parameter)
    assert (
        strat.to_basic(template) ==
        strat.to_basic(strat.from_basic(strat.to_basic(template)))
    )
    minimal_template = last(strat.simplify_such_that(
        random,
        template,
        lambda x: True
    ))
    strat.reify(minimal_template)
    assert (
        strat.to_basic(minimal_template) ==
        strat.to_basic(strat.from_basic(strat.to_basic(minimal_template)))
    )
Exemple #52
0
def test_can_grow_the_set_of_available_parameters_if_doing_badly():
    runs = 10
    number_grown = 0
    for _ in hrange(runs):
        source = ParameterSource(
            context=BuildContext(random.Random()),
            strategy=strategy(int),
            min_parameters=1,
        )
        i = 0
        for example in source.examples():
            if example < 0:
                source.mark_bad()
            i += 1
            if i >= 100:
                break
        if len(source.parameters) > 1:
            number_grown += 1
        assert len(source.parameters) < 100
Exemple #53
0
def test_simple_data_validation(specifier, data):
    converter = strategy(specifier)
    with pytest.raises(BadData):
        converter.from_basic(data)
Exemple #54
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
            test_runner = executor(selfy)

            for example in getattr(wrapped_test,
                                   '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

                test_runner(lambda: test(*arguments, **example_kwargs))

            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 v.value
                else:
                    return just(v)

            given_specifier = (tuple(map(convert_to_specifier, arguments)),
                               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_for(
                    given_specifier, search_strategy)
            else:
                storage = None

            def is_template_example(xs):
                try:
                    test_runner(reify_and_execute(search_strategy, xs, test))
                    return False
                except UnsatisfiedAssumption as e:
                    raise e
                except Exception:
                    return True

            is_template_example.__name__ = test.__name__
            is_template_example.__qualname__ = getattr(test, '__qualname__',
                                                       test.__name__)

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

            test_runner(
                reify_and_execute(search_strategy,
                                  falsifying_template,
                                  test,
                                  print_example=True))

            test_runner(
                reify_and_execute(search_strategy,
                                  falsifying_template,
                                  test_is_flaky(test),
                                  print_example=True))
Exemple #55
0
def define_binary_strategy(specifier, settings):
    return BinaryStringStrategy(
        strategy=strategy([specifiers.integers_in_range(0, 255)], settings),
    )
Exemple #56
0
def define_text_type_strategy(specifier, settings):
    return StringStrategy(strategy([OneCharStringStrategy()], settings))
Exemple #57
0
def define_text_type_from_alphabet(specifier, settings):
    if not specifier.alphabet:
        return StringStrategy(strategy([], settings))
    return StringStrategy(strategy(
        [specifiers.sampled_from(specifier.alphabet)], settings))
def define_dictionary_strategy(specifier, settings):
    return strategy(
        [(specifier.keys, specifier.values)], settings
    ).map(specifier.dict_class)