@@ -81,7 +81,7 @@ def to_match_disk_TODO(self, sub: str = "") -> "DiskSelfie":
8181 return self
8282 else :
8383 raise _selfieSystem ().fs .assert_failed (
84- f"Can't call `toMatchDisk_TODO` in { Mode .readonly } mode!"
84+ message = f"Can't call `toMatchDisk_TODO` in { Mode .readonly } mode!"
8585 )
8686
8787 def facet (self , facet : str ) -> "StringFacet" :
@@ -186,17 +186,71 @@ def __init__(self, actual: Snapshot, disk: DiskStorage, only_facet: str):
186186 f"The facet { only_facet } is a string, not a binary snapshot"
187187 )
188188
189- def to_be_base64 (self , expected : str ) -> bytes :
190- raise NotImplementedError
189+ def _actual_bytes (self ) -> bytes :
190+ return self .actual .subject_or_facet (self .only_facet ).value_binary ()
191+
192+ def to_match_disk (self , sub : str = "" ) -> "BinarySelfie" :
193+ super ().to_match_disk (sub )
194+ return self
195+
196+ def to_match_disk_TODO (self , sub : str = "" ) -> "BinarySelfie" :
197+ super ().to_match_disk_TODO (sub )
198+ return self
191199
192200 def to_be_base64_TODO (self , _ : Any = None ) -> bytes :
193- raise NotImplementedError
201+ _toBeDidntMatch (None , self ._actual_string (), LiteralString ())
202+ return self ._actual_bytes ()
194203
195- def to_be_file (self , subpath : str ) -> bytes :
196- raise NotImplementedError
204+ def to_be_base64 (self , expected : str ) -> bytes :
205+ expected_bytes = base64 .b64decode (expected )
206+ actual_bytes = self ._actual_bytes ()
207+ if actual_bytes == expected_bytes :
208+ return _checkSrc (actual_bytes )
209+ else :
210+ _toBeDidntMatch (expected , self ._actual_string (), LiteralString ())
211+ return actual_bytes
212+
213+ def _actual_string (self ) -> str :
214+ return base64 .b64encode (self ._actual_bytes ()).decode ().replace ("\r " , "" )
215+
216+ def _to_be_file_impl (self , subpath : str , is_todo : bool ) -> bytes :
217+ call = recordCall (False )
218+ writable = _selfieSystem ().mode .can_write (is_todo , call , _selfieSystem ())
219+ actual_bytes = self ._actual_bytes ()
220+ path = _selfieSystem ().layout .root_folder ().resolve_file (subpath )
221+
222+ if writable :
223+ if is_todo :
224+ _selfieSystem ().write_inline (TodoStub .to_be_file .create_literal (), call )
225+ _selfieSystem ().write_to_be_file (path , actual_bytes , call )
226+ return actual_bytes
227+ else :
228+ if is_todo :
229+ raise _selfieSystem ().fs .assert_failed (
230+ f"Can't call `to_be_file_TODO` in { Mode .readonly } mode!"
231+ )
232+ else :
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 )
236+ )
237+ expected = _selfieSystem ().fs .file_read_binary (path )
238+ if expected == actual_bytes :
239+ return actual_bytes
240+ else :
241+ raise _selfieSystem ().fs .assert_failed (
242+ message = _selfieSystem ().mode .msg_snapshot_mismatch_binary (
243+ expected , actual_bytes
244+ ),
245+ expected = expected ,
246+ actual = actual_bytes ,
247+ )
197248
198249 def to_be_file_TODO (self , subpath : str ) -> bytes :
199- raise NotImplementedError
250+ return self ._to_be_file_impl (subpath , True )
251+
252+ def to_be_file (self , subpath : str ) -> bytes :
253+ return self ._to_be_file_impl (subpath , False )
200254
201255
202256def _checkSrc (value : T ) -> T :
@@ -216,16 +270,27 @@ def _toBeDidntMatch(expected: Optional[T], actual: T, fmt: LiteralFormat[T]) ->
216270 f"Can't call `toBe_TODO` in { Mode .readonly } mode!"
217271 )
218272 else :
219- raise _selfieSystem ().fs .assert_failed (
220- _selfieSystem ().mode .msg_snapshot_mismatch (), expected , actual
221- )
273+ expectedStr = repr (expected )
274+ actualStr = repr (actual )
275+ if expectedStr == actualStr :
276+ raise ValueError (
277+ f"Value of type { type (actual )} is not `==` to the expected value, but they both have the same `repr` value:\n ${ expectedStr } "
278+ )
279+ else :
280+ raise _selfieSystem ().fs .assert_failed (
281+ message = _selfieSystem ().mode .msg_snapshot_mismatch (
282+ expected = expectedStr , actual = actualStr
283+ ),
284+ expected = expected ,
285+ actual = actual ,
286+ )
222287
223288
224289def _assertEqual (
225290 expected : Optional [Snapshot ], actual : Snapshot , storage : SnapshotSystem
226291):
227292 if expected is None :
228- raise storage .fs .assert_failed (storage .mode .msg_snapshot_not_found ())
293+ raise storage .fs .assert_failed (message = storage .mode .msg_snapshot_not_found ())
229294 elif expected == actual :
230295 return
231296 else :
@@ -240,10 +305,14 @@ def _assertEqual(
240305 ),
241306 )
242307 )
308+ expectedFacets = _serializeOnlyFacets (expected , mismatched_keys )
309+ actualFacets = _serializeOnlyFacets (actual , mismatched_keys )
243310 raise storage .fs .assert_failed (
244- storage .mode .msg_snapshot_mismatch (),
245- _serializeOnlyFacets (expected , mismatched_keys ),
246- _serializeOnlyFacets (actual , mismatched_keys ),
311+ message = storage .mode .msg_snapshot_mismatch (
312+ expected = expectedFacets , actual = actualFacets
313+ ),
314+ expected = expectedFacets ,
315+ actual = actualFacets ,
247316 )
248317
249318
0 commit comments