Exemplo n.º 1
0
def test_ghostwriter_idempotent(func, ex):
    source_code = ghostwriter.idempotent(func, except_=ex)
    test = get_test_function(source_code)
    if "=st.nothing()" in source_code:
        with pytest.raises(Unsatisfiable):
            test()
    else:
        test()
Exemplo n.º 2
0
 [
     ("fuzz_sorted", ghostwriter.fuzz(sorted)),
     ("fuzz_classmethod", ghostwriter.fuzz(A_Class.a_classmethod)),
     ("fuzz_ufunc", ghostwriter.fuzz(numpy.add)),
     ("magic_gufunc", ghostwriter.magic(numpy.matmul)),
     ("magic_base64_roundtrip", ghostwriter.magic(base64.b64encode)),
     ("re_compile", ghostwriter.fuzz(re.compile)),
     (
         "re_compile_except",
         ghostwriter.fuzz(re.compile, except_=re.error)
         # re.error fixed it's __module__ in Python 3.7
         .replace("import sre_constants\n", "").replace("sre_constants.", "re."),
     ),
     ("re_compile_unittest", ghostwriter.fuzz(re.compile, style="unittest")),
     ("base64_magic", ghostwriter.magic(base64)),
     ("sorted_idempotent", ghostwriter.idempotent(sorted)),
     ("timsort_idempotent", ghostwriter.idempotent(timsort)),
     (
         "timsort_idempotent_asserts",
         ghostwriter.idempotent(timsort, except_=AssertionError),
     ),
     ("eval_equivalent", ghostwriter.equivalent(eval, ast.literal_eval)),
     ("sorted_self_equivalent", ghostwriter.equivalent(sorted, sorted, sorted)),
     ("addition_op_magic", ghostwriter.magic(add)),
     ("addition_op_multimagic", ghostwriter.magic(add, operator.add, numpy.add)),
     ("division_fuzz_error_handler", ghostwriter.fuzz(divide)),
     (
         "division_binop_error_handler",
         ghostwriter.binary_operation(divide, identity=1),
     ),
     (
Exemplo n.º 3
0
from hypothesis.errors import StopTest
from hypothesis.extra.ghostwriter import (
    binary_operation,
    equivalent,
    fuzz,
    idempotent,
    roundtrip,
)


@pytest.mark.parametrize(
    "cli,code",
    [
        # Passing one argument falls back to one-argument tests
        ("--equivalent re.compile", lambda: fuzz(re.compile)),
        ("--roundtrip sorted", lambda: idempotent(sorted)),
        # For multiple arguments, they're equivalent to the function call
        (
            "--equivalent eval ast.literal_eval",
            lambda: equivalent(eval, ast.literal_eval),
        ),
        (
            "--roundtrip json.loads json.dumps --except ValueError",
            lambda: roundtrip(json.loads, json.dumps, except_=ValueError),
        ),
        # Imports submodule (importlib.import_module passes; __import__ fails)
        ("hypothesis.errors.StopTest", lambda: fuzz(StopTest)),
        # Search for identity element does not print e.g. "You can use @seed ..."
        ("--binary-op operator.add", lambda: binary_operation(operator.add)),
    ],
)
Exemplo n.º 4
0
 ("fuzz_sorted", ghostwriter.fuzz(sorted)),
 ("fuzz_classmethod", ghostwriter.fuzz(A_Class.a_classmethod)),
 ("fuzz_ufunc", ghostwriter.fuzz(numpy.add)),
 ("magic_gufunc", ghostwriter.magic(numpy.matmul)),
 ("re_compile", ghostwriter.fuzz(re.compile)),
 (
     "re_compile_except",
     ghostwriter.fuzz(re.compile, except_=re.error)
     # re.error fixed it's __module__ in Python 3.7
     .replace("import sre_constants\n", "").replace(
         "sre_constants.", "re."),
 ),
 ("re_compile_unittest", ghostwriter.fuzz(re.compile,
                                          style="unittest")),
 ("base64_magic", ghostwriter.magic(base64)),
 ("sorted_idempotent", ghostwriter.idempotent(sorted)),
 ("timsort_idempotent", ghostwriter.idempotent(timsort)),
 ("eval_equivalent", ghostwriter.equivalent(eval, ast.literal_eval)),
 ("sorted_self_equivalent",
  ghostwriter.equivalent(sorted, sorted, sorted)),
 ("addition_op_magic", ghostwriter.magic(add)),
 ("addition_op_multimagic",
  ghostwriter.magic(add, operator.add, numpy.add)),
 (
     "division_operator",
     ghostwriter.binary_operation(
         operator.truediv, associative=False, commutative=False),
 ),
 (
     "multiplication_operator",
     ghostwriter.binary_operation(