Skip to content

Commit 4ca0716

Browse files
authored
feat: numerically sort snapshots if possible, close #657
1 parent 3d296e1 commit 4ca0716

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed

src/syrupy/extensions/amber/serializer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Dict,
1212
Generator,
1313
Iterable,
14+
List,
1415
NamedTuple,
1516
Optional,
1617
Set,
@@ -87,6 +88,25 @@ class Marker:
8788
Name = "name"
8889
Divider = "---"
8990

91+
@classmethod
92+
def __maybe_int(cls, part: str) -> Tuple[int, Union[str, int]]:
93+
try:
94+
# cast to int only if the string is the exact representation of the int
95+
# for example, '012' != str(int('012'))
96+
i = int(part)
97+
if str(i) == part:
98+
return (1, i)
99+
return (0, part)
100+
except ValueError:
101+
# the nested tuple is to prevent comparing a str to an int
102+
return (0, part)
103+
104+
@classmethod
105+
def __snapshot_sort_key(
106+
cls, snapshot: "Snapshot"
107+
) -> List[Tuple[int, Union[str, int]]]:
108+
return [cls.__maybe_int(part) for part in snapshot.name.split(".")]
109+
90110
@classmethod
91111
def write_file(
92112
cls, snapshot_collection: "SnapshotCollection", merge: bool = False
@@ -102,7 +122,9 @@ def write_file(
102122

103123
with open(filepath, "w", encoding=TEXT_ENCODING, newline=None) as f:
104124
f.write(f"{cls._marker_prefix}{cls.Marker.Version}: {cls.VERSION}\n")
105-
for snapshot in sorted(snapshot_collection, key=lambda s: s.name):
125+
for snapshot in sorted(
126+
snapshot_collection, key=lambda s: cls.__snapshot_sort_key(s)
127+
):
106128
snapshot_data = str(snapshot.data)
107129
if snapshot_data is not None:
108130
f.write(f"{cls._marker_prefix}{cls.Marker.Name}: {snapshot.name}\n")

tests/syrupy/__snapshots__/test_doctest.ambr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
obj_attr='test class attr',
55
)
66
# ---
7-
# name: DocTestClass.1
8-
DocTestClass(
9-
obj_attr='test class attr',
10-
)
11-
# ---
127
# name: DocTestClass.NestedDocTestClass
138
NestedDocTestClass(
149
nested_obj_attr='nested doc test class attr',
@@ -20,6 +15,11 @@
2015
# name: DocTestClass.doctest_method
2116
'doc test method return value'
2217
# ---
18+
# name: DocTestClass.1
19+
DocTestClass(
20+
obj_attr='test class attr',
21+
)
22+
# ---
2323
# name: doctest_fn
2424
'doc test fn return value'
2525
# ---

tests/syrupy/extensions/amber/__snapshots__/test_amber_serializer.ambr

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,81 @@
247247
}),
248248
])
249249
# ---
250+
# name: test_many_sorted
251+
0
252+
# ---
253+
# name: test_many_sorted.1
254+
1
255+
# ---
256+
# name: test_many_sorted.2
257+
2
258+
# ---
259+
# name: test_many_sorted.3
260+
3
261+
# ---
262+
# name: test_many_sorted.4
263+
4
264+
# ---
265+
# name: test_many_sorted.5
266+
5
267+
# ---
268+
# name: test_many_sorted.6
269+
6
270+
# ---
271+
# name: test_many_sorted.7
272+
7
273+
# ---
274+
# name: test_many_sorted.8
275+
8
276+
# ---
277+
# name: test_many_sorted.9
278+
9
279+
# ---
280+
# name: test_many_sorted.10
281+
10
282+
# ---
283+
# name: test_many_sorted.11
284+
11
285+
# ---
286+
# name: test_many_sorted.12
287+
12
288+
# ---
289+
# name: test_many_sorted.13
290+
13
291+
# ---
292+
# name: test_many_sorted.14
293+
14
294+
# ---
295+
# name: test_many_sorted.15
296+
15
297+
# ---
298+
# name: test_many_sorted.16
299+
16
300+
# ---
301+
# name: test_many_sorted.17
302+
17
303+
# ---
304+
# name: test_many_sorted.18
305+
18
306+
# ---
307+
# name: test_many_sorted.19
308+
19
309+
# ---
310+
# name: test_many_sorted.20
311+
20
312+
# ---
313+
# name: test_many_sorted.21
314+
21
315+
# ---
316+
# name: test_many_sorted.22
317+
22
318+
# ---
319+
# name: test_many_sorted.23
320+
23
321+
# ---
322+
# name: test_many_sorted.24
323+
24
324+
# ---
250325
# name: test_multiline_string_in_dict
251326
dict({
252327
'value': '''

tests/syrupy/extensions/amber/test_amber_serializer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,8 @@ def test_ordered_dict(snapshot):
228228
d["b"] = 0
229229
d["a"] = 1
230230
assert snapshot == d
231+
232+
233+
def test_many_sorted(snapshot):
234+
for i in range(25):
235+
assert i == snapshot

0 commit comments

Comments
 (0)