Skip to content

Commit 417a519

Browse files
committed
Add a repr method to Snapshot.
1 parent 7bb08b7 commit 417a519

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

python/selfie-lib/selfie_lib/Snapshot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ def of_items(items: Iterator[tuple[str, SnapshotValue]]) -> "Snapshot":
8787
def items(self) -> Iterator[tuple[str, SnapshotValue]]:
8888
yield ("", self._subject)
8989
yield from self._facet_data.items()
90+
91+
def __repr__(self) -> str:
92+
pieces = [f"Snapshot.of({self.subject.value_string()!r})"]
93+
for e in self.facets.items():
94+
pieces.append(f"\n .plus_facet({e[0]!r}, {e[1].value_string()!r})") # noqa: PERF401
95+
return "".join(pieces)

python/selfie-lib/tests/Snapshot_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,20 @@ def test_items():
77
)
88
roundtrip = Snapshot.of_items(undertest.items())
99
assert roundtrip == undertest
10+
11+
12+
def test_repr():
13+
assert repr(Snapshot.of("subject")) == "Snapshot.of('subject')"
14+
assert repr(Snapshot.of("subject\nline2")) == "Snapshot.of('subject\\nline2')"
15+
assert (
16+
repr(Snapshot.of("subject").plus_facet("apple", "green"))
17+
== "Snapshot.of('subject')\n .plus_facet('apple', 'green')"
18+
)
19+
assert (
20+
repr(
21+
Snapshot.of("subject")
22+
.plus_facet("orange", "peel")
23+
.plus_facet("apple", "green")
24+
)
25+
== "Snapshot.of('subject')\n .plus_facet('apple', 'green')\n .plus_facet('orange', 'peel')"
26+
)

0 commit comments

Comments
 (0)