Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update serialization to support named tuple fields #108

Merged
merged 6 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ From v1.0.0 onwards, this project adheres to [Semantic Versioning](https://semve

## Master (Unreleased)

Up to date with releases.
- Adds support for named tuple fields (#108)
- Add trailing commas to class fields (#108)

## [v0.2.0](https://github.com/tophat/syrupy/compare/v0.1.0...v0.2.0)

Expand Down
31 changes: 30 additions & 1 deletion src/syrupy/extensions/amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@ def serialize_dict(
+ cls.with_indent("}", depth)
)

@classmethod
def __is_namedtuple(cls, obj: Any) -> bool:
return isinstance(obj, tuple) and all(
type(n) == str for n in getattr(obj, "_fields", [None])
)

@classmethod
def serialize_namedtuple(
cls, data: Any, *, depth: int = 0, visited: Optional[Set[Any]] = None
) -> str:
return (
cls.with_indent(f"{cls.object_type(data)} (\n", depth)
+ "".join(
f"{serialized_key}={serialized_value.lstrip(cls._indent)},\n"
for serialized_key, serialized_value in (
(
cls.with_indent(name, depth=depth + 1),
cls.serialize(
data=getattr(data, name), depth=depth + 1, visited=visited
),
)
for name in cls.sort(data._fields)
)
)
+ cls.with_indent(")", depth)
)

@classmethod
def serialize_iterable(
cls,
Expand Down Expand Up @@ -189,7 +216,7 @@ def serialize_unknown(
return (
cls.with_indent(f"{cls.object_type(data)} {{\n", depth)
+ "".join(
f"{serialized_key}={serialized_value.lstrip(cls._indent)}\n"
f"{serialized_key}={serialized_value.lstrip(cls._indent)},\n"
for serialized_key, serialized_value in (
(
cls.with_indent(name, depth=depth + 1),
Expand Down Expand Up @@ -231,6 +258,8 @@ def serialize(
serialize_method = cls.serialize_set
elif isinstance(data, dict):
serialize_method = cls.serialize_dict
elif cls.__is_namedtuple(data):
serialize_method = cls.serialize_namedtuple
elif isinstance(data, (list, tuple, GeneratorType)):
serialize_method = cls.serialize_iterable
return serialize_method(**serialize_kwargs)
Expand Down
44 changes: 22 additions & 22 deletions tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@
---
# name: test_custom_object_repr
<class 'CustomClass'> {
a=1
b='2'
a=1,
b='2',
c=<class 'list'> [
1,
2,
3,
...,
]
],
d=<class 'dict'> {
'a': 1,
'b': 2,
'c': 3,
'd': ...,
}
},
x=<class 'CustomClass'> {
a=1
b='2'
a=1,
b='2',
c=<class 'list'> [
1,
2,
3,
...,
]
],
d=<class 'dict'> {
'a': 1,
'b': 2,
'c': 3,
'd': ...,
}
x=None
}
},
x=None,
},
}
---
# name: test_cycle[cyclic0]
Expand Down Expand Up @@ -107,10 +107,10 @@
'a': 'Some ttext.',
1: True,
<class 'ExampleTuple'> (
1,
2,
3,
4,
a=1,
b=2,
c=3,
d=4,
): <class 'dict'> {
'e': False,
},
Expand Down Expand Up @@ -193,10 +193,10 @@
'contains',
'namedtuple',
<class 'ExampleTuple'> (
1,
2,
3,
4,
a=1,
b=2,
c=3,
d=4,
),
}
---
Expand Down Expand Up @@ -253,10 +253,10 @@
---
# name: test_tuple.1
<class 'ExampleTuple'> (
'this',
'is',
'a',
<class 'set'> {
a='this',
b='is',
c='a',
d=<class 'set'> {
'named',
'tuple',
},
Expand Down
6 changes: 3 additions & 3 deletions tests/examples/__snapshots__/test_custom_object_repr.ambr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# name: test_snapshot_custom_class
<class 'MyCustomClass'> {
prop1=1
prop2='a'
prop1=1,
prop2='a',
prop3=<class 'set'> {
1,
2,
3,
}
},
}
---
# name: test_snapshot_custom_repr_class
Expand Down