Skip to content

implement a snapshot_arg() function to simplify some advanced snapshot usages #197

@15r10nk

Description

@15r10nk

Problem:

snapshot() can not only be used in assertions but also used as argument for functions or methods (everything callable).

One example:

from inline_snapshot import snapshot


def check_string(string, length, is_upper):
    assert len(string) == length
    assert string.isupper() == is_upper


def test_string_len():
    check_string("abc", length=snapshot(3),is_upper=snapshot(False))

The problem is that you have to use the following code to create the snapshots

def test_string_len():
    check_string("abc", length=snapshot(),is_upper=snapshot())

Idea:

I want to move the snapshot inside the function and call it snapshot_arg which will then manage the value of the argument.

from inline_snapshot import snapshot_arg


def check_string(string, length, is_upper):
    assert len(string) == snapshot_arg(length)
    assert string.isupper() == snapshot_arg(is_upper)


def test_string_len():
    check_string("abc")

inline-snapshot will then create the arguments without the use of snapshot().

def test_string_len():
    check_string("abc", length=3,is_upper=False)

inline-snapshot will use the default arguments to decide if a argument has to be created.

There are some rules to follow.

  • snapshot_arg(arg) can only be used for arguments.
  • inline-snapshot will "manage" the argument of the function and try to change the code if it has to.
    This can become a problem if you try to use it for existing code. There are maybe ways to make smarter decision here, but not in the first iteration.

possible use cases:

feedback is very welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions