Skip to content

Commit d3eb198

Browse files
committed
tests(textframe): Switch to SingleFileSnapshotExtension
why: Individual .frame files provide cleaner git diffs and easier review than a single .ambr file with all snapshots. what: - Replace AmberSnapshotExtension with SingleFileSnapshotExtension - Set file extension to .frame - Simplify serialize() method (removed nested serializer class)
1 parent 4181df6 commit d3eb198

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

tests/textframe/plugin.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,63 @@
11
"""Syrupy snapshot extension for TextFrame objects.
22
3-
This module provides a custom serializer that renders TextFrame objects
4-
and ContentOverflowError exceptions as ASCII art in snapshot files.
3+
This module provides a single-file snapshot extension that renders TextFrame
4+
objects and ContentOverflowError exceptions as ASCII art in .frame files.
55
"""
66

77
from __future__ import annotations
88

99
import typing as t
1010

11-
from syrupy.extensions.amber import AmberSnapshotExtension
12-
from syrupy.extensions.amber.serializer import AmberDataSerializer
11+
from syrupy.extensions.single_file import SingleFileSnapshotExtension, WriteMode
1312

1413
from .core import ContentOverflowError, TextFrame
1514

1615

17-
class TextFrameSerializer(AmberDataSerializer):
18-
"""Custom serializer that renders TextFrame objects as ASCII frames.
16+
class TextFrameExtension(SingleFileSnapshotExtension):
17+
"""Single-file extension for TextFrame snapshots (.frame files).
1918
20-
This serializer intercepts TextFrame and ContentOverflowError objects,
21-
converting them to their ASCII representation before passing them
22-
to the base serializer for formatting.
19+
Each test snapshot is stored in its own .frame file, providing cleaner
20+
git diffs compared to the multi-snapshot .ambr format.
2321
2422
Notes
2523
-----
26-
By subclassing AmberDataSerializer, we ensure TextFrame objects are
27-
correctly rendered even when nested inside lists, dicts, or other
28-
data structures.
24+
This extension serializes:
25+
- TextFrame objects → their render() output
26+
- ContentOverflowError → their overflow_visual attribute
27+
- Other types → str() representation
2928
"""
3029

31-
@classmethod
32-
def _serialize(
33-
cls,
30+
_write_mode = WriteMode.TEXT
31+
file_extension = "frame"
32+
33+
def serialize(
34+
self,
3435
data: t.Any,
3536
*,
36-
depth: int = 0,
37-
**kwargs: t.Any,
37+
exclude: t.Any = None,
38+
include: t.Any = None,
39+
matcher: t.Any = None,
3840
) -> str:
39-
"""Serialize data, converting TextFrame objects to ASCII.
41+
"""Serialize data to ASCII frame representation.
4042
4143
Parameters
4244
----------
4345
data : Any
4446
The data to serialize.
45-
depth : int
46-
Current indentation depth.
47-
**kwargs : Any
48-
Additional serialization options.
47+
exclude : Any
48+
Properties to exclude (unused for TextFrame).
49+
include : Any
50+
Properties to include (unused for TextFrame).
51+
matcher : Any
52+
Custom matcher (unused for TextFrame).
4953
5054
Returns
5155
-------
5256
str
53-
Serialized representation.
57+
ASCII representation of the data.
5458
"""
55-
# Intercept TextFrame: Render it to ASCII
5659
if isinstance(data, TextFrame):
57-
return super()._serialize(data.render(), depth=depth, **kwargs)
58-
59-
# Intercept ContentOverflowError: Render the visual diff
60+
return data.render()
6061
if isinstance(data, ContentOverflowError):
61-
return super()._serialize(data.overflow_visual, depth=depth, **kwargs)
62-
63-
# Default behavior for all other types
64-
return super()._serialize(data, depth=depth, **kwargs)
65-
66-
67-
class TextFrameExtension(AmberSnapshotExtension):
68-
"""Syrupy extension that uses the TextFrameSerializer."""
69-
70-
serializer_class = TextFrameSerializer
62+
return data.overflow_visual
63+
return str(data)

0 commit comments

Comments
 (0)