-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
186 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
|
||
::: inline_snapshot.extras | ||
options: | ||
heading_level: 1 | ||
show_root_heading: true | ||
show_source: true | ||
separate_signature: false | ||
show_signature_annotations: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
|
||
::: inline_snapshot | ||
options: | ||
heading_level: 1 | ||
members: [Snapshot,Category] | ||
show_root_heading: true | ||
show_bases: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,75 @@ | ||
"""The following types are for type checking only.""" | ||
|
||
... # prevent lint error with black and reorder-python-imports | ||
|
||
from typing import Generic | ||
from typing import Literal | ||
from typing import TYPE_CHECKING | ||
from typing import TypeVar | ||
|
||
from typing_extensions import Annotated | ||
from typing_extensions import TypeAlias | ||
|
||
if TYPE_CHECKING: | ||
|
||
T = TypeVar("T") | ||
|
||
Snapshot: TypeAlias = T | ||
|
||
else: | ||
T = TypeVar("T") | ||
|
||
class Snapshot(Generic[T]): | ||
"""Can be used to annotate function arguments which accept snapshot | ||
values. | ||
You can annotate function arguments with `Snapshot[T]` to declare that a snapshot-value can be passed as function argument. | ||
`Snapshot[T]` is a type alias for `T`, which allows you to pass `int` values instead of `int` snapshots. | ||
Example: | ||
<!-- inline-snapshot: create fix trim this outcome-passed=2 --> | ||
```python | ||
from typing import Optional | ||
from inline_snapshot import snapshot, Snapshot | ||
# required snapshots | ||
def check_in_bounds(value, lower: Snapshot[int], upper: Snapshot[int]): | ||
assert lower <= value <= upper | ||
def test_numbers(): | ||
for c in "hello world": | ||
check_in_bounds(ord(c), snapshot(32), snapshot(119)) | ||
# use with normal values | ||
check_in_bounds(5, 0, 10) | ||
# optional snapshots | ||
def check_container( | ||
value, | ||
*, | ||
value_repr: Optional[Snapshot[str]] = None, | ||
length: Optional[Snapshot[int]] = None | ||
): | ||
if value_repr is not None: | ||
assert repr(value) == value_repr | ||
if length is not None: | ||
assert len(value) == length | ||
T = TypeVar("T") | ||
def test_container(): | ||
check_container([1, 2], value_repr=snapshot("[1, 2]"), length=snapshot(2)) | ||
Snapshot = Annotated[T, "just an alias"] | ||
check_container({1, 1}, length=snapshot(1)) | ||
``` | ||
""" | ||
|
||
|
||
Category = Literal["update", "fix", "create", "trim"] | ||
"""See [categories](categories.md)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""The following functions are build on top of inline-snapshot and could also | ||
be implemented in an extra library. | ||
They are part of inline-snapshot because they are general useful and do | ||
not depend on other libraries. | ||
""" | ||
|
||
... # prevent lint error with black and reorder-python-imports | ||
import contextlib | ||
from inline_snapshot import Snapshot | ||
|
||
|
||
@contextlib.contextmanager | ||
def raises(message: Snapshot[str]): | ||
"""Check that an exception is raised. | ||
Parameters: | ||
message: snapshot which is compared with `#!python f"{type}: {message}"` if an exception occured or `#!python "<no exception>"` if no exception was raised. | ||
=== "original" | ||
<!-- inline-snapshot: outcome-passed=1 outcome-errors=1 --> | ||
```python | ||
from inline_snapshot import snapshot | ||
from inline_snapshot.extras import raises | ||
def test_raises(): | ||
with raises(snapshot()): | ||
1 / 0 | ||
``` | ||
=== "--inline-snapshot=create" | ||
<!-- inline-snapshot: create outcome-passed=1 --> | ||
```python | ||
from inline_snapshot import snapshot | ||
from inline_snapshot.extras import raises | ||
def test_raises(): | ||
with raises(snapshot("ZeroDivisionError: division by zero")): | ||
1 / 0 | ||
``` | ||
""" | ||
|
||
try: | ||
yield | ||
except Exception as exception: | ||
msg = str(exception) | ||
if "\n" in msg: | ||
assert f"{type(exception).__name__}:\n{exception}" == message | ||
else: | ||
assert f"{type(exception).__name__}: {exception}" == message | ||
else: | ||
assert "<no exception>" == message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from inline_snapshot import snapshot | ||
from inline_snapshot.extras import raises | ||
|
||
|
||
def test_raises(): | ||
with raises(snapshot("ZeroDivisionError: division by zero")): | ||
0 / 0 | ||
|
||
with raises(snapshot("<no exception>")): | ||
pass | ||
|
||
with raises( | ||
snapshot( | ||
"""\ | ||
ValueError: | ||
with | ||
two lines\ | ||
""" | ||
) | ||
): | ||
raise ValueError("with\ntwo lines") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters