|
1 | 1 | """Syrupy snapshot extension for TextFrame objects. |
2 | 2 |
|
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. |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | 9 | import typing as t |
10 | 10 |
|
11 | | -from syrupy.extensions.amber import AmberSnapshotExtension |
12 | | -from syrupy.extensions.amber.serializer import AmberDataSerializer |
| 11 | +from syrupy.extensions.single_file import SingleFileSnapshotExtension, WriteMode |
13 | 12 |
|
14 | 13 | from .core import ContentOverflowError, TextFrame |
15 | 14 |
|
16 | 15 |
|
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). |
19 | 18 |
|
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. |
23 | 21 |
|
24 | 22 | Notes |
25 | 23 | ----- |
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 |
29 | 28 | """ |
30 | 29 |
|
31 | | - @classmethod |
32 | | - def _serialize( |
33 | | - cls, |
| 30 | + _write_mode = WriteMode.TEXT |
| 31 | + file_extension = "frame" |
| 32 | + |
| 33 | + def serialize( |
| 34 | + self, |
34 | 35 | data: t.Any, |
35 | 36 | *, |
36 | | - depth: int = 0, |
37 | | - **kwargs: t.Any, |
| 37 | + exclude: t.Any = None, |
| 38 | + include: t.Any = None, |
| 39 | + matcher: t.Any = None, |
38 | 40 | ) -> str: |
39 | | - """Serialize data, converting TextFrame objects to ASCII. |
| 41 | + """Serialize data to ASCII frame representation. |
40 | 42 |
|
41 | 43 | Parameters |
42 | 44 | ---------- |
43 | 45 | data : Any |
44 | 46 | 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). |
49 | 53 |
|
50 | 54 | Returns |
51 | 55 | ------- |
52 | 56 | str |
53 | | - Serialized representation. |
| 57 | + ASCII representation of the data. |
54 | 58 | """ |
55 | | - # Intercept TextFrame: Render it to ASCII |
56 | 59 | 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() |
60 | 61 | 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