Example #1
0
def test_overlapping_args_use_union_of_strategies():
    def f(arg: int) -> None:
        pass

    def g(arg: float) -> None:
        pass

    source_code = ghostwriter.equivalent(f, g)
    assert "arg=st.one_of(st.integers(), st.floats())" in source_code
 ("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),
 ),
 (
     "division_roundtrip_error_handler",
     ghostwriter.roundtrip(divide, operator.mul),
 ),
 (
     "division_roundtrip_arithmeticerror_handler",
     ghostwriter.roundtrip(divide, operator.mul, except_=ArithmeticError),
Example #3
0
     .replace("import sre_constants\n", "").replace(
         "sre_constants.", "re."),
 ),
 ("re_compile_unittest", ghostwriter.fuzz(re.compile,
                                          style="unittest")),
 pytest.param(
     ("base64_magic", ghostwriter.magic(base64)),
     marks=pytest.mark.skipif("sys.version_info[:2] >= (3, 10)"),
 ),
 ("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),
 ),
 (
     "division_roundtrip_error_handler",
     ghostwriter.roundtrip(divide, operator.mul),
 ),
 (
    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)),
    ],
)
def test_cli_python_equivalence(cli, code):
    result = subprocess.run(
        "hypothesis write " + cli,
        stderr=subprocess.PIPE,