def test_ghostwriter_on_hypothesis(update_recorded_outputs): actual = ghostwriter.magic(hypothesis).replace("Strategy[+Ex]", "Strategy") expected = get_recorded("hypothesis_module_magic", actual * update_recorded_outputs) if sys.version_info[:2] < (3, 10): assert actual == expected exec(expected, {"not_set": not_set})
def test_ghostwriter_on_hypothesis(update_recorded_outputs): actual = ghostwriter.magic(hypothesis).replace("Strategy[+Ex]", "Strategy") expected = get_recorded("hypothesis_module_magic", actual * update_recorded_outputs) # The py36 typing module has some different handling of generics (SearchStrategy) # and contents (collections.abc vs typing), but we can still check the code works. if sys.version_info[:2] > (3, 6): assert actual == expected exec(expected, {"not_set": not_set})
"""This is a RST-style docstring for `divide`. :raises ZeroDivisionError: if b == 0 """ return a / b # Note: for some of the `expected` outputs, we replace away some small # parts which vary between minor versions of Python. @pytest.mark.parametrize( "data", [ ("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),
def test_module_with_mock_does_not_break(): # Before we added an explicit check for unspec'd mocks, they would pass # through the initial validation and then fail when used in more detailed # logic in the ghostwriter machinery. ghostwriter.magic(unittest.mock)
def test_socket_module(): source_code = ghostwriter.magic(socket) exec(source_code, {})
pass def add(a: float, b: float) -> float: return a + b # Note: for some of the `expected` outputs, we replace away some small # parts which vary between minor versions of Python. @pytest.mark.parametrize( "data", [ ("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",
""" This is an example script of how to generate boilerplate for hypothesis tests """ from hypothesis.extra import ghostwriter import privacyraven.extraction.metrics as metrics # The above line imports the source code file_name = "test_this_code.py" f = open(file_name, "w+") f.write(ghostwriter.magic(metrics))