@@ -81,7 +81,7 @@ def to_match_disk_TODO(self, sub: str = "") -> "DiskSelfie":
81
81
return self
82
82
else :
83
83
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!"
85
85
)
86
86
87
87
def facet (self , facet : str ) -> "StringFacet" :
@@ -186,17 +186,71 @@ def __init__(self, actual: Snapshot, disk: DiskStorage, only_facet: str):
186
186
f"The facet { only_facet } is a string, not a binary snapshot"
187
187
)
188
188
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
191
199
192
200
def to_be_base64_TODO (self , _ : Any = None ) -> bytes :
193
- raise NotImplementedError
201
+ _toBeDidntMatch (None , self ._actual_string (), LiteralString ())
202
+ return self ._actual_bytes ()
194
203
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
+ )
197
248
198
249
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 )
200
254
201
255
202
256
def _checkSrc (value : T ) -> T :
@@ -216,16 +270,27 @@ def _toBeDidntMatch(expected: Optional[T], actual: T, fmt: LiteralFormat[T]) ->
216
270
f"Can't call `toBe_TODO` in { Mode .readonly } mode!"
217
271
)
218
272
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
+ )
222
287
223
288
224
289
def _assertEqual (
225
290
expected : Optional [Snapshot ], actual : Snapshot , storage : SnapshotSystem
226
291
):
227
292
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 ())
229
294
elif expected == actual :
230
295
return
231
296
else :
@@ -240,10 +305,14 @@ def _assertEqual(
240
305
),
241
306
)
242
307
)
308
+ expectedFacets = _serializeOnlyFacets (expected , mismatched_keys )
309
+ actualFacets = _serializeOnlyFacets (actual , mismatched_keys )
243
310
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 ,
247
316
)
248
317
249
318
0 commit comments