Selfie
Loading...
Searching...
No Matches
Selfie.py
Go to the documentation of this file.
1from typing import Any, Callable, TypeVar, overload
2
3from .Lens import Camera
4from .SelfieImplementations import BinarySelfie, DiskSelfie, ReprSelfie, StringSelfie
5from .Snapshot import Snapshot
6from .SnapshotSystem import _selfieSystem
7
8T = TypeVar("T")
9
10
11@overload
12def expect_selfie(actual: str) -> StringSelfie: ...
13
14
15@overload
16def expect_selfie(actual: Snapshot) -> StringSelfie: ...
17
18
19@overload
20def expect_selfie(actual: bytes) -> BinarySelfie: ...
21
22
23@overload
24def expect_selfie(actual: T) -> ReprSelfie[T]: ...
25
26
27@overload
28def expect_selfie(actual: T, camera: Camera[T]) -> StringSelfie: ...
29
30
31@overload
32def expect_selfie(actual: T, camera: Callable[[T], Snapshot]) -> StringSelfie: ...
33
34
35def expect_selfie(actual: Any, camera: Any = None) -> DiskSelfie:
36 disk_storage = _selfieSystem().disk_thread_local()
37 if camera is not None:
38 if isinstance(camera, Camera):
39 actual_snapshot = camera.snapshot(actual)
40 else:
41 actual_snapshot = camera(actual)
42 return StringSelfie(actual_snapshot, disk_storage)
43 elif isinstance(actual, str):
44 return StringSelfie(Snapshot.of(actual), disk_storage)
45 elif isinstance(actual, Snapshot):
46 return StringSelfie(actual, disk_storage)
47 elif isinstance(actual, bytes):
48 return BinarySelfie(Snapshot.of(actual), disk_storage, "")
49 else:
50 return ReprSelfie(actual, Snapshot.of(repr(actual)), disk_storage)