diff --git a/src/syrupy/extensions/amber/serializer.py b/src/syrupy/extensions/amber/serializer.py index 07e25177..742593a4 100644 --- a/src/syrupy/extensions/amber/serializer.py +++ b/src/syrupy/extensions/amber/serializer.py @@ -1,7 +1,9 @@ import collections +import inspect import os from collections import OrderedDict from types import ( + FunctionType, GeneratorType, MappingProxyType, ) @@ -257,6 +259,8 @@ def _serialize( serialize_method = cls.serialize_namedtuple elif isinstance(data, (list, tuple, GeneratorType)): serialize_method = cls.serialize_iterable + elif isinstance(data, FunctionType): + serialize_method = cls.serialize_function return serialize_method(**serialize_kwargs) @classmethod @@ -337,6 +341,14 @@ def serialize_dict( **kwargs, ) + @classmethod + def serialize_function( + cls, data: FunctionType, *, depth: int = 0, **kwargs: Any + ) -> str: + return cls.__serialize_plain( + data=f"{data.__qualname__}{str(inspect.signature(data))}", depth=depth + ) + @classmethod def serialize_unknown(cls, data: Any, *, depth: int = 0, **kwargs: Any) -> str: if data.__class__.__repr__ != object.__repr__: diff --git a/src/syrupy/extensions/json/__init__.py b/src/syrupy/extensions/json/__init__.py index 5b52a8d5..bd26a65c 100644 --- a/src/syrupy/extensions/json/__init__.py +++ b/src/syrupy/extensions/json/__init__.py @@ -1,7 +1,11 @@ import datetime +import inspect import json from collections import OrderedDict -from types import GeneratorType +from types import ( + FunctionType, + GeneratorType, +) from typing import ( TYPE_CHECKING, Any, @@ -133,6 +137,12 @@ def _filter( if isinstance(data, (datetime.datetime,)): return data.strftime("%Y-%m-%dT%H:%M:%S.%f%z") + if isinstance(data, FunctionType): + return ( + f"<{FunctionType.__name__} " + f"{data.__qualname__}{str(inspect.signature(data))}>" + ) + if data.__class__.__repr__ != object.__repr__: return repr(data) diff --git a/tests/syrupy/extensions/amber/__snapshots__/test_amber_serializer.ambr b/tests/syrupy/extensions/amber/__snapshots__/test_amber_serializer.ambr index 72f06006..34a0fabb 100644 --- a/tests/syrupy/extensions/amber/__snapshots__/test_amber_serializer.ambr +++ b/tests/syrupy/extensions/amber/__snapshots__/test_amber_serializer.ambr @@ -217,6 +217,12 @@ # name: test_empty_snapshot.1 '' # --- +# name: test_function_in_file + "function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> str" +# --- +# name: test_function_local + "test_function_local..local_function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> int" +# --- # name: test_list[actual0] list([ ]) diff --git a/tests/syrupy/extensions/amber/test_amber_serializer.py b/tests/syrupy/extensions/amber/test_amber_serializer.py index 9edad07c..365bc7fe 100644 --- a/tests/syrupy/extensions/amber/test_amber_serializer.py +++ b/tests/syrupy/extensions/amber/test_amber_serializer.py @@ -233,3 +233,22 @@ def test_ordered_dict(snapshot): def test_many_sorted(snapshot): for i in range(25): assert i == snapshot + + +def function_to_test( + var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val" +) -> str: + return "2" + + +def test_function_in_file(snapshot): + assert snapshot == function_to_test + + +def test_function_local(snapshot): + def local_function_to_test( + var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val" + ) -> int: + return 1 + + assert snapshot == local_function_to_test diff --git a/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_in_file.json b/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_in_file.json new file mode 100644 index 00000000..c02919a3 --- /dev/null +++ b/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_in_file.json @@ -0,0 +1 @@ +" str>" diff --git a/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_local.json b/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_local.json new file mode 100644 index 00000000..bc801efe --- /dev/null +++ b/tests/syrupy/extensions/json/__snapshots__/test_json_serializer/test_function_local.json @@ -0,0 +1 @@ +".local_function_to_test(var1, var2='test_val', var3: str = 'test_val2', *, kwvar1, kwvar2='some_val') -> int>" diff --git a/tests/syrupy/extensions/json/test_json_serializer.py b/tests/syrupy/extensions/json/test_json_serializer.py index e957c09e..449cfcf3 100644 --- a/tests/syrupy/extensions/json/test_json_serializer.py +++ b/tests/syrupy/extensions/json/test_json_serializer.py @@ -232,3 +232,22 @@ def test_ordered_dict(snapshot_json): d["b"] = 0 d["a"] = OrderedDict(b=True, a=False) assert snapshot_json == d + + +def function_to_test( + var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val" +) -> str: + return "2" + + +def test_function_in_file(snapshot_json): + assert snapshot_json == function_to_test + + +def test_function_local(snapshot_json): + def local_function_to_test( + var1, var2="test_val", var3: str = "test_val2", *, kwvar1, kwvar2="some_val" + ) -> int: + return 1 + + assert snapshot_json == local_function_to_test