2from abc
import ABC, abstractmethod
3from itertools
import chain
4from typing
import Any, Generic, Optional, TypeVar
13from .Snapshot
import Snapshot
14from .SnapshotFile
import SnapshotFile
15from .SnapshotSystem
import DiskStorage, Mode, SnapshotSystem, _selfieSystem
16from .WriteTracker
import recordCall
as recordCall
23 def facet(self, facet: str) ->
"StringFacet":
24 """Extract a single facet from a snapshot in order to do an inline snapshot."""
27 def facets(self, *facets: str) ->
"StringFacet":
28 """Extract multiple facets from a snapshot in order to do an inline snapshot."""
37 def to_be(self, expected: str) -> str:
64 def __init__(self, actual: Snapshot, disk: DiskStorage):
69 call = recordCall(
False)
70 if _selfieSystem().mode.can_write(
False, call, _selfieSystem()):
77 call = recordCall(
False)
78 if _selfieSystem().mode.can_write(
True, call, _selfieSystem()):
80 _selfieSystem().write_inline(TodoStub.to_match_disk.create_literal(), call)
83 raise _selfieSystem().fs.assert_failed(
84 message=f
"Can't call `toMatchDisk_TODO` in {Mode.readonly} mode!"
87 def facet(self, facet: str) ->
"StringFacet":
90 def facets(self, *facets: str) ->
"StringFacet":
98 def __init__(self, actual_before_repr: T, actual: Snapshot, disk: DiskStorage):
117 only_facets: Optional[list[str]] =
None,
119 super().
__init__(
"<IT IS AN ERROR FOR THIS TO BE VISIBLE>", actual, disk)
124 facet ==
"" or facet
in actual.facets
for facet
in self.
only_facets
125 ), f
"The following facets were not found in the snapshot: {[facet for facet in self.only_facets if actual.subject_or_facet_maybe(facet) is None]}\navailable facets are: {list(actual.facets.keys())}"
128 ),
"Must have at least one facet to display, this was empty."
132 ), f
'If you\'re going to specify the subject facet (""), you have to list it first, this was {self.only_facets}'
142 def __actual(self) -> str:
148 if only_value.is_binary:
150 base64.b64encode(only_value.value_binary())
155 return only_value.value_string()
164 def to_be(self, expected: str) -> str:
166 if actual_string == expected:
177 def __init__(self, actual: Snapshot, disk: DiskStorage, only_facet: str):
178 super().
__init__(actual.subject.value_binary(), actual, disk)
181 facet_value = actual.subject_or_facet_maybe(only_facet)
182 if facet_value
is None:
183 raise ValueError(f
"The facet {only_facet} was not found in the snapshot")
184 elif not facet_value.is_binary:
186 f
"The facet {only_facet} is a string, not a binary snapshot"
205 expected_bytes = base64.b64decode(expected)
207 if actual_bytes == expected_bytes:
214 return base64.b64encode(self.
_actual_bytes()).decode().replace(
"\r",
"")
217 call = recordCall(
False)
218 writable = _selfieSystem().mode.can_write(is_todo, call, _selfieSystem())
220 path = _selfieSystem().layout.root_folder().resolve_file(subpath)
224 _selfieSystem().write_inline(TodoStub.to_be_file.create_literal(), call)
225 _selfieSystem().write_to_be_file(path, actual_bytes, call)
229 raise _selfieSystem().fs.assert_failed(
230 f
"Can't call `to_be_file_TODO` in {Mode.readonly} mode!"
233 if not _selfieSystem().fs.file_exists(path):
234 raise _selfieSystem().fs.assert_failed(
235 _selfieSystem().mode.msg_snapshot_not_found_no_such_file(path)
237 expected = _selfieSystem().fs.file_read_binary(path)
238 if expected == actual_bytes:
241 raise _selfieSystem().fs.assert_failed(
242 message=_selfieSystem().mode.msg_snapshot_mismatch_binary(
243 expected, actual_bytes
257 _selfieSystem().mode.can_write(
False, recordCall(
True), _selfieSystem())
262 call = recordCall(
False)
263 writable = _selfieSystem().mode.can_write(expected
is None, call, _selfieSystem())
265 _selfieSystem().write_inline(
LiteralValue(expected, actual, fmt), call)
269 raise _selfieSystem().fs.assert_failed(
270 f
"Can't call `to_be_TODO` in {Mode.readonly} mode!"
273 expectedStr = repr(expected)
274 actualStr = repr(actual)
275 if expectedStr == actualStr:
277 f
"Value of type {type(actual)} is not `==` to the expected value, but they both have the same `repr` value:\n${expectedStr}"
280 raise _selfieSystem().fs.assert_failed(
281 message=_selfieSystem().mode.msg_snapshot_mismatch(
282 expected=expectedStr, actual=actualStr
290 expected: Optional[Snapshot], actual: Snapshot, storage: SnapshotSystem
293 raise storage.fs.assert_failed(message=storage.mode.msg_snapshot_not_found())
294 elif expected == actual:
297 mismatched_keys = sorted(
299 lambda facet: expected.subject_or_facet_maybe(facet)
300 != actual.subject_or_facet_maybe(facet),
303 expected.facets.keys(),
304 (facet
for facet
in actual.facets
if facet
not in expected.facets),
310 raise storage.fs.assert_failed(
311 message=storage.mode.msg_snapshot_mismatch(
312 expected=expectedFacets, actual=actualFacets
314 expected=expectedFacets,
323 SnapshotFile.writeEntry(writer,
"",
None, snapshot.subject_or_facet(key))
325 value = snapshot.subject_or_facet(key)
326 if value
is not None:
327 SnapshotFile.writeEntry(writer,
"", key, value)
329 EMPTY_KEY_AND_FACET =
"╔═ ═╗\n"
330 writer_str =
"".join(writer)
332 if writer_str.startswith(EMPTY_KEY_AND_FACET):
334 return writer_str[len(EMPTY_KEY_AND_FACET) : -1]
336 return writer_str[:-1]
bytes to_be_base64_TODO(self, Any _=None)
bytes to_be_file(self, str subpath)
bytes to_be_base64(self, str expected)
bytes to_be_file_TODO(self, str subpath)
"BinarySelfie" to_match_disk_TODO(self, str sub="")
bytes to_be_file(self, str subpath)
__init__(self, Snapshot actual, DiskStorage disk, str only_facet)
bytes _to_be_file_impl(self, str subpath, bool is_todo)
bytes to_be_file_TODO(self, str subpath)
bytes to_be_base64_TODO(self, Any _=None)
bytes to_be_base64(self, str expected)
"BinarySelfie" to_match_disk(self, str sub="")
bytes _actual_bytes(self)
"StringFacet" facet(self, str facet)
__init__(self, Snapshot actual, DiskStorage disk)
"BinaryFacet" facet_binary(self, str facet)
"StringFacet" facets(self, *str facets)
"DiskSelfie" to_match_disk_TODO(self, str sub="")
"DiskSelfie" to_match_disk(self, str sub="")
"StringFacet" facet(self, str facet)
"BinaryFacet" facet_binary(self, str facet)
"StringFacet" facets(self, *str facets)
__init__(self, T actual_before_repr, Snapshot actual, DiskStorage disk)
T to_be(self, T expected)
T to_be_TODO(self, Optional[T] _=None)
str to_be(self, str expected)
str to_be_TODO(self, Any _=None)
__init__(self, Snapshot actual, DiskStorage disk, Optional[list[str]] only_facets=None)
"StringSelfie" to_match_disk_TODO(self, str sub="")
str to_be_TODO(self, Any _=None)
"StringSelfie" to_match_disk(self, str sub="")
str to_be(self, str expected)
T _to_be_didnt_match(Optional[T] expected, T actual, LiteralFormat[T] fmt)
str _serialize_only_facets(Snapshot snapshot, list[str] keys)
_assert_equal(Optional[Snapshot] expected, Snapshot actual, SnapshotSystem storage)