Skip to content

Commit ef60d95

Browse files
committed
test: more tests
1 parent 845e18c commit ef60d95

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/inline_snapshot/testing/_example.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def parse_outcomes(lines):
8383

8484

8585
class Example:
86-
def __init__(self, files: str | dict[str, str]):
86+
files: dict[str, str | bytes]
87+
88+
def __init__(self, files: str | dict[str, str | bytes]):
8789
"""
8890
Parameters:
8991
files: a collection of files where inline-snapshot operates on,
@@ -131,12 +133,20 @@ def normalize_path(path):
131133
and ".pytest_cache" not in p.parts
132134
}
133135

134-
def with_files(self, extra_files):
135-
return Example(self.files | extra_files)
136+
def with_files(self, extra_files: dict[str, str | bytes]) -> Example:
137+
return Example({**self.files, **extra_files})
138+
139+
def read_text(self, name: str) -> str:
140+
text = self.files[name]
141+
assert isinstance(text, str)
142+
return text
136143

137-
def change_code(self, func):
144+
def change_code(self, func) -> Example:
138145
return Example({name: func(text) for name, text in self.files.items()})
139146

147+
def replace(self, text, new_text) -> Example:
148+
return self.change_code(lambda code: code.replace(text, new_text))
149+
140150
def run_inline(
141151
self,
142152
args: list[str] = [],

tests/test_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def test_block(block: Block):
381381

382382
new_code = code
383383
if flags:
384-
new_code = example.files["test_example.py"]
384+
new_code = example.read_text("test_example.py")
385385
new_code.replace("\n\n", "\n")
386386

387387
if "show_error" in options:

tests/test_external_formats.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def test_a():
2929
""",
3030
}
3131
),
32+
).run_inline(
33+
["--inline-snapshot=disable"]
3234
)
3335

3436

tests/test_storage.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,44 @@ def test_uuid_storage():
77
"""
88
from inline_snapshot import external
99
10+
s=external("uuid:")
11+
1012
def test_a():
1113
assert "value" == external("uuid:")
14+
assert "blub"==s
1215
"""
1316
).run_inline(
1417
["--inline-snapshot=create"],
1518
changed_files=snapshot(
1619
{
20+
"__inline_snapshot__/test_something/__module__/f728b4fa-4248-4e3a-8a5d-2f346baa9455.txt": "blub",
1721
"__inline_snapshot__/test_something/test_a/e3e70682-c209-4cac-a29f-6fbed82c07cd.txt": "value",
1822
"test_something.py": """\
1923
2024
from inline_snapshot import external
2125
26+
s=external("uuid:f728b4fa-4248-4e3a-8a5d-2f346baa9455.txt")
27+
2228
def test_a():
2329
assert "value" == external("uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt")
30+
assert "blub"==s
2431
\
2532
""",
2633
}
2734
),
35+
).replace(
36+
'"value"', '"new_value"'
37+
).run_inline(
38+
["--inline-snapshot=create"],
39+
changed_files=snapshot({}),
40+
raises=snapshot("AssertionError:\n"),
41+
).run_inline(
42+
["--inline-snapshot=fix"],
43+
changed_files=snapshot(
44+
{
45+
"__inline_snapshot__/test_something/test_a/e3e70682-c209-4cac-a29f-6fbed82c07cd.txt": "new_value"
46+
}
47+
),
48+
).run_inline(
49+
["--inline-snapshot=disable"], changed_files=snapshot({})
2850
)

0 commit comments

Comments
 (0)