def test_unrepr_identity_elem(): # Works with inferred identity element source_code = ghostwriter.binary_operation(compose_types) exec(source_code, {}) # and also works with explicit identity element source_code = ghostwriter.binary_operation(compose_types, identity=type) exec(source_code, {})
("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), ), ( "division_roundtrip_typeerror_handler", ghostwriter.roundtrip(divide, operator.mul, except_=TypeError), ), ( "division_operator",
# 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, stdout=subprocess.PIPE, shell=True, universal_newlines=True, ) cli_output = result.stdout.strip() assert not result.stderr code_output = code().strip() assert code_output == cli_output result.check_returncode()
def test_binary_op_also_handles_frozensets(): # Using str.replace in a loop would convert `frozensets()` into # `st.frozenst.sets()` instead of `st.frozensets()`; fixed with re.sub. source_code = ghostwriter.binary_operation(takes_frozensets) exec(source_code, {})