def test_round_tripping_lists_via_the_database(spec):
    random = Random(hashlib.md5(
        (show(spec) + ':test_round_tripping_via_the_database').encode('utf-8')
    ).digest())
    strat = lists(spec)
    template = some_template(strat, random)
    template_via_db = via_database(spec, strat, template)
    assert show(strat.reify(template)) == show(strat.reify(template_via_db))
Ejemplo n.º 2
0
def test_round_tripping_lists_via_the_database(spec):
    random = Random(
        hashlib.md5((
            show(spec) +
            ':test_round_tripping_via_the_database').encode('utf-8')).digest())
    strat = lists(spec)
    template = some_template(strat, random)
    template_via_db = via_database(spec, strat, template)
    assert show(strat.reify(template)) == show(strat.reify(template_via_db))
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(hashlib.md5((
        show(spec) + ':test_all_minimal_elements_round_trip_via_the_database'
    ).encode('utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        assert show(strat.reify(elt)) == show(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
Ejemplo n.º 4
0
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(
        hashlib.md5((show(spec) +
                     ':test_all_minimal_elements_round_trip_via_the_database'
                     ).encode('utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        assert show(strat.reify(elt)) == show(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
def test_round_tripping_via_the_database(spec):
    random = Random(
        hashlib.md5((show(spec) + u':test_round_tripping_via_the_database'
                     ).encode(u'utf-8')).digest())
    strat = spec
    template = some_template(strat, random)
    strat.from_basic(strat.to_basic(template))
    template_via_db = via_database(spec, strat, template)
    with BuildContext():
        assert show(strat.reify(template)) == show(
            strat.reify(template_via_db))
Ejemplo n.º 6
0
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(hashlib.md5((
        show(spec) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = spec
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        with BuildContext():
            assert show(strat.reify(elt)) == show(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
def test_round_tripping_via_the_database(spec):
    random = Random(hashlib.md5((
        show(spec) + u':test_round_tripping_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = strategy(spec)
    template = some_template(strat, random)
    strat.from_basic(strat.to_basic(template))
    template_via_db = via_database(spec, strat, template)
    with BuildContext():
        assert show(strat.reify(template)) == show(
            strat.reify(template_via_db))
def test_all_minimal_elements_round_trip_via_the_database(spec):
    random = Random(
        hashlib.md5((show(spec) +
                     u':test_all_minimal_elements_round_trip_via_the_database'
                     ).encode(u'utf-8')).digest())
    strat = spec
    for elt in minimal_elements(strat, random):
        elt_via_db = via_database(spec, strat, elt)
        with BuildContext():
            assert show(strat.reify(elt)) == show(strat.reify(elt_via_db))
        elt_via_db_2 = via_database(spec, strat, elt_via_db)
        assert elt_via_db == elt_via_db_2
Ejemplo n.º 9
0
def arg_string(f, args, kwargs):
    from hypothesis.utils.show import show
    args, kwargs = convert_positional_arguments(f, args, kwargs)

    argspec = inspect.getargspec(f)

    bits = []

    for a in argspec.args:
        if a in kwargs:
            bits.append('%s=%s' % (a, show(kwargs.pop(a))))
    if kwargs:
        for a in sorted(kwargs):
            bits.append('%s=%s' % (a, show(kwargs[a])))

    return ', '.join([show(x) for x in args] + bits)
Ejemplo n.º 10
0
 def __init__(self, backend, specifier, strategy, format, database):
     self.database = database
     self.backend = backend
     self.specifier = specifier
     self.format = format
     self.strategy = strategy
     self.key = show(specifier)
Ejemplo n.º 11
0
def test_all_minimal_elements_reify(strat):
    random = Random(hashlib.md5((
        show(strat) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 12
0
 def do_map(value):
     rep = show(value)
     random = Random(hashlib.md5((mixer + rep).encode("utf-8")).digest())
     if random.random() <= p:
         return result1
     else:
         return result2
Ejemplo n.º 13
0
 def __repr__(self):
     return (
         'DescriptorWithValue(specifier=%s, template=%r, '
         'value=%r, random=%r)'
     ) % (
         show(self.specifier), self.template, self.value,
         self.random,
     )
Ejemplo n.º 14
0
def test_all_minimal_elements_reify(spec):
    random = Random(hashlib.md5((
        show(spec) + ':test_all_minimal_elements_round_trip_via_the_database'
    ).encode('utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 15
0
 def do_map(value):
     rep = show(value)
     random = Random(
         hashlib.md5((mixer + rep).encode(u'utf-8')).digest())
     if random.random() <= p:
         return result1
     else:
         return result2
Ejemplo n.º 16
0
def test_all_minimal_elements_reify(spec):
    random = Random(hashlib.md5((
        show(spec) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = strategy(spec, Settings(average_list_length=2))
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 17
0
def test_all_minimal_elements_reify(spec):
    random = Random(hashlib.md5((
        show(spec) + u':test_all_minimal_elements_round_trip_via_the_database'
    ).encode(u'utf-8')).digest())
    strat = spec
    for elt in minimal_elements(strat, random):
        with BuildContext():
            strat.reify(elt)
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
 def __repr__(self):
     return (
         'DescriptorWithValue(specifier=%s, template=%r, '
         'value=%r, random=%r)'
     ) % (
         show(self.specifier), self.template, self.value,
         self.random,
     )
Ejemplo n.º 20
0
def test_just_show_should_respect_its_values_reprs():
    class Stuff(object):

        def __repr__(self):
            return u'Things()'
    assert show(
        specifiers.Just(Stuff())
    ) == u'Just(value=Things())'
Ejemplo n.º 21
0
    def print_step(self, step):
        """Print a step to the current reporter.

        This is called right before a step is executed.

        """
        self.step_count = getattr(self, 'step_count', 0) + 1
        report('Step #%d: %s' % (self.step_count, show(step)))
Ejemplo n.º 22
0
def test_just_show_should_respect_its_values_reprs():
    class Stuff(object):

        def __repr__(self):
            return 'Things()'
    assert show(
        specifiers.Just(Stuff())
    ) == 'Just(value=Things())'
Ejemplo n.º 23
0
 def do_map(value):
     rep = show(value)
     try:
         return deepcopy(cache[rep])
     except KeyError:
         pass
     random = Random(hashlib.md5((mixer + rep).encode("utf-8")).digest())
     outcome_template = result.draw_and_produce(random)
     cache[rep] = result.reify(outcome_template)
     return deepcopy(cache[rep])
Ejemplo n.º 24
0
def test_can_handle_recursion():
    x = []
    x.append(x)
    assert show(x) == u'[(...)]'

    d = {}
    d[1] = d
    assert show(d) == u'{1: (...)}'

    t = ([], )
    t[0].append(t)
    assert show(t) == u'([(...)],)'

    class Foo(object):
        pass

    f = Foo()
    f.stuff = f
    assert show(f) == u'Foo(stuff=(...))'
Ejemplo n.º 25
0
 def __init__(
     self, backend, specifier, strategy, format,
     database
 ):
     self.database = database
     self.backend = backend
     self.specifier = specifier
     self.format = format
     self.strategy = strategy
     self.key = show(specifier)
Ejemplo n.º 26
0
def test_can_handle_recursion():
    x = []
    x.append(x)
    assert show(x) == u'[(...)]'

    d = {}
    d[1] = d
    assert show(d) == u'{1: (...)}'

    t = ([],)
    t[0].append(t)
    assert show(t) == u'([(...)],)'

    class Foo(object):
        pass

    f = Foo()
    f.stuff = f
    assert show(f) == u'Foo(stuff=(...))'
Ejemplo n.º 27
0
def test_can_handle_recursion():
    x = []
    x.append(x)
    assert show(x) == "[(...)]"

    d = {}
    d[1] = d
    assert show(d) == "{1: (...)}"

    t = ([],)
    t[0].append(t)
    assert show(t) == "([(...)],)"

    class Foo(object):
        pass

    f = Foo()
    f.stuff = f
    assert show(f) == "Foo(stuff=(...))"
Ejemplo n.º 28
0
def arg_string(f, args, kwargs):
    from hypothesis.utils.show import show
    args, kwargs = convert_positional_arguments(f, args, kwargs)

    argspec = inspect.getargspec(f)

    bits = []

    for a in argspec.args:
        if a in kwargs:
            bits.append('%s=%s' % (a, show(kwargs.pop(a))))
    if kwargs:
        for a in sorted(kwargs):
            bits.append('%s=%s' % (a, show(kwargs[a])))

    return ', '.join(
        [show(x) for x in args] +
        bits
    )
Ejemplo n.º 29
0
 def do_map(value):
     rep = show(value)
     try:
         return deepcopy(cache[rep])
     except KeyError:
         pass
     random = Random(
         hashlib.md5((mixer + rep).encode(u'utf-8')).digest())
     outcome_template = result.draw_and_produce(random)
     cache[rep] = result.reify(outcome_template)
     return deepcopy(cache[rep])
Ejemplo n.º 30
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
Ejemplo n.º 31
0
 def print_step(self, step):
     rule, data = step
     data_repr = {}
     for k, v in data.items():
         if isinstance(v, VarReference):
             data_repr[k] = v.name
         else:
             data_repr[k] = show(v)
     self.step_count = getattr(self, 'step_count', 0) + 1
     report('Step #%d: %s%s(%s)' % (
         self.step_count,
         '%s = ' % (self.upcoming_name(),) if rule.targets else '',
         rule.function.__name__,
         ', '.join('%s=%s' % kv for kv in data_repr.items())
     ))
Ejemplo n.º 32
0
def test_nice_strint_for_nice_floats():
    assert show(0.5) == repr(0.5)
Ejemplo n.º 33
0
def test_show_for_nice_complex():
    assert show(1 + 1j) == u'(1+1j)'
Ejemplo n.º 34
0
def test_show_evals_as_specifier(desc):
    s = show(desc)
    read_desc = eval(s)
    assert show(read_desc) == s
Ejemplo n.º 35
0
def test_uses_binary_literals_for_binary_type():
    assert show(b'foo') == u"b'foo'"
Ejemplo n.º 36
0
def test_show_for_nasty_in_just():
    assert show(specifiers.just(
        complex(u'inf+1.9j'))) == u"Just(value=complex('inf+1.9j'))"
Ejemplo n.º 37
0
def test_uses_show_inside_unnamed_tuples():
    assert show((1, float(u'nan'))) == u"(1, float('nan'))"
Ejemplo n.º 38
0
def test_can_nicely_display_things_without_repr():
    assert show(X(1)) == u'X(x=1)'
Ejemplo n.º 39
0
def test_show_for_sets_is_not_a_dict():
    assert show(set()) == repr(set())
    assert show(frozenset()) == repr(frozenset())
Ejemplo n.º 40
0
 def nope(x):
     if hash(show(specifier)) % 2:
         raise Rejected()
Ejemplo n.º 41
0
def test_does_not_strip_brackets_when_not_present():
    assert show(complex(u'nanj')) == u"complex('nanj')"
Ejemplo n.º 42
0
def test_show_for_nasty_complex():
    assert show(complex(float(u'inf'), 0.0)) == u"complex('inf+0j')"
Ejemplo n.º 43
0
def test_uses_show_inside_unnamed_tuples():
    assert show((1, float(u'nan'))) == u"(1, float('nan'))"
Ejemplo n.º 44
0
 def from_basic(self, data):
     if data is not None:
         raise BadData('Expected None but got %s' % (show(data,)))
     return None
Ejemplo n.º 45
0
def test_show_for_nasty_complex():
    assert show(
        complex(float(u'inf'), 0.0)) == u"complex('inf+0j')"
Ejemplo n.º 46
0
def test_can_nicely_display_things_without_repr():
    assert show(X(1)) == u'X(x=1)'
Ejemplo n.º 47
0
def test_show_for_nasty_in_just():
    assert show(
        specifiers.just(complex(u'inf+1.9j'))
    ) == u"Just(value=complex('inf+1.9j'))"
Ejemplo n.º 48
0
 def test(x):
     assert Random(
         hashlib.md5(show(x).encode(u'utf-8')).digest()
     ).random() <= p
Ejemplo n.º 49
0
def test_non_empty_frozensets_should_use_set_representation():
    assert show(frozenset([int])) == u'frozenset({int})'
Ejemplo n.º 50
0
 def is_good(x):
     return bool(Random(
         hashlib.md5((mixer + show(x)).encode(u'utf-8')).digest()
     ).randint(0, level))
Ejemplo n.º 51
0
def test_uses_show_inside_named_tuples():
    Foo = namedtuple(u'Foo', (u'b', u'a'))
    assert show(
        Foo(1, float(u'nan'))
    ) == u"Foo(b=1, a=float('nan'))"
Ejemplo n.º 52
0
def test_uses_text_literals_for_text_type():
    assert show(u'foo') == u"'foo'"
Ejemplo n.º 53
0
def test_does_not_strip_brackets_when_not_present():
    assert show(complex(u'nanj')) == u"complex('nanj')"
Ejemplo n.º 54
0
def test_copies_all_its_values_correctly(desc, random):
    strat = strategy(desc, settings)
    value = strat.produce_template(
        BuildContext(random), strat.draw_parameter(random))
    assert show(strat.reify(value)) == show(strat.reify(value))
Ejemplo n.º 55
0
def test_fetched_repr_is_in_stream_repr(s):
    assert repr(s) == u'Stream(...)'
    assert show(next(iter(s))) in show(s)
Ejemplo n.º 56
0
def test_uses_show_inside_named_tuples():
    Foo = namedtuple(u'Foo', (u'b', u'a'))
    assert show(Foo(1, float(u'nan'))) == u"Foo(b=1, a=float('nan'))"
Ejemplo n.º 57
0
 def check_serialization(self, st):
     strat, template = st
     as_basic = strat.to_basic(template)
     assert show(strat.reify(template)) == show(strat.reify(
         strat.from_basic(as_basic)))
     assert as_basic == strat.to_basic(strat.from_basic(as_basic))
Ejemplo n.º 58
0
def test_non_empty_frozensets_should_use_set_representation():
    assert show(frozenset([int])) == u'frozenset({int})'
Ejemplo n.º 59
0
 def __repr__(self):
     return 'strategy_test_suite(%s)' % (show(specifier), )
Ejemplo n.º 60
0
def test_show_for_sets_is_not_a_dict():
    assert show(set()) == repr(set())
    assert show(frozenset()) == repr(frozenset())