inline_snapshot.extra
¶
The following functions are built on top of inline-snapshot and could also be implemented in an extra library. The source is added to the documentation, which allows you to look at how they are implemented and implement similar functions if you need them.
They are part of inline-snapshot because they are generally useful and do not depend on other libraries.
Transformed
¶
Transformed
allows you to move transformations of your values from one side of the ==
to the other.
from inline_snapshot import snapshot
from inline_snapshot.extra import Transformed
def test_transform():
numbers = [1, 8, 3, 7, 5]
assert sorted(numbers) == snapshot()
assert numbers == Transformed(sorted, snapshot())
Both assertions create the same snapshots.
from inline_snapshot import snapshot
from inline_snapshot.extra import Transformed
def test_transform():
numbers = [1, 8, 3, 7, 5]
assert sorted(numbers) == snapshot([1, 3, 5, 7, 8])
assert numbers == Transformed(sorted, snapshot([1, 3, 5, 7, 8]))
Transformed
is more flexible to use because you can also use it deep inside data structures.
The following example shows how Transformed
is used inside a dictionary.
from random import shuffle
from inline_snapshot import snapshot
from inline_snapshot.extra import Transformed
def request():
data = [1, 8, 18748, 493]
shuffle(data)
return {"name": "example", "data": data}
def test_request():
assert request() == snapshot(
{
"name": "example",
"data": Transformed(sorted, snapshot([1, 8, 493, 18748])),
}
)
Or to normalize strings.
from inline_snapshot.extra import Transformed
from inline_snapshot import snapshot
import re
class Thing:
def __repr__(self):
return "<Thing with some random id 152897513>"
def without_ids(text):
return re.sub(r"<([^0-9]*)[^>]+>", lambda m: f"<{m[1]} ...>", text)
def test_text_with_objects():
text = f"text can contain {Thing()}"
assert {"logs": text} == snapshot(
{
"logs": Transformed(
without_ids,
snapshot("text can contain <Thing with some random id ...>"),
)
}
)
Tip
You can use @transformation if you want to use the same transformation multiple times.
Source code in src/inline_snapshot/extra.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
__init__(func, value, should_be=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[[Any], Any]
|
functions which is used to transform the value which is compared. |
required |
value
|
Snapshot
|
the result of the transformation |
required |
should_be
|
Any
|
this argument is unused and only reported in the |
None
|
Source code in src/inline_snapshot/extra.py
prints(*, stdout='', stderr='')
¶
Uses contextlib.redirect_stderr/stdout
to capture the output and
compare it with the snapshots. dirty_equals.IsStr
can be used to ignore
the output if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stdout
|
Snapshot[str]
|
Snapshot that is compared to the recorded output. |
''
|
stderr
|
Snapshot[str]
|
Snapshot that is compared to the recorded error output. |
''
|
Source code in src/inline_snapshot/extra.py
raises(exception)
¶
Check that an exception is raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
Snapshot[str]
|
Snapshot that is compared with |
required |
Source code in src/inline_snapshot/extra.py
transformation(func)
¶
@transformation
can be used to bind a function to Transformed,
which simplifies your code if you want to use the same transformation multiple times.
from inline_snapshot.extra import transformation
from inline_snapshot import snapshot
import re
class Thing:
def __repr__(self):
return "<Thing with some random id 152897513>"
@transformation
def WithoutIds(text):
return re.sub(r"<([^0-9]*)[^>]+>", lambda m: f"<{m[1]} ...>", text)
def test_text_with_objects():
text = f"text can contain {Thing()}"
assert {"logs": [text]} == snapshot(
{
"logs": [
WithoutIds(
snapshot(
"text can contain <Thing with some random id ...>"
)
)
]
}
)
Tip
The argument of WithoutIds
can also be an external WithoutIds(external())
if you want to store a large log in an external file.
Source code in src/inline_snapshot/extra.py
warns(expected_warnings, /, include_line=False, include_file=False)
¶
Captures warnings with warnings.catch_warnings
and compares them against expected warnings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected_warnings
|
Snapshot[List[Warning]]
|
Snapshot containing a list of expected warnings. |
required |
include_line
|
bool
|
If |
False
|
include_file
|
bool
|
If |
False
|
The format of the expected warning:
(filename, line_number, message)
if bothinclude_line
andinclude_file
areTrue
.(line_number, message)
if onlyinclude_line
isTrue
.(filename, message)
if onlyinclude_file
isTrue
.- A string
message
if both areFalse
.